Add GPL header to mpris script
[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
22 #include <QDebug>
23 #include <QStringList>
24
25 #include "aliasmanager.h"
26 #include "network.h"
27
28 AliasManager &AliasManager::operator=(const AliasManager &other) {
29   if(this == &other)
30     return *this;
31
32   SyncableObject::operator=(other);
33   _aliases = other._aliases;
34   return *this;
35 }
36
37 int AliasManager::indexOf(const QString &name) const {
38   for(int i = 0; i < _aliases.count(); i++) {
39     if(_aliases[i].name == name)
40       return i;
41   }
42   return -1;
43 }
44
45 QVariantMap AliasManager::initAliases() const {
46   QVariantMap aliases;
47   QStringList names;
48   QStringList expansions;
49
50   for(int i = 0; i < _aliases.count(); i++) {
51     names << _aliases[i].name;
52     expansions << _aliases[i].expansion;
53   }
54
55   aliases["names"] = names;
56   aliases["expansions"] = expansions;
57   return aliases;
58 }
59
60 void AliasManager::initSetAliases(const QVariantMap &aliases) {
61   QStringList names = aliases["names"].toStringList();
62   QStringList expansions = aliases["expansions"].toStringList();
63
64   if(names.count() != expansions.count()) {
65     qWarning() << "AliasesManager::initSetAliases: received" << names.count() << "alias names but only" << expansions.count() << "expansions!";
66     return;
67   }
68
69   _aliases.clear();
70   for(int i = 0; i < names.count(); i++) {
71     _aliases << Alias(names[i], expansions[i]);
72   }
73 }
74
75
76 void AliasManager::addAlias(const QString &name, const QString &expansion) {
77   if(contains(name)) {
78     return;
79   }
80
81   _aliases << Alias(name, expansion);
82
83   emit aliasAdded(name, expansion);
84 }
85
86 AliasManager::AliasList AliasManager::defaults() {
87   AliasList aliases;
88   aliases << Alias("j", "/join $0")
89           << Alias("ns", "/msg nickserv $0")
90           << Alias("nickserv", "/msg nickserv $0")
91           << Alias("cs", "/msg chanserv $0")
92           << Alias("chanserv",  "/msg chanserv $0")
93           << Alias("hs", "/msg hostserv $0")
94           << Alias("hostserv", "/msg hostserv $0")
95           << Alias("back", "/quote away");
96
97 #ifdef Q_OS_LINUX
98   // let's add aliases for scripts that only run on linux
99   aliases << Alias("inxi", "/exec inxi $0")
100           << 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   CommandList result;
108   processInput(info, msg, result);
109   return result;
110 }
111
112 void AliasManager::processInput(const BufferInfo &info, const QString &msg_, CommandList &list) {
113   QString msg = msg_;
114
115   // leading slashes indicate there's a command to call unless there is another one in the first section (like a path /proc/cpuinfo)
116   int secondSlashPos = msg.indexOf('/', 1);
117   int firstSpacePos = msg.indexOf(' ');
118   if(!msg.startsWith('/') || (secondSlashPos != -1 && (secondSlashPos < firstSpacePos || firstSpacePos == -1))) {
119     if(msg.startsWith("//"))
120       msg.remove(0, 1); // //asdf is transformed to /asdf
121     msg.prepend("/SAY ");  // make sure we only send proper commands to the core
122   } else {
123     // check for aliases
124     QString cmd = msg.section(' ', 0, 0).remove(0, 1).toUpper();
125     for(int i = 0; i < count(); i++) {
126       if((*this)[i].name.toUpper() == cmd) {
127         expand((*this)[i].expansion, info, msg.section(' ', 1), list);
128         return;
129       }
130     }
131   }
132
133   list.append(qMakePair(info, msg));
134 }
135
136 void AliasManager::expand(const QString &alias, const BufferInfo &bufferInfo, const QString &msg, CommandList &list) {
137   const Network *net = network(bufferInfo.networkId());
138   if(!net) {
139     // FIXME send error as soon as we have a method for that!
140     return;
141   }
142
143   QRegExp paramRangeR("\\$(\\d+)\\.\\.(\\d*)");
144   QStringList commands = alias.split(QRegExp("; ?"));
145   QStringList params = msg.split(' ');
146   QStringList expandedCommands;
147   for(int i = 0; i < commands.count(); i++) {
148     QString command = commands[i];
149
150     // replace ranges like $1..3
151     if(!params.isEmpty()) {
152       int pos;
153       while((pos = paramRangeR.indexIn(command)) != -1) {
154         int start = paramRangeR.cap(1).toInt();
155         bool ok;
156         int end = paramRangeR.cap(2).toInt(&ok);
157         if(!ok) {
158           end = params.count();
159         }
160         if(end < start)
161           command = command.replace(pos, paramRangeR.matchedLength(), QString());
162         else {
163           command = command.replace(pos, paramRangeR.matchedLength(), QStringList(params.mid(start - 1, end - start + 1)).join(" "));
164         }
165       }
166     }
167
168     for(int j = params.count(); j > 0; j--) {
169       IrcUser *ircUser = net->ircUser(params[j - 1]);
170       command = command.replace(QString("$%1:hostname").arg(j), ircUser ? ircUser->host() : QString("*"));
171       command = command.replace(QString("$%1").arg(j), params[j - 1]);
172     }
173     command = command.replace("$0", msg);
174     command = command.replace("$channelname", bufferInfo.bufferName()); // legacy
175     command = command.replace("$channel", bufferInfo.bufferName());
176     command = command.replace("$currentnick", net->myNick()); // legacy
177     command = command.replace("$nick", net->myNick());
178     expandedCommands << command;
179   }
180
181   while(!expandedCommands.isEmpty()) {
182     QString command;
183     if(expandedCommands[0].trimmed().toLower().startsWith("/wait")) {
184       command = expandedCommands.join("; ");
185       expandedCommands.clear();
186     } else {
187       command = expandedCommands.takeFirst();
188     }
189     list.append(qMakePair(bufferInfo, command));
190   }
191 }