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