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