Debian Codegraph @

modules/memoserv/ignore.c

raw
1 /*
2 * Copyright (c) 2005 Atheme Development Group
3 * Rights to this code are as documented in doc/LICENSE.
4 *
5 * This file contains code for the Memoserv IGNORE functions
6 *
7 */
8
9 #include "atheme.h"
10
11 DECLARE_MODULE_V1
12 (
13 "memoserv/ignore", false, _modinit, _moddeinit,
14 PACKAGE_STRING,
15 VENDOR_STRING
16 );
17
18 static void ms_cmd_ignore(sourceinfo_t *si, int parc, char *parv[]);
19 static void ms_cmd_ignore_add(sourceinfo_t *si, int parc, char *parv[]);
20 static void ms_cmd_ignore_del(sourceinfo_t *si, int parc, char *parv[]);
21 static void ms_cmd_ignore_clear(sourceinfo_t *si, int parc, char *parv[]);
22 static void ms_cmd_ignore_list(sourceinfo_t *si, int parc, char *parv[]);
23
24 command_t ms_ignore = { "IGNORE", N_(N_("Ignores memos.")), AC_AUTHENTICATED, 2, ms_cmd_ignore, { .path = "memoserv/ignore" } };
25 command_t ms_ignore_add = { "ADD", N_(N_("Ignores memos from a user.")), AC_AUTHENTICATED, 1, ms_cmd_ignore_add, { .path = "" } };
26 command_t ms_ignore_del = { "DEL", N_(N_("Stops ignoring memos from a user.")), AC_AUTHENTICATED, 1, ms_cmd_ignore_del, { .path = "" } };
27 command_t ms_ignore_clear = { "CLEAR", N_(N_("Clears your memo ignore list.")), AC_AUTHENTICATED, 1, ms_cmd_ignore_clear, { .path = "" } };
28 command_t ms_ignore_list = { "LIST", N_(N_("Shows all users you are ignoring memos from.")), AC_AUTHENTICATED, 1, ms_cmd_ignore_list, { .path = "" } };
29
30 mowgli_patricia_t *ms_ignore_cmds;
31
32 void _modinit(module_t *m)
33 {
34 service_named_bind_command("memoserv", &ms_ignore);
35
36 ms_ignore_cmds = mowgli_patricia_create(strcasecanon);
37
38 /* Add sub-commands */
39 command_add(&ms_ignore_add, ms_ignore_cmds);
40 command_add(&ms_ignore_del, ms_ignore_cmds);
41 command_add(&ms_ignore_clear, ms_ignore_cmds);
42 command_add(&ms_ignore_list, ms_ignore_cmds);
43 }
44
45 void _moddeinit(module_unload_intent_t intent)
46 {
47 service_named_unbind_command("memoserv", &ms_ignore);
48
49 /* Delete sub-commands */
50 command_delete(&ms_ignore_add, ms_ignore_cmds);
51 command_delete(&ms_ignore_del, ms_ignore_cmds);
52 command_delete(&ms_ignore_clear, ms_ignore_cmds);
53 command_delete(&ms_ignore_list, ms_ignore_cmds);
54
55 mowgli_patricia_destroy(ms_ignore_cmds, NULL, NULL);
56 }
57
58 static void ms_cmd_ignore(sourceinfo_t *si, int parc, char *parv[])
59 {
60 /* Grab args */
61 char *cmd = parv[0];
62 command_t *c;
63
64 /* Bad/missing arg */
65 if (!cmd)
66 {
67 command_fail(si, fault_needmoreparams,
68 STR_INSUFFICIENT_PARAMS, "IGNORE");
69
70 command_fail(si, fault_needmoreparams, _("Syntax: IGNORE ADD|DEL|LIST|CLEAR [account]"));
71 return;
72 }
73
74 c = command_find(ms_ignore_cmds, cmd);
75 if (c == NULL)
76 {
77 command_fail(si, fault_badparams, _("Invalid command. Use \2/%s%s help\2 for a command listing."), (ircd->uses_rcommand == false) ? "msg " : "", si->service->disp);
78 return;
79 }
80
81 command_exec(si->service, si, c, parc - 1, parv + 1);
82 }
83
84 static void ms_cmd_ignore_add(sourceinfo_t *si, int parc, char *parv[])
85 {
86 myuser_t *tmu;
87 mowgli_node_t *n;
88 const char *newnick;
89 char *temp;
90
91 /* Arg check */
92 if (parc < 1)
93 {
94 command_fail(si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, "IGNORE");
95
96 command_fail(si, fault_needmoreparams, _("Syntax: IGNORE ADD|DEL|LIST|CLEAR <account>"));
97 return;
98 }
99
100 /* User attempting to ignore themself? */
101 if (!irccasecmp(parv[0], entity(si->smu)->name))
102 {
103 command_fail(si, fault_badparams, _("Silly wabbit, you can't ignore yourself."));
104 return;
105 }
106
107 /* Does the target account exist? */
108 if (!(tmu = myuser_find_ext(parv[0])))
109 {
110 command_fail(si, fault_nosuch_target, _("\2%s\2 is not registered."), parv[0]);
111 return;
112 }
113 newnick = entity(tmu)->name;
114
115 /* Ignore list is full */
116 if (si->smu->memo_ignores.count >= MAXMSIGNORES)
117 {
118 command_fail(si, fault_toomany, _("Your ignore list is full, please DEL an account."));
119 return;
120 }
121
122 /* Iterate through list, make sure target not in it, if last node append */
123 MOWGLI_ITER_FOREACH(n, si->smu->memo_ignores.head)
124 {
125 temp = (char *)n->data;
126
127 /* Already in the list */
128 if (!irccasecmp(temp, newnick))
129 {
130 command_fail(si, fault_nochange, _("Account \2%s\2 is already in your ignore list."), temp);
131 return;
132 }
133 }
134
135 /* Add to ignore list */
136 temp = sstrdup(newnick);
137 mowgli_node_add(temp, mowgli_node_create(), &si->smu->memo_ignores);
138 logcommand(si, CMDLOG_SET, "IGNORE:ADD: \2%s\2", newnick);
139 command_success_nodata(si, _("Account \2%s\2 added to your ignore list."), newnick);
140 return;
141 }
142
143 static void ms_cmd_ignore_del(sourceinfo_t *si, int parc, char *parv[])
144 {
145 mowgli_node_t *n, *tn;
146 char *temp;
147
148 /* Arg check */
149 if (parc < 1)
150 {
151 command_fail(si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, "IGNORE");
152 command_fail(si, fault_needmoreparams, _("Syntax: IGNORE ADD|DEL|LIST|CLEAR <account>"));
153 return;
154 }
155
156 /* Iterate through list, make sure they're not in it, if last node append */
157 MOWGLI_ITER_FOREACH_SAFE(n, tn, si->smu->memo_ignores.head)
158 {
159 temp = (char *)n->data;
160
161 /* Not using list or clear, but we've found our target in the ignore list */
162 if (!irccasecmp(temp, parv[0]))
163 {
164 logcommand(si, CMDLOG_SET, "IGNORE:DEL: \2%s\2", temp);
165 command_success_nodata(si, _("Account \2%s\2 removed from ignore list."), temp);
166 mowgli_node_delete(n, &si->smu->memo_ignores);
167 mowgli_node_free(n);
168 free(temp);
169
170 return;
171 }
172 }
173
174 command_fail(si, fault_nosuch_target, _("\2%s\2 is not in your ignore list."), parv[0]);
175 return;
176 }
177
178 static void ms_cmd_ignore_clear(sourceinfo_t *si, int parc, char *parv[])
179 {
180 mowgli_node_t *n, *tn;
181
182 if (MOWGLI_LIST_LENGTH(&si->smu->memo_ignores) == 0)
183 {
184 command_fail(si, fault_nochange, _("Ignore list already empty."));
185 return;
186 }
187
188 MOWGLI_ITER_FOREACH_SAFE(n, tn, si->smu->memo_ignores.head)
189 {
190 free(n->data);
191 mowgli_node_delete(n,&si->smu->memo_ignores);
192 mowgli_node_free(n);
193 }
194
195 /* Let them know list is clear */
196 command_success_nodata(si, _("Ignore list cleared."));
197 logcommand(si, CMDLOG_SET, "IGNORE:CLEAR");
198 return;
199 }
200
201 static void ms_cmd_ignore_list(sourceinfo_t *si, int parc, char *parv[])
202 {
203 mowgli_node_t *n;
204 unsigned int i = 1;
205
206 /* Throw in list header */
207 command_success_nodata(si, _("Ignore list:"));
208 command_success_nodata(si, "-------------------------");
209
210 /* Iterate through list, make sure they're not in it, if last node append */
211 MOWGLI_ITER_FOREACH(n, si->smu->memo_ignores.head)
212 {
213 command_success_nodata(si, "%d - %s", i, (char *)n->data);
214 i++;
215 }
216
217 /* Ignore list footer */
218 if (i == 1)
219 command_success_nodata(si, _("list empty"));
220
221 command_success_nodata(si, "-------------------------");
222 return;
223 }
224
225 /* vim:cinoptions=>s,e0,n0,f0,{0,}0,^0,=s,ps,t0,c3,+s,(2s,us,)20,*30,gs,hs
226 * vim:ts=8
227 * vim:sw=8
228 * vim:noexpandtab
229 */