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