ns_set_misc.cpp

Go to the documentation of this file.
00001 /*
00002  *
00003  * (C) 2003-2013 Anope Team
00004  * Contact us at team@anope.org
00005  *
00006  * Please read COPYING and README for further details.
00007  *
00008  * Based on the original code of Epona by Lara.
00009  * Based on the original code of Services by Andy Church.
00010  */
00011 
00012 /*************************************************************************/
00013 
00014 #include "module.h"
00015 
00016 static std::map<Anope::string, Anope::string> descriptions;
00017 
00018 struct NSMiscData : ExtensibleItem, Serializable
00019 {
00020         Serialize::Reference<NickCore> nc;
00021         Anope::string name;
00022         Anope::string data;
00023 
00024         NSMiscData(NickCore *ncore, const Anope::string &n, const Anope::string &d) : Serializable("NSMiscData"), nc(ncore), name(n), data(d)
00025         {
00026         }
00027 
00028         void Serialize(Serialize::Data &sdata) const anope_override
00029         {
00030                 sdata["nc"] << this->nc->display;
00031                 sdata["name"] << this->name;
00032                 sdata["data"] << this->data;
00033         }
00034 
00035         static Serializable* Unserialize(Serializable *obj, Serialize::Data &data)
00036         {
00037                 Anope::string snc, sname, sdata;
00038 
00039                 data["nc"] >> snc;
00040                 data["name"] >> sname;
00041                 data["data"] >> sdata;
00042 
00043                 NickCore *nc = NickCore::Find(snc);
00044                 if (nc == NULL)
00045                         return NULL;
00046 
00047                 NSMiscData *d;
00048                 if (obj)
00049                 {
00050                         d = anope_dynamic_static_cast<NSMiscData *>(obj);
00051                         d->nc = nc;
00052                         data["name"] >> d->name;
00053                         data["data"] >> d->data;
00054                 }
00055                 else
00056                 {
00057                         d = new NSMiscData(nc, sname, sdata);
00058                         nc->Extend(sname, d);
00059                 }
00060 
00061                 return d;
00062         }
00063 };
00064 
00065 static Anope::string GetAttribute(const Anope::string &command)
00066 {
00067         size_t sp = command.rfind(' ');
00068         if (sp != Anope::string::npos)
00069                 return command.substr(sp + 1);
00070         return command;
00071 }
00072 
00073 class CommandNSSetMisc : public Command
00074 {
00075  public:
00076         CommandNSSetMisc(Module *creator, const Anope::string &cname = "nickserv/set/misc", size_t min = 0) : Command(creator, cname, min, min + 1)
00077         {
00078                 this->SetSyntax(_("[\037parameter\037]"));
00079         }
00080 
00081         void Run(CommandSource &source, const Anope::string &user, const Anope::string &param)
00082         {
00083                 const NickAlias *na = NickAlias::Find(user);
00084                 if (!na)
00085                 {
00086                         source.Reply(NICK_X_NOT_REGISTERED, user.c_str());
00087                         return;
00088                 }
00089                 NickCore *nc = na->nc;
00090 
00091                 EventReturn MOD_RESULT;
00092                 FOREACH_RESULT(I_OnSetNickOption, OnSetNickOption(source, this, nc, param));
00093                 if (MOD_RESULT == EVENT_STOP)
00094                         return;
00095 
00096                 Anope::string scommand = GetAttribute(source.command);
00097                 Anope::string key = "ns_set_misc:" + scommand;
00098                 nc->Shrink(key);
00099                 if (!param.empty())
00100                 {
00101                         nc->Extend(key, new NSMiscData(nc, key, param));
00102                         source.Reply(CHAN_SETTING_CHANGED, scommand.c_str(), nc->display.c_str(), param.c_str());
00103                 }
00104                 else
00105                         source.Reply(CHAN_SETTING_UNSET, scommand.c_str(), nc->display.c_str());
00106 
00107                 return;
00108         }
00109 
00110         void Execute(CommandSource &source, const std::vector<Anope::string> &params) anope_override
00111         {
00112                 this->Run(source, source.nc->display, !params.empty() ? params[0] : "");
00113         }
00114 
00115         void OnServHelp(CommandSource &source) anope_override
00116         {
00117                 if (descriptions.count(source.command))
00118                 {
00119                         this->SetDesc(descriptions[source.command]);
00120                         Command::OnServHelp(source);
00121                 }
00122         }
00123 
00124         bool OnHelp(CommandSource &source, const Anope::string &subcommand) anope_override
00125         {
00126                 if (descriptions.count(source.command))
00127                 {
00128                         source.Reply("%s", Language::Translate(source.nc, descriptions[source.command].c_str()));
00129                         return true;
00130                 }
00131                 return false;
00132         }
00133 };
00134 
00135 class CommandNSSASetMisc : public CommandNSSetMisc
00136 {
00137  public:
00138         CommandNSSASetMisc(Module *creator) : CommandNSSetMisc(creator, "nickserv/saset/misc", 1)
00139         {
00140                 this->ClearSyntax();
00141                 this->SetSyntax(_("\037nickname\037 [\037parameter\037]"));
00142         }
00143 
00144         void Execute(CommandSource &source, const std::vector<Anope::string> &params) anope_override
00145         {
00146                 this->Run(source, params[0], params.size() > 1 ? params[1] : "");
00147         }
00148 };
00149 
00150 class NSSetMisc : public Module
00151 {
00152         Serialize::Type nsmiscdata_type;
00153         CommandNSSetMisc commandnssetmisc;
00154         CommandNSSASetMisc commandnssasetmisc;
00155 
00156  public:
00157         NSSetMisc(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, CORE),
00158                 nsmiscdata_type("NSMiscData", NSMiscData::Unserialize), commandnssetmisc(this), commandnssasetmisc(this)
00159         {
00160                 this->SetAuthor("Anope");
00161 
00162                 Implementation i[] = { I_OnReload, I_OnNickInfo };
00163                 ModuleManager::Attach(i, this, sizeof(i) / sizeof(Implementation));
00164 
00165                 this->OnReload();
00166         }
00167 
00168         void OnReload()
00169         {
00170                 ConfigReader config;
00171 
00172                 descriptions.clear();
00173 
00174                 for (int i = 0; i < config.Enumerate("command"); ++i)
00175                 {
00176                         if (config.ReadValue("command", "command", "", i) != "nickserv/set/misc" && config.ReadValue("command", "command", "", i) != "nickserv/saset/misc")
00177                                 continue;
00178 
00179                         Anope::string cname = config.ReadValue("command", "name", "", i);
00180                         Anope::string desc = config.ReadValue("command", "misc_description", "", i);
00181 
00182                         if (cname.empty() || desc.empty())
00183                                 continue;
00184 
00185                         descriptions[cname] = desc;
00186                 }
00187         }
00188 
00189         void OnNickInfo(CommandSource &source, NickAlias *na, InfoFormatter &info, bool ShowHidden) anope_override
00190         {
00191                 std::deque<Anope::string> list;
00192                 na->nc->GetExtList(list);
00193 
00194                 for (unsigned i = 0; i < list.size(); ++i)
00195                 {
00196                         if (list[i].find("ns_set_misc:") != 0)
00197                                 continue;
00198 
00199                         NSMiscData *data = na->nc->GetExt<NSMiscData *>(list[i]);
00200                         if (data)
00201                                 info[list[i].substr(12).replace_all_cs("_", " ")] = data->data;
00202                 }
00203         }
00204 };
00205 
00206 MODULE_INIT(NSSetMisc)