1 /***************************************************************************
2 * Copyright (C) 2005-09 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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
23 #include <QStringList>
25 #include "aliasmanager.h"
28 AliasManager &AliasManager::operator=(const AliasManager &other) {
32 SyncableObject::operator=(other);
33 _aliases = other._aliases;
37 int AliasManager::indexOf(const QString &name) const {
38 for(int i = 0; i < _aliases.count(); i++) {
39 if(_aliases[i].name == name)
45 QVariantMap AliasManager::initAliases() const {
48 QStringList expansions;
50 for(int i = 0; i < _aliases.count(); i++) {
51 names << _aliases[i].name;
52 expansions << _aliases[i].expansion;
55 aliases["names"] = names;
56 aliases["expansions"] = expansions;
60 void AliasManager::initSetAliases(const QVariantMap &aliases) {
61 QStringList names = aliases["names"].toStringList();
62 QStringList expansions = aliases["expansions"].toStringList();
64 if(names.count() != expansions.count()) {
65 qWarning() << "AliasesManager::initSetAliases: received" << names.count() << "alias names but only" << expansions.count() << "expansions!";
70 for(int i = 0; i < names.count(); i++) {
71 _aliases << Alias(names[i], expansions[i]);
76 void AliasManager::addAlias(const QString &name, const QString &expansion) {
81 _aliases << Alias(name, expansion);
83 emit aliasAdded(name, expansion);
86 AliasManager::AliasList AliasManager::defaults() {
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");
99 AliasManager::CommandList AliasManager::processInput(const BufferInfo &info, const QString &msg) {
101 processInput(info, msg, result);
105 void AliasManager::processInput(const BufferInfo &info, const QString &msg_, CommandList &list) {
108 // leading slashes indicate there's a command to call unless there is another one in the first section (like a path /proc/cpuinfo)
109 int secondSlashPos = msg.indexOf('/', 1);
110 int firstSpacePos = msg.indexOf(' ');
111 if(!msg.startsWith('/') || (secondSlashPos != -1 && (secondSlashPos < firstSpacePos || firstSpacePos == -1))) {
112 if(msg.startsWith("//"))
113 msg.remove(0, 1); // //asdf is transformed to /asdf
114 msg.prepend("/SAY "); // make sure we only send proper commands to the core
117 QString cmd = msg.section(' ', 0, 0).remove(0, 1).toUpper();
118 for(int i = 0; i < count(); i++) {
119 if((*this)[i].name.toUpper() == cmd) {
120 expand((*this)[i].expansion, info, msg.section(' ', 1), list);
126 list.append(qMakePair(info, msg));
129 void AliasManager::expand(const QString &alias, const BufferInfo &bufferInfo, const QString &msg, CommandList &list) {
130 const Network *net = network(bufferInfo.networkId());
132 // FIXME send error as soon as we have a method for that!
136 QRegExp paramRangeR("\\$(\\d+)\\.\\.(\\d*)");
137 QStringList commands = alias.split(QRegExp("; ?"));
138 QStringList params = msg.split(' ');
139 QStringList expandedCommands;
140 for(int i = 0; i < commands.count(); i++) {
141 QString command = commands[i];
143 // replace ranges like $1..3
144 if(!params.isEmpty()) {
146 while((pos = paramRangeR.indexIn(command)) != -1) {
147 int start = paramRangeR.cap(1).toInt();
149 int end = paramRangeR.cap(2).toInt(&ok);
151 end = params.count();
154 command = command.replace(pos, paramRangeR.matchedLength(), QString());
156 command = command.replace(pos, paramRangeR.matchedLength(), QStringList(params.mid(start - 1, end - start + 1)).join(" "));
161 for(int j = params.count(); j > 0; j--) {
162 IrcUser *ircUser = net->ircUser(params[j - 1]);
163 command = command.replace(QString("$%1:hostname").arg(j), ircUser ? ircUser->host() : QString("*"));
164 command = command.replace(QString("$%1").arg(j), params[j - 1]);
166 command = command.replace("$0", msg);
167 command = command.replace("$channelname", bufferInfo.bufferName()); // legacy
168 command = command.replace("$channel", bufferInfo.bufferName());
169 command = command.replace("$currentnick", net->myNick()); // legacy
170 command = command.replace("$nick", net->myNick());
171 expandedCommands << command;
174 while(!expandedCommands.isEmpty()) {
176 if(expandedCommands[0].trimmed().toLower().startsWith("/wait")) {
177 command = expandedCommands.join("; ");
178 expandedCommands.clear();
180 command = expandedCommands.takeFirst();
182 list.append(qMakePair(bufferInfo, command));