1 /* modules/m_services.c
2 * Copyright (C) 2005 Lee Hardy <lee -at- leeh.co.uk>
3 * Copyright (C) 2005 ircd-ratbox development team
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
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.
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.
40 #include "s_newconf.h"
48 #include "supported.h"
50 static const char services_desc
[] = "Provides support for running a services daemon";
52 static int _modinit(void);
53 static void _moddeinit(void);
55 static void mark_services(void);
56 static void unmark_services(void);
58 static void me_su(struct MsgBuf
*, struct Client
*, struct Client
*, int, const char **);
59 static void me_login(struct MsgBuf
*, struct Client
*, struct Client
*, int, const char **);
60 static void me_rsfnc(struct MsgBuf
*, struct Client
*, struct Client
*, int, const char **);
61 static void me_nickdelay(struct MsgBuf
*, struct Client
*, struct Client
*, int, const char **);
63 static void h_svc_server_introduced(hook_data_client
*);
64 static void h_svc_whois(hook_data_client
*);
65 static void h_svc_stats(hook_data_int
*);
66 static void h_svc_conf_read_start(void *);
67 static void h_svc_conf_read_end(void *);
69 struct Message su_msgtab
= {
71 {mg_ignore
, mg_ignore
, mg_ignore
, mg_ignore
, {me_su
, 2}, mg_ignore
}
73 struct Message login_msgtab
= {
75 {mg_ignore
, mg_ignore
, mg_ignore
, mg_ignore
, {me_login
, 2}, mg_ignore
}
77 struct Message rsfnc_msgtab
= {
79 {mg_ignore
, mg_ignore
, mg_ignore
, mg_ignore
, {me_rsfnc
, 4}, mg_ignore
}
81 struct Message nickdelay_msgtab
= {
82 "NICKDELAY", 0, 0, 0, 0,
83 {mg_unreg
, mg_ignore
, mg_ignore
, mg_ignore
, {me_nickdelay
, 3}, mg_ignore
}
86 mapi_clist_av1 services_clist
[] = {
87 &su_msgtab
, &login_msgtab
, &rsfnc_msgtab
, &nickdelay_msgtab
, NULL
89 mapi_hfn_list_av1 services_hfnlist
[] = {
90 { "doing_stats", (hookfn
) h_svc_stats
},
91 { "doing_whois", (hookfn
) h_svc_whois
},
92 { "doing_whois_global", (hookfn
) h_svc_whois
},
93 { "server_introduced", (hookfn
) h_svc_server_introduced
},
94 { "conf_read_start", (hookfn
) h_svc_conf_read_start
},
95 { "conf_read_end", (hookfn
) h_svc_conf_read_end
},
99 DECLARE_MODULE_AV2(services
, _modinit
, _moddeinit
, services_clist
, NULL
, services_hfnlist
, NULL
, NULL
, services_desc
);
105 add_isupport("FNC", isupport_string
, "");
112 delete_isupport("FNC");
117 me_su(struct MsgBuf
*msgbuf_p
, struct Client
*client_p
, struct Client
*source_p
,
118 int parc
, const char *parv
[])
120 struct Client
*target_p
;
122 if(!(source_p
->flags
& FLAGS_SERVICE
))
124 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
125 "Non-service server %s attempting to execute services-only command SU", source_p
->name
);
129 if((target_p
= find_client(parv
[1])) == NULL
)
135 if(EmptyString(parv
[2]))
136 target_p
->user
->suser
[0] = '\0';
138 rb_strlcpy(target_p
->user
->suser
, parv
[2], sizeof(target_p
->user
->suser
));
140 sendto_common_channels_local_butone(target_p
, CLICAP_ACCOUNT_NOTIFY
, NOCAPS
, ":%s!%s@%s ACCOUNT %s",
141 target_p
->name
, target_p
->username
, target_p
->host
,
142 EmptyString(target_p
->user
->suser
) ? "*" : target_p
->user
->suser
);
144 invalidate_bancache_user(target_p
);
148 me_login(struct MsgBuf
*msgbuf_p
, struct Client
*client_p
, struct Client
*source_p
,
149 int parc
, const char *parv
[])
151 if(!IsPerson(source_p
))
154 rb_strlcpy(source_p
->user
->suser
, parv
[1], sizeof(source_p
->user
->suser
));
158 me_rsfnc(struct MsgBuf
*msgbuf_p
, struct Client
*client_p
, struct Client
*source_p
,
159 int parc
, const char *parv
[])
161 struct Client
*target_p
;
162 struct Client
*exist_p
;
164 char note
[NICKLEN
+ 10];
166 if(!(source_p
->flags
& FLAGS_SERVICE
))
168 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
169 "Non-service server %s attempting to execute services-only command RSFNC", source_p
->name
);
173 if((target_p
= find_person(parv
[1])) == NULL
)
176 if(!MyClient(target_p
))
179 if(!clean_nick(parv
[2], 0) || IsDigit(parv
[2][0]))
182 curts
= atol(parv
[4]);
184 /* if tsinfo is different from what it was when services issued the
185 * RSFNC, then we ignore it. This can happen when a client changes
186 * nicknames before the RSFNC arrives.. --anfl
188 if(target_p
->tsinfo
!= curts
)
191 if((exist_p
= find_named_client(parv
[2])))
195 /* this would be one hell of a race condition to trigger
196 * this one given the tsinfo check above, but its here for
199 if(target_p
== exist_p
)
202 if(MyClient(exist_p
))
203 sendto_one(exist_p
, ":%s KILL %s :(Nickname regained by services)",
204 me
.name
, exist_p
->name
);
206 exist_p
->flags
|= FLAGS_KILLED
;
207 /* Do not send kills to servers for unknowns -- jilles */
208 if(IsClient(exist_p
))
210 kill_client_serv_butone(NULL
, exist_p
, "%s (Nickname regained by services)",
212 sendto_realops_snomask(SNO_SKILL
, L_ALL
,
213 "Nick collision due to services forced nick change on %s",
217 snprintf(buf
, sizeof(buf
), "Killed (%s (Nickname regained by services))",
219 exit_client(NULL
, exist_p
, &me
, buf
);
223 newts
= atol(parv
[3]);
225 /* timestamp is older than 15mins, ignore it */
226 if(newts
< (rb_current_time() - 900))
227 newts
= rb_current_time() - 900;
229 target_p
->tsinfo
= newts
;
231 monitor_signoff(target_p
);
233 invalidate_bancache_user(target_p
);
235 sendto_realops_snomask(SNO_NCHANGE
, L_ALL
,
236 "Nick change: From %s to %s [%s@%s]",
237 target_p
->name
, parv
[2], target_p
->username
,
240 sendto_common_channels_local(target_p
, NOCAPS
, NOCAPS
, ":%s!%s@%s NICK :%s",
241 target_p
->name
, target_p
->username
,
242 target_p
->host
, parv
[2]);
244 whowas_add_history(target_p
, 1);
245 sendto_server(NULL
, NULL
, CAP_TS6
, NOCAPS
, ":%s NICK %s :%ld",
246 use_id(target_p
), parv
[2], (long) target_p
->tsinfo
);
248 del_from_client_hash(target_p
->name
, target_p
);
249 rb_strlcpy(target_p
->name
, parv
[2], NICKLEN
);
250 add_to_client_hash(target_p
->name
, target_p
);
252 monitor_signon(target_p
);
254 del_all_accepts(target_p
);
256 snprintf(note
, NICKLEN
+ 10, "Nick: %s", target_p
->name
);
257 rb_note(target_p
->localClient
->F
, note
);
262 ** parv[1] = duration in seconds (0 to remove)
266 me_nickdelay(struct MsgBuf
*msgbuf_p
, struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
271 if(!(source_p
->flags
& FLAGS_SERVICE
))
273 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
274 "Non-service server %s attempting to execute services-only command NICKDELAY", source_p
->name
);
278 duration
= atoi(parv
[1]);
281 nd
= rb_dictionary_retrieve(nd_dict
, parv
[2]);
287 if (duration
> 86400)
289 add_nd_entry(parv
[2]);
290 nd
= rb_dictionary_retrieve(nd_dict
, parv
[2]);
292 nd
->expire
= rb_current_time() + duration
;
297 h_svc_server_introduced(hook_data_client
*hdata
)
301 RB_DLINK_FOREACH(ptr
, service_list
.head
)
303 if(!irccmp((const char *) ptr
->data
, hdata
->target
->name
))
305 hdata
->target
->flags
|= FLAGS_SERVICE
;
312 h_svc_whois(hook_data_client
*data
)
314 char *p
= data
->target
->user
->suser
;
317 /* Try to strip off any leading digits as this may be used to
318 * store both an ID number and an account name in one field.
319 * If only digits are present, leave as is.
324 p
= data
->target
->user
->suser
;
326 sendto_one_numeric(data
->client
, RPL_WHOISLOGGEDIN
,
327 form_str(RPL_WHOISLOGGEDIN
),
328 data
->target
->name
, p
);
333 h_svc_stats(hook_data_int
*data
)
335 char statchar
= (char) data
->arg2
;
338 if (statchar
== 'U' && IsOper(data
->client
))
340 RB_DLINK_FOREACH(ptr
, service_list
.head
)
342 sendto_one_numeric(data
->client
, RPL_STATSULINE
,
343 form_str(RPL_STATSULINE
),
344 (const char *)ptr
->data
, "*", "*", "s");
350 h_svc_conf_read_start(void *dummy
)
356 unmark_services(void)
358 struct Client
*target_p
;
361 RB_DLINK_FOREACH(ptr
, global_serv_list
.head
)
363 target_p
= ptr
->data
;
365 target_p
->flags
&= ~FLAGS_SERVICE
;
370 h_svc_conf_read_end(void *dummy
)
378 struct Client
*target_p
;
381 RB_DLINK_FOREACH(ptr
, service_list
.head
)
383 target_p
= find_server(NULL
, (const char *)ptr
->data
);
386 target_p
->flags
|= FLAGS_SERVICE
;