Event backend porting
[quassel.git] / src / core / coresessioneventprocessor.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2010 by the Quassel Project                        *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
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.                                           *
9  *                                                                         *
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.                          *
14  *                                                                         *
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  ***************************************************************************/
20
21 #include "coresessioneventprocessor.h"
22
23 #include "corenetwork.h"
24 #include "coresession.h"
25 #include "ircevent.h"
26
27 CoreSessionEventProcessor::CoreSessionEventProcessor(CoreSession *session)
28   : QObject(session),
29   _coreSession(session)
30 {
31
32 }
33
34 bool CoreSessionEventProcessor::checkParamCount(IrcEvent *e, int minParams) {
35   if(e->params().count() < minParams) {
36     if(e->type() == EventManager::IrcEventNumeric) {
37       qWarning() << "Command " << static_cast<IrcEventNumeric *>(e)->number() << " requires " << minParams << "params, got: " << e->params();
38     } else {
39       QString name = coreSession()->eventManager()->enumName(e->type());
40       qWarning() << qPrintable(name) << "requires" << minParams << "params, got:" << e->params();
41     }
42     e->stop();
43     return false;
44   }
45   return true;
46 }
47
48 void CoreSessionEventProcessor::processIrcEventNumeric(IrcEventNumeric *e) {
49   switch(e->number()) {
50
51   // CAP stuff
52   case 903: case 904: case 905: case 906: case 907:
53     qobject_cast<CoreNetwork *>(e->network())->putRawLine("CAP END");
54     break;
55
56   default:
57     break;
58   }
59 }
60
61 void CoreSessionEventProcessor::processIrcEventAuthenticate(IrcEvent *e) {
62   if(!checkParamCount(e, 1))
63     return;
64
65   if(e->params().at(0) != "+") {
66     qWarning() << "Invalid AUTHENTICATE" << e;
67     return;
68   }
69
70   CoreNetwork *net = coreNetwork(e);
71
72   QString construct = net->saslAccount();
73   construct.append(QChar(QChar::Null));
74   construct.append(net->saslAccount());
75   construct.append(QChar(QChar::Null));
76   construct.append(net->saslPassword());
77   QByteArray saslData = QByteArray(construct.toAscii().toBase64());
78   saslData.prepend("AUTHENTICATE ");
79   net->putRawLine(saslData);
80 }
81
82 void CoreSessionEventProcessor::processIrcEventCap(IrcEvent *e) {
83   // for SASL, there will only be a single param of 'sasl', however you can check here for
84   // additional CAP messages (ls, multi-prefix, et cetera).
85
86   if(e->params().count() == 3) {
87     if(e->params().at(2) == "sasl") {
88       // FIXME use event
89       coreNetwork(e)->putRawLine(coreNetwork(e)->serverEncode("AUTHENTICATE PLAIN")); // Only working with PLAIN atm, blowfish later
90     }
91   }
92 }
93
94 void CoreSessionEventProcessor::processIrcEventInvite(IrcEvent *e) {
95   if(checkParamCount(e, 2)) {
96     e->network()->updateNickFromMask(e->prefix());
97   }
98 }
99
100 void CoreSessionEventProcessor::processIrcEventKick(IrcEvent *e) {
101   if(checkParamCount(e, 2)) {
102     e->network()->updateNickFromMask(e->prefix());
103     IrcUser *victim = e->network()->ircUser(e->params().at(1));
104     if(victim) {
105       victim->partChannel(e->params().at(0));
106       //if(e->network()->isMe(victim)) e->network()->setKickedFromChannel(channel);
107     }
108   }
109 }
110
111 void CoreSessionEventProcessor::processIrcEventNick(IrcEvent *e) {
112   if(checkParamCount(e, 1)) {
113     IrcUser *ircuser = e->network()->updateNickFromMask(e->prefix());
114     if(!ircuser) {
115       qWarning() << Q_FUNC_INFO << "Unknown IrcUser!";
116       return;
117     }
118     QString newnick = e->params().at(0);
119     QString oldnick = ircuser->nick();
120
121     // the order is cruicial
122     // otherwise the client would rename the buffer, see that the assigned ircuser doesn't match anymore
123     // and remove the ircuser from the querybuffer leading to a wrong on/offline state
124     ircuser->setNick(newnick);
125     coreSession()->renameBuffer(e->networkId(), newnick, oldnick);
126   }
127 }
128
129 void CoreSessionEventProcessor::processIrcEventPart(IrcEvent *e) {
130   if(checkParamCount(e, 1)) {
131     IrcUser *ircuser = e->network()->updateNickFromMask(e->prefix());
132     if(!ircuser) {
133       qWarning() << Q_FUNC_INFO<< "Unknown IrcUser!";
134       return;
135     }
136     QString channel = e->params().at(0);
137     ircuser->partChannel(channel);
138     if(e->network()->isMe(ircuser))
139       qobject_cast<CoreNetwork *>(e->network())->setChannelParted(channel);
140   }
141 }
142
143 void CoreSessionEventProcessor::processIrcEventPong(IrcEvent *e) {
144   // the server is supposed to send back what we passed as param. and we send a timestamp
145   // but using quote and whatnought one can send arbitrary pings, so we have to do some sanity checks
146   if(checkParamCount(e, 2)) {
147     QString timestamp = e->params().at(1);
148     QTime sendTime = QTime::fromString(timestamp, "hh:mm:ss.zzz");
149     if(sendTime.isValid())
150       e->network()->setLatency(sendTime.msecsTo(QTime::currentTime()) / 2);
151   }
152 }
153
154 void CoreSessionEventProcessor::processIrcEventTopic(IrcEvent *e) {
155   if(checkParamCount(e, 2)) {
156     e->network()->updateNickFromMask(e->prefix());
157     IrcChannel *channel = e->network()->ircChannel(e->params().at(0));
158     if(channel)
159       channel->setTopic(e->params().at(1));
160   }
161 }
162
163 /* RPL_WELCOME */
164 void CoreSessionEventProcessor::processIrcEvent001(IrcEvent *e) {
165   if(!checkParamCount(e, 1))
166     return;
167
168   QString myhostmask = e->params().at(0).section(' ', -1, -1);
169   e->network()->setCurrentServer(e->prefix());
170   e->network()->setMyNick(nickFromMask(myhostmask));
171 }
172
173 /* template
174 void CoreSessionEventProcessor::processIrcEvent(IrcEvent *e) {
175   if(!checkParamCount(e, 1))
176     return;
177
178 }
179 */