fb9f903c1397d044ead91f6f3991b7cfdae9ce27
[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 /* RPL_UMODEIS - "<user_modes> [<user_mode_params>]" */
174 void CoreSessionEventProcessor::processIrcEvent221(IrcEvent *) {
175   // TODO: save information in network object
176 }
177
178 /* RPL_STATSCONN - "Highest connection cout: 8000 (7999 clients)" */
179 void CoreSessionEventProcessor::processIrcEvent250(IrcEvent *) {
180   // TODO: save information in network object
181 }
182
183 /* RPL_LOCALUSERS - "Current local user: 5024  Max: 7999 */
184 void CoreSessionEventProcessor::processIrcEvent265(IrcEvent *) {
185   // TODO: save information in network object
186 }
187
188 /* RPL_GLOBALUSERS - "Current global users: 46093  Max: 47650" */
189 void CoreSessionEventProcessor::processIrcEvent266(IrcEvent *) {
190   // TODO: save information in network object
191 }
192
193 /*
194 WHOIS-Message:
195    Replies 311 - 313, 317 - 319 are all replies generated in response to a WHOIS message.
196   and 301 (RPL_AWAY)
197               "<nick> :<away message>"
198 WHO-Message:
199    Replies 352 and 315 paired are used to answer a WHO message.
200
201 WHOWAS-Message:
202    Replies 314 and 369 are responses to a WHOWAS message.
203
204 */
205
206 /* RPL_AWAY - "<nick> :<away message>" */
207 void CoreSessionEventProcessor::processIrcEvent301(IrcEvent *e) {
208   if(!checkParamCount(e, 2))
209     return;
210
211   IrcUser *ircuser = e->network()->ircUser(e->params().at(0));
212   if(ircuser) {
213     ircuser->setAway(true);
214     ircuser->setAwayMessage(e->params().at(1));
215     //ircuser->setLastAwayMessage(now);
216   }
217 }
218
219 /* RPL_UNAWAY - ":You are no longer marked as being away" */
220 void CoreSessionEventProcessor::processIrcEvent305(IrcEvent *e) {
221   IrcUser *me = e->network()->me();
222   if(me)
223     me->setAway(false);
224
225   if(e->network()->autoAwayActive())
226     e->network()->setAutoAwayActive(false);
227 }
228
229 /* RPL_NOWAWAY - ":You have been marked as being away" */
230 void CoreSessionEventProcessor::processIrcEvent306(IrcEvent *e) {
231   IrcUser *me = e->network()->me();
232   if(me)
233     me->setAway(true);
234 }
235
236 /* template
237 void CoreSessionEventProcessor::processIrcEvent(IrcEvent *e) {
238   if(!checkParamCount(e, 1))
239     return;
240
241 }
242 */