]>
Commit | Line | Data |
---|---|---|
212380e3 AC |
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 | ||
212380e3 AC |
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" | |
212380e3 AC |
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" | |
212380e3 AC |
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 | |
f427c8b0 | 108 | rb_strlcpy(target_p->user->suser, parv[2], sizeof(target_p->user->suser)); |
212380e3 AC |
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 | ||
f427c8b0 | 122 | rb_strlcpy(source_p->user->suser, parv[1], sizeof(source_p->user->suser)); |
212380e3 AC |
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; | |
75d60088 | 154 | char note[NICKLEN + 10]; |
212380e3 AC |
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 | ||
c2f73e5d | 198 | rb_snprintf(buf, sizeof(buf), "Killed (%s (Nickname regained by services))", |
212380e3 AC |
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 */ | |
e3354945 VY |
206 | if(newts < (rb_current_time() - 900)) |
207 | newts = rb_current_time() - 900; | |
212380e3 AC |
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); | |
212380e3 AC |
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 | ||
75d60088 AC |
236 | rb_snprintf(note, NICKLEN + 10, "Nick: %s", target_p->name); |
237 | rb_note(target_p->localClient->F, note); | |
212380e3 AC |
238 | return 0; |
239 | } | |
240 | ||
241 | /* | |
242 | ** me_nickdelay | |
243 | ** parv[0] = sender prefix | |
244 | ** parv[1] = duration in seconds (0 to remove) | |
245 | ** parv[2] = nick | |
246 | */ | |
247 | static int | |
248 | me_nickdelay(struct Client *client_p, struct Client *source_p, int parc, const char *parv[]) | |
249 | { | |
250 | int duration; | |
251 | struct nd_entry *nd; | |
252 | ||
253 | if(!(source_p->flags & FLAGS_SERVICE)) | |
254 | return 0; | |
255 | ||
256 | duration = atoi(parv[1]); | |
257 | if (duration <= 0) | |
258 | { | |
b37021a4 | 259 | nd = irc_dictionary_retrieve(nd_dict, parv[2]); |
212380e3 AC |
260 | if (nd != NULL) |
261 | free_nd_entry(nd); | |
262 | } | |
263 | else | |
264 | { | |
265 | if (duration > 86400) | |
266 | duration = 86400; | |
267 | add_nd_entry(parv[2]); | |
b37021a4 | 268 | nd = irc_dictionary_retrieve(nd_dict, parv[2]); |
212380e3 | 269 | if (nd != NULL) |
e3354945 | 270 | nd->expire = rb_current_time() + duration; |
212380e3 AC |
271 | } |
272 | ||
273 | return 0; | |
274 | } | |
275 | ||
276 | static void | |
277 | h_svc_server_introduced(hook_data_client *hdata) | |
278 | { | |
5b96d9a6 | 279 | rb_dlink_node *ptr; |
212380e3 | 280 | |
5b96d9a6 | 281 | RB_DLINK_FOREACH(ptr, service_list.head) |
212380e3 AC |
282 | { |
283 | if(!irccmp((const char *) ptr->data, hdata->target->name)) | |
284 | { | |
285 | hdata->target->flags |= FLAGS_SERVICE; | |
286 | return; | |
287 | } | |
288 | } | |
289 | } | |
290 | ||
291 | static void | |
292 | h_svc_whois(hook_data_client *data) | |
293 | { | |
294 | char *p = data->target->user->suser; | |
295 | if(!EmptyString(p)) | |
296 | { | |
297 | /* Try to strip off any leading digits as this may be used to | |
298 | * store both an ID number and an account name in one field. | |
299 | * If only digits are present, leave as is. | |
300 | */ | |
301 | while(IsDigit(*p)) | |
302 | p++; | |
303 | if(*p == '\0') | |
304 | p = data->target->user->suser; | |
305 | ||
306 | sendto_one(data->client, form_str(RPL_WHOISLOGGEDIN), | |
307 | get_id(&me, data->client), | |
308 | get_id(data->client, data->client), | |
309 | data->target->name, p); | |
310 | } | |
311 | } | |
312 | ||
313 | static void | |
314 | h_svc_stats(hook_data_int *data) | |
315 | { | |
316 | char statchar = (char) data->arg2; | |
5b96d9a6 | 317 | rb_dlink_node *ptr; |
212380e3 AC |
318 | |
319 | if (statchar == 'U' && IsOper(data->client)) | |
320 | { | |
5b96d9a6 | 321 | RB_DLINK_FOREACH(ptr, service_list.head) |
212380e3 AC |
322 | { |
323 | sendto_one_numeric(data->client, RPL_STATSULINE, | |
324 | form_str(RPL_STATSULINE), | |
325 | ptr->data, "*", "*", "s"); | |
326 | } | |
327 | } | |
328 | } |