]> jfr.im git - irc/rqf/shadowircd.git/blob - modules/m_services.c
Clean these modules up.
[irc/rqf/shadowircd.git] / modules / m_services.c
1 /* modules/m_services.c
2 * Copyright (C) 2005 Lee Hardy <lee -at- leeh.co.uk>
3 * Copyright (C) 2005 ircd-ratbox development team
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * 1.Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 * 2.Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3.The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
21 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
26 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
28 *
29 * $Id: m_services.c 1907 2006-08-29 19:18:15Z jilles $
30 */
31
32 #include "stdinc.h"
33
34 #include "tools.h"
35 #include "send.h"
36 #include "channel.h"
37 #include "client.h"
38 #include "common.h"
39 #include "config.h"
40 #include "ircd.h"
41 #include "numeric.h"
42 #include "memory.h"
43 #include "s_conf.h"
44 #include "s_newconf.h"
45 #include "s_serv.h"
46 #include "hash.h"
47 #include "msg.h"
48 #include "parse.h"
49 #include "modules.h"
50 #include "sprintf_irc.h"
51 #include "whowas.h"
52 #include "monitor.h"
53
54 static int me_su(struct Client *, struct Client *, int, const char **);
55 static int me_login(struct Client *, struct Client *, int, const char **);
56 static int me_rsfnc(struct Client *, struct Client *, int, const char **);
57 static int me_nickdelay(struct Client *, struct Client *, int, const char **);
58
59 static void h_svc_server_introduced(hook_data_client *);
60 static void h_svc_whois(hook_data_client *);
61 static void h_svc_stats(hook_data_int *);
62
63 struct Message su_msgtab = {
64 "SU", 0, 0, 0, MFLG_SLOW,
65 {mg_ignore, mg_ignore, mg_ignore, mg_ignore, {me_su, 2}, mg_ignore}
66 };
67 struct Message login_msgtab = {
68 "LOGIN", 0, 0, 0, MFLG_SLOW,
69 {mg_ignore, mg_ignore, mg_ignore, mg_ignore, {me_login, 2}, mg_ignore}
70 };
71 struct Message rsfnc_msgtab = {
72 "RSFNC", 0, 0, 0, MFLG_SLOW,
73 {mg_ignore, mg_ignore, mg_ignore, mg_ignore, {me_rsfnc, 4}, mg_ignore}
74 };
75 struct Message nickdelay_msgtab = {
76 "NICKDELAY", 0, 0, 0, MFLG_SLOW,
77 {mg_unreg, mg_ignore, mg_ignore, mg_ignore, {me_nickdelay, 3}, mg_ignore}
78 };
79
80 mapi_clist_av1 services_clist[] = {
81 &su_msgtab, &login_msgtab, &rsfnc_msgtab, &nickdelay_msgtab, NULL
82 };
83 mapi_hfn_list_av1 services_hfnlist[] = {
84 { "doing_stats", (hookfn) h_svc_stats },
85 { "doing_whois", (hookfn) h_svc_whois },
86 { "doing_whois_global", (hookfn) h_svc_whois },
87 { "server_introduced", (hookfn) h_svc_server_introduced },
88 { NULL, NULL }
89 };
90
91 DECLARE_MODULE_AV1(services, NULL, NULL, services_clist, NULL, services_hfnlist, "$Revision: 1907 $");
92
93 static int
94 me_su(struct Client *client_p, struct Client *source_p,
95 int parc, const char *parv[])
96 {
97 struct Client *target_p;
98
99 if(!(source_p->flags & FLAGS_SERVICE))
100 return 0;
101
102 if((target_p = find_client(parv[1])) == NULL)
103 return 0;
104
105 if(!target_p->user)
106 return 0;
107
108 if(EmptyString(parv[2]))
109 target_p->user->suser[0] = '\0';
110 else
111 strlcpy(target_p->user->suser, parv[2], sizeof(target_p->user->suser));
112
113 invalidate_bancache_user(target_p);
114
115 return 0;
116 }
117
118 static int
119 me_login(struct Client *client_p, struct Client *source_p,
120 int parc, const char *parv[])
121 {
122 if(!IsPerson(source_p))
123 return 0;
124
125 strlcpy(source_p->user->suser, parv[1], sizeof(source_p->user->suser));
126 return 0;
127 }
128
129 static int
130 clean_nick(const char *nick)
131 {
132 int len = 0;
133
134 if(EmptyString(nick) || *nick == '-' || IsDigit(*nick))
135 return 0;
136
137 for(; *nick; nick++)
138 {
139 len++;
140 if(!IsNickChar(*nick))
141 return 0;
142 }
143
144 if(len >= NICKLEN)
145 return 0;
146
147 return 1;
148 }
149
150 static int
151 me_rsfnc(struct Client *client_p, struct Client *source_p,
152 int parc, const char *parv[])
153 {
154 struct Client *target_p;
155 struct Client *exist_p;
156 time_t newts, curts;
157 char note[NICKLEN + 10];
158
159 if(!(source_p->flags & FLAGS_SERVICE))
160 return 0;
161
162 if((target_p = find_person(parv[1])) == NULL)
163 return 0;
164
165 if(!MyClient(target_p))
166 return 0;
167
168 if(!clean_nick(parv[2]))
169 return 0;
170
171 curts = atol(parv[4]);
172
173 /* if tsinfo is different from what it was when services issued the
174 * RSFNC, then we ignore it. This can happen when a client changes
175 * nicknames before the RSFNC arrives.. --anfl
176 */
177 if(target_p->tsinfo != curts)
178 return 0;
179
180 if((exist_p = find_named_client(parv[2])))
181 {
182 char buf[BUFSIZE];
183
184 /* this would be one hell of a race condition to trigger
185 * this one given the tsinfo check above, but its here for
186 * safety --anfl
187 */
188 if(target_p == exist_p)
189 return 0;
190
191 if(MyClient(exist_p))
192 sendto_one(exist_p, ":%s KILL %s :(Nickname regained by services)",
193 me.name, exist_p->name);
194
195 exist_p->flags |= FLAGS_KILLED;
196 /* Do not send kills to servers for unknowns -- jilles */
197 if(IsClient(exist_p))
198 kill_client_serv_butone(NULL, exist_p, "%s (Nickname regained by services)",
199 me.name);
200
201 snprintf(buf, sizeof(buf), "Killed (%s (Nickname regained by services))",
202 me.name);
203 exit_client(NULL, exist_p, &me, buf);
204 }
205
206 newts = atol(parv[3]);
207
208 /* timestamp is older than 15mins, ignore it */
209 if(newts < (CurrentTime - 900))
210 newts = CurrentTime - 900;
211
212 target_p->tsinfo = newts;
213
214 monitor_signoff(target_p);
215
216 invalidate_bancache_user(target_p);
217
218 sendto_realops_snomask(SNO_NCHANGE, L_ALL,
219 "Nick change: From %s to %s [%s@%s]",
220 target_p->name, parv[2], target_p->username,
221 target_p->host);
222
223 sendto_common_channels_local(target_p, ":%s!%s@%s NICK :%s",
224 target_p->name, target_p->username,
225 target_p->host, parv[2]);
226
227 add_history(target_p, 1);
228 sendto_server(NULL, NULL, CAP_TS6, NOCAPS, ":%s NICK %s :%ld",
229 use_id(target_p), parv[2], (long) target_p->tsinfo);
230 sendto_server(NULL, NULL, NOCAPS, CAP_TS6, ":%s NICK %s :%ld",
231 target_p->name, parv[2], (long) target_p->tsinfo);
232
233 del_from_client_hash(target_p->name, target_p);
234 strcpy(target_p->name, parv[2]);
235 add_to_client_hash(target_p->name, target_p);
236
237 monitor_signon(target_p);
238
239 del_all_accepts(target_p);
240
241 rb_snprintf(note, NICKLEN + 10, "Nick: %s", target_p->name);
242 rb_note(target_p->localClient->F, note);
243 return 0;
244 }
245
246 /*
247 ** me_nickdelay
248 ** parv[0] = sender prefix
249 ** parv[1] = duration in seconds (0 to remove)
250 ** parv[2] = nick
251 */
252 static int
253 me_nickdelay(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
254 {
255 int duration;
256 struct nd_entry *nd;
257
258 if(!(source_p->flags & FLAGS_SERVICE))
259 return 0;
260
261 duration = atoi(parv[1]);
262 if (duration <= 0)
263 {
264 nd = irc_dictionary_retrieve(nd_dict, parv[2]);
265 if (nd != NULL)
266 free_nd_entry(nd);
267 }
268 else
269 {
270 if (duration > 86400)
271 duration = 86400;
272 add_nd_entry(parv[2]);
273 nd = irc_dictionary_retrieve(nd_dict, parv[2]);
274 if (nd != NULL)
275 nd->expire = CurrentTime + duration;
276 }
277
278 return 0;
279 }
280
281 static void
282 h_svc_server_introduced(hook_data_client *hdata)
283 {
284 rb_dlink_node *ptr;
285
286 RB_DLINK_FOREACH(ptr, service_list.head)
287 {
288 if(!irccmp((const char *) ptr->data, hdata->target->name))
289 {
290 hdata->target->flags |= FLAGS_SERVICE;
291 return;
292 }
293 }
294 }
295
296 static void
297 h_svc_whois(hook_data_client *data)
298 {
299 char *p = data->target->user->suser;
300 if(!EmptyString(p))
301 {
302 /* Try to strip off any leading digits as this may be used to
303 * store both an ID number and an account name in one field.
304 * If only digits are present, leave as is.
305 */
306 while(IsDigit(*p))
307 p++;
308 if(*p == '\0')
309 p = data->target->user->suser;
310
311 sendto_one(data->client, form_str(RPL_WHOISLOGGEDIN),
312 get_id(&me, data->client),
313 get_id(data->client, data->client),
314 data->target->name, p);
315 }
316 }
317
318 static void
319 h_svc_stats(hook_data_int *data)
320 {
321 char statchar = (char) data->arg2;
322 rb_dlink_node *ptr;
323
324 if (statchar == 'U' && IsOper(data->client))
325 {
326 RB_DLINK_FOREACH(ptr, service_list.head)
327 {
328 sendto_one_numeric(data->client, RPL_STATSULINE,
329 form_str(RPL_STATSULINE),
330 ptr->data, "*", "*", "s");
331 }
332 }
333 }