Debian Codegraph @

modules/statserv/netsplit.c

raw
1 /*
2 * Copyright (c) 2011 Alexandria Wolcott
3 * Released under the same terms as Atheme itself.
4 *
5 * Netsplit monitor
6 *
7 */
8
9 #include "atheme.h"
10
11 DECLARE_MODULE_V1("statserv/netsplit", false, _modinit, _moddeinit,
12 PACKAGE_STRING, "Alexandria Wolcott <[email protected]>");
13
14 static void ss_cmd_netsplit(sourceinfo_t * si, int parc, char *parv[]);
15 static void ss_cmd_netsplit_list(sourceinfo_t * si, int parc, char *parv[]);
16 static void ss_cmd_netsplit_remove(sourceinfo_t * si, int parc, char *parv[]);
17
18 command_t ss_netsplit =
19 { "NETSPLIT", N_("Monitor network splits."), PRIV_SERVER_AUSPEX, 2, ss_cmd_netsplit, {.path = "statserv/netsplit"} };
20
21 command_t ss_netsplit_list =
22 { "LIST", N_("List currently split servers."), PRIV_SERVER_AUSPEX, 1, ss_cmd_netsplit_list, {.path = ""} };
23
24 command_t ss_netsplit_remove =
25 { "REMOVE", N_("Remove a server from the netsplit list."), PRIV_JUPE, 2, ss_cmd_netsplit_remove, {.path = ""} };
26
27 mowgli_patricia_t *ss_netsplit_cmds;
28 mowgli_patricia_t *splitlist;
29 mowgli_heap_t *split_heap;
30
31 typedef struct {
32 char *name;
33 time_t disconnected_since;
34 unsigned int flags;
35 } split_t;
36
37 static void netsplit_delete_serv(split_t *s)
38 {
39 mowgli_patricia_delete(splitlist, s->name);
40 free(s->name);
41
42 mowgli_heap_free(split_heap, s);
43 }
44
45 static void netsplit_server_add(server_t *s)
46 {
47 split_t *serv = mowgli_patricia_retrieve(splitlist, s->name);
48 if (serv != NULL)
49 {
50 netsplit_delete_serv(serv);
51 }
52 }
53
54 static void netsplit_server_delete(hook_server_delete_t *serv)
55 {
56 split_t *s;
57
58 s = mowgli_heap_alloc(split_heap);
59 s->name = sstrdup(serv->s->name);
60 s->disconnected_since = CURRTIME;
61 s->flags = serv->s->flags;
62 mowgli_patricia_add(splitlist, s->name, s);
63 }
64
65 static void ss_cmd_netsplit(sourceinfo_t * si, int parc, char *parv[])
66 {
67 command_t *c;
68 char *cmd = parv[0];
69
70 if (!cmd)
71 {
72 command_fail(si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, "NETSPLIT");
73 command_fail(si, fault_needmoreparams,
74 _("Syntax: NETSPLIT [LIST|REMOVE] [parameters]"));
75 return;
76 }
77
78 c = command_find(ss_netsplit_cmds, cmd);
79 if (c == NULL)
80 {
81 command_fail(si, fault_badparams,
82 _("Invalid command. Use \2/%s%s help\2 for a command listing."),
83 (ircd->uses_rcommand == false) ? "msg " : "", si->service->disp);
84 return;
85 }
86
87 command_exec(si->service, si, c, parc - 1, parv + 1);
88 }
89
90 static void ss_cmd_netsplit_list(sourceinfo_t * si, int parc, char *parv[])
91 {
92 split_t *s;
93 mowgli_patricia_iteration_state_t state;
94 int i = 0;
95
96 MOWGLI_PATRICIA_FOREACH(s, &state, splitlist)
97 {
98 i++;
99 command_success_nodata(si, _("%d: %s [Split %s ago]"), i, s->name, time_ago(s->disconnected_since));
100 }
101 command_success_nodata(si, _("End of netsplit list."));
102 }
103
104 static void ss_cmd_netsplit_remove(sourceinfo_t * si, int parc, char *parv[])
105 {
106 char *name = parv[0];
107 split_t *s;
108
109 if (!name)
110 {
111 command_fail(si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, "NETSPLIT REMOVE");
112 command_fail(si, fault_needmoreparams,
113 _("Syntax: NETSPLIT REMOVE <server>"));
114 return;
115 }
116
117 s = mowgli_patricia_retrieve(splitlist, name);
118
119 if (s != NULL)
120 {
121 netsplit_delete_serv(s);
122 command_success_nodata(si, _("%s removed from the netsplit list."), name);
123 }
124 else
125 command_fail(si, fault_nosuch_target, _("The server \2%s\2 does is not a split server."), name);
126 }
127
128 void _modinit(module_t * m)
129 {
130 service_named_bind_command("statserv", &ss_netsplit);
131
132 ss_netsplit_cmds = mowgli_patricia_create(strcasecanon);
133
134 command_add(&ss_netsplit_list, ss_netsplit_cmds);
135 command_add(&ss_netsplit_remove, ss_netsplit_cmds);
136
137 hook_add_event("server_add");
138 hook_add_event("server_delete");
139 hook_add_server_add(netsplit_server_add);
140 hook_add_server_delete(netsplit_server_delete);
141
142 split_heap = mowgli_heap_create(sizeof(split_t), 30, BH_NOW);
143
144 if (split_heap == NULL)
145 {
146 slog(LG_INFO, "statserv/netsplit _modinit(): block allocator failure.");
147 exit(EXIT_FAILURE);
148 }
149
150 splitlist = mowgli_patricia_create(irccasecanon);
151 }
152
153 void _moddeinit(module_unload_intent_t intent)
154 {
155 mowgli_patricia_iteration_state_t state;
156 split_t *s;
157
158 MOWGLI_PATRICIA_FOREACH(s, &state, splitlist)
159 netsplit_delete_serv(s);
160
161 mowgli_heap_destroy(split_heap);
162
163 service_named_unbind_command("statserv", &ss_netsplit);
164
165 command_delete(&ss_netsplit_list, ss_netsplit_cmds);
166 command_delete(&ss_netsplit_remove, ss_netsplit_cmds);
167
168 hook_del_event("server_add");
169 hook_del_event("server_delete");
170 hook_del_server_add(netsplit_server_add);
171 hook_del_server_delete(netsplit_server_delete);
172
173 mowgli_patricia_destroy(ss_netsplit_cmds, NULL, NULL);
174 mowgli_patricia_destroy(splitlist, NULL, NULL);
175 }