1 /***************************************************************************
2 * Copyright (C) 2005-2016 by the Quassel Project *
3 * devel@quassel-irc.org *
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. *
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. *
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 ***************************************************************************/
22 #include <QStringList>
24 #include "aliasmanager.h"
27 INIT_SYNCABLE_OBJECT(AliasManager)
28 AliasManager &AliasManager::operator=(const AliasManager &other)
33 SyncableObject::operator=(other);
34 _aliases = other._aliases;
39 int AliasManager::indexOf(const QString &name) const
41 for (int i = 0; i < _aliases.count(); i++) {
42 if (_aliases[i].name == name)
49 QVariantMap AliasManager::initAliases() const
53 QStringList expansions;
55 for (int i = 0; i < _aliases.count(); i++) {
56 names << _aliases[i].name;
57 expansions << _aliases[i].expansion;
60 aliases["names"] = names;
61 aliases["expansions"] = expansions;
66 void AliasManager::initSetAliases(const QVariantMap &aliases)
68 QStringList names = aliases["names"].toStringList();
69 QStringList expansions = aliases["expansions"].toStringList();
71 if (names.count() != expansions.count()) {
72 qWarning() << "AliasesManager::initSetAliases: received" << names.count() << "alias names but only" << expansions.count() << "expansions!";
77 for (int i = 0; i < names.count(); i++) {
78 _aliases << Alias(names[i], expansions[i]);
83 void AliasManager::addAlias(const QString &name, const QString &expansion)
89 _aliases << Alias(name, expansion);
91 SYNC(ARG(name), ARG(expansion))
95 AliasManager::AliasList AliasManager::defaults()
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");
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");
118 AliasManager::CommandList AliasManager::processInput(const BufferInfo &info, const QString &msg)
121 processInput(info, msg, result);
126 void AliasManager::processInput(const BufferInfo &info, const QString &msg_, CommandList &list)
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
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);
152 list.append(qMakePair(info, msg));
156 void AliasManager::expand(const QString &alias, const BufferInfo &bufferInfo, const QString &msg, CommandList &list)
158 const Network *net = network(bufferInfo.networkId());
160 // FIXME send error as soon as we have a method for that!
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];
171 // replace ranges like $1..3
172 if (!params.isEmpty()) {
174 while ((pos = paramRangeR.indexIn(command)) != -1) {
175 int start = paramRangeR.cap(1).toInt();
177 int end = paramRangeR.cap(2).toInt(&ok);
179 end = params.count();
182 command = command.replace(pos, paramRangeR.matchedLength(), QString());
184 command = command.replace(pos, paramRangeR.matchedLength(), QStringList(params.mid(start - 1, end - start + 1)).join(" "));
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").arg(j), params[j - 1]);
194 command = command.replace("$0", msg);
195 command = command.replace("$channelname", bufferInfo.bufferName()); // legacy
196 command = command.replace("$channel", bufferInfo.bufferName());
197 command = command.replace("$currentnick", net->myNick()); // legacy
198 command = command.replace("$nick", net->myNick());
199 expandedCommands << command;
202 while (!expandedCommands.isEmpty()) {
204 if (expandedCommands[0].trimmed().toLower().startsWith("/wait ")) {
205 command = expandedCommands.join("; ");
206 expandedCommands.clear();
209 command = expandedCommands.takeFirst();
211 list.append(qMakePair(bufferInfo, command));