1 /***************************************************************************
2 * Copyright (C) 2005-2020 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 ***************************************************************************/
21 #include "aliasmanager.h"
24 #include <QStringList>
28 AliasManager& AliasManager::operator=(const AliasManager& other)
33 SyncableObject::operator=(other);
34 _aliases = other._aliases;
38 int AliasManager::indexOf(const QString& name) const
40 for (int i = 0; i < _aliases.count(); i++) {
41 if (_aliases[i].name == name)
47 QVariantMap AliasManager::initAliases() const
51 QStringList expansions;
53 for (int i = 0; i < _aliases.count(); i++) {
54 names << _aliases[i].name;
55 expansions << _aliases[i].expansion;
58 aliases["names"] = names;
59 aliases["expansions"] = expansions;
63 void AliasManager::initSetAliases(const QVariantMap& aliases)
65 QStringList names = aliases["names"].toStringList();
66 QStringList expansions = aliases["expansions"].toStringList();
68 if (names.count() != expansions.count()) {
69 qWarning() << "AliasesManager::initSetAliases: received" << names.count() << "alias names but only" << expansions.count()
75 for (int i = 0; i < names.count(); i++) {
76 _aliases << Alias(names[i], expansions[i]);
80 void AliasManager::addAlias(const QString& name, const QString& expansion)
86 _aliases << Alias(name, expansion);
88 SYNC(ARG(name), ARG(expansion))
91 AliasManager::AliasList AliasManager::defaults()
94 aliases << Alias("j", "/join $0") << Alias("ns", "/msg nickserv $0") << Alias("nickserv", "/msg nickserv $0")
95 << Alias("cs", "/msg chanserv $0") << Alias("chanserv", "/msg chanserv $0") << Alias("hs", "/msg hostserv $0")
96 << Alias("hostserv", "/msg hostserv $0") << Alias("wii", "/whois $0 $0") << Alias("back", "/quote away");
99 // let's add aliases for scripts that only run on linux
100 aliases << Alias("inxi", "/exec inxi $0") << Alias("sysinfo", "/exec inxi -d");
106 AliasManager::CommandList AliasManager::processInput(const BufferInfo& info, const QString& msg)
109 processInput(info, msg, result);
113 void AliasManager::processInput(const BufferInfo& info, const QString& msg_, CommandList& list)
117 // leading slashes indicate there's a command to call unless there is another one in the first section (like a path /proc/cpuinfo)
118 // For those habitally tied to irssi, "/ " also makes the rest of the line a literal message
119 int secondSlashPos = msg.indexOf('/', 1);
120 int firstSpacePos = msg.indexOf(' ');
121 if (!msg.startsWith('/') || firstSpacePos == 1 || (secondSlashPos != -1 && (secondSlashPos < firstSpacePos || firstSpacePos == -1))) {
122 if (msg.startsWith("//"))
123 msg.remove(0, 1); // "//asdf" is transformed to "/asdf"
124 else if (msg.startsWith("/ "))
125 msg.remove(0, 2); // "/ /asdf" is transformed to "/asdf"
126 msg.prepend("/SAY "); // make sure we only send proper commands to the core
130 QString cmd = msg.section(' ', 0, 0).remove(0, 1).toUpper();
131 for (int i = 0; i < count(); i++) {
132 if ((*this)[i].name.toUpper() == cmd) {
133 expand((*this)[i].expansion, info, msg.section(' ', 1), list);
139 list.append(qMakePair(info, msg));
142 void AliasManager::expand(const QString& alias, const BufferInfo& bufferInfo, const QString& msg, CommandList& list)
144 const Network* net = network(bufferInfo.networkId());
146 // FIXME send error as soon as we have a method for that!
150 QRegExp paramRangeR(R"(\$(\d+)\.\.(\d*))");
151 QStringList commands = alias.split(QRegExp("; ?"));
152 QStringList params = msg.split(' ');
153 QStringList expandedCommands;
154 for (int i = 0; i < commands.count(); i++) {
155 QString command = commands[i];
157 // replace ranges like $1..3
158 if (!params.isEmpty()) {
160 while ((pos = paramRangeR.indexIn(command)) != -1) {
161 int start = paramRangeR.cap(1).toInt();
163 int end = paramRangeR.cap(2).toInt(&ok);
165 end = params.count();
168 command = command.replace(pos, paramRangeR.matchedLength(), QString());
170 command = command.replace(pos, paramRangeR.matchedLength(), QStringList(params.mid(start - 1, end - start + 1)).join(" "));
175 for (int j = params.count(); j > 0; j--) {
176 // Find the referenced IRC user...
177 IrcUser* ircUser = net->ircUser(params[j - 1]);
178 // ...and replace components, using short-circuit evaluation as ircUser might be null
180 // Account, or "*" if blank/nonexistent/logged out
181 command = command.replace(QString("$%1:account").arg(j),
182 (ircUser && !ircUser->account().isEmpty()) ? ircUser->account() : QString("*"));
184 // Hostname, or "*" if blank/nonexistent
185 command = command.replace(QString("$%1:hostname").arg(j),
186 (ircUser && !ircUser->host().isEmpty()) ? ircUser->host() : QString("*"));
189 // Ident if verified, or "*" if blank/unknown/unverified (prefixed with "~")
191 // Most IRC daemons have the option to prefix an ident with "~" if it could not be
192 // verified via an identity daemon such as oidentd. In these cases, it can be handy to
193 // have a way to ban via ident if verified, or all idents if not verified. If the
194 // server does not verify idents, it usually won't add "~".
196 // Identd must be replaced before ident to avoid being treated as "$i:ident" + "d"
197 command = command.replace(QString("$%1:identd").arg(j),
198 (ircUser && !ircUser->user().isEmpty() && !ircUser->user().startsWith("~")) ? ircUser->user()
201 // Ident, or "*" if blank/nonexistent
202 command = command.replace(QString("$%1:ident").arg(j), (ircUser && !ircUser->user().isEmpty()) ? ircUser->user() : QString("*"));
205 // Must be replaced last to avoid interferring with more specific aliases
206 command = command.replace(QString("$%1").arg(j), params[j - 1]);
208 command = command.replace("$0", msg);
209 command = command.replace("$channelname", bufferInfo.bufferName()); // legacy
210 command = command.replace("$channel", bufferInfo.bufferName());
211 command = command.replace("$currentnick", net->myNick()); // legacy
212 command = command.replace("$nick", net->myNick());
213 expandedCommands << command;
216 while (!expandedCommands.isEmpty()) {
218 if (expandedCommands[0].trimmed().toLower().startsWith("/wait ")) {
219 command = expandedCommands.join("; ");
220 expandedCommands.clear();
223 command = expandedCommands.takeFirst();
225 list.append(qMakePair(bufferInfo, command));