Debian Codegraph @

modules/contrib/src/os_savechanmodes.c

raw
1 /*
2 * Copyright (c) 2008 Jilles Tjoelker
3 * Rights to this code are as documented in doc/LICENSE.
4 *
5 * Dump/restore channel modes
6 */
7
8 #include "atheme-compat.h"
9
10 static void
11 os_cmd_savechanmodes(sourceinfo_t *si, int parc, char *parv[])
12 {
13 FILE *out;
14 mowgli_patricia_iteration_state_t state;
15 channel_t *c;
16 mowgli_node_t *n;
17 chanban_t *cb;
18
19 if (!(out = fopen(DATADIR "/chanmodes.txt", "w")))
20 {
21 command_fail(si, fault_nosuch_source, "Cannot open %s: %s",
22 DATADIR "/chanmodes.txt", strerror(errno));
23 return;
24 }
25
26 logcommand(si, CMDLOG_ADMIN, "SAVECHANMODES");
27 wallops("\2%s\2 is dumping channel modes", get_oper_name(si));
28
29 MOWGLI_PATRICIA_FOREACH(c, &state, chanlist)
30 {
31 fprintf(out, "chan %s %s\n", c->name, channel_modes(c, true));
32 if (c->topic)
33 fprintf(out, "topic %s %lu %s\n", c->topic_setter,
34 (unsigned long)c->topicts,
35 c->topic);
36 MOWGLI_ITER_FOREACH(n, c->bans.head)
37 {
38 cb = n->data;
39 fprintf(out, "ban %c %s\n", cb->type, cb->mask);
40 }
41 }
42
43 fclose(out);
44
45 command_success_nodata(si, "Channel modes saved to %s.",
46 DATADIR "/chanmodes.txt");
47 }
48
49 static channel_t *
50 restore_channel(char *name, char *modes)
51 {
52 channel_t *c;
53 int modeparc;
54 char *modeparv[256];
55 service_t *svs;
56
57 svs = service_find("operserv");
58 if (svs == NULL)
59 svs = chansvs.me;
60
61 join(name, chansvs.nick);
62 c = channel_find(name);
63 if (c != NULL)
64 {
65 modeparc = sjtoken(modes, ' ', modeparv);
66 channel_mode(svs->me, c, modeparc, modeparv);
67 }
68 return c;
69 }
70
71 static void
72 os_cmd_loadchanmodes(sourceinfo_t *si, int parc, char *parv[])
73 {
74 FILE *in;
75 char *item, buf[2048];
76 char *name, *modes, *setter, *tsstr, *topic, *type, *mask;
77 time_t ts, prevtopicts;
78 channel_t *c;
79 int line;
80
81 if (!(in = fopen(DATADIR "/chanmodes.txt", "r")))
82 {
83 command_fail(si, fault_nosuch_source, "Cannot open %s: %s",
84 DATADIR "/chanmodes.txt", strerror(errno));
85 return;
86 }
87
88 logcommand(si, CMDLOG_ADMIN, "LOADCHANMODES");
89 wallops("\2%s\2 is restoring channel modes", get_oper_name(si));
90
91 line = 0;
92 c = NULL;
93 while (fgets(buf, sizeof buf, in))
94 {
95 line++;
96 item = strtok(buf, " ");
97 strip(item);
98
99 if (item == NULL || *item == '#')
100 continue;
101
102 if (!strcmp(item, "chan"))
103 {
104 name = strtok(NULL, " ");
105 modes = strtok(NULL, "\n");
106
107 if (name == NULL || modes == NULL)
108 continue;
109 c = restore_channel(name, modes);
110 }
111 else if (!strcmp(item, "topic"))
112 {
113 if (c == NULL)
114 continue;
115
116 setter = strtok(NULL, " ");
117 tsstr = strtok(NULL, " ");
118 topic = strtok(NULL, "\n");
119
120 if (setter == NULL || tsstr == NULL || topic == NULL)
121 continue;
122 if (c->topic != NULL)
123 continue;
124 prevtopicts = c->topicts;
125 ts = strtoul(tsstr, NULL, 10);
126 handle_topic(c, setter, ts, topic);
127 topic_sts(c, chansvs.me->me, setter, ts, prevtopicts, topic);
128 }
129 else if (!strcmp(item, "ban"))
130 {
131 if (c == NULL)
132 continue;
133
134 type = strtok(NULL, " ");
135 mask = strtok(NULL, "\n");
136
137 if (type == NULL || mask == NULL)
138 continue;
139 modestack_mode_param(chansvs.nick, c, MTYPE_ADD, type[0], mask);
140 chanban_add(c, mask, type[0]);
141 }
142 }
143
144 fclose(in);
145
146 command_success_nodata(si, "Channel modes restored from %s.",
147 DATADIR "/chanmodes.txt");
148 command_success_nodata(si, "Remember to restart services to make %s leave channels it should not be in.",
149 chansvs.nick);
150 }
151
152 static command_t os_savechanmodes = {
153 .name = "SAVECHANMODES",
154 .desc = N_("Dumps channel modes to a file."),
155 .access = PRIV_ADMIN,
156 .maxparc = 1,
157 .cmd = &os_cmd_savechanmodes,
158 .help = { .path = "contrib/savechanmodes" },
159 };
160
161 static command_t os_loadchanmodes = {
162 .name = "LOADCHANMODES",
163 .desc = N_("Restores channel modes from a file."),
164 .access = PRIV_ADMIN,
165 .maxparc = 1,
166 .cmd = &os_cmd_loadchanmodes,
167 .help = { .path = "contrib/loadchanmodes" },
168 };
169
170 static void
171 mod_init(module_t *const restrict m)
172 {
173 service_named_bind_command("operserv", &os_savechanmodes);
174 service_named_bind_command("operserv", &os_loadchanmodes);
175 }
176
177 static void
178 mod_deinit(const module_unload_intent_t intent)
179 {
180 service_named_unbind_command("operserv", &os_savechanmodes);
181 service_named_unbind_command("operserv", &os_loadchanmodes);
182 }
183
184 VENDOR_DECLARE_MODULE_V1("contrib/os_savechanmodes", MODULE_UNLOAD_CAPABILITY_OK, CONTRIB_VENDOR_JILLEST)