1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
/*
* Copyright (c) 2011 Kevin Smith
* Licensed under the GNU General Public License v3.
* See Documentation/Licenses/GPLv3.txt for more information.
*/
#include <Swiftob/Commands.h>
#include <Swiften/Base/foreach.h>
#include <iostream>
#include <boost/bind.hpp>
#include <Swiften/Client/Client.h>
typedef std::pair<std::string, Commands::Command*> NamedCommand;
Commands::Commands(Users* users, Swift::Client* client, Storage* storage, MUCs* mucs) {
users_ = users;
client_ = client;
mucs_ = mucs;
storage_ = storage;
resetCommands();
}
void Commands::resetCommands() {
foreach (NamedCommand command, commands_) {
delete command.second;
}
commands_.clear();
registerCommand("quit", Owner, "Quit the bot", boost::bind(&Commands::handleQuitCommand, this, _1, _2, _3));
registerCommand("help", Anyone, "Get help", boost::bind(&Commands::handleHelpCommand, this, _1, _2, _3));
registerCommand("join", Owner, "Join a MUC", boost::bind(&Commands::handleJoinCommand, this, _1, _2, _3));
registerCommand("part", Owner, "Leave a MUC", boost::bind(&Commands::handlePartCommand, this, _1, _2, _3));
registerCommand("rehash", Owner, "Reload scripts", boost::bind(&Commands::handleRehashCommand, this, _1, _2, _3));
onReset();
}
void Commands::registerCommand(const std::string& name, RoleList roles, const std::string& description, boost::function<void(const std::string& /*command*/, const std::string& /*params*/, Swift::Message::ref)> callback) {
Command* command = new Command(roles, description);
commands_[name] = command;
command->onReceived.connect(callback);
}
void Commands::registerListener(ListenerCallback listener) {
listeners_.push_back(listener);
}
bool Commands::hasCommand(const std::string& name) {
return commands_.find(name) != commands_.end();
}
bool Commands::runCommand(const std::string& name, const std::string& params, Swift::Message::ref message) {
Users::User::Role userRole = users_->getRoleForSender(message);
Command* command = commands_[name];
if (roleIn(userRole, command->getAllowedBy())) {
command->onReceived(name, params, message);
return true;
} else {
replyTo(message, "You may not run this command", true);
}
return false;
}
void Commands::runListeners(Swift::Message::ref message) {
foreach (ListenerCallback listener, listeners_) {
listener(message);
}
}
bool Commands::roleIn(const Users::User::Role userRole, RoleList roleList) {
switch (roleList) {
case Owner : return userRole == Users::User::Owner;
case Anyone : return true;
}
std::cerr << "Unrecognised role list" << std::endl;
return false;
}
void Commands::handleQuitCommand(const std::string& /*command*/, const std::string& /*params*/, Swift::Message::ref message) {
replyTo(message, "Shutting down");
std::cout << "Quitting at the behest of " << message->getFrom().toString() << std::endl;
exit(0);
}
void Commands::setRehashError(const std::string& error) {
if (!rehashError_.empty()) {
rehashError_ += "; ";
}
rehashError_ += error;
}
void Commands::handleRehashCommand(const std::string& /*command*/, const std::string& /*params*/, Swift::Message::ref message) {
rehashError_ = "";
replyTo(message, "Rehashing now.");
std::cout << "Rehashing at the behest of " << message->getFrom().toString() << std::endl;
resetCommands();
listeners_.clear();
if (rehashError_.empty()) {
replyTo(message, "Rehash complete");
} else {
replyTo(message, "I have suffered a tremendous failure: " + rehashError_);
}
}
void Commands::handleJoinCommand(const std::string& /*command*/, const std::string& params, Swift::Message::ref message) {
Swift::JID room(params);
if (!room.isValid() || !room.getResource().empty() || room.getNode().empty()) {
replyTo(message, "Can't join " + room.toString() + ", not a valid room JID.");
return;
}
if (mucs_->contains(room)) {
replyTo(message, "I'm already (trying to be?) in " + room.toString() + ".");
return;
}
replyTo(message, "Trying to join " + room.toString() + ".");
mucs_->join(room, boost::bind(&Commands::handleJoinCommandSuccess, this, room, message), boost::bind(&Commands::handleJoinCommandFailure, this, room, _1, message));
}
void Commands::handlePartCommand(const std::string& /*command*/, const std::string& params, Swift::Message::ref message) {
Swift::JID room(params);
if (!room.isValid() || !room.getResource().empty() || room.getNode().empty()) {
replyTo(message, "Can't leave " + room.toString() + ", not a valid room JID.");
return;
}
if (mucs_->contains(room)) {
replyTo(message, "I'm not in " + room.toString() + ".");
return;
}
replyTo(message, "Leaving " + room.toString() + ".");
mucs_->part(room);
}
void Commands::handleJoinCommandSuccess(const Swift::JID& room, Swift::Message::ref message) {
replyTo(message, "Joined " + room.toString());
}
void Commands::handleJoinCommandFailure(const Swift::JID& room, const std::string& error, Swift::Message::ref message) {
replyTo(message, "Join to " + room.toString() + "failed. " + error);
}
void Commands::handleHelpCommand(const std::string& /*command*/, const std::string& /*params*/, Swift::Message::ref message) {
Users::User::Role userRole = users_->getRoleForSender(message);
std::string result("Available commands:");
std::string bang = message->getType() == Swift::Message::Groupchat ? "\n!" : "\n";
foreach (NamedCommand pair, commands_) {
if (roleIn(userRole, pair.second->getAllowedBy())) {
result += bang + pair.first + " - " + pair.second->getDescription();
}
}
replyTo(message, result, true);
}
/**
* \param outOfMUC Reply to the sender directly, don't spam MUCs with the reply
*/
void Commands::replyTo(Swift::Message::ref source, std::string replyBody, bool outOfMUC) {
Swift::Message::ref reply(new Swift::Message());
Swift::Message::Type type = source->getType();
reply->setType(type);
reply->setBody(type == Swift::Message::Groupchat ? source->getFrom().getResource() + ": " + replyBody : replyBody);
Swift::JID to = source->getFrom();
if (type == Swift::Message::Groupchat) {
if (outOfMUC) {
reply->setType(Swift::Message::Chat);
} else {
to = to.toBare();
}
}
reply->setTo(to);
if (client_->isAvailable()) {
client_->sendMessage(reply);
} else {
std::cout << "Dropping '" + reply->getBody() + "' -> " + reply->getTo().toString() + " on the floor due to missing connection." << std::endl;
}
}
|