1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
|
/* OperServ core functions
*
* (C) 2003-2012 Anope Team
* Contact us at team@anope.org
*
* Please read COPYING and README for further details.
*
* Based on the original code of Epona by Lara.
* Based on the original code of Services by Andy Church.
*/
/*************************************************************************/
#include "module.h"
class CommandOSConfig : public Command
{
void ChangeHash(ConfigDataHash &hash, const Anope::string &block, const Anope::string &iname, const Anope::string &value)
{
ConfigDataHash::iterator it = hash.find(block);
KeyValList &list = it->second;
for (unsigned i = 0; i < list.size(); ++i)
{
const Anope::string &item = list[i].first;
if (item == iname)
{
list[i] = std::make_pair(item, value);
break;
}
}
}
public:
CommandOSConfig(Module *creator) : Command(creator, "operserv/config", 1, 4)
{
this->SetDesc(_("View and change configuration file settings"));
this->SetSyntax(_("{\037MODIFY\037|\037VIEW\037} [\037block name\037 \037item name\037 \037item value\037]"));
}
void Execute(CommandSource &source, const std::vector<Anope::string> ¶ms)
{
const Anope::string &what = params[0];
if (what.equals_ci("MODIFY") && params.size() > 3)
{
ConfigItems configitems(Config);
for (unsigned i = 0; !configitems.Values[i].tag.empty(); ++i)
{
ConfigItems::Item *v = &configitems.Values[i];
if (v->tag.equals_cs(params[1]) && v->value.equals_cs(params[2]))
{
try
{
ValueItem vi(params[3]);
if (!v->validation_function(Config, v->tag, v->value, vi))
throw ConfigException("Parameter failed to validate.");
int dt = v->datatype;
if (dt & DT_NORELOAD)
throw ConfigException("This item can not be changed while Anope is running.");
bool allow_wild = dt & DT_ALLOW_WILD;
dt &= ~(DT_ALLOW_NEWLINE | DT_ALLOW_WILD);
/* Yay for *massive* copypaste from config.cpp */
switch (dt)
{
case DT_NOSPACES:
{
ValueContainerString *vcs = debug_cast<ValueContainerString *>(v->val);
Config->ValidateNoSpaces(vi.GetValue(), v->tag, v->value);
vcs->Set(vi.GetValue());
break;
}
case DT_HOSTNAME:
{
ValueContainerString *vcs = debug_cast<ValueContainerString *>(v->val);
Config->ValidateHostname(vi.GetValue(), v->tag, v->value);
vcs->Set(vi.GetValue());
break;
}
case DT_IPADDRESS:
{
ValueContainerString *vcs = debug_cast<ValueContainerString *>(v->val);
Config->ValidateIP(vi.GetValue(), v->tag, v->value, allow_wild);
vcs->Set(vi.GetValue());
break;
}
case DT_STRING:
{
ValueContainerString *vcs = debug_cast<ValueContainerString *>(v->val);
vcs->Set(vi.GetValue());
break;
}
case DT_INTEGER:
{
int val = vi.GetInteger();
ValueContainerInt *vci = debug_cast<ValueContainerInt *>(v->val);
vci->Set(&val, sizeof(int));
break;
}
case DT_UINTEGER:
{
unsigned val = vi.GetInteger();
ValueContainerUInt *vci = debug_cast<ValueContainerUInt *>(v->val);
vci->Set(&val, sizeof(unsigned));
break;
}
case DT_LUINTEGER:
{
unsigned long val = vi.GetInteger();
ValueContainerLUInt *vci = debug_cast<ValueContainerLUInt *>(v->val);
vci->Set(&val, sizeof(unsigned long));
break;
}
case DT_TIME:
{
time_t time = dotime(vi.GetValue());
ValueContainerTime *vci = debug_cast<ValueContainerTime *>(v->val);
vci->Set(&time, sizeof(time_t));
break;
}
case DT_BOOLEAN:
{
bool val = vi.GetBool();
ValueContainerBool *vcb = debug_cast<ValueContainerBool *>(v->val);
vcb->Set(&val, sizeof(bool));
break;
}
default:
break;
}
}
catch (const ConfigException &ex)
{
source.Reply(_("Error changing configuration value: ") + ex.GetReason());
return;
}
ChangeHash(Config->config_data, params[1], params[2], params[3]);
Log(LOG_ADMIN, source.u, this) << "to change the configuration value of " << params[1] << ":" << params[2] << " to " << params[3];
source.Reply(_("Value of %s:%s changed to %s"), params[1].c_str(), params[2].c_str(), params[3].c_str());
return;
}
}
source.Reply("There is no configuration value named %s:%s", params[1].c_str(), params[2].c_str());
}
else if (what.equals_ci("VIEW"))
{
/* Blocks we should show */
const Anope::string show_blocks[] = { "botserv", "chanserv", "defcon", "global", "memoserv", "nickserv", "networkinfo", "operserv", "options", "" };
Log(LOG_ADMIN, source.u, this) << "VIEW";
for (ConfigDataHash::const_iterator it = Config->config_data.begin(), it_end = Config->config_data.end(); it != it_end; ++it)
{
const Anope::string &bname = it->first;
const KeyValList &list = it->second;
bool ok = false;
for (unsigned i = 0; !show_blocks[i].empty(); ++i)
if (bname == show_blocks[i])
ok = true;
if (ok == false)
continue;
ListFormatter lflist;
lflist.addColumn("Name").addColumn("Value");
for (unsigned i = 0; i < list.size(); ++i)
{
const Anope::string &first = list[i].first, second = list[i].second;
ListFormatter::ListEntry entry;
entry["Name"] = first;
entry["Value"] = second;
lflist.addEntry(entry);
}
std::vector<Anope::string> replies;
lflist.Process(replies);
source.Reply(_("%s settings:"), bname.c_str());
for (unsigned i = 0; i < replies.size(); ++i)
source.Reply(replies[i]);
}
source.Reply(_("End of configuration."));
}
else
this->OnSyntaxError(source, what);
return;
}
bool OnHelp(CommandSource &source, const Anope::string &subcommand)
{
this->SendSyntax(source);
source.Reply(" ");
source.Reply(_("Allows you to change and view configuration settings.\n"
"Settings changed by this command are temporary and will not be reflected\n"
"back into the configuration file, and will be lost if Anope is shut down,\n"
"restarted, or the RELOAD command is used.\n"
" \n"
"Example:\n"
" \002MODIFY nickserv forcemail no\002"));
return true;
}
};
class OSConfig : public Module
{
CommandOSConfig commandosconfig;
public:
OSConfig(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, CORE),
commandosconfig(this)
{
this->SetAuthor("Anope");
}
};
MODULE_INIT(OSConfig)
|