Moving alias expansion yet again - this time into AliasManager itself
[quassel.git] / src / common / aliasmanager.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-09 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
22 #include <QDebug>
23 #include <QStringList>
24
25 #include "aliasmanager.h"
26 #include "network.h"
27
28 AliasManager &AliasManager::operator=(const AliasManager &other) {
29   if(this == &other)
30     return *this;
31
32   SyncableObject::operator=(other);
33   _aliases = other._aliases;
34   return *this;
35 }
36
37 int AliasManager::indexOf(const QString &name) const {
38   for(int i = 0; i < _aliases.count(); i++) {
39     if(_aliases[i].name == name)
40       return i;
41   }
42   return -1;
43 }
44
45 QVariantMap AliasManager::initAliases() const {
46   QVariantMap aliases;
47   QStringList names;
48   QStringList expansions;
49
50   for(int i = 0; i < _aliases.count(); i++) {
51     names << _aliases[i].name;
52     expansions << _aliases[i].expansion;
53   }
54
55   aliases["names"] = names;
56   aliases["expansions"] = expansions;
57   return aliases;
58 }
59
60 void AliasManager::initSetAliases(const QVariantMap &aliases) {
61   QStringList names = aliases["names"].toStringList();
62   QStringList expansions = aliases["expansions"].toStringList();
63
64   if(names.count() != expansions.count()) {
65     qWarning() << "AliasesManager::initSetAliases: received" << names.count() << "alias names but only" << expansions.count() << "expansions!";
66     return;
67   }
68
69   _aliases.clear();
70   for(int i = 0; i < names.count(); i++) {
71     _aliases << Alias(names[i], expansions[i]);
72   }
73 }
74
75
76 void AliasManager::addAlias(const QString &name, const QString &expansion) {
77   if(contains(name)) {
78     return;
79   }
80
81   _aliases << Alias(name, expansion);
82
83   emit aliasAdded(name, expansion);
84 }
85
86 AliasManager::AliasList AliasManager::defaults() {
87   AliasList aliases;
88   aliases << Alias("j", "/join $0")
89           << Alias("ns", "/msg nickserv $0")
90           << Alias("nickserv", "/msg nickserv $0")
91           << Alias("cs", "/msg chanserv $0")
92           << Alias("chanserv",  "/msg chanserv $0")
93           << Alias("hs", "/msg hostserv $0")
94           << Alias("hostserv", "/msg hostserv $0")
95           << Alias("back", "/quote away");
96   return aliases;
97 }
98
99 AliasManager::CommandList AliasManager::processInput(const BufferInfo &info, const QString &msg) {
100   CommandList result;
101   processInput(info, msg, result);
102   return result;
103 }
104
105 void AliasManager::processInput(const BufferInfo &info, const QString &msg_, CommandList &list) {
106   QString msg = msg_;
107
108   // leading slashes indicate there's a command to call unless there is another one in the first section (like a path /proc/cpuinfo)
109   int secondSlashPos = msg.indexOf('/', 1);
110   int firstSpacePos = msg.indexOf(' ');
111   if(!msg.startsWith('/') || (secondSlashPos != -1 && (secondSlashPos < firstSpacePos || firstSpacePos == -1))) {
112     if(msg.startsWith("//"))
113       msg.remove(0, 1); // //asdf is transformed to /asdf
114     msg.prepend("/SAY ");  // make sure we only send proper commands to the core
115   } else {
116     // check for aliases
117     QString cmd = msg.section(' ', 0, 0).remove(0, 1).toUpper();
118     for(int i = 0; i < count(); i++) {
119       if((*this)[i].name.toUpper() == cmd) {
120         expand((*this)[i].expansion, info, msg.section(' ', 1), list);
121         return;
122       }
123     }
124   }
125
126   list.append(qMakePair(info, msg));
127 }
128
129 void AliasManager::expand(const QString &alias, const BufferInfo &bufferInfo, const QString &msg, CommandList &list) {
130   const Network *net = network(bufferInfo.networkId());
131   if(!net) {
132     // FIXME send error as soon as we have a method for that!
133     return;
134   }
135
136   QRegExp paramRangeR("\\$(\\d+)\\.\\.(\\d*)");
137   QStringList commands = alias.split(QRegExp("; ?"));
138   QStringList params = msg.split(' ');
139   QStringList expandedCommands;
140   for(int i = 0; i < commands.count(); i++) {
141     QString command = commands[i];
142
143     // replace ranges like $1..3
144     if(!params.isEmpty()) {
145       int pos;
146       while((pos = paramRangeR.indexIn(command)) != -1) {
147         int start = paramRangeR.cap(1).toInt();
148         bool ok;
149         int end = paramRangeR.cap(2).toInt(&ok);
150         if(!ok) {
151           end = params.count();
152         }
153         if(end < start)
154           command = command.replace(pos, paramRangeR.matchedLength(), QString());
155         else {
156           command = command.replace(pos, paramRangeR.matchedLength(), QStringList(params.mid(start - 1, end - start + 1)).join(" "));
157         }
158       }
159     }
160
161     for(int j = params.count(); j > 0; j--) {
162       IrcUser *ircUser = net->ircUser(params[j - 1]);
163       command = command.replace(QString("$%1:hostname").arg(j), ircUser ? ircUser->host() : QString("*"));
164       command = command.replace(QString("$%1").arg(j), params[j - 1]);
165     }
166     command = command.replace("$0", msg);
167     command = command.replace("$channelname", bufferInfo.bufferName()); // legacy
168     command = command.replace("$channel", bufferInfo.bufferName());
169     command = command.replace("$currentnick", net->myNick()); // legacy
170     command = command.replace("$nick", net->myNick());
171     expandedCommands << command;
172   }
173
174   while(!expandedCommands.isEmpty()) {
175     QString command;
176     if(expandedCommands[0].trimmed().toLower().startsWith("/wait")) {
177       command = expandedCommands.join("; ");
178       expandedCommands.clear();
179     } else {
180       command = expandedCommands.takeFirst();
181     }
182     list.append(qMakePair(bufferInfo, command));
183   }
184 }