]> jfr.im git - irc/rqf/shadowircd.git/blob - modules/m_services.c
CurrentTime -> rb_currenttime();
[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 "send.h"
35 #include "channel.h"
36 #include "client.h"
37 #include "common.h"
38 #include "config.h"
39 #include "ircd.h"
40 #include "numeric.h"
41 #include "s_conf.h"
42 #include "s_newconf.h"
43 #include "s_serv.h"
44 #include "hash.h"
45 #include "msg.h"
46 #include "parse.h"
47 #include "modules.h"
48 #include "sprintf_irc.h"
49 #include "whowas.h"
50 #include "monitor.h"
51
52 static int me_su(struct Client *, struct Client *, int, const char **);
53 static int me_login(struct Client *, struct Client *, int, const char **);
54 static int me_rsfnc(struct Client *, struct Client *, int, const char **);
55 static int me_nickdelay(struct Client *, struct Client *, int, const char **);
56
57 static void h_svc_server_introduced(hook_data_client *);
58 static void h_svc_whois(hook_data_client *);
59 static void h_svc_stats(hook_data_int *);
60
61 struct Message su_msgtab = {
62 "SU", 0, 0, 0, MFLG_SLOW,
63 {mg_ignore, mg_ignore, mg_ignore, mg_ignore, {me_su, 2}, mg_ignore}
64 };
65 struct Message login_msgtab = {
66 "LOGIN", 0, 0, 0, MFLG_SLOW,
67 {mg_ignore, mg_ignore, mg_ignore, mg_ignore, {me_login, 2}, mg_ignore}
68 };
69 struct Message rsfnc_msgtab = {
70 "RSFNC", 0, 0, 0, MFLG_SLOW,
71 {mg_ignore, mg_ignore, mg_ignore, mg_ignore, {me_rsfnc, 4}, mg_ignore}
72 };
73 struct Message nickdelay_msgtab = {
74 "NICKDELAY", 0, 0, 0, MFLG_SLOW,
75 {mg_unreg, mg_ignore, mg_ignore, mg_ignore, {me_nickdelay, 3}, mg_ignore}
76 };
77
78 mapi_clist_av1 services_clist[] = {
79 &su_msgtab, &login_msgtab, &rsfnc_msgtab, &nickdelay_msgtab, NULL
80 };
81 mapi_hfn_list_av1 services_hfnlist[] = {
82 { "doing_stats", (hookfn) h_svc_stats },
83 { "doing_whois", (hookfn) h_svc_whois },
84 { "doing_whois_global", (hookfn) h_svc_whois },
85 { "server_introduced", (hookfn) h_svc_server_introduced },
86 { NULL, NULL }
87 };
88
89 DECLARE_MODULE_AV1(services, NULL, NULL, services_clist, NULL, services_hfnlist, "$Revision: 1907 $");
90
91 static int
92 me_su(struct Client *client_p, struct Client *source_p,
93 int parc, const char *parv[])
94 {
95 struct Client *target_p;
96
97 if(!(source_p->flags & FLAGS_SERVICE))
98 return 0;
99
100 if((target_p = find_client(parv[1])) == NULL)
101 return 0;
102
103 if(!target_p->user)
104 return 0;
105
106 if(EmptyString(parv[2]))
107 target_p->user->suser[0] = '\0';
108 else
109 strlcpy(target_p->user->suser, parv[2], sizeof(target_p->user->suser));
110
111 invalidate_bancache_user(target_p);
112
113 return 0;
114 }
115
116 static int
117 me_login(struct Client *client_p, struct Client *source_p,
118 int parc, const char *parv[])
119 {
120 if(!IsPerson(source_p))
121 return 0;
122
123 strlcpy(source_p->user->suser, parv[1], sizeof(source_p->user->suser));
124 return 0;
125 }
126
127 static int
128 clean_nick(const char *nick)
129 {
130 int len = 0;
131
132 if(EmptyString(nick) || *nick == '-' || IsDigit(*nick))
133 return 0;
134
135 for(; *nick; nick++)
136 {
137 len++;
138 if(!IsNickChar(*nick))
139 return 0;
140 }
141
142 if(len >= NICKLEN)
143 return 0;
144
145 return 1;
146 }
147
148 static int
149 me_rsfnc(struct Client *client_p, struct Client *source_p,
150 int parc, const char *parv[])
151 {
152 struct Client *target_p;
153 struct Client *exist_p;
154 time_t newts, curts;
155 char note[NICKLEN + 10];
156
157 if(!(source_p->flags & FLAGS_SERVICE))
158 return 0;
159
160 if((target_p = find_person(parv[1])) == NULL)
161 return 0;
162
163 if(!MyClient(target_p))
164 return 0;
165
166 if(!clean_nick(parv[2]))
167 return 0;
168
169 curts = atol(parv[4]);
170
171 /* if tsinfo is different from what it was when services issued the
172 * RSFNC, then we ignore it. This can happen when a client changes
173 * nicknames before the RSFNC arrives.. --anfl
174 */
175 if(target_p->tsinfo != curts)
176 return 0;
177
178 if((exist_p = find_named_client(parv[2])))
179 {
180 char buf[BUFSIZE];
181
182 /* this would be one hell of a race condition to trigger
183 * this one given the tsinfo check above, but its here for
184 * safety --anfl
185 */
186 if(target_p == exist_p)
187 return 0;
188
189 if(MyClient(exist_p))
190 sendto_one(exist_p, ":%s KILL %s :(Nickname regained by services)",
191 me.name, exist_p->name);
192
193 exist_p->flags |= FLAGS_KILLED;
194 /* Do not send kills to servers for unknowns -- jilles */
195 if(IsClient(exist_p))
196 kill_client_serv_butone(NULL, exist_p, "%s (Nickname regained by services)",
197 me.name);
198
199 snprintf(buf, sizeof(buf), "Killed (%s (Nickname regained by services))",
200 me.name);
201 exit_client(NULL, exist_p, &me, buf);
202 }
203
204 newts = atol(parv[3]);
205
206 /* timestamp is older than 15mins, ignore it */
207 if(newts < (rb_current_time() - 900))
208 newts = rb_current_time() - 900;
209
210 target_p->tsinfo = newts;
211
212 monitor_signoff(target_p);
213
214 invalidate_bancache_user(target_p);
215
216 sendto_realops_snomask(SNO_NCHANGE, L_ALL,
217 "Nick change: From %s to %s [%s@%s]",
218 target_p->name, parv[2], target_p->username,
219 target_p->host);
220
221 sendto_common_channels_local(target_p, ":%s!%s@%s NICK :%s",
222 target_p->name, target_p->username,
223 target_p->host, parv[2]);
224
225 add_history(target_p, 1);
226 sendto_server(NULL, NULL, CAP_TS6, NOCAPS, ":%s NICK %s :%ld",
227 use_id(target_p), parv[2], (long) target_p->tsinfo);
228 sendto_server(NULL, NULL, NOCAPS, CAP_TS6, ":%s NICK %s :%ld",
229 target_p->name, parv[2], (long) target_p->tsinfo);
230
231 del_from_client_hash(target_p->name, target_p);
232 strcpy(target_p->name, parv[2]);
233 add_to_client_hash(target_p->name, target_p);
234
235 monitor_signon(target_p);
236
237 del_all_accepts(target_p);
238
239 rb_snprintf(note, NICKLEN + 10, "Nick: %s", target_p->name);
240 rb_note(target_p->localClient->F, note);
241 return 0;
242 }
243
244 /*
245 ** me_nickdelay
246 ** parv[0] = sender prefix
247 ** parv[1] = duration in seconds (0 to remove)
248 ** parv[2] = nick
249 */
250 static int
251 me_nickdelay(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
252 {
253 int duration;
254 struct nd_entry *nd;
255
256 if(!(source_p->flags & FLAGS_SERVICE))
257 return 0;
258
259 duration = atoi(parv[1]);
260 if (duration <= 0)
261 {
262 nd = irc_dictionary_retrieve(nd_dict, parv[2]);
263 if (nd != NULL)
264 free_nd_entry(nd);
265 }
266 else
267 {
268 if (duration > 86400)
269 duration = 86400;
270 add_nd_entry(parv[2]);
271 nd = irc_dictionary_retrieve(nd_dict, parv[2]);
272 if (nd != NULL)
273 nd->expire = rb_current_time() + duration;
274 }
275
276 return 0;
277 }
278
279 static void
280 h_svc_server_introduced(hook_data_client *hdata)
281 {
282 rb_dlink_node *ptr;
283
284 RB_DLINK_FOREACH(ptr, service_list.head)
285 {
286 if(!irccmp((const char *) ptr->data, hdata->target->name))
287 {
288 hdata->target->flags |= FLAGS_SERVICE;
289 return;
290 }
291 }
292 }
293
294 static void
295 h_svc_whois(hook_data_client *data)
296 {
297 char *p = data->target->user->suser;
298 if(!EmptyString(p))
299 {
300 /* Try to strip off any leading digits as this may be used to
301 * store both an ID number and an account name in one field.
302 * If only digits are present, leave as is.
303 */
304 while(IsDigit(*p))
305 p++;
306 if(*p == '\0')
307 p = data->target->user->suser;
308
309 sendto_one(data->client, form_str(RPL_WHOISLOGGEDIN),
310 get_id(&me, data->client),
311 get_id(data->client, data->client),
312 data->target->name, p);
313 }
314 }
315
316 static void
317 h_svc_stats(hook_data_int *data)
318 {
319 char statchar = (char) data->arg2;
320 rb_dlink_node *ptr;
321
322 if (statchar == 'U' && IsOper(data->client))
323 {
324 RB_DLINK_FOREACH(ptr, service_list.head)
325 {
326 sendto_one_numeric(data->client, RPL_STATSULINE,
327 form_str(RPL_STATSULINE),
328 ptr->data, "*", "*", "s");
329 }
330 }
331 }