cmake: avoid de-duplication of user's CXXFLAGS
[quassel.git] / src / common / aliasmanager.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2019 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 "aliasmanager.h"
22
23 #include <QDebug>
24 #include <QStringList>
25
26 #include "network.h"
27
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 int AliasManager::indexOf(const QString& name) const
39 {
40     for (int i = 0; i < _aliases.count(); i++) {
41         if (_aliases[i].name == name)
42             return i;
43     }
44     return -1;
45 }
46
47 QVariantMap AliasManager::initAliases() const
48 {
49     QVariantMap aliases;
50     QStringList names;
51     QStringList expansions;
52
53     for (int i = 0; i < _aliases.count(); i++) {
54         names << _aliases[i].name;
55         expansions << _aliases[i].expansion;
56     }
57
58     aliases["names"] = names;
59     aliases["expansions"] = expansions;
60     return aliases;
61 }
62
63 void AliasManager::initSetAliases(const QVariantMap& aliases)
64 {
65     QStringList names = aliases["names"].toStringList();
66     QStringList expansions = aliases["expansions"].toStringList();
67
68     if (names.count() != expansions.count()) {
69         qWarning() << "AliasesManager::initSetAliases: received" << names.count() << "alias names but only" << expansions.count()
70                    << "expansions!";
71         return;
72     }
73
74     _aliases.clear();
75     for (int i = 0; i < names.count(); i++) {
76         _aliases << Alias(names[i], expansions[i]);
77     }
78 }
79
80 void AliasManager::addAlias(const QString& name, const QString& expansion)
81 {
82     if (contains(name)) {
83         return;
84     }
85
86     _aliases << Alias(name, expansion);
87
88     SYNC(ARG(name), ARG(expansion))
89 }
90
91 AliasManager::AliasList AliasManager::defaults()
92 {
93     AliasList aliases;
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");
97
98 #ifdef Q_OS_LINUX
99     // let's add aliases for scripts that only run on linux
100     aliases << Alias("inxi", "/exec inxi $0") << Alias("sysinfo", "/exec inxi -d");
101 #endif
102
103     return aliases;
104 }
105
106 AliasManager::CommandList AliasManager::processInput(const BufferInfo& info, const QString& msg)
107 {
108     CommandList result;
109     processInput(info, msg, result);
110     return result;
111 }
112
113 void AliasManager::processInput(const BufferInfo& info, const QString& msg_, CommandList& list)
114 {
115     QString msg = msg_;
116
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
127     }
128     else {
129         // check for aliases
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);
134                 return;
135             }
136         }
137     }
138
139     list.append(qMakePair(info, msg));
140 }
141
142 void AliasManager::expand(const QString& alias, const BufferInfo& bufferInfo, const QString& msg, CommandList& list)
143 {
144     const Network* net = network(bufferInfo.networkId());
145     if (!net) {
146         // FIXME send error as soon as we have a method for that!
147         return;
148     }
149
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];
156
157         // replace ranges like $1..3
158         if (!params.isEmpty()) {
159             int pos;
160             while ((pos = paramRangeR.indexIn(command)) != -1) {
161                 int start = paramRangeR.cap(1).toInt();
162                 bool ok;
163                 int end = paramRangeR.cap(2).toInt(&ok);
164                 if (!ok) {
165                     end = params.count();
166                 }
167                 if (end < start)
168                     command = command.replace(pos, paramRangeR.matchedLength(), QString());
169                 else {
170                     command = command.replace(pos, paramRangeR.matchedLength(), QStringList(params.mid(start - 1, end - start + 1)).join(" "));
171                 }
172             }
173         }
174
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
179
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("*"));
183
184             // Hostname, or "*" if blank/nonexistent
185             command = command.replace(QString("$%1:hostname").arg(j),
186                                       (ircUser && !ircUser->host().isEmpty()) ? ircUser->host() : QString("*"));
187
188             // Identd
189             // Ident if verified, or "*" if blank/unknown/unverified (prefixed with "~")
190             //
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 "~".
195             //
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()
199                                                                                                                   : QString("*"));
200
201             // Ident, or "*" if blank/nonexistent
202             command = command.replace(QString("$%1:ident").arg(j), (ircUser && !ircUser->user().isEmpty()) ? ircUser->user() : QString("*"));
203
204             // Nickname
205             // Must be replaced last to avoid interferring with more specific aliases
206             command = command.replace(QString("$%1").arg(j), params[j - 1]);
207         }
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;
214     }
215
216     while (!expandedCommands.isEmpty()) {
217         QString command;
218         if (expandedCommands[0].trimmed().toLower().startsWith("/wait ")) {
219             command = expandedCommands.join("; ");
220             expandedCommands.clear();
221         }
222         else {
223             command = expandedCommands.takeFirst();
224         }
225         list.append(qMakePair(bufferInfo, command));
226     }
227 }