Debian Codegraph @

modules/contrib/src/ns_ajoin.c

raw
1 /*
2 * Copyright (c) 2007 Jilles Tjoelker
3 * Copyright (c) 2008 Robin Burchell
4 * Rights to this code are as documented in doc/LICENSE.
5 *
6 * Services-side autojoin using SVSJOIN
7 */
8
9 #include "atheme-compat.h"
10
11 #if (CURRENT_ABI_REVISION < 730000)
12 # include "uplink.h"
13 #endif
14
15 static void
16 ns_cmd_ajoin_syntaxerr(sourceinfo_t *si)
17 {
18 command_fail(si, fault_badparams, STR_INSUFFICIENT_PARAMS, "AJOIN");
19 command_fail(si, fault_badparams, _("Syntax: AJOIN <LIST|ADD|DEL|CLEAR> [#channel]"));
20 }
21
22 static void
23 ns_cmd_ajoin(sourceinfo_t *si, int parc, char *parv[])
24 {
25 char buf[512];
26 char *chan;
27 metadata_t *md;
28
29 if (!parv[0])
30 return ns_cmd_ajoin_syntaxerr(si);
31
32 if (!strcasecmp(parv[0], "LIST"))
33 {
34 command_success_nodata(si, "\2AJOIN LIST\2:");
35
36 if ((md = metadata_find(si->smu, "private:autojoin")))
37 {
38 mowgli_strlcpy(buf, md->value, sizeof buf);
39 chan = strtok(buf, ",");
40
41 while (chan != NULL)
42 {
43 command_success_nodata(si, "%s", chan);
44 chan = strtok(NULL, ",");
45 }
46 }
47
48 command_success_nodata(si, _("End of \2AJOIN LIST\2"));
49 }
50 else if (!strcasecmp(parv[0], "ADD"))
51 {
52 if (!parv[1])
53 return ns_cmd_ajoin_syntaxerr(si);
54
55 if ((md = metadata_find(si->smu, "private:autojoin")))
56 {
57 mowgli_strlcpy(buf, md->value, sizeof buf);
58 chan = strtok(buf, ",");
59
60 while (chan != NULL)
61 {
62 if (!strcasecmp(chan, parv[1]))
63 {
64 command_fail(si, fault_badparams, _("%s is already on your AJOIN list."), parv[1]);
65 return;
66 }
67
68 chan = strtok(NULL, ",");
69 }
70
71 // Little arbitrary, but stop both overflow and RAM consumption going out of control
72 if (strlen(md->value) + strlen(parv[1]) > 400)
73 {
74 command_fail(si, fault_badparams, _("Sorry, you have too many AJOIN entries set."));
75 return;
76 }
77
78 mowgli_strlcpy(buf, md->value, sizeof buf);
79 mowgli_strlcat(buf, ",", sizeof buf);
80 mowgli_strlcat(buf, parv[1], sizeof buf);
81 metadata_delete(si->smu, "private:autojoin");
82 metadata_add(si->smu, "private:autojoin", buf);
83 }
84 else
85 {
86 metadata_add(si->smu, "private:autojoin", parv[1]);
87 }
88
89 command_success_nodata(si, _("%s added to AJOIN successfully."), parv[1]);
90 }
91 else if (!strcasecmp(parv[0], "CLEAR"))
92 {
93 metadata_delete(si->smu, "private:autojoin");
94 command_success_nodata(si, _("AJOIN list cleared successfully."));
95 }
96 else if (!strcasecmp(parv[0], "DEL"))
97 {
98 if (!parv[1])
99 return ns_cmd_ajoin_syntaxerr(si);
100
101 if (!(md = metadata_find(si->smu, "private:autojoin")))
102 {
103 command_fail(si, fault_badparams, _("%s is not on your AJOIN list."), parv[1]);
104 return;
105 }
106
107 // Thanks to John Brooks for his help with this.
108 char *list = md->value;
109 char *remove1 = parv[1];
110
111 int listlen = 0;
112 int rmlen = 0;
113 int itempos = 0;
114 int i = 0, j = 0;
115
116 // This loop will find the item (if present), find the length of the item,
117 // and find the length of the entire string.
118 for (; list[i]; i++)
119 {
120 if (!rmlen)
121 {
122 // We have not found the string yet
123 if (tolower(list[i]) == tolower(remove1[j]))
124 {
125 if (j == 0)
126 {
127 // First character of a potential match; remember it's location
128 itempos = i;
129 }
130
131 j++;
132
133 if (!remove1[j])
134 {
135 // Found the entire string
136 rmlen = j;
137 }
138 }
139 else
140 j = 0;
141 }
142 }
143
144 if (remove1[j])
145 {
146 command_fail(si, fault_badparams, _("%s is not on your AJOIN list."), parv[1]);
147 return;
148 }
149
150 listlen = i;
151
152 // listlen is the length of the list, rmlen is the length of the item to remove,
153 // itempos is the beginning of that item.
154 if (!list[itempos + rmlen])
155 {
156 // This item is the last item in the list, so we can simply truncate
157 if (itempos > 0)
158 {
159 itempos--;
160 list[itempos] = '\0';
161 }
162 else
163 metadata_delete(si->smu, "private:autojoin");
164 }
165 else
166 {
167 // There are items after this one, so we must copy memory
168 // Account for the comma following this item (if there is a space,
169 // account for that too, depends on how you format your list)
170 rmlen += 1;
171 memmove(list + itempos, list + itempos + rmlen, listlen - rmlen - itempos);
172 list[listlen - rmlen] = '\0';
173 }
174
175 command_success_nodata(si, _("%s removed from AJOIN successfully."), parv[1]);
176 }
177 }
178
179 static void
180 ajoin_on_identify(user_t *u)
181 {
182 myuser_t *mu = u->myuser;
183 metadata_t *md;
184 char buf[512];
185 char *chan;
186
187 if (!(md = metadata_find(mu, "private:autojoin")))
188 return;
189
190 mowgli_strlcpy(buf, md->value, sizeof buf);
191 chan = strtok(buf, " ,");
192 while (chan != NULL)
193 {
194 if(ircd->type == PROTOCOL_ELEMENTAL_IRCD)
195 {
196 sts(":%s ENCAP * SVSJOIN %s %s", ME, CLIENT_NAME(u), chan);
197 }
198 else
199 {
200 sts(":%s SVSJOIN %s %s", CLIENT_NAME(nicksvs.me->me), CLIENT_NAME(u), chan);
201 }
202
203 chan = strtok(NULL, ",");
204 }
205 }
206
207 static command_t ns_ajoin = {
208 .name = "AJOIN",
209 .desc = N_("Manages automatic-join on identify."),
210 .access = AC_AUTHENTICATED,
211 .maxparc = 2,
212 .cmd = &ns_cmd_ajoin,
213 .help = { .path = "contrib/ajoin" },
214 };
215
216 static void
217 mod_init(module_t *const restrict m)
218 {
219 hook_add_event("user_identify");
220 hook_add_user_identify(ajoin_on_identify);
221
222 service_named_bind_command("nickserv", &ns_ajoin);
223 }
224
225 static void
226 mod_deinit(const module_unload_intent_t intent)
227 {
228 hook_del_user_identify(ajoin_on_identify);
229
230 service_named_unbind_command("nickserv", &ns_ajoin);
231 }
232
233 SIMPLE_DECLARE_MODULE_V1("contrib/ns_ajoin", MODULE_UNLOAD_CAPABILITY_OK)