forked from atheme/atheme-contrib-modules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathos_savechanmodes.c
184 lines (156 loc) · 4.26 KB
/
os_savechanmodes.c
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
/*
* Copyright (c) 2008 Jilles Tjoelker
* Rights to this code are as documented in doc/LICENSE.
*
* Dump/restore channel modes
*/
#include "atheme-compat.h"
static void
os_cmd_savechanmodes(sourceinfo_t *si, int parc, char *parv[])
{
FILE *out;
mowgli_patricia_iteration_state_t state;
channel_t *c;
mowgli_node_t *n;
chanban_t *cb;
if (!(out = fopen(DATADIR "/chanmodes.txt", "w")))
{
command_fail(si, fault_nosuch_source, "Cannot open %s: %s",
DATADIR "/chanmodes.txt", strerror(errno));
return;
}
logcommand(si, CMDLOG_ADMIN, "SAVECHANMODES");
wallops("\2%s\2 is dumping channel modes", get_oper_name(si));
MOWGLI_PATRICIA_FOREACH(c, &state, chanlist)
{
fprintf(out, "chan %s %s\n", c->name, channel_modes(c, true));
if (c->topic)
fprintf(out, "topic %s %lu %s\n", c->topic_setter,
(unsigned long)c->topicts,
c->topic);
MOWGLI_ITER_FOREACH(n, c->bans.head)
{
cb = n->data;
fprintf(out, "ban %c %s\n", cb->type, cb->mask);
}
}
fclose(out);
command_success_nodata(si, "Channel modes saved to %s.",
DATADIR "/chanmodes.txt");
}
static channel_t *
restore_channel(char *name, char *modes)
{
channel_t *c;
int modeparc;
char *modeparv[256];
service_t *svs;
svs = service_find("operserv");
if (svs == NULL)
svs = chansvs.me;
join(name, chansvs.nick);
c = channel_find(name);
if (c != NULL)
{
modeparc = sjtoken(modes, ' ', modeparv);
channel_mode(svs->me, c, modeparc, modeparv);
}
return c;
}
static void
os_cmd_loadchanmodes(sourceinfo_t *si, int parc, char *parv[])
{
FILE *in;
char *item, buf[2048];
char *name, *modes, *setter, *tsstr, *topic, *type, *mask;
time_t ts, prevtopicts;
channel_t *c;
int line;
if (!(in = fopen(DATADIR "/chanmodes.txt", "r")))
{
command_fail(si, fault_nosuch_source, "Cannot open %s: %s",
DATADIR "/chanmodes.txt", strerror(errno));
return;
}
logcommand(si, CMDLOG_ADMIN, "LOADCHANMODES");
wallops("\2%s\2 is restoring channel modes", get_oper_name(si));
line = 0;
c = NULL;
while (fgets(buf, sizeof buf, in))
{
line++;
item = strtok(buf, " ");
strip(item);
if (item == NULL || *item == '#')
continue;
if (!strcmp(item, "chan"))
{
name = strtok(NULL, " ");
modes = strtok(NULL, "\n");
if (name == NULL || modes == NULL)
continue;
c = restore_channel(name, modes);
}
else if (!strcmp(item, "topic"))
{
if (c == NULL)
continue;
setter = strtok(NULL, " ");
tsstr = strtok(NULL, " ");
topic = strtok(NULL, "\n");
if (setter == NULL || tsstr == NULL || topic == NULL)
continue;
if (c->topic != NULL)
continue;
prevtopicts = c->topicts;
ts = strtoul(tsstr, NULL, 10);
handle_topic(c, setter, ts, topic);
topic_sts(c, chansvs.me->me, setter, ts, prevtopicts, topic);
}
else if (!strcmp(item, "ban"))
{
if (c == NULL)
continue;
type = strtok(NULL, " ");
mask = strtok(NULL, "\n");
if (type == NULL || mask == NULL)
continue;
modestack_mode_param(chansvs.nick, c, MTYPE_ADD, type[0], mask);
chanban_add(c, mask, type[0]);
}
}
fclose(in);
command_success_nodata(si, "Channel modes restored from %s.",
DATADIR "/chanmodes.txt");
command_success_nodata(si, "Remember to restart services to make %s leave channels it should not be in.",
chansvs.nick);
}
static command_t os_savechanmodes = {
.name = "SAVECHANMODES",
.desc = N_("Dumps channel modes to a file."),
.access = PRIV_ADMIN,
.maxparc = 1,
.cmd = &os_cmd_savechanmodes,
.help = { .path = "contrib/savechanmodes" },
};
static command_t os_loadchanmodes = {
.name = "LOADCHANMODES",
.desc = N_("Restores channel modes from a file."),
.access = PRIV_ADMIN,
.maxparc = 1,
.cmd = &os_cmd_loadchanmodes,
.help = { .path = "contrib/loadchanmodes" },
};
static void
mod_init(module_t *const restrict m)
{
service_named_bind_command("operserv", &os_savechanmodes);
service_named_bind_command("operserv", &os_loadchanmodes);
}
static void
mod_deinit(const module_unload_intent_t intent)
{
service_named_unbind_command("operserv", &os_savechanmodes);
service_named_unbind_command("operserv", &os_loadchanmodes);
}
VENDOR_DECLARE_MODULE_V1("contrib/os_savechanmodes", MODULE_UNLOAD_CAPABILITY_OK, CONTRIB_VENDOR_JILLEST)