17 #include "../contrib/libdazeus-irc/src/utils.h"
35 std::vector<std::string> args;
37 Command(
Network &n) : network(n), origin(), channel(),
38 command(), fullArgs(), args(), whoisSent(
false) {}
41 struct RequirementInfo {
46 std::string wantedReceiver;
47 std::string wantedSender;
49 RequirementInfo() : needsNetwork(
false), needsReceiver(
false),
50 needsSender(
false), wantedNetwork(0), wantedReceiver(),
53 RequirementInfo(
Network *n) : needsNetwork(
true), needsReceiver(
false),
54 needsSender(
false), wantedNetwork(n), wantedReceiver(),
58 RequirementInfo(
Network *n, std::string obj,
bool isSender) :
59 needsNetwork(
true), needsReceiver(
false),
60 needsSender(
false), wantedNetwork(n), wantedReceiver(),
64 needsSender =
true; wantedSender = obj;
66 needsReceiver =
true; wantedReceiver = obj;
71 RequirementInfo(
const RequirementInfo&);
72 void operator=(
const RequirementInfo&);
77 SocketInfo(std::string t = std::string()) : type(t),
78 subscriptions(), commands(), waitingSize(0), readahead(),
79 protocol_version(0) {}
80 bool isSubscribed(std::string t)
const {
83 bool unsubscribe(std::string t) {
84 if(!isSubscribed(t))
return false;
88 bool subscribe(std::string t) {
94 bool isSubscribedToCommand(
const std::string &cmd,
const std::string &recv,
95 const std::string &sender,
bool identified,
const Network &network)
97 std::multimap<std::string,RequirementInfo*>::iterator it;
98 for(it = commands.begin(); it != commands.end(); ++it) {
99 if(it->first != cmd) {
101 }
else if(it->second->needsNetwork && it->second->wantedNetwork != &network) {
103 }
else if(it->second->needsReceiver && it->second->wantedReceiver != recv) {
105 }
else if(it->second->needsSender) {
106 if(!identified || it->second->wantedSender != sender)
113 bool commandMightNeedWhois(
const std::string &cmd) {
114 std::multimap<std::string,RequirementInfo*>::iterator it;
115 for(it = commands.begin(); it != commands.end(); ++it) {
116 if(it->first != cmd) {
118 }
else if(it->second->needsSender) {
124 void subscribeToCommand(
const std::string &cmd, RequirementInfo *info) {
125 commands.insert(std::make_pair(cmd, info));
127 void dispatch(
int d, std::string event, std::vector<std::string> parameters) {
130 json_t *params = json_array();
131 std::vector<std::string>::iterator it;
132 for(it = parameters.begin(); it != parameters.end(); ++it) {
133 json_array_append_new(params, json_string(it->c_str()));
136 json_t *n = json_object();
137 json_object_set_new(n,
"event", json_string(event.c_str()));
138 json_object_set_new(n,
"params", params);
140 char *json_raw = json_dumps(n, 0);
141 std::string jsonMsg = json_raw;
143 std::stringstream mstr;
144 mstr << jsonMsg.length();
147 std::string final_message = mstr.str();
148 if(write(d, final_message.c_str(), final_message.length()) != (
unsigned)final_message.length()) {
149 fprintf(stderr,
"Failed to write correct number of JSON bytes to client socket in dispatch().\n");
154 bool didHandshake() {
155 return protocol_version != 0;
158 std::vector<std::string> subscriptions;
159 std::multimap<std::string,RequirementInfo*> commands;
161 std::string readahead;
162 std::string plugin_name;
163 std::string plugin_version;
164 int protocol_version;
165 std::string config_group;
171 void dispatch(
const std::string &event,
const std::vector<std::string> ¶meters);
173 void ircEvent(
const std::string &event,
const std::string &origin,
174 const std::vector<std::string> ¶ms,
Network *n );
175 void run(
int timeout);
182 void newTcpConnection();
183 void newLocalConnection();
185 void messageReceived(
const std::string &origin,
const std::string &message,
const std::string &receiver,
Network *n);
187 std::vector<int> tcpServers_;
188 std::vector<int> localServers_;
189 std::vector<Command*> commandQueue_;
190 std::map<int,SocketInfo> sockets_;
194 void handle(
int dev,
const std::string &line, SocketInfo &info);
195 void flushCommandQueue(
const std::string &nick = std::string(),
bool identified =
false);