summaryrefslogtreecommitdiffstats
path: root/kopete/protocols/irc/ircservercontact.cpp
blob: 5be40dff3e45b1891443ef202a2ad6019204f329 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
    ircservercontact.cpp - IRC Server Contact

    Copyright (c) 2003      by Michel Hermier <mhermier@kde.org>
    Copyright (c) 2002      by Nick Betcher <nbetcher@kde.org>

    Kopete    (c) 2002-2003 by the Kopete developers <kopete-devel@kde.org>

    *************************************************************************
    *                                                                       *
    * This program is free software; you can redistribute it and/or modify  *
    * it under the terms of the GNU General Public License as published by  *
    * the Free Software Foundation; either version 2 of the License, or     *
    * (at your option) any later version.                                   *
    *                                                                       *
    *************************************************************************
*/

#include "ircusercontact.h"
#include "ircservercontact.h"
#include "ircaccount.h"
#include "ircprotocol.h"

#include "kopetechatsessionmanager.h"
#include "kopeteview.h"

#include <kaction.h>
#include <kdebug.h>
#include <klocale.h>

#include <tqtimer.h>

IRCServerContact::IRCServerContact(IRCContactManager *contactManager, const TQString &servername, Kopete::MetaContact *m)
	: IRCContact(contactManager, servername, m, "irc_server")
{
	KIRC::Engine *engine = kircEngine();

	TQObject::connect(engine, TQT_SIGNAL(internalError(KIRC::Engine::Error, KIRC::Message &)),
			this, TQT_SLOT(engineInternalError(KIRC::Engine::Error, KIRC::Message &)));
/*
	//FIXME: Have some kind of a debug option for raw input/ouput display??
	TQObject::connect(engine, TQT_SIGNAL(sentMessage(KIRC::Message &)),
			this, TQT_SLOT(engineSentMessage(KIRC::Message &)));
	TQObject::connect(engine, TQT_SIGNAL(receivedMessage(KIRC::Message &)),
			this, TQT_SLOT(engineReceivedMessage(KIRC::Message &)));
*/

	TQObject::connect(engine, TQT_SIGNAL(incomingNotice(const TQString &, const TQString &)),
			this, TQT_SLOT(slotIncomingNotice(const TQString &, const TQString &)));

	TQObject::connect(engine, TQT_SIGNAL(incomingCannotSendToChannel(const TQString &, const TQString &)),
			this, TQT_SLOT(slotCannotSendToChannel(const TQString &, const TQString &)));

	TQObject::connect(engine, TQT_SIGNAL(incomingUnknown(const TQString &)),
			this, TQT_SLOT(slotIncomingUnknown(const TQString &)));

	TQObject::connect(engine, TQT_SIGNAL(incomingConnectString(const TQString &)),
			this, TQT_SLOT(slotIncomingConnect(const TQString &)));

	TQObject::connect(engine, TQT_SIGNAL(incomingMotd(const TQString &)),
			this, TQT_SLOT(slotIncomingMotd(const TQString &)));

	TQObject::connect(Kopete::ChatSessionManager::self(), TQT_SIGNAL(viewCreated(KopeteView*)),
			this, TQT_SLOT(slotViewCreated(KopeteView*)) );

	updateStatus();
}

void IRCServerContact::updateStatus()
{
	KIRC::Engine::Status status = kircEngine()->status();
	switch( status )
	{
		case KIRC::Engine::Idle:
		case KIRC::Engine::Connecting:
			if( m_chatSession )
				m_chatSession->setDisplayName( caption() );
			setOnlineStatus( m_protocol->m_ServerStatusOffline );
			break;

		case KIRC::Engine::Authentifying:
		case KIRC::Engine::Connected:
		case KIRC::Engine::Closing:
			// should make some extra check here
			setOnlineStatus( m_protocol->m_ServerStatusOnline );
			break;

		default:
			setOnlineStatus( m_protocol->m_StatusUnknown );
	}
}

const TQString IRCServerContact::caption() const
{
	return i18n("%1 @ %2").arg(ircAccount()->mySelf()->nickName() ).arg(
		kircEngine()->currentHost().isEmpty() ? ircAccount()->networkName() : kircEngine()->currentHost()
	);
}

void IRCServerContact::engineInternalError(KIRC::Engine::Error engineError, KIRC::Message &ircmsg)
{
	TQString error;
	switch (engineError)
	{
		case KIRC::Engine::ParsingFailed:
			error = i18n("KIRC Error - Parse error: ");
			break;
		case KIRC::Engine::UnknownCommand:
			error = i18n("KIRC Error - Unknown command: ");
			break;
		case KIRC::Engine::UnknownNumericReply:
			error = i18n("KIRC Error - Unknown numeric reply: ");
			break;
		case KIRC::Engine::InvalidNumberOfArguments:
			error = i18n("KIRC Error - Invalid number of arguments: ");
			break;
		case KIRC::Engine::MethodFailed:
			error = i18n("KIRC Error - Method failed: ");
			break;
		default:
			error = i18n("KIRC Error - Unknown error: ");
	}

	ircAccount()->appendMessage(error + TQString(ircmsg.raw()), IRCAccount::ErrorReply);
}

void IRCServerContact::slotSendMsg(Kopete::Message &, Kopete::ChatSession *manager)
{
	manager->messageSucceeded();
	Kopete::Message msg( manager->myself(), manager->members(),
		i18n("You can not talk to the server, you can only issue commands here. Type /help for supported commands."), Kopete::Message::Internal, Kopete::Message::PlainText, CHAT_VIEW);
	manager->appendMessage(msg);
}

void IRCServerContact::appendMessage( const TQString &message )
{
	Kopete::ContactPtrList members;
	members.append( this );
	Kopete::Message msg( this, members, message, Kopete::Message::Internal,
		Kopete::Message::RichText, CHAT_VIEW );
	appendMessage(msg);
}

void IRCServerContact::slotIncomingNotice( const TQString &orig, const TQString &notice )
{
	if (orig.isEmpty()) {
		// Prefix missing.
		// NOTICE AUTH :*** Checking Ident

		ircAccount()->appendMessage(i18n("NOTICE from %1: %2").arg(kircEngine()->currentHost(), notice),
				IRCAccount::NoticeReply);

	} else {
		// :Global!service@rizon.net NOTICE foobar :[Logon News - Oct 12 2005] Due to growing problems ...
		// :somenick!~fooobar@somehostname.fi NOTICE foobar :hello

		if (orig.contains('!')) {
			ircAccount()->appendMessage(i18n("NOTICE from %1 (%2): %3").arg(
						orig.section('!', 0, 0),
						orig.section('!', 1, 1),
						notice),
					IRCAccount::NoticeReply);
		} else {
			ircAccount()->appendMessage(i18n("NOTICE from %1: %2").arg(
						orig, notice), IRCAccount::NoticeReply);
		}
	}
}

void IRCServerContact::slotIncomingUnknown(const TQString &message)
{
	ircAccount()->appendMessage(message, IRCAccount::UnknownReply);
}

void IRCServerContact::slotIncomingConnect(const TQString &message)
{
	ircAccount()->appendMessage(message, IRCAccount::ConnectReply);
}

void IRCServerContact::slotIncomingMotd(const TQString &message)
{
	ircAccount()->appendMessage(message, IRCAccount::InfoReply);
}

void IRCServerContact::slotCannotSendToChannel(const TQString &channel, const TQString &message)
{
	ircAccount()->appendMessage(TQString::fromLatin1("%1: %2").arg(channel).arg(message), IRCAccount::ErrorReply);
}

void IRCServerContact::appendMessage(Kopete::Message &msg)
{
	msg.setImportance( Kopete::Message::Low ); //to don't distrub the user

	if (m_chatSession && m_chatSession->view(false))
		m_chatSession->appendMessage(msg);
/*
//	disable the buffering for now: cause a memleak since we don't made it a *fixed size fifo*
	else
		mMsgBuffer.append( msg );
*/
}

void IRCServerContact::slotDumpMessages()
{
	if (!mMsgBuffer.isEmpty())
	{
		manager()->appendMessage( mMsgBuffer.front() );
		mMsgBuffer.pop_front();
		TQTimer::singleShot( 0, this, TQT_SLOT( slotDumpMessages() ) );
	}
}

void IRCServerContact::slotViewCreated( KopeteView *v )
{
	kdDebug(14121) << k_funcinfo << "Created: " << v << ", mgr: " << v->msgManager() << ", Mine: " << m_chatSession << endl;
	if (m_chatSession && v->msgManager() == m_chatSession)
		TQTimer::singleShot(500, this, TQT_SLOT(slotDumpMessages()));
}

#include "ircservercontact.moc"