Debian Codegraph @

modules/global/main.c

raw
1 /*
2 * Copyright (c) 2005 Atheme Development Group
3 * Rights to this code are documented in doc/LICENSE.
4 *
5 * This file contains the main() routine.
6 *
7 */
8
9 #include "atheme.h"
10
11 DECLARE_MODULE_V1
12 (
13 "global/main", false, _modinit, _moddeinit,
14 PACKAGE_STRING,
15 VENDOR_STRING
16 );
17
18 /* global list struct */
19 struct global_ {
20 char *text;
21 };
22
23 service_t *globsvs = NULL;
24
25 static void gs_cmd_global(sourceinfo_t *si, const int parc, char *parv[]);
26 static void gs_cmd_help(sourceinfo_t *si, const int parc, char *parv[]);
27
28 command_t gs_help = { "HELP", N_("Displays contextual help information."),
29 PRIV_GLOBAL, 1, gs_cmd_help, { .path = "help" } };
30 command_t gs_global = { "GLOBAL", N_("Sends a global notice."),
31 PRIV_GLOBAL, 1, gs_cmd_global, { .path = "gservice/global" } };
32
33 /* *INDENT-ON* */
34
35 /* HELP <command> [params] */
36 static void gs_cmd_help(sourceinfo_t *si, const int parc, char *parv[])
37 {
38 char *command = parv[0];
39
40 if (!command)
41 {
42 command_help(si, si->service->commands);
43 command_success_nodata(si, "For more specific help use \2HELP \37command\37\2.");
44
45 return;
46 }
47
48 /* take the command through the hash table */
49 help_display(si, si->service, command, si->service->commands);
50 }
51
52 /* GLOBAL <parameters>|SEND|CLEAR */
53 static void gs_cmd_global(sourceinfo_t *si, const int parc, char *parv[])
54 {
55 static mowgli_heap_t *glob_heap = NULL;
56 struct global_ *global;
57 static mowgli_list_t globlist;
58 mowgli_node_t *n, *tn;
59 char *params = parv[0];
60 static char *sender = NULL;
61 bool isfirst;
62 char buf[BUFSIZE];
63
64 if (!params)
65 {
66 command_fail(si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, "GLOBAL");
67 command_fail(si, fault_needmoreparams, _("Syntax: GLOBAL <parameters>|SEND|CLEAR"));
68 return;
69 }
70
71 if (!strcasecmp("CLEAR", params))
72 {
73 if (!globlist.count)
74 {
75 command_fail(si, fault_nochange, _("No message to clear."));
76 return;
77 }
78
79 /* destroy the list we made */
80 MOWGLI_ITER_FOREACH_SAFE(n, tn, globlist.head)
81 {
82 global = (struct global_ *)n->data;
83 mowgli_node_delete(n, &globlist);
84 mowgli_node_free(n);
85 free(global->text);
86 mowgli_heap_free(glob_heap, global);
87 }
88
89 mowgli_heap_destroy(glob_heap);
90 glob_heap = NULL;
91 free(sender);
92 sender = NULL;
93
94 command_success_nodata(si, "The pending message has been deleted.");
95
96 return;
97 }
98
99 if (!strcasecmp("SEND", params))
100 {
101 if (!globlist.count)
102 {
103 command_fail(si, fault_nosuch_target, _("No message to send."));
104 return;
105 }
106
107 isfirst = true;
108 MOWGLI_ITER_FOREACH(n, globlist.head)
109 {
110 global = (struct global_ *)n->data;
111
112 snprintf(buf, sizeof buf, "[Network Notice] %s%s%s",
113 isfirst ? get_source_name(si) : "",
114 isfirst ? " - " : "",
115 global->text);
116 /* Cannot use si->service->me here, global notices
117 * should come from global even if /os global was
118 * used. */
119 notice_global_sts(globsvs->me, "*", buf);
120 isfirst = false;
121 /* log everything */
122 logcommand(si, CMDLOG_ADMIN, "GLOBAL: \2%s\2", global->text);
123 }
124 logcommand(si, CMDLOG_ADMIN, "GLOBAL: (\2%zu\2 lines sent)", MOWGLI_LIST_LENGTH(&globlist));
125
126 /* destroy the list we made */
127 MOWGLI_ITER_FOREACH_SAFE(n, tn, globlist.head)
128 {
129 global = (struct global_ *)n->data;
130 mowgli_node_delete(n, &globlist);
131 mowgli_node_free(n);
132 free(global->text);
133 mowgli_heap_free(glob_heap, global);
134 }
135
136 mowgli_heap_destroy(glob_heap);
137 glob_heap = NULL;
138 free(sender);
139 sender = NULL;
140
141 command_success_nodata(si, "The global notice has been sent.");
142
143 return;
144 }
145
146 if (!strcasecmp("LIST", params))
147 {
148 if (!globlist.count)
149 {
150 command_fail(si, fault_nosuch_target, _("No messages to list."));
151 return;
152 }
153
154 isfirst = true;
155 MOWGLI_ITER_FOREACH(n, globlist.head)
156 {
157 global = (struct global_ *)n->data;
158
159 snprintf(buf, sizeof buf, "[Network Notice] %s%s%s",
160 isfirst ? get_source_name(si) : "",
161 isfirst ? " - " : "",
162 global->text);
163 /* Use command_success_nodata here, we only want to send
164 * to the person running the command.
165 */
166 command_success_nodata(si, "%s", buf);
167 isfirst = false;
168 }
169 logcommand(si, CMDLOG_ADMIN, "GLOBAL:LIST");
170
171 command_success_nodata(si, "End of list.");
172
173 return;
174 }
175
176 if (!glob_heap)
177 glob_heap = mowgli_heap_create(sizeof(struct global_), 5, BH_NOW);
178
179 if (!sender)
180 sender = sstrdup(get_source_name(si));
181
182 if (irccasecmp(sender, get_source_name(si)))
183 {
184 command_fail(si, fault_noprivs, _("There is already a GLOBAL in progress by \2%s\2."), sender);
185 return;
186 }
187
188 global = mowgli_heap_alloc(glob_heap);
189
190 global->text = sstrdup(params);
191
192 n = mowgli_node_create();
193 mowgli_node_add(global, n, &globlist);
194
195 command_success_nodata(si,
196 "Stored text to be sent as line %zu. Use \2GLOBAL SEND\2 "
197 "to send message, \2GLOBAL CLEAR\2 to delete the pending message, " "\2GLOBAL LIST\2 to preview what will be sent, " "or \2GLOBAL\2 to store additional lines.", MOWGLI_LIST_LENGTH(&globlist));
198 }
199
200 void _modinit(module_t *m)
201 {
202 globsvs = service_add("global", NULL);
203
204 service_bind_command(globsvs, &gs_global);
205 service_named_bind_command("operserv", &gs_global);
206
207 service_bind_command(globsvs, &gs_help);
208 }
209
210 void _moddeinit(module_unload_intent_t intent)
211 {
212 service_unbind_command(globsvs, &gs_help);
213 service_unbind_command(globsvs, &gs_global);
214 service_named_unbind_command("operserv", &gs_global);
215
216 if (globsvs != NULL)
217 service_delete(globsvs);
218 }
219
220 /* vim:cinoptions=>s,e0,n0,f0,{0,}0,^0,=s,ps,t0,c3,+s,(2s,us,)20,*30,gs,hs
221 * vim:ts=8
222 * vim:sw=8
223 * vim:noexpandtab
224 */