Reformat ALL the source!
[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 #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("back", "/quote away");
106
107 #ifdef Q_OS_LINUX
108     // let's add aliases for scripts that only run on linux
109     aliases << Alias("inxi", "/exec inxi $0")
110             << Alias("sysinfo", "/exec inxi -d");
111 #endif
112
113     return aliases;
114 }
115
116
117 AliasManager::CommandList AliasManager::processInput(const BufferInfo &info, const QString &msg)
118 {
119     CommandList result;
120     processInput(info, msg, result);
121     return result;
122 }
123
124
125 void AliasManager::processInput(const BufferInfo &info, const QString &msg_, CommandList &list)
126 {
127     QString msg = msg_;
128
129     // leading slashes indicate there's a command to call unless there is another one in the first section (like a path /proc/cpuinfo)
130     int secondSlashPos = msg.indexOf('/', 1);
131     int firstSpacePos = msg.indexOf(' ');
132     if (!msg.startsWith('/') || (secondSlashPos != -1 && (secondSlashPos < firstSpacePos || firstSpacePos == -1))) {
133         if (msg.startsWith("//"))
134             msg.remove(0, 1);  // //asdf is transformed to /asdf
135         msg.prepend("/SAY "); // make sure we only send proper commands to the core
136     }
137     else {
138         // check for aliases
139         QString cmd = msg.section(' ', 0, 0).remove(0, 1).toUpper();
140         for (int i = 0; i < count(); i++) {
141             if ((*this)[i].name.toUpper() == cmd) {
142                 expand((*this)[i].expansion, info, msg.section(' ', 1), list);
143                 return;
144             }
145         }
146     }
147
148     list.append(qMakePair(info, msg));
149 }
150
151
152 void AliasManager::expand(const QString &alias, const BufferInfo &bufferInfo, const QString &msg, CommandList &list)
153 {
154     const Network *net = network(bufferInfo.networkId());
155     if (!net) {
156         // FIXME send error as soon as we have a method for that!
157         return;
158     }
159
160     QRegExp paramRangeR("\\$(\\d+)\\.\\.(\\d*)");
161     QStringList commands = alias.split(QRegExp("; ?"));
162     QStringList params = msg.split(' ');
163     QStringList expandedCommands;
164     for (int i = 0; i < commands.count(); i++) {
165         QString command = commands[i];
166
167         // replace ranges like $1..3
168         if (!params.isEmpty()) {
169             int pos;
170             while ((pos = paramRangeR.indexIn(command)) != -1) {
171                 int start = paramRangeR.cap(1).toInt();
172                 bool ok;
173                 int end = paramRangeR.cap(2).toInt(&ok);
174                 if (!ok) {
175                     end = params.count();
176                 }
177                 if (end < start)
178                     command = command.replace(pos, paramRangeR.matchedLength(), QString());
179                 else {
180                     command = command.replace(pos, paramRangeR.matchedLength(), QStringList(params.mid(start - 1, end - start + 1)).join(" "));
181                 }
182             }
183         }
184
185         for (int j = params.count(); j > 0; j--) {
186             IrcUser *ircUser = net->ircUser(params[j - 1]);
187             command = command.replace(QString("$%1:hostname").arg(j), ircUser ? ircUser->host() : QString("*"));
188             command = command.replace(QString("$%1").arg(j), params[j - 1]);
189         }
190         command = command.replace("$0", msg);
191         command = command.replace("$channelname", bufferInfo.bufferName()); // legacy
192         command = command.replace("$channel", bufferInfo.bufferName());
193         command = command.replace("$currentnick", net->myNick()); // legacy
194         command = command.replace("$nick", net->myNick());
195         expandedCommands << command;
196     }
197
198     while (!expandedCommands.isEmpty()) {
199         QString command;
200         if (expandedCommands[0].trimmed().toLower().startsWith("/wait")) {
201             command = expandedCommands.join("; ");
202             expandedCommands.clear();
203         }
204         else {
205             command = expandedCommands.takeFirst();
206         }
207         list.append(qMakePair(bufferInfo, command));
208     }
209 }