anope- supernets anope source code & configuration |
git clone git://git.acid.vegas/anope.git |
Log | Files | Refs | Archive | README |
m_xmlrpc_main.cpp (9179B)
1 /* 2 * 3 * (C) 2010-2022 Anope Team 4 * Contact us at team@anope.org 5 * 6 * Please read COPYING and README for further details. 7 */ 8 9 #include "module.h" 10 #include "modules/xmlrpc.h" 11 12 static Module *me; 13 14 class XMLRPCIdentifyRequest : public IdentifyRequest 15 { 16 XMLRPCRequest request; 17 HTTPReply repl; /* Request holds a reference to the HTTPReply, because we might exist long enough to invalidate it 18 we'll copy it here then reset the reference before we use it */ 19 Reference<HTTPClient> client; 20 Reference<XMLRPCServiceInterface> xinterface; 21 22 public: 23 XMLRPCIdentifyRequest(Module *m, XMLRPCRequest& req, HTTPClient *c, XMLRPCServiceInterface* iface, const Anope::string &acc, const Anope::string &pass) : IdentifyRequest(m, acc, pass), request(req), repl(request.r), client(c), xinterface(iface) { } 24 25 void OnSuccess() anope_override 26 { 27 if (!xinterface || !client) 28 return; 29 30 request.r = this->repl; 31 32 request.reply("result", "Success"); 33 request.reply("account", GetAccount()); 34 35 xinterface->Reply(request); 36 client->SendReply(&request.r); 37 } 38 39 void OnFail() anope_override 40 { 41 if (!xinterface || !client) 42 return; 43 44 request.r = this->repl; 45 46 request.reply("error", "Invalid password"); 47 48 xinterface->Reply(request); 49 client->SendReply(&request.r); 50 } 51 }; 52 53 class MyXMLRPCEvent : public XMLRPCEvent 54 { 55 public: 56 bool Run(XMLRPCServiceInterface *iface, HTTPClient *client, XMLRPCRequest &request) anope_override 57 { 58 if (request.name == "command") 59 this->DoCommand(iface, client, request); 60 else if (request.name == "checkAuthentication") 61 return this->DoCheckAuthentication(iface, client, request); 62 else if (request.name == "stats") 63 this->DoStats(iface, client, request); 64 else if (request.name == "channel") 65 this->DoChannel(iface, client, request); 66 else if (request.name == "user") 67 this->DoUser(iface, client, request); 68 else if (request.name == "opers") 69 this->DoOperType(iface, client, request); 70 else if (request.name == "notice") 71 this->DoNotice(iface, client, request); 72 73 return true; 74 } 75 76 private: 77 void DoCommand(XMLRPCServiceInterface *iface, HTTPClient *client, XMLRPCRequest &request) 78 { 79 Anope::string service = request.data.size() > 0 ? request.data[0] : ""; 80 Anope::string user = request.data.size() > 1 ? request.data[1] : ""; 81 Anope::string command = request.data.size() > 2 ? request.data[2] : ""; 82 83 if (service.empty() || user.empty() || command.empty()) 84 request.reply("error", "Invalid parameters"); 85 else 86 { 87 BotInfo *bi = BotInfo::Find(service, true); 88 if (!bi) 89 request.reply("error", "Invalid service"); 90 else 91 { 92 request.reply("result", "Success"); 93 94 NickAlias *na = NickAlias::Find(user); 95 96 Anope::string out; 97 98 struct XMLRPCommandReply : CommandReply 99 { 100 Anope::string &str; 101 102 XMLRPCommandReply(Anope::string &s) : str(s) { } 103 104 void SendMessage(BotInfo *, const Anope::string &msg) anope_override 105 { 106 str += msg + "\n"; 107 }; 108 } 109 reply(out); 110 111 User *u = User::Find(user, true); 112 CommandSource source(user, u, na ? *na->nc : NULL, &reply, bi); 113 Command::Run(source, command); 114 115 if (!out.empty()) 116 request.reply("return", iface->Sanitize(out)); 117 } 118 } 119 } 120 121 bool DoCheckAuthentication(XMLRPCServiceInterface *iface, HTTPClient *client, XMLRPCRequest &request) 122 { 123 Anope::string username = request.data.size() > 0 ? request.data[0] : ""; 124 Anope::string password = request.data.size() > 1 ? request.data[1] : ""; 125 126 if (username.empty() || password.empty()) 127 request.reply("error", "Invalid parameters"); 128 else 129 { 130 XMLRPCIdentifyRequest *req = new XMLRPCIdentifyRequest(me, request, client, iface, username, password); 131 FOREACH_MOD(OnCheckAuthentication, (NULL, req)); 132 req->Dispatch(); 133 return false; 134 } 135 136 return true; 137 } 138 139 void DoStats(XMLRPCServiceInterface *iface, HTTPClient *client, XMLRPCRequest &request) 140 { 141 request.reply("uptime", stringify(Anope::CurTime - Anope::StartTime)); 142 request.reply("uplinkname", Me->GetLinks().front()->GetName()); 143 { 144 Anope::string buf; 145 for (std::set<Anope::string>::iterator it = Servers::Capab.begin(); it != Servers::Capab.end(); ++it) 146 buf += " " + *it; 147 if (!buf.empty()) 148 buf.erase(buf.begin()); 149 request.reply("uplinkcapab", buf); 150 } 151 request.reply("usercount", stringify(UserListByNick.size())); 152 request.reply("maxusercount", stringify(MaxUserCount)); 153 request.reply("channelcount", stringify(ChannelList.size())); 154 } 155 156 void DoChannel(XMLRPCServiceInterface *iface, HTTPClient *client, XMLRPCRequest &request) 157 { 158 if (request.data.empty()) 159 return; 160 161 Channel *c = Channel::Find(request.data[0]); 162 163 request.reply("name", iface->Sanitize(c ? c->name : request.data[0])); 164 165 if (c) 166 { 167 request.reply("bancount", stringify(c->HasMode("BAN"))); 168 int count = 0; 169 std::vector<Anope::string> v = c->GetModeList("BAN"); 170 for (unsigned int i = 0; i < v.size(); ++i) 171 request.reply("ban" + stringify(++count), iface->Sanitize(v[i])); 172 173 request.reply("exceptcount", stringify(c->HasMode("EXCEPT"))); 174 count = 0; 175 v = c->GetModeList("EXCEPT"); 176 for (unsigned int i = 0; i < v.size(); ++i) 177 request.reply("except" + stringify(++count), iface->Sanitize(v[i])); 178 179 request.reply("invitecount", stringify(c->HasMode("INVITEOVERRIDE"))); 180 count = 0; 181 v = c->GetModeList("INVITEOVERRIDE"); 182 for (unsigned int i = 0; i < v.size(); ++i) 183 request.reply("invite" + stringify(++count), iface->Sanitize(v[i])); 184 185 Anope::string users; 186 for (Channel::ChanUserList::const_iterator it = c->users.begin(); it != c->users.end(); ++it) 187 { 188 ChanUserContainer *uc = it->second; 189 users += uc->status.BuildModePrefixList() + uc->user->nick + " "; 190 } 191 if (!users.empty()) 192 { 193 users.erase(users.length() - 1); 194 request.reply("users", iface->Sanitize(users)); 195 } 196 197 if (!c->topic.empty()) 198 request.reply("topic", iface->Sanitize(c->topic)); 199 200 if (!c->topic_setter.empty()) 201 request.reply("topicsetter", iface->Sanitize(c->topic_setter)); 202 203 request.reply("topictime", stringify(c->topic_time)); 204 request.reply("topicts", stringify(c->topic_ts)); 205 } 206 } 207 208 void DoUser(XMLRPCServiceInterface *iface, HTTPClient *client, XMLRPCRequest &request) 209 { 210 if (request.data.empty()) 211 return; 212 213 User *u = User::Find(request.data[0]); 214 215 request.reply("nick", iface->Sanitize(u ? u->nick : request.data[0])); 216 217 if (u) 218 { 219 request.reply("ident", iface->Sanitize(u->GetIdent())); 220 request.reply("vident", iface->Sanitize(u->GetVIdent())); 221 request.reply("host", iface->Sanitize(u->host)); 222 if (!u->vhost.empty()) 223 request.reply("vhost", iface->Sanitize(u->vhost)); 224 if (!u->chost.empty()) 225 request.reply("chost", iface->Sanitize(u->chost)); 226 request.reply("ip", u->ip.addr()); 227 request.reply("timestamp", stringify(u->timestamp)); 228 request.reply("signon", stringify(u->signon)); 229 if (u->Account()) 230 { 231 request.reply("account", iface->Sanitize(u->Account()->display)); 232 if (u->Account()->o) 233 request.reply("opertype", iface->Sanitize(u->Account()->o->ot->GetName())); 234 } 235 236 Anope::string channels; 237 for (User::ChanUserList::const_iterator it = u->chans.begin(); it != u->chans.end(); ++it) 238 { 239 ChanUserContainer *cc = it->second; 240 channels += cc->status.BuildModePrefixList() + cc->chan->name + " "; 241 } 242 if (!channels.empty()) 243 { 244 channels.erase(channels.length() - 1); 245 request.reply("channels", channels); 246 } 247 } 248 } 249 250 void DoOperType(XMLRPCServiceInterface *iface, HTTPClient *client, XMLRPCRequest &request) 251 { 252 for (unsigned i = 0; i < Config->MyOperTypes.size(); ++i) 253 { 254 OperType *ot = Config->MyOperTypes[i]; 255 Anope::string perms; 256 257 std::list<Anope::string> privs = ot->GetPrivs(); 258 for (std::list<Anope::string>::const_iterator it2 = privs.begin(), it2_end = privs.end(); it2 != it2_end; ++it2) 259 perms += " " + *it2; 260 261 std::list<Anope::string> commands = ot->GetCommands(); 262 for (std::list<Anope::string>::const_iterator it2 = commands.begin(), it2_end = commands.end(); it2 != it2_end; ++it2) 263 perms += " " + *it2; 264 request.reply(ot->GetName(), perms); 265 } 266 } 267 268 void DoNotice(XMLRPCServiceInterface *iface, HTTPClient *client, XMLRPCRequest &request) 269 { 270 Anope::string from = request.data.size() > 0 ? request.data[0] : ""; 271 Anope::string to = request.data.size() > 1 ? request.data[1] : ""; 272 Anope::string message = request.data.size() > 2 ? request.data[2] : ""; 273 274 BotInfo *bi = BotInfo::Find(from, true); 275 User *u = User::Find(to, true); 276 277 if (!bi || !u || message.empty()) 278 return; 279 280 u->SendMessage(bi, message); 281 282 request.reply("result", "Success"); 283 } 284 }; 285 286 class ModuleXMLRPCMain : public Module 287 { 288 ServiceReference<XMLRPCServiceInterface> xmlrpc; 289 290 MyXMLRPCEvent stats; 291 292 public: 293 ModuleXMLRPCMain(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, EXTRA | VENDOR), xmlrpc("XMLRPCServiceInterface", "xmlrpc") 294 { 295 me = this; 296 297 if (!xmlrpc) 298 throw ModuleException("Unable to find xmlrpc reference, is m_xmlrpc loaded?"); 299 300 xmlrpc->Register(&stats); 301 } 302 303 ~ModuleXMLRPCMain() 304 { 305 if (xmlrpc) 306 xmlrpc->Unregister(&stats); 307 } 308 }; 309 310 MODULE_INIT(ModuleXMLRPCMain)