00001 #include "module.h"
00002 #include "xmlrpc.h"
00003
00004 static Module *me;
00005
00006 class XMLRPCIdentifyRequest : public IdentifyRequest
00007 {
00008 XMLRPCRequest request;
00009 HTTPReply repl;
00010
00011 Reference<HTTPClient> client;
00012 Reference<XMLRPCServiceInterface> xinterface;
00013
00014 public:
00015 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) { }
00016
00017 void OnSuccess() anope_override
00018 {
00019 if (!xinterface || !client)
00020 return;
00021
00022 request.r = this->repl;
00023
00024 request.reply("result", "Success");
00025 request.reply("account", GetAccount());
00026
00027 xinterface->Reply(request);
00028 client->SendReply(&request.r);
00029 }
00030
00031 void OnFail() anope_override
00032 {
00033 if (!xinterface || !client)
00034 return;
00035
00036 request.r = this->repl;
00037
00038 request.reply("error", "Invalid password");
00039
00040 xinterface->Reply(request);
00041 client->SendReply(&request.r);
00042 }
00043 };
00044
00045 class MyXMLRPCEvent : public XMLRPCEvent
00046 {
00047 public:
00048 bool Run(XMLRPCServiceInterface *iface, HTTPClient *client, XMLRPCRequest &request) anope_override
00049 {
00050 if (request.name == "command")
00051 this->DoCommand(iface, client, request);
00052 else if (request.name == "checkAuthentication")
00053 return this->DoCheckAuthentication(iface, client, request);
00054 else if (request.name == "stats")
00055 this->DoStats(iface, client, request);
00056 else if (request.name == "channel")
00057 this->DoChannel(iface, client, request);
00058 else if (request.name == "user")
00059 this->DoUser(iface, client, request);
00060 else if (request.name == "opers")
00061 this->DoOperType(iface, client, request);
00062
00063 return true;
00064 }
00065
00066 private:
00067 void DoCommand(XMLRPCServiceInterface *iface, HTTPClient *client, XMLRPCRequest &request)
00068 {
00069 Anope::string service = request.data.size() > 0 ? request.data[0] : "";
00070 Anope::string user = request.data.size() > 1 ? request.data[1] : "";
00071 Anope::string command = request.data.size() > 2 ? request.data[2] : "";
00072
00073 if (service.empty() || user.empty() || command.empty())
00074 request.reply("error", "Invalid parameters");
00075 else
00076 {
00077 BotInfo *bi = BotInfo::Find(service);
00078 if (!bi)
00079 request.reply("error", "Invalid service");
00080 else
00081 {
00082 request.reply("result", "Success");
00083
00084 NickAlias *na = NickAlias::Find(user);
00085
00086 Anope::string out;
00087
00088 struct XMLRPCommandReply : CommandReply
00089 {
00090 Anope::string &str;
00091
00092 XMLRPCommandReply(Anope::string &s) : str(s) { }
00093
00094 void SendMessage(const BotInfo *source, const Anope::string &msg) anope_override
00095 {
00096 str += msg + "\n";
00097 };
00098 }
00099 reply(out);
00100
00101 CommandSource source(user, NULL, na ? *na->nc : NULL, &reply, bi);
00102 RunCommand(source, command);
00103
00104 if (!out.empty())
00105 request.reply("return", iface->Sanitize(out));
00106 }
00107 }
00108 }
00109
00110 bool DoCheckAuthentication(XMLRPCServiceInterface *iface, HTTPClient *client, XMLRPCRequest &request)
00111 {
00112 Anope::string username = request.data.size() > 0 ? request.data[0] : "";
00113 Anope::string password = request.data.size() > 1 ? request.data[1] : "";
00114
00115 if (username.empty() || password.empty())
00116 request.reply("error", "Invalid parameters");
00117 else
00118 {
00119 XMLRPCIdentifyRequest *req = new XMLRPCIdentifyRequest(me, request, client, iface, username, password);
00120 FOREACH_MOD(I_OnCheckAuthentication, OnCheckAuthentication(NULL, req));
00121 req->Dispatch();
00122 return false;
00123 }
00124
00125 return true;
00126 }
00127
00128 void DoStats(XMLRPCServiceInterface *iface, HTTPClient *client, XMLRPCRequest &request)
00129 {
00130 request.reply("uptime", stringify(Anope::CurTime - Anope::StartTime));
00131 request.reply("uplinkname", Me->GetLinks().front()->GetName());
00132 {
00133 Anope::string buf;
00134 for (std::set<Anope::string>::iterator it = Servers::Capab.begin(); it != Servers::Capab.end(); ++it)
00135 buf += " " + *it;
00136 if (!buf.empty())
00137 buf.erase(buf.begin());
00138 request.reply("uplinkcapab", buf);
00139 }
00140 request.reply("usercount", stringify(UserListByNick.size()));
00141 request.reply("maxusercount", stringify(MaxUserCount));
00142 request.reply("channelcount", stringify(ChannelList.size()));
00143 }
00144
00145 void DoChannel(XMLRPCServiceInterface *iface, HTTPClient *client, XMLRPCRequest &request)
00146 {
00147 if (request.data.empty())
00148 return;
00149
00150 Channel *c = Channel::Find(request.data[0]);
00151
00152 request.reply("name", iface->Sanitize(c ? c->name : request.data[0]));
00153
00154 if (c)
00155 {
00156 request.reply("bancount", stringify(c->HasMode("BAN")));
00157 int count = 0;
00158 std::pair<Channel::ModeList::iterator, Channel::ModeList::iterator> its = c->GetModeList("BAN");
00159 for (; its.first != its.second; ++its.first)
00160 request.reply("ban" + stringify(++count), iface->Sanitize(its.first->second));
00161
00162 request.reply("exceptcount", stringify(c->HasMode("EXCEPT")));
00163 count = 0;
00164 its = c->GetModeList("EXCEPT");
00165 for (; its.first != its.second; ++its.first)
00166 request.reply("except" + stringify(++count), iface->Sanitize(its.first->second));
00167
00168 request.reply("invitecount", stringify(c->HasMode("INVITEOVERRIDE")));
00169 count = 0;
00170 its = c->GetModeList("INVITEOVERRIDE");
00171 for (; its.first != its.second; ++its.first)
00172 request.reply("invite" + stringify(++count), iface->Sanitize(its.first->second));
00173
00174 Anope::string users;
00175 for (Channel::ChanUserList::const_iterator it = c->users.begin(); it != c->users.end(); ++it)
00176 {
00177 ChanUserContainer *uc = *it;
00178 users += uc->status.BuildModePrefixList() + uc->user->nick + " ";
00179 }
00180 if (!users.empty())
00181 {
00182 users.erase(users.length() - 1);
00183 request.reply("users", iface->Sanitize(users));
00184 }
00185
00186 if (!c->topic.empty())
00187 request.reply("topic", iface->Sanitize(c->topic));
00188
00189 if (!c->topic_setter.empty())
00190 request.reply("topicsetter", iface->Sanitize(c->topic_setter));
00191
00192 request.reply("topictime", stringify(c->topic_time));
00193 request.reply("topicts", stringify(c->topic_ts));
00194 }
00195 }
00196
00197 void DoUser(XMLRPCServiceInterface *iface, HTTPClient *client, XMLRPCRequest &request)
00198 {
00199 if (request.data.empty())
00200 return;
00201
00202 User *u = User::Find(request.data[0]);
00203
00204 request.reply("nick", iface->Sanitize(u ? u->nick : request.data[0]));
00205
00206 if (u)
00207 {
00208 request.reply("ident", iface->Sanitize(u->GetIdent()));
00209 request.reply("vident", iface->Sanitize(u->GetVIdent()));
00210 request.reply("host", iface->Sanitize(u->host));
00211 if (!u->vhost.empty())
00212 request.reply("vhost", iface->Sanitize(u->vhost));
00213 if (!u->chost.empty())
00214 request.reply("chost", iface->Sanitize(u->chost));
00215 if (!u->ip.empty())
00216 request.reply("ip", u->ip);
00217 request.reply("timestamp", stringify(u->timestamp));
00218 request.reply("signon", stringify(u->signon));
00219 if (u->Account())
00220 {
00221 request.reply("account", iface->Sanitize(u->Account()->display));
00222 if (u->Account()->o)
00223 request.reply("opertype", iface->Sanitize(u->Account()->o->ot->GetName()));
00224 }
00225
00226 Anope::string channels;
00227 for (User::ChanUserList::const_iterator it = u->chans.begin(); it != u->chans.end(); ++it)
00228 {
00229 ChanUserContainer *cc = *it;
00230 channels += cc->status.BuildModePrefixList() + cc->chan->name + " ";
00231 }
00232 if (!channels.empty())
00233 {
00234 channels.erase(channels.length() - 1);
00235 request.reply("channels", channels);
00236 }
00237 }
00238 }
00239
00240 void DoOperType(XMLRPCServiceInterface *iface, HTTPClient *client, XMLRPCRequest &request)
00241 {
00242 for (std::list<OperType *>::const_iterator it = Config->MyOperTypes.begin(), it_end = Config->MyOperTypes.end(); it != it_end; ++it)
00243 {
00244 Anope::string perms;
00245 for (std::list<Anope::string>::const_iterator it2 = (*it)->GetPrivs().begin(), it2_end = (*it)->GetPrivs().end(); it2 != it2_end; ++it2)
00246 perms += " " + *it2;
00247 for (std::list<Anope::string>::const_iterator it2 = (*it)->GetCommands().begin(), it2_end = (*it)->GetCommands().end(); it2 != it2_end; ++it2)
00248 perms += " " + *it2;
00249 request.reply((*it)->GetName(), perms);
00250 }
00251 }
00252 };
00253
00254 class ModuleXMLRPCMain : public Module
00255 {
00256 ServiceReference<XMLRPCServiceInterface> xmlrpc;
00257
00258 MyXMLRPCEvent stats;
00259
00260 public:
00261 ModuleXMLRPCMain(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, SUPPORTED), xmlrpc("XMLRPCServiceInterface", "xmlrpc")
00262 {
00263 me = this;
00264
00265 if (!xmlrpc)
00266 throw ModuleException("Unable to find xmlrpc reference, is m_xmlrpc loaded?");
00267
00268 xmlrpc->Register(&stats);
00269 }
00270
00271 ~ModuleXMLRPCMain()
00272 {
00273 if (xmlrpc)
00274 xmlrpc->Unregister(&stats);
00275 }
00276 };
00277
00278 MODULE_INIT(ModuleXMLRPCMain)
00279