anope

- supernets anope source code & configuration
git clone git://git.acid.vegas/anope.git
Log | Files | Refs | Archive | README

cs_set_misc.cpp (5464B)

      1 /*
      2  *
      3  * (C) 2003-2022 Anope Team
      4  * Contact us at team@anope.org
      5  *
      6  * Please read COPYING and README for further details.
      7  *
      8  * Based on the original code of Epona by Lara.
      9  * Based on the original code of Services by Andy Church.
     10  */
     11 
     12 #include "module.h"
     13 #include "modules/set_misc.h"
     14 
     15 static Module *me;
     16 
     17 static Anope::map<Anope::string> descriptions;
     18 
     19 struct CSMiscData;
     20 static Anope::map<ExtensibleItem<CSMiscData> *> items;
     21 
     22 static ExtensibleItem<CSMiscData> *GetItem(const Anope::string &name)
     23 {
     24 	ExtensibleItem<CSMiscData>* &it = items[name];
     25 	if (!it)
     26 		try
     27 		{
     28 			it = new ExtensibleItem<CSMiscData>(me, name);
     29 		}
     30 		catch (const ModuleException &) { }
     31 	return it;
     32 }
     33 
     34 struct CSMiscData : MiscData, Serializable
     35 {
     36 	CSMiscData(Extensible *obj) : Serializable("CSMiscData") { }
     37 
     38 	CSMiscData(ChannelInfo *c, const Anope::string &n, const Anope::string &d) : Serializable("CSMiscData")
     39 	{
     40 		object = c->name;
     41 		name = n;
     42 		data = d;
     43 	}
     44 
     45 	void Serialize(Serialize::Data &sdata) const anope_override
     46 	{
     47 		sdata["ci"] << this->object;
     48 		sdata["name"] << this->name;
     49 		sdata["data"] << this->data;
     50 	}
     51 
     52 	static Serializable* Unserialize(Serializable *obj, Serialize::Data &data)
     53 	{
     54 		Anope::string sci, sname, sdata;
     55 
     56 		data["ci"] >> sci;
     57 		data["name"] >> sname;
     58 		data["data"] >> sdata;
     59 
     60 		ChannelInfo *ci = ChannelInfo::Find(sci);
     61 		if (ci == NULL)
     62 			return NULL;
     63 
     64 		CSMiscData *d = NULL;
     65 		if (obj)
     66 		{
     67 			d = anope_dynamic_static_cast<CSMiscData *>(obj);
     68 			d->object = ci->name;
     69 			data["name"] >> d->name;
     70 			data["data"] >> d->data;
     71 		}
     72 		else
     73 		{
     74 			ExtensibleItem<CSMiscData> *item = GetItem(sname);
     75 			if (item)
     76 				d = item->Set(ci, CSMiscData(ci, sname, sdata));
     77 		}
     78 
     79 		return d;
     80 	}
     81 };
     82 
     83 static Anope::string GetAttribute(const Anope::string &command)
     84 {
     85 	size_t sp = command.rfind(' ');
     86 	if (sp != Anope::string::npos)
     87 		return command.substr(sp + 1);
     88 	return command;
     89 }
     90 
     91 class CommandCSSetMisc : public Command
     92 {
     93  public:
     94 	CommandCSSetMisc(Module *creator, const Anope::string &cname = "chanserv/set/misc") : Command(creator, cname, 1, 2)
     95 	{
     96 		this->SetSyntax(_("\037channel\037 [\037parameters\037]"));
     97 	}
     98 
     99 	void Execute(CommandSource &source, const std::vector<Anope::string> &params) anope_override
    100 	{
    101 		if (Anope::ReadOnly)
    102 		{
    103 			source.Reply(READ_ONLY_MODE);
    104 			return;
    105 		}
    106 
    107 		ChannelInfo *ci = ChannelInfo::Find(params[0]);
    108 		const Anope::string &param = params.size() > 1 ? params[1] : "";
    109 		if (ci == NULL)
    110 		{
    111 			source.Reply(CHAN_X_NOT_REGISTERED, params[0].c_str());
    112 			return;
    113 		}
    114 
    115 		EventReturn MOD_RESULT;
    116 		FOREACH_RESULT(OnSetChannelOption, MOD_RESULT, (source, this, ci, param));
    117 		if (MOD_RESULT == EVENT_STOP)
    118 			return;
    119 
    120 		if (MOD_RESULT != EVENT_ALLOW && !source.AccessFor(ci).HasPriv("SET") && source.permission.empty() && !source.HasPriv("chanserv/administration"))
    121 		{
    122 			source.Reply(ACCESS_DENIED);
    123 			return;
    124 		}
    125 
    126 		Anope::string scommand = GetAttribute(source.command);
    127 		Anope::string key = "cs_set_misc:" + scommand;
    128 		ExtensibleItem<CSMiscData> *item = GetItem(key);
    129 		if (item == NULL)
    130 			return;
    131 
    132 		if (!param.empty())
    133 		{
    134 			item->Set(ci, CSMiscData(ci, key, param));
    135 			Log(source.AccessFor(ci).HasPriv("SET") ? LOG_COMMAND : LOG_OVERRIDE, source, this, ci) << "to change it to " << param;
    136 			source.Reply(CHAN_SETTING_CHANGED, scommand.c_str(), ci->name.c_str(), params[1].c_str());
    137 		}
    138 		else
    139 		{
    140 			item->Unset(ci);
    141 			Log(source.AccessFor(ci).HasPriv("SET") ? LOG_COMMAND : LOG_OVERRIDE, source, this, ci) << "to unset it";
    142 			source.Reply(CHAN_SETTING_UNSET, scommand.c_str(), ci->name.c_str());
    143 		}
    144 	}
    145 
    146 	void OnServHelp(CommandSource &source) anope_override
    147 	{
    148 		if (descriptions.count(source.command))
    149 		{
    150 			this->SetDesc(descriptions[source.command]);
    151 			Command::OnServHelp(source);
    152 		}
    153 	}
    154 
    155 	bool OnHelp(CommandSource &source, const Anope::string &subcommand) anope_override
    156 	{
    157 		if (descriptions.count(source.command))
    158 		{
    159 			this->SendSyntax(source);
    160 			source.Reply("%s", Language::Translate(source.nc, descriptions[source.command].c_str()));
    161 			return true;
    162 		}
    163 		return false;
    164 	}
    165 };
    166 
    167 class CSSetMisc : public Module
    168 {
    169 	CommandCSSetMisc commandcssetmisc;
    170 	Serialize::Type csmiscdata_type;
    171 
    172  public:
    173 	CSSetMisc(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, VENDOR),
    174 		commandcssetmisc(this), csmiscdata_type("CSMiscData", CSMiscData::Unserialize)
    175 	{
    176 		me = this;
    177 	}
    178 
    179 	~CSSetMisc()
    180 	{
    181 		for (Anope::map<ExtensibleItem<CSMiscData> *>::iterator it = items.begin(); it != items.end(); ++it)
    182 			delete it->second;
    183 	}
    184 
    185 	void OnReload(Configuration::Conf *conf) anope_override
    186 	{
    187 		descriptions.clear();
    188 
    189 		for (int i = 0; i < conf->CountBlock("command"); ++i)
    190 		{
    191 			Configuration::Block *block = conf->GetBlock("command", i);
    192 
    193 			if (block->Get<const Anope::string>("command") != "chanserv/set/misc")
    194 				continue;
    195 
    196 			Anope::string cname = block->Get<const Anope::string>("name");
    197 			Anope::string desc = block->Get<const Anope::string>("misc_description");
    198 
    199 			if (cname.empty() || desc.empty())
    200 				continue;
    201 
    202 			descriptions[cname] = desc;
    203 		}
    204 	}
    205 
    206 	void OnChanInfo(CommandSource &source, ChannelInfo *ci, InfoFormatter &info, bool) anope_override
    207 	{
    208 		for (Anope::map<ExtensibleItem<CSMiscData> *>::iterator it = items.begin(); it != items.end(); ++it)
    209 		{
    210 			ExtensibleItem<CSMiscData> *e = it->second;
    211 			MiscData *data = e->Get(ci);
    212 
    213 			if (data != NULL)
    214 				info[e->name.substr(12).replace_all_cs("_", " ")] = data->data;
    215 		}
    216 	}
    217 };
    218 
    219 MODULE_INIT(CSSetMisc)