1 /***************************************************************************
2 * Copyright (C) 2005-2018 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 // Find the referenced IRC user...
191 IrcUser *ircUser = net->ircUser(params[j - 1]);
192 // ...and replace components, using short-circuit evaluation as ircUser might be null
194 // Account, or "*" if blank/nonexistent/logged out
195 command = command.replace(
196 QString("$%1:account").arg(j),
197 (ircUser && !ircUser->account().isEmpty()) ? ircUser->account()
200 // Hostname, or "*" if blank/nonexistent
201 command = command.replace(
202 QString("$%1:hostname").arg(j),
203 (ircUser && !ircUser->host().isEmpty()) ? ircUser->host() : QString("*"));
206 // Ident if verified, or "*" if blank/unknown/unverified (prefixed with "~")
208 // Most IRC daemons have the option to prefix an ident with "~" if it could not be
209 // verified via an identity daemon such as oidentd. In these cases, it can be handy to
210 // have a way to ban via ident if verified, or all idents if not verified. If the
211 // server does not verify idents, it usually won't add "~".
213 // Identd must be replaced before ident to avoid being treated as "$i:ident" + "d"
214 command = command.replace(
215 QString("$%1:identd").arg(j),
216 (ircUser && !ircUser->user().isEmpty()
217 && !ircUser->user().startsWith("~"))
218 ? ircUser->user() : QString("*"));
220 // Ident, or "*" if blank/nonexistent
221 command = command.replace(
222 QString("$%1:ident").arg(j),
223 (ircUser && !ircUser->user().isEmpty()) ? ircUser->user() : QString("*"));
226 // Must be replaced last to avoid interferring with more specific aliases
227 command = command.replace(QString("$%1").arg(j), params[j - 1]);
229 command = command.replace("$0", msg);
230 command = command.replace("$channelname", bufferInfo.bufferName()); // legacy
231 command = command.replace("$channel", bufferInfo.bufferName());
232 command = command.replace("$currentnick", net->myNick()); // legacy
233 command = command.replace("$nick", net->myNick());
234 expandedCommands << command;
237 while (!expandedCommands.isEmpty()) {
239 if (expandedCommands[0].trimmed().toLower().startsWith("/wait ")) {
240 command = expandedCommands.join("; ");
241 expandedCommands.clear();
244 command = expandedCommands.takeFirst();
246 list.append(qMakePair(bufferInfo, command));