removing hardcoded aliases from userinputhandler. some minor, internal improvements...
[quassel.git] / src / core / userinputhandler.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-08 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 #include "userinputhandler.h"
21
22 #include "util.h"
23
24 #include "networkconnection.h"
25 #include "network.h"
26 #include "ctcphandler.h"
27 #include "ircuser.h"
28
29 #include <QDebug>
30
31 UserInputHandler::UserInputHandler(NetworkConnection *parent) : BasicHandler(parent) {
32 }
33
34 void UserInputHandler::handleUserInput(const BufferInfo &bufferInfo, const QString &msg_) {
35   try {
36     if(msg_.isEmpty())
37       return;
38     QString cmd;
39     QString msg = msg_;
40     if(!msg.startsWith('/')) {
41       cmd = QString("SAY");
42     } else {
43       cmd = msg.section(' ', 0, 0).remove(0, 1).toUpper();
44       msg = msg.section(' ', 1);
45     }
46     handle(cmd, Q_ARG(BufferInfo, bufferInfo), Q_ARG(QString, msg));
47   } catch(Exception e) {
48     emit displayMsg(Message::Error, bufferInfo.type(), "", e.msg());
49   }
50 }
51
52 // ====================
53 //  Public Slots
54 // ====================
55
56 void UserInputHandler::handleAway(const BufferInfo &bufferInfo, const QString &msg) {
57   Q_UNUSED(bufferInfo)
58
59   QString awayMsg = msg;
60   IrcUser *me = network()->me();
61
62   // if there is no message supplied we have to check if we are already away or not
63   if(msg.isEmpty()) {
64     if(me && !me->isAway())
65       awayMsg = networkConnection()->identity()->awayReason();
66   }
67   if(me)
68     me->setAwayMessage(awayMsg);
69   
70   putCmd("AWAY", serverEncode(awayMsg));
71 }
72
73 void UserInputHandler::handleBan(const BufferInfo &bufferInfo, const QString &msg) {
74   QString banChannel;
75   QString banUser;
76
77   QStringList params = msg.split(" ");
78
79   if(!params.isEmpty() && isChannelName(params[0])) {
80     banChannel = params.takeFirst();
81   } else if(bufferInfo.type() == BufferInfo::ChannelBuffer) {
82     banChannel = bufferInfo.bufferName();
83   } else {
84     emit displayMsg(Message::Error, BufferInfo::StatusBuffer, "", QString("Error: channel unknown in command: /BAN %1").arg(msg));
85     return;
86   }
87
88   if(!params.isEmpty() && !params.contains("!") && network()->ircUser(params[0])) {
89     IrcUser *ircuser = network()->ircUser(params[0]);
90     // generalizedHost changes <nick> to  *!ident@*.sld.tld.
91     QString generalizedHost = ircuser->host();
92     if(generalizedHost.isEmpty()) {
93       emit displayMsg(Message::Error, BufferInfo::StatusBuffer, "", QString("Error: host unknown in command: /BAN %1").arg(msg));
94       return;
95     }
96
97     if(generalizedHost.lastIndexOf(".") != -1 && generalizedHost.lastIndexOf(".", generalizedHost.lastIndexOf(".")-1) != -1) {
98       int secondLastPeriodPosition = generalizedHost.lastIndexOf(".", generalizedHost.lastIndexOf(".")-1);
99       generalizedHost.replace(0, secondLastPeriodPosition, "*");
100     }
101     banUser = QString("*!%1@%2").arg(ircuser->user()).arg(generalizedHost);
102   } else {
103     banUser = params.join(" ");
104   }
105
106   QString banMsg = QString("MODE %1 +b %2").arg(banChannel).arg(banUser);
107   emit putRawLine(serverEncode(banMsg));
108 }
109
110 void UserInputHandler::handleCtcp(const BufferInfo &bufferInfo, const QString &msg) {
111   Q_UNUSED(bufferInfo)
112   QString nick = msg.section(' ', 0, 0);
113   QString ctcpTag = msg.section(' ', 1, 1).toUpper();
114   if (ctcpTag.isEmpty()) return;
115   QString message = "";
116   QString verboseMessage = tr("sending CTCP-%1 request").arg(ctcpTag);
117
118   if(ctcpTag == "PING") {
119     uint now = QDateTime::currentDateTime().toTime_t();
120     message = QString::number(now);
121   }
122
123   networkConnection()->ctcpHandler()->query(nick, ctcpTag, message);
124   emit displayMsg(Message::Action, BufferInfo::StatusBuffer, "", verboseMessage, network()->myNick());
125 }
126
127 void UserInputHandler::handleDeop(const BufferInfo &bufferInfo, const QString &msg) {
128   QStringList nicks = msg.split(' ', QString::SkipEmptyParts);
129   QString m = "-"; for(int i = 0; i < nicks.count(); i++) m += 'o';
130   QStringList params;
131   params << bufferInfo.bufferName() << m << nicks;
132   emit putCmd("MODE", serverEncode(params));
133 }
134
135 void UserInputHandler::handleDevoice(const BufferInfo &bufferInfo, const QString &msg) {
136   QStringList nicks = msg.split(' ', QString::SkipEmptyParts);
137   QString m = "-"; for(int i = 0; i < nicks.count(); i++) m += 'v';
138   QStringList params;
139   params << bufferInfo.bufferName() << m << nicks;
140   emit putCmd("MODE", serverEncode(params));
141 }
142
143 void UserInputHandler::handleInvite(const BufferInfo &bufferInfo, const QString &msg) {
144   QStringList params;
145   params << msg << bufferInfo.bufferName();
146   emit putCmd("INVITE", serverEncode(params));
147 }
148
149 void UserInputHandler::handleJoin(const BufferInfo &bufferInfo, const QString &msg) {
150   Q_UNUSED(bufferInfo)
151   QStringList params = msg.trimmed().split(" ");
152   QStringList chans = params[0].split(",");
153   QStringList keys;
154   if(params.count() > 1) keys = params[1].split(",");
155   emit putCmd("JOIN", serverEncode(params)); // FIXME handle messages longer than 512 bytes!
156   int i = 0;
157   for(; i < keys.count(); i++) {
158     if(i >= chans.count()) break;
159     networkConnection()->addChannelKey(chans[i], keys[i]);
160   }
161   for(; i < chans.count(); i++) {
162     networkConnection()->removeChannelKey(chans[i]);
163   }
164 }
165
166 void UserInputHandler::handleKick(const BufferInfo &bufferInfo, const QString &msg) {
167   QString nick = msg.section(' ', 0, 0, QString::SectionSkipEmpty);
168   QString reason = msg.section(' ', 1, -1, QString::SectionSkipEmpty).trimmed();
169   if(reason.isEmpty())
170     reason = networkConnection()->identity()->kickReason();
171
172   QList<QByteArray> params;
173   params << serverEncode(bufferInfo.bufferName()) << serverEncode(nick) << channelEncode(bufferInfo.bufferName(), reason);
174   emit putCmd("KICK", params);
175 }
176
177 void UserInputHandler::handleKill(const BufferInfo &bufferInfo, const QString &msg) {
178   Q_UNUSED(bufferInfo)
179   QString nick = msg.section(' ', 0, 0, QString::SectionSkipEmpty);
180   QString pass = msg.section(' ', 1, -1, QString::SectionSkipEmpty);
181   QList<QByteArray> params;
182   params << serverEncode(nick) << serverEncode(pass);
183   emit putCmd("KILL", params);
184 }
185
186
187 void UserInputHandler::handleList(const BufferInfo &bufferInfo, const QString &msg) {
188   Q_UNUSED(bufferInfo)
189   emit putCmd("LIST", serverEncode(msg.split(' ', QString::SkipEmptyParts)));
190 }
191
192 void UserInputHandler::handleMe(const BufferInfo &bufferInfo, const QString &msg) {
193   if(bufferInfo.bufferName().isEmpty()) return; // server buffer
194   networkConnection()->ctcpHandler()->query(bufferInfo.bufferName(), "ACTION", msg);
195   emit displayMsg(Message::Action, bufferInfo.type(), bufferInfo.bufferName(), msg, network()->myNick());
196 }
197
198 void UserInputHandler::handleMode(const BufferInfo &bufferInfo, const QString &msg) {
199   Q_UNUSED(bufferInfo)
200
201   QStringList params = msg.split(' ', QString::SkipEmptyParts);
202   // if the first argument is neither a channel nor us (user modes are only to oneself) the current buffer is assumed to be the target
203   if(!params.isEmpty() && !network()->isChannelName(params[0]) && !network()->isMyNick(params[0]))
204     params.prepend(bufferInfo.bufferName());
205   
206   // TODO handle correct encoding for buffer modes (channelEncode())
207   emit putCmd("MODE", serverEncode(params));
208 }
209
210 // TODO: show privmsgs
211 void UserInputHandler::handleMsg(const BufferInfo &bufferInfo, const QString &msg) {
212   Q_UNUSED(bufferInfo);
213   if(!msg.contains(' '))
214     return;
215
216   QList<QByteArray> params;
217   params << serverEncode(msg.section(' ', 0, 0));
218   params << userEncode(params[0], msg.section(' ', 1));
219
220   emit putCmd("PRIVMSG", params);
221 }
222
223 void UserInputHandler::handleNick(const BufferInfo &bufferInfo, const QString &msg) {
224   Q_UNUSED(bufferInfo)
225   QString nick = msg.section(' ', 0, 0);
226   emit putCmd("NICK", serverEncode(nick));
227 }
228
229 void UserInputHandler::handleOp(const BufferInfo &bufferInfo, const QString &msg) {
230   QStringList nicks = msg.split(' ', QString::SkipEmptyParts);
231   QString m = "+"; for(int i = 0; i < nicks.count(); i++) m += 'o';
232   QStringList params;
233   params << bufferInfo.bufferName() << m << nicks;
234   emit putCmd("MODE", serverEncode(params));
235 }
236
237 void UserInputHandler::handleOper(const BufferInfo &bufferInfo, const QString &msg) {
238   Q_UNUSED(bufferInfo)
239   emit putRawLine(serverEncode(QString("OPER %1").arg(msg)));
240 }
241
242 void UserInputHandler::handlePart(const BufferInfo &bufferInfo, const QString &msg) {
243   QList<QByteArray> params;
244   QString partReason;
245
246   // msg might contain either a channel name and/or a reaon, so we have to check if the first word is a known channel
247   QString channelName = msg.section(' ', 0, 0);
248   if(channelName.isEmpty() || !network()->ircChannel(channelName)) {
249     channelName = bufferInfo.bufferName();
250     partReason = msg;
251   } else {
252     partReason = msg.mid(channelName.length() + 1);
253   }
254   
255   if(partReason.isEmpty())
256     partReason = networkConnection()->identity()->partReason();
257
258   params << serverEncode(channelName) << channelEncode(bufferInfo.bufferName(), partReason);
259   emit putCmd("PART", params);
260 }
261
262 void UserInputHandler::handlePing(const BufferInfo &bufferInfo, const QString &msg) {
263   Q_UNUSED(bufferInfo)
264
265   QString param = msg;
266   if(param.isEmpty())
267     param = QTime::currentTime().toString("hh:mm:ss.zzz");
268
269   putCmd("PING", serverEncode(param));
270 }
271
272 // TODO: implement queries
273 void UserInputHandler::handleQuery(const BufferInfo &bufferInfo, const QString &msg) {
274   Q_UNUSED(bufferInfo)
275   QString target = msg.section(' ', 0, 0);
276   QString message = msg.section(' ', 1);
277   if(message.isEmpty())
278     emit displayMsg(Message::Server, BufferInfo::QueryBuffer, target, "Starting query with " + target, network()->myNick(), Message::Self);
279   else
280     emit displayMsg(Message::Plain, BufferInfo::QueryBuffer, target, message, network()->myNick(), Message::Self);
281   handleMsg(bufferInfo, msg);
282 }
283
284 void UserInputHandler::handleQuit(const BufferInfo &bufferInfo, const QString &msg) {
285   Q_UNUSED(bufferInfo)
286
287   QString quitReason;
288   if(msg.isEmpty())
289     quitReason = networkConnection()->identity()->quitReason();
290   else
291     quitReason = msg;
292
293   emit putCmd("QUIT", serverEncode(quitReason));
294   networkConnection()->disconnectFromIrc();
295 }
296
297 void UserInputHandler::handleQuote(const BufferInfo &bufferInfo, const QString &msg) {
298   Q_UNUSED(bufferInfo)
299   emit putRawLine(serverEncode(msg));
300 }
301
302 void UserInputHandler::handleSay(const BufferInfo &bufferInfo, const QString &msg) {
303   if(bufferInfo.bufferName().isEmpty()) return;  // server buffer
304   QList<QByteArray> params;
305   params << serverEncode(bufferInfo.bufferName()) << channelEncode(bufferInfo.bufferName(), msg);
306   emit putCmd("PRIVMSG", params);
307   emit displayMsg(Message::Plain, bufferInfo.type(), bufferInfo.bufferName(), msg, network()->myNick(), Message::Self);
308 }
309
310 void UserInputHandler::handleTopic(const BufferInfo &bufferInfo, const QString &msg) {
311   if(bufferInfo.bufferName().isEmpty()) return;
312   if(!msg.isEmpty()) {
313     QList<QByteArray> params;
314     params << serverEncode(bufferInfo.bufferName()) << channelEncode(bufferInfo.bufferName(), msg);
315     emit putCmd("TOPIC", params);
316   } else {
317     emit networkConnection()->putRawLine("TOPIC " + serverEncode(bufferInfo.bufferName()) + " :");
318   }
319 }
320
321 void UserInputHandler::handleVoice(const BufferInfo &bufferInfo, const QString &msg) {
322   QStringList nicks = msg.split(' ', QString::SkipEmptyParts);
323   QString m = "+"; for(int i = 0; i < nicks.count(); i++) m += 'v';
324   QStringList params;
325   params << bufferInfo.bufferName() << m << nicks;
326   emit putCmd("MODE", serverEncode(params));
327 }
328
329 void UserInputHandler::handleWho(const BufferInfo &bufferInfo, const QString &msg) {
330   Q_UNUSED(bufferInfo)
331   emit putCmd("WHO", serverEncode(msg.split(' ')));
332 }
333
334 void UserInputHandler::handleWhois(const BufferInfo &bufferInfo, const QString &msg) {
335   Q_UNUSED(bufferInfo)
336   emit putCmd("WHOIS", serverEncode(msg.split(' ')));
337 }
338
339 void UserInputHandler::handleWhowas(const BufferInfo &bufferInfo, const QString &msg) {
340   Q_UNUSED(bufferInfo)
341   emit putCmd("WHOWAS", serverEncode(msg.split(' ')));
342 }
343
344 void UserInputHandler::defaultHandler(QString cmd, const BufferInfo &bufferInfo, const QString &msg) {
345   for(int i = 0; i < coreSession()->aliasManager().count(); i++) {
346     if(coreSession()->aliasManager()[i].name.toLower() == cmd.toLower()) {
347       expand(coreSession()->aliasManager()[i].expansion, bufferInfo, msg);
348       return;
349     }
350   }
351   emit displayMsg(Message::Error, BufferInfo::StatusBuffer, "", QString("Error: %1 %2").arg(cmd).arg(msg));
352 }
353
354 void UserInputHandler::expand(const QString &alias, const BufferInfo &bufferInfo, const QString &msg) {
355   QStringList commands = alias.split(QRegExp("; ?"));
356   QStringList params = msg.split(' ');
357   for(int i = 0; i < commands.count(); i++) {
358     QString command = commands[i];
359     for(int j = params.count(); j > 0; j--) {
360       command = command.replace(QString("$%1").arg(j), params[j - 1]);
361     }
362     command = command.replace("$0", msg);
363     handleUserInput(bufferInfo, command);
364   }
365 }
366
367
368