1 /***************************************************************************
2 * Copyright (C) 2005-2010 by the Quassel Project *
3 * devel@quassel-irc.org *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) version 3. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
21 #include "coresessioneventprocessor.h"
23 #include "coreirclisthelper.h"
24 #include "corenetwork.h"
25 #include "coresession.h"
29 CoreSessionEventProcessor::CoreSessionEventProcessor(CoreSession *session)
36 bool CoreSessionEventProcessor::checkParamCount(IrcEvent *e, int minParams) {
37 if(e->params().count() < minParams) {
38 if(e->type() == EventManager::IrcEventNumeric) {
39 qWarning() << "Command " << static_cast<IrcEventNumeric *>(e)->number() << " requires " << minParams << "params, got: " << e->params();
41 QString name = coreSession()->eventManager()->enumName(e->type());
42 qWarning() << qPrintable(name) << "requires" << minParams << "params, got:" << e->params();
50 void CoreSessionEventProcessor::processIrcEventNumeric(IrcEventNumeric *e) {
54 case 903: case 904: case 905: case 906: case 907:
55 qobject_cast<CoreNetwork *>(e->network())->putRawLine("CAP END");
63 void CoreSessionEventProcessor::processIrcEventAuthenticate(IrcEvent *e) {
64 if(!checkParamCount(e, 1))
67 if(e->params().at(0) != "+") {
68 qWarning() << "Invalid AUTHENTICATE" << e;
72 CoreNetwork *net = coreNetwork(e);
74 QString construct = net->saslAccount();
75 construct.append(QChar(QChar::Null));
76 construct.append(net->saslAccount());
77 construct.append(QChar(QChar::Null));
78 construct.append(net->saslPassword());
79 QByteArray saslData = QByteArray(construct.toAscii().toBase64());
80 saslData.prepend("AUTHENTICATE ");
81 net->putRawLine(saslData);
84 void CoreSessionEventProcessor::processIrcEventCap(IrcEvent *e) {
85 // for SASL, there will only be a single param of 'sasl', however you can check here for
86 // additional CAP messages (ls, multi-prefix, et cetera).
88 if(e->params().count() == 3) {
89 if(e->params().at(2) == "sasl") {
91 coreNetwork(e)->putRawLine(coreNetwork(e)->serverEncode("AUTHENTICATE PLAIN")); // Only working with PLAIN atm, blowfish later
96 void CoreSessionEventProcessor::processIrcEventInvite(IrcEvent *e) {
97 if(checkParamCount(e, 2)) {
98 e->network()->updateNickFromMask(e->prefix());
102 void CoreSessionEventProcessor::processIrcEventKick(IrcEvent *e) {
103 if(checkParamCount(e, 2)) {
104 e->network()->updateNickFromMask(e->prefix());
105 IrcUser *victim = e->network()->ircUser(e->params().at(1));
107 victim->partChannel(e->params().at(0));
108 //if(e->network()->isMe(victim)) e->network()->setKickedFromChannel(channel);
113 void CoreSessionEventProcessor::processIrcEventNick(IrcEvent *e) {
114 if(checkParamCount(e, 1)) {
115 IrcUser *ircuser = e->network()->updateNickFromMask(e->prefix());
117 qWarning() << Q_FUNC_INFO << "Unknown IrcUser!";
120 QString newnick = e->params().at(0);
121 QString oldnick = ircuser->nick();
123 // the order is cruicial
124 // otherwise the client would rename the buffer, see that the assigned ircuser doesn't match anymore
125 // and remove the ircuser from the querybuffer leading to a wrong on/offline state
126 ircuser->setNick(newnick);
127 coreSession()->renameBuffer(e->networkId(), newnick, oldnick);
131 void CoreSessionEventProcessor::processIrcEventPart(IrcEvent *e) {
132 if(checkParamCount(e, 1)) {
133 IrcUser *ircuser = e->network()->updateNickFromMask(e->prefix());
135 qWarning() << Q_FUNC_INFO<< "Unknown IrcUser!";
138 QString channel = e->params().at(0);
139 ircuser->partChannel(channel);
140 if(e->network()->isMe(ircuser))
141 qobject_cast<CoreNetwork *>(e->network())->setChannelParted(channel);
145 void CoreSessionEventProcessor::processIrcEventPong(IrcEvent *e) {
146 // the server is supposed to send back what we passed as param. and we send a timestamp
147 // but using quote and whatnought one can send arbitrary pings, so we have to do some sanity checks
148 if(checkParamCount(e, 2)) {
149 QString timestamp = e->params().at(1);
150 QTime sendTime = QTime::fromString(timestamp, "hh:mm:ss.zzz");
151 if(sendTime.isValid())
152 e->network()->setLatency(sendTime.msecsTo(QTime::currentTime()) / 2);
156 void CoreSessionEventProcessor::processIrcEventTopic(IrcEvent *e) {
157 if(checkParamCount(e, 2)) {
158 e->network()->updateNickFromMask(e->prefix());
159 IrcChannel *channel = e->network()->ircChannel(e->params().at(0));
161 channel->setTopic(e->params().at(1));
166 void CoreSessionEventProcessor::processIrcEvent001(IrcEvent *e) {
167 if(!checkParamCount(e, 1))
170 QString myhostmask = e->params().at(0).section(' ', -1, -1);
171 e->network()->setCurrentServer(e->prefix());
172 e->network()->setMyNick(nickFromMask(myhostmask));
176 // TODO Complete 005 handling, also use sensible defaults for non-sent stuff
177 void CoreSessionEventProcessor::processIrcEvent005(IrcEvent *e) {
178 if(!checkParamCount(e, 1))
182 for(int i = 0; i < e->params().count() - 1; i++) {
183 QString key = e->params()[i].section("=", 0, 0);
184 QString value = e->params()[i].section("=", 1);
185 e->network()->addSupport(key, value);
188 /* determine our prefixes here to get an accurate result */
189 e->network()->determinePrefixes();
192 /* RPL_UMODEIS - "<user_modes> [<user_mode_params>]" */
193 void CoreSessionEventProcessor::processIrcEvent221(IrcEvent *) {
194 // TODO: save information in network object
197 /* RPL_STATSCONN - "Highest connection cout: 8000 (7999 clients)" */
198 void CoreSessionEventProcessor::processIrcEvent250(IrcEvent *) {
199 // TODO: save information in network object
202 /* RPL_LOCALUSERS - "Current local user: 5024 Max: 7999 */
203 void CoreSessionEventProcessor::processIrcEvent265(IrcEvent *) {
204 // TODO: save information in network object
207 /* RPL_GLOBALUSERS - "Current global users: 46093 Max: 47650" */
208 void CoreSessionEventProcessor::processIrcEvent266(IrcEvent *) {
209 // TODO: save information in network object
214 Replies 311 - 313, 317 - 319 are all replies generated in response to a WHOIS message.
216 "<nick> :<away message>"
218 Replies 352 and 315 paired are used to answer a WHO message.
221 Replies 314 and 369 are responses to a WHOWAS message.
225 /* RPL_AWAY - "<nick> :<away message>" */
226 void CoreSessionEventProcessor::processIrcEvent301(IrcEvent *e) {
227 if(!checkParamCount(e, 2))
230 IrcUser *ircuser = e->network()->ircUser(e->params().at(0));
232 ircuser->setAway(true);
233 ircuser->setAwayMessage(e->params().at(1));
234 //ircuser->setLastAwayMessage(now);
238 /* RPL_UNAWAY - ":You are no longer marked as being away" */
239 void CoreSessionEventProcessor::processIrcEvent305(IrcEvent *e) {
240 IrcUser *me = e->network()->me();
244 if(e->network()->autoAwayActive()) {
245 e->network()->setAutoAwayActive(false);
246 e->setFlag(EventManager::Silent);
250 /* RPL_NOWAWAY - ":You have been marked as being away" */
251 void CoreSessionEventProcessor::processIrcEvent306(IrcEvent *e) {
252 IrcUser *me = e->network()->me();
257 /* RPL_WHOISSERVICE - "<user> is registered nick" */
258 void CoreSessionEventProcessor::processIrcEvent307(IrcEvent *e) {
259 if(!checkParamCount(e, 1))
262 IrcUser *ircuser = e->network()->ircUser(e->params().at(0));
264 ircuser->setWhoisServiceReply(e->params().join(" "));
267 /* RPL_SUSERHOST - "<user> is available for help." */
268 void CoreSessionEventProcessor::processIrcEvent310(IrcEvent *e) {
269 if(!checkParamCount(e, 1))
272 IrcUser *ircuser = e->network()->ircUser(e->params().at(0));
274 ircuser->setSuserHost(e->params().join(" "));
277 /* RPL_WHOISUSER - "<nick> <user> <host> * :<real name>" */
278 void CoreSessionEventProcessor::processIrcEvent311(IrcEvent *e) {
279 if(!checkParamCount(e, 3))
282 IrcUser *ircuser = e->network()->ircUser(e->params().at(0));
284 ircuser->setUser(e->params().at(1));
285 ircuser->setHost(e->params().at(2));
286 ircuser->setRealName(e->params().last());
290 /* RPL_WHOISSERVER - "<nick> <server> :<server info>" */
291 void CoreSessionEventProcessor::processIrcEvent312(IrcEvent *e) {
292 if(!checkParamCount(e, 2))
295 IrcUser *ircuser = e->network()->ircUser(e->params().at(0));
297 ircuser->setServer(e->params().at(1));
300 /* RPL_WHOISOPERATOR - "<nick> :is an IRC operator" */
301 void CoreSessionEventProcessor::processIrcEvent313(IrcEvent *e) {
302 if(!checkParamCount(e, 1))
305 IrcUser *ircuser = e->network()->ircUser(e->params().at(0));
307 ircuser->setIrcOperator(e->params().last());
310 /* RPL_ENDOFWHO: "<name> :End of WHO list" */
311 void CoreSessionEventProcessor::processIrcEvent315(IrcEvent *e) {
312 if(!checkParamCount(e, 1))
315 if(coreNetwork(e)->setAutoWhoDone(e->params()[0]))
316 e->setFlag(EventManager::Silent);
319 /* RPL_WHOISIDLE - "<nick> <integer> :seconds idle"
320 (real life: "<nick> <integer> <integer> :seconds idle, signon time) */
321 void CoreSessionEventProcessor::processIrcEvent317(IrcEvent *e) {
322 if(!checkParamCount(e, 2))
327 int idleSecs = e->params()[1].toInt();
328 if(e->params().count() > 3) { // if we have more then 3 params we have the above mentioned "real life" situation
329 int logintime = e->params()[2].toInt();
330 loginTime = QDateTime::fromTime_t(logintime);
333 IrcUser *ircuser = e->network()->ircUser(e->params()[0]);
335 ircuser->setIdleTime(e->timestamp().addSecs(-idleSecs));
336 if(loginTime.isValid())
337 ircuser->setLoginTime(loginTime);
341 /* RPL_LIST - "<channel> <# visible> :<topic>" */
342 void CoreSessionEventProcessor::processIrcEvent322(IrcEvent *e) {
343 if(!checkParamCount(e, 1))
347 quint32 userCount = 0;
350 switch(e->params().count()) {
352 topic = e->params()[2];
354 userCount = e->params()[1].toUInt();
356 channelName = e->params()[0];
360 if(coreSession()->ircListHelper()->addChannel(e->networkId(), channelName, userCount, topic))
361 e->stop(); // consumed by IrcListHelper, so don't further process/show this event
364 /* RPL_LISTEND ":End of LIST" */
365 void CoreSessionEventProcessor::processIrcEvent323(IrcEvent *e) {
366 if(!checkParamCount(e, 1))
369 if(coreSession()->ircListHelper()->endOfChannelList(e->networkId()))
370 e->stop(); // consumed by IrcListHelper, so don't further process/show this event
374 void CoreSessionEventProcessor::processIrcEvent331(IrcEvent *e) {
375 if(!checkParamCount(e, 1))
378 IrcChannel *chan = e->network()->ircChannel(e->params()[0]);
380 chan->setTopic(QString());
384 void CoreSessionEventProcessor::processIrcEvent332(IrcEvent *e) {
385 if(!checkParamCount(e, 2))
388 IrcChannel *chan = e->network()->ircChannel(e->params()[0]);
390 chan->setTopic(e->params()[1]);
393 /* RPL_WHOREPLY: "<channel> <user> <host> <server> <nick>
394 ( "H" / "G" > ["*"] [ ( "@" / "+" ) ] :<hopcount> <real name>" */
395 void CoreSessionEventProcessor::processIrcEvent352(IrcEvent *e) {
396 if(!checkParamCount(e, 6))
399 QString channel = e->params()[0];
400 IrcUser *ircuser = e->network()->ircUser(e->params()[4]);
402 ircuser->setUser(e->params()[1]);
403 ircuser->setHost(e->params()[2]);
405 bool away = e->params()[5].startsWith("G");
406 ircuser->setAway(away);
407 ircuser->setServer(e->params()[3]);
408 ircuser->setRealName(e->params().last().section(" ", 1));
411 if(coreNetwork(e)->isAutoWhoInProgress(channel))
412 e->setFlag(EventManager::Silent);
416 void CoreSessionEventProcessor::processIrcEvent353(IrcEvent *e) {
417 if(!checkParamCount(e, 3))
420 // param[0] is either "=", "*" or "@" indicating a public, private or secret channel
421 // we don't use this information at the time beeing
422 QString channelname = e->params()[1];
424 IrcChannel *channel = e->network()->ircChannel(channelname);
426 qWarning() << Q_FUNC_INFO << "Received unknown target channel:" << channelname;
433 foreach(QString nick, e->params()[2].split(' ')) {
436 if(e->network()->prefixes().contains(nick[0])) {
437 mode = e->network()->prefixToMode(nick[0]);
445 channel->joinIrcUsers(nicks, modes);
449 void CoreSessionEventProcessor::processIrcEvent(IrcEvent *e) {
450 if(!checkParamCount(e, 1))