ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/ermyth/src/ptasks.C
Revision: 1.11
Committed: Sat Sep 22 14:27:30 2007 UTC (16 years, 8 months ago) by pippijn
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 1.10: +24 -30 lines
Log Message:
split up ermyth into ermyth-modules, libermyth (currently just ermyth-util) and ermyth-core

File Contents

# User Rev Content
1 pippijn 1.1 /*
2     * ptasks.C: Implementation of common protocol tasks.
3 pippijn 1.10 *
4     * Copyright © 2007 Pippijn van Steenhoven / The Ermyth Team
5     * Rights to this code are as documented in COPYING.
6     *
7     *
8     * Portions of this file were derived from sources bearing the following license:
9 pippijn 1.2 * Rights to this code are documented in doc/pod/license.pod.
10 pippijn 1.4 * Copyright © 2005-2007 Atheme Project (http://www.atheme.org)
11 pippijn 1.1 */
12    
13 pippijn 1.11 static char const rcsid[] = "$Id: ptasks.C,v 1.10 2007-09-16 18:54:45 pippijn Exp $";
14    
15     #include <boost/foreach.hpp>
16 pippijn 1.1
17     #include "atheme.h"
18 pippijn 1.11 #include <libermyth.h>
19     #include <ermyth/system.h>
20     #include <util/time.h>
21 pippijn 1.9 #include "servers.h"
22 pippijn 1.1 #include <account/myuser.h>
23     #include <account/mynick.h>
24     #include <account/kline.h>
25     #include <account/svsignore.h>
26 pippijn 1.11 #include <util/random.h>
27 pippijn 1.1 #include "uplink.h"
28     #include "pmodule.h"
29     #include "privs.h"
30 pippijn 1.6 #include "connection.h"
31 pippijn 1.1
32     void
33     handle_info (user_t *u)
34     {
35     unsigned int i;
36    
37     if (u == NULL)
38     return;
39     if (floodcheck (u, NULL))
40     return;
41    
42     for (i = 0; infotext[i]; i++)
43 pippijn 1.4 phandler->numeric_sts (me.name, 371, u->nick, ":%s", infotext[i]);
44 pippijn 1.1
45 pippijn 1.4 phandler->numeric_sts (me.name, 374, u->nick, ":End of /INFO list");
46 pippijn 1.1 }
47    
48     void
49     handle_version (user_t *u)
50     {
51     if (u == NULL)
52     return;
53     if (floodcheck (u, NULL))
54     return;
55    
56 pippijn 1.4 phandler->numeric_sts (me.name, 351, u->nick, ":" PACKAGE_NAME "-%s. %s %s%s%s%s%s%s%s%s%s%s [%s]", version, me.name, (match_mapping) ? "A" : "", log_debug_enabled ()? "d" : "", (me.auth) ? "e" : "", (config_options.flood_msgs) ? "F" : "", (config_options.leave_chans) ? "l" : "", (config_options.join_chans) ? "j" : "", (chansvs.changets) ? "t" : "", (!match_mapping) ? "R" : "", (config_options.raw) ? "r" : "", (runflags & RF_LIVE) ? "n" : "", ircd->ircdname);
57     phandler->numeric_sts (me.name, 351, u->nick, ":Compile time: %s, build %s", creation, generation);
58 pippijn 1.1 }
59    
60     void
61     handle_admin (user_t *u)
62     {
63     if (u == NULL)
64     return;
65    
66     if (floodcheck (u, NULL))
67     return;
68    
69 pippijn 1.4 phandler->numeric_sts (me.name, 256, u->nick, ":Administrative info about %s", me.name);
70     phandler->numeric_sts (me.name, 257, u->nick, ":%s", me.adminname);
71     phandler->numeric_sts (me.name, 258, u->nick, ":Ermyth IRC Services (" PACKAGE_NAME "-%s)", version);
72     phandler->numeric_sts (me.name, 259, u->nick, ":<%s>", me.adminemail);
73 pippijn 1.1 }
74    
75     static void
76 pippijn 1.4 connection_stats_cb (char const * const line, void *privdata)
77 pippijn 1.1 {
78 pippijn 1.4 phandler->numeric_sts (me.name, 249, ((user_t *) privdata)->nick, "F :%s", line);
79 pippijn 1.1 }
80    
81     void
82     handle_stats (user_t *u, char req)
83     {
84     kline_t *k;
85 pippijn 1.9 kline_t::list_type::iterator klit = kline_t::list.begin ();
86     kline_t::list_type::iterator klet = kline_t::list.end ();
87 pippijn 1.1 int i, j;
88     char fl[10];
89    
90     if (floodcheck (u, NULL))
91     return;
92     logcommand_user (NULL, u, CMDLOG_GET, "STATS %c", req);
93    
94     switch (req)
95     {
96 pippijn 1.4 #if 0 // bye for now, dictionaries
97 pippijn 1.1 case 'B':
98     if (!has_priv_user (u, PRIV_SERVER_AUSPEX))
99     break;
100    
101     dictionary_stats (dictionary_stats_cb, u);
102     break;
103 pippijn 1.4 #endif
104 pippijn 1.1
105     case 'C':
106     case 'c':
107     if (!has_priv_user (u, PRIV_SERVER_AUSPEX))
108     break;
109    
110 pippijn 1.11 foreach (uplink_t *ul, uplinks)
111     phandler->numeric_sts (me.name, 213, u->nick, "C *@127.0.0.1 A %s %d uplink", ul->name, ul->port);
112 pippijn 1.1 break;
113    
114     case 'E':
115     case 'e':
116     if (!has_priv_user (u, PRIV_SERVER_AUSPEX))
117     break;
118    
119 pippijn 1.4 phandler->numeric_sts (me.name, 249, u->nick, "E :Last event to run: %s", last_event_ran);
120 pippijn 1.1
121 pippijn 1.4 phandler->numeric_sts (me.name, 249, u->nick, "E :%-28s %s", "Operation", "Next Execution");
122 pippijn 1.1 for (i = 0; i < MAX_EVENTS; i++)
123     {
124     if (event_table[i].active)
125 pippijn 1.4 phandler->numeric_sts (me.name, 249, u->nick, "E :%-28s %4d seconds (%d)", event_table[i].name, event_table[i].when - NOW, event_table[i].frequency);
126 pippijn 1.1 }
127    
128     break;
129    
130     case 'f':
131     case 'F':
132     if (!has_priv_user (u, PRIV_SERVER_AUSPEX))
133     break;
134    
135 pippijn 1.6 connection_t::stats (connection_stats_cb, u);
136 pippijn 1.1 break;
137    
138     case 'H':
139     case 'h':
140     if (!has_priv_user (u, PRIV_SERVER_AUSPEX))
141     break;
142    
143 pippijn 1.11 foreach (uplink_t *ul, uplinks)
144     phandler->numeric_sts (me.name, 244, u->nick, "H * * %s", ul->name);
145 pippijn 1.1 break;
146    
147     case 'I':
148     case 'i':
149 pippijn 1.4 phandler->numeric_sts (me.name, 215, u->nick, "I * * *@%s 0 nonopered", me.name);
150 pippijn 1.1 break;
151    
152     case 'K':
153     case 'k':
154     if (!has_priv_user (u, PRIV_AKILL))
155     break;
156    
157 pippijn 1.4 while (klit != klet)
158 pippijn 1.1 {
159     k = *klit;
160    
161 pippijn 1.4 phandler->numeric_sts (me.name, 216, u->nick, "K %s * %s :%s", k->host, k->user, k->reason);
162     ++klit;
163 pippijn 1.1 }
164    
165     break;
166    
167     case 'o':
168     case 'O':
169     if (!has_priv_user (u, PRIV_VIEWPRIVS))
170     break;
171    
172 pippijn 1.11 foreach (soper_t *soper, soper_t::list)
173     {
174     j = 0;
175     if (!(soper->flags & SOPER_CONF))
176     fl[j++] = 'D';
177     if (soper->operclass != NULL && soper->operclass->flags & OPERCLASS_NEEDOPER)
178     fl[j++] = 'O';
179     if (j == 0)
180     fl[j++] = '*';
181     fl[j] = '\0';
182     phandler->numeric_sts (me.name, 243, u->nick, "O *@* %s %s %s %s", fl, soper->myuser ? soper->myuser->name : soper->name, soper->operclass ? soper->operclass->name : soper->classname, "-1");
183     }
184 pippijn 1.1 break;
185 pippijn 1.11
186 pippijn 1.1 case 'T':
187     case 't':
188     if (!has_priv_user (u, PRIV_SERVER_AUSPEX))
189     break;
190    
191 pippijn 1.4 phandler->numeric_sts (me.name, 249, u->nick, "T :event %7d", system_state.event);
192     phandler->numeric_sts (me.name, 249, u->nick, "T :node %7d", system_state.node);
193 pippijn 1.6 phandler->numeric_sts (me.name, 249, u->nick, "T :connection %7d", connection_t::count ());
194 pippijn 1.4 phandler->numeric_sts (me.name, 249, u->nick, "T :operclass %7d", cnt.operclass);
195     phandler->numeric_sts (me.name, 249, u->nick, "T :soper %7d", cnt.soper);
196     phandler->numeric_sts (me.name, 249, u->nick, "T :tld %7d", cnt.tld);
197     phandler->numeric_sts (me.name, 249, u->nick, "T :kline %7d", cnt.kline);
198     phandler->numeric_sts (me.name, 249, u->nick, "T :server %7d", cnt.server);
199     phandler->numeric_sts (me.name, 249, u->nick, "T :user %7d", cnt.user);
200     phandler->numeric_sts (me.name, 249, u->nick, "T :chan %7d", cnt.chan);
201     phandler->numeric_sts (me.name, 249, u->nick, "T :chanuser %7d", cnt.chanuser);
202     phandler->numeric_sts (me.name, 249, u->nick, "T :myuser %7d", cnt.myuser);
203     phandler->numeric_sts (me.name, 249, u->nick, "T :myuser_acc %7d", cnt.myuser_access);
204     phandler->numeric_sts (me.name, 249, u->nick, "T :mynick %7d", cnt.mynick);
205     phandler->numeric_sts (me.name, 249, u->nick, "T :mychan %7d", cnt.mychan);
206     phandler->numeric_sts (me.name, 249, u->nick, "T :chanacs %7d", cnt.chanacs);
207 pippijn 1.1
208 pippijn 1.4 phandler->numeric_sts (me.name, 249, u->nick, "T :bytes sent %7.2f%s", bytes (cnt.bout), sbytes (cnt.bout));
209     phandler->numeric_sts (me.name, 249, u->nick, "T :bytes recv %7.2f%s", bytes (cnt.bin), sbytes (cnt.bin));
210 pippijn 1.1 break;
211    
212     case 'u':
213 pippijn 1.4 phandler->numeric_sts (me.name, 242, u->nick, ":Services Uptime: %s", timediff (NOW - me.start));
214 pippijn 1.1 break;
215    
216     case 'V':
217     case 'v':
218     if (!has_priv_user (u, PRIV_SERVER_AUSPEX))
219     break;
220    
221     /* we received this command from the uplink, so,
222     * hmm, it is not idle */
223 pippijn 1.4 phandler->numeric_sts (me.name, 249, u->nick, "V :%s (AutoConn.!*@*) Idle: 0 SendQ: ? Connected: %s", curr_uplink->name, timediff (NOW - curr_uplink->conn->first_recv));
224 pippijn 1.1 break;
225    
226     default:
227     break;
228     }
229    
230 pippijn 1.4 phandler->numeric_sts (me.name, 219, u->nick, "%c :End of /STATS report", req);
231 pippijn 1.1 }
232    
233     void
234     handle_whois (user_t *u, char *target)
235     {
236     user_t *t = user_find_named (target);
237    
238     if (u == NULL)
239     return;
240     if (floodcheck (u, NULL))
241     return;
242    
243     if (t != NULL)
244     {
245 pippijn 1.4 phandler->numeric_sts (me.name, 311, u->nick, "%s %s %s * :%s", t->nick, t->user, t->vhost, t->gecos);
246 pippijn 1.1 /* channels purposely omitted */
247 pippijn 1.4 phandler->numeric_sts (me.name, 312, u->nick, "%s %s :%s", t->nick, t->server->name, t->server->desc);
248 pippijn 1.1 if (t->flags & UF_AWAY)
249 pippijn 1.4 phandler->numeric_sts (me.name, 301, u->nick, "%s :Gone", t->nick);
250 pippijn 1.1 if (is_ircop (t))
251 pippijn 1.4 phandler->numeric_sts (me.name, 313, u->nick, "%s :%s", t->nick, is_internal_client (t) ? "is a Network Service" : "is an IRC Operator");
252 pippijn 1.1 if (t->myuser)
253 pippijn 1.4 phandler->numeric_sts (me.name, 330, u->nick, "%s %s :is logged in as", t->nick, t->myuser->name);
254 pippijn 1.1 }
255     else
256 pippijn 1.4 phandler->numeric_sts (me.name, 401, u->nick, "%s :No such nick", target);
257     phandler->numeric_sts (me.name, 318, u->nick, "%s :End of WHOIS", target);
258 pippijn 1.1 }
259    
260     static void
261     single_trace (user_t *u, user_t *t)
262     {
263     if (is_ircop (t))
264 pippijn 1.4 phandler->numeric_sts (me.name, 204, u->nick, "Oper service %s[%s@%s] (255.255.255.255) 0 0", t->nick, t->user, t->vhost);
265 pippijn 1.1 else
266 pippijn 1.4 phandler->numeric_sts (me.name, 205, u->nick, "User service %s[%s@%s] (255.255.255.255) 0 0", t->nick, t->user, t->vhost);
267 pippijn 1.1 }
268    
269     /* target -> object to trace
270     * dest -> server to execute command on
271     */
272     void
273     handle_trace (user_t *u, char *target, char *dest)
274     {
275     user_t *t;
276     node_t *n;
277     int nusers;
278    
279     if (u == NULL)
280     return;
281     if (floodcheck (u, NULL))
282     return;
283     if (!match (target, me.name) || !irccasecmp (target, ME))
284     {
285     nusers = cnt.user;
286     LIST_FOREACH (n, me.me->userlist.head)
287     {
288     t = static_cast<user_t *> (n->data);
289     single_trace (u, t);
290     nusers--;
291     }
292     if (has_priv_user (u, PRIV_SERVER_AUSPEX))
293 pippijn 1.4 phandler->numeric_sts (me.name, 206, u->nick, "Serv uplink %dS %dC %s *!*@%s 0", cnt.server - 1, nusers, me.actual, me.name);
294 pippijn 1.1 target = me.name;
295     }
296     else
297     {
298     t = dest != NULL ? user_find_named (target) : user_find (target);
299     if (t != NULL && t->server == me.me)
300     {
301     single_trace (u, t);
302     target = t->nick;
303     }
304     }
305 pippijn 1.4 phandler->numeric_sts (me.name, 262, u->nick, "%s :End of TRACE", target);
306 pippijn 1.1 }
307    
308     void
309     handle_motd (user_t *u)
310     {
311     FILE *f;
312     char lbuf[BUFSIZE];
313     char nebuf[BUFSIZE];
314     char cebuf[BUFSIZE];
315     char ubuf[BUFSIZE];
316     char cbuf[BUFSIZE];
317     char nbuf[BUFSIZE];
318    
319     if (u == NULL)
320     return;
321     if (floodcheck (u, NULL))
322     return;
323    
324     f = fopen (SYSCONFDIR "/" PACKAGE_NAME ".motd", "r");
325     if (!f)
326     {
327 pippijn 1.4 phandler->numeric_sts (me.name, 422, u->nick, ":The MOTD file is unavailable.");
328 pippijn 1.1 return;
329     }
330    
331 pippijn 1.5 snprintf (nebuf, BUFSIZE, "%" PRItime, nicksvs.expiry / 86400);
332     snprintf (cebuf, BUFSIZE, "%" PRItime, chansvs.expiry / 86400);
333 pippijn 1.1 snprintf (ubuf, BUFSIZE, "%d", cnt.myuser);
334     snprintf (nbuf, BUFSIZE, "%d", nicksvs.no_nick_ownership ? 0 : cnt.mynick);
335     snprintf (cbuf, BUFSIZE, "%d", cnt.mychan);
336    
337 pippijn 1.4 phandler->numeric_sts (me.name, 375, u->nick, ":- %s Message of the Day -", me.name);
338 pippijn 1.1
339     while (fgets (lbuf, BUFSIZE, f))
340     {
341     strip (lbuf);
342    
343     replace (lbuf, BUFSIZE, "&network&", me.netname);
344     replace (lbuf, BUFSIZE, "&nickexpiry&", nebuf);
345     replace (lbuf, BUFSIZE, "&chanexpiry&", cebuf);
346     replace (lbuf, BUFSIZE, "&myusers&", ubuf);
347     replace (lbuf, BUFSIZE, "&mynicks&", nbuf);
348     replace (lbuf, BUFSIZE, "&mychans&", cbuf);
349    
350 pippijn 1.4 phandler->numeric_sts (me.name, 372, u->nick, ":- %s", lbuf);
351 pippijn 1.1 }
352    
353 pippijn 1.4 phandler->numeric_sts (me.name, 376, u->nick, ":End of the message of the day.");
354 pippijn 1.1
355     fclose (f);
356     }
357    
358     void
359 pippijn 1.4 handle_away (user_t *u, char const * const message)
360 pippijn 1.1 {
361     if (message == NULL || *message == '\0')
362     {
363     if (u->flags & UF_AWAY)
364     {
365     u->flags &= ~UF_AWAY;
366 pippijn 1.4 u->callback.away (u);
367 pippijn 1.1 }
368     }
369     else
370     {
371     if (!(u->flags & UF_AWAY))
372     {
373     u->flags |= UF_AWAY;
374 pippijn 1.4 u->callback.away (u);
375 pippijn 1.1 }
376     }
377     }
378    
379     void
380 pippijn 1.7 handle_channel_message (sourceinfo_t *si, char *target, bool is_notice, char *message)
381     {
382     char *vec[3];
383     node_t *n;
384    
385     /* Call hook here */
386     channel_t *c = channel_find (target);
387     /* No such channel, ignore... */
388     if (c == NULL)
389     return;
390     c->callback.message (c, si->su, message);
391    
392     vec[0] = target;
393     vec[1] = message;
394     vec[2] = NULL;
395    
396     LIST_FOREACH (n, c->members.head)
397     {
398     chanuser_t *cu = static_cast<chanuser_t *> (n->data);
399    
400     if (!is_internal_client (cu->user))
401     continue;
402    
403     si->service = find_service (cu->user->nick);
404    
405     if (si->service == NULL)
406     continue;
407 pippijn 1.8
408     if (si->service->chanmsg == false)
409     continue;
410 pippijn 1.7
411     if (is_notice)
412     si->service->notice_handler (si, 2, vec);
413     else
414     si->service->handler (si, 2, vec);
415     }
416     }
417    
418     void
419 pippijn 1.1 handle_message (sourceinfo_t *si, char *target, bool is_notice, char *message)
420     {
421     char *vec[3];
422    
423     /* message from server, ignore */
424     if (si->su == NULL)
425     return;
426    
427 pippijn 1.7 /* if this is a channel, handle it differently. */
428 pippijn 1.1 if (*target == '#')
429     {
430 pippijn 1.7 handle_channel_message (si, target, is_notice, message);
431     return;
432 pippijn 1.1 }
433 pippijn 1.7
434     si->service = find_service (target);
435 pippijn 1.1
436     if (si->service == NULL)
437     {
438     if (!is_notice && (isalnum (target[0]) || strchr ("[\\]^_`{|}~", target[0])))
439     {
440     /* If it's not a notice and looks like a nick or
441     * user@server, send back an error message */
442     if (strchr (target, '@') || !ircd->uses_uid || (!ircd->uses_p10 && !isdigit (target[0])))
443 pippijn 1.4 phandler->numeric_sts (me.name, 401, si->su->nick, "%s :No such nick", target);
444 pippijn 1.1 else
445 pippijn 1.4 phandler->numeric_sts (me.name, 401, si->su->nick, "* :Target left IRC. Failed to deliver: [%.20s]", message);
446 pippijn 1.1 }
447     return;
448     }
449    
450     /* Run it through flood checks. Channel commands are checked
451     * separately.
452     */
453 pippijn 1.7 if (si->service->me != NULL && floodcheck (si->su, si->service->me))
454 pippijn 1.1 return;
455    
456 pippijn 1.7 if (!is_notice && config_options.secure && irccasecmp (target, si->service->disp))
457 pippijn 1.1 {
458 pippijn 1.4 notice (si->service->me->nick, si->su->nick, "For security reasons, \2/msg %s\2 has been disabled."
459     " Use \2/%s%s <command>\2 to send a command.",
460     si->service->me->nick,
461 pippijn 1.7 ircd->uses_rcommand ? "" : "msg ",
462 pippijn 1.4 si->service->disp);
463 pippijn 1.1 return;
464     }
465    
466     vec[0] = target;
467     vec[1] = message;
468     vec[2] = NULL;
469     if (is_notice)
470     si->service->notice_handler (si, 2, vec);
471     else
472     si->service->handler (si, 2, vec);
473     }
474    
475     void
476 pippijn 1.4 handle_topic_from (sourceinfo_t *si, channel_t *c, char const * const setter, time_t ts, char const *topic)
477 pippijn 1.1 {
478 pippijn 1.4 bool approved = false;
479 pippijn 1.1 if (topic != NULL && topic[0] == '\0')
480     topic = NULL;
481 pippijn 1.4
482 pippijn 1.1 if (topic != NULL ? c->topic == NULL || strcmp (topic, c->topic) : c->topic != NULL)
483 pippijn 1.4 /* Only call the hook if the topic actually changed */
484     approved = c->callback.can_change_topic (c, si->su, si->s, setter, ts, topic);
485    
486     if (approved)
487 pippijn 1.1 {
488 pippijn 1.4 /* Allowed, process the change further */
489     handle_topic (c, setter, ts, topic);
490 pippijn 1.1 }
491     else
492     {
493     /* Not allowed, change it back */
494     if (c->topic != NULL)
495 pippijn 1.4 phandler->topic_sts (c, c->topic_setter, c->topicts, ts, c->topic);
496 pippijn 1.1 else
497 pippijn 1.4 /* Ick, there was no topic */
498     phandler->topic_sts (c, chansvs.nick != NULL ? chansvs.nick : me.name, ts - 1, ts, "");
499 pippijn 1.1 }
500     }
501    
502     void
503 pippijn 1.4 handle_topic (channel_t *c, char const * const setter, time_t ts, char const * const topic)
504 pippijn 1.1 {
505     char newsetter[HOSTLEN], *p;
506    
507     /* setter can be a nick, nick!user@host or server.
508     * strip off !user@host part if it's there
509     * (do we really want this?) */
510     strlcpy (newsetter, setter, sizeof newsetter);
511     p = strchr (newsetter, '!');
512     if (p != NULL)
513     *p = '\0';
514     /* drop identical topics from servers or chanserv, and ones with
515     * identical topicts */
516     if (topic != NULL && c->topic != NULL && !strcmp (topic, c->topic) && (strchr (newsetter, '.') || (chansvs.nick && !irccasecmp (newsetter, chansvs.nick)) || ts == c->topicts))
517     return;
518     if (c->topic != NULL)
519 pippijn 1.4 sfree (c->topic);
520 pippijn 1.1 if (c->topic_setter != NULL)
521 pippijn 1.4 sfree (c->topic_setter);
522 pippijn 1.1 if (topic != NULL && topic[0] != '\0')
523     {
524     c->topic = sstrdup (topic);
525     c->topic_setter = sstrdup (newsetter);
526     }
527     else
528     c->topic = c->topic_setter = NULL;
529     c->topicts = ts;
530    
531 pippijn 1.4 c->callback.topic (c);
532 pippijn 1.1 }
533    
534     void
535     handle_kill (sourceinfo_t *si, char *victim, char const *reason)
536     {
537 pippijn 1.4 char const * const source = get_oper_name (si);
538 pippijn 1.1 user_t *u;
539     static time_t lastkill = 0;
540     static unsigned int killcount = 0;
541    
542     u = user_find (victim);
543     if (u == NULL)
544     slog (LG_DEBUG, "handle_kill(): %s killed unknown user %s (%s)", source, victim, reason);
545     else if (u->server == me.me)
546     {
547     slog (LG_INFO, "handle_kill(): %s killed service %s (%s)", source, u->nick, reason);
548     if (lastkill != NOW && killcount < 5 + me.me->users)
549     killcount = 0, lastkill = NOW;
550     killcount++;
551     if (killcount < 5 + me.me->users)
552     reintroduce_user (u);
553     else
554     {
555     slog (LG_ERROR, "handle_kill(): services kill fight (%s -> %s), shutting down", source, u->nick);
556     wallops (_("Services kill fight (%s -> %s), shutting down!"), source, u->nick);
557     snoop (_("ERROR: Services kill fight (%s -> %s), shutting down!"), source, u->nick);
558     runflags |= RF_SHUTDOWN;
559     }
560     }
561     else
562     {
563     slog (LG_DEBUG, "handle_kill(): %s killed user %s (%s)", source, u->nick, reason);
564     user_delete (u);
565     }
566     }
567    
568     server_t *
569 pippijn 1.4 handle_server (sourceinfo_t *si, char const * const name, char const * const sid, int hops, char const * const desc)
570 pippijn 1.1 {
571     server_t *s = NULL;
572    
573     if (si->s != NULL)
574     {
575     /* A server introducing another server */
576     s = server_add (name, hops, si->s->name, sid, desc);
577     }
578     else if (cnt.server == 1)
579     {
580     /* Our uplink introducing itself */
581     if (irccasecmp (name, curr_uplink->name))
582     slog (LG_ERROR, "handle_server(): uplink %s actually has name %s, continuing anyway", curr_uplink->name, name);
583     s = server_add (name, hops, me.name, sid, desc);
584     me.actual = s->name;
585     me.recvsvr = true;
586     }
587     else
588     slog (LG_ERROR, "handle_server(): unregistered/unknown server attempting to introduce another server %s", name);
589     return s;
590     }
591    
592     void
593     handle_eob (server_t *s)
594     {
595     node_t *n;
596     server_t *s2;
597    
598     if (s == NULL)
599     return;
600     if (s->flags & SF_EOB)
601     return;
602     slog (LG_NETWORK, "handle_eob(): end of burst from %s (%d users)", s->name, s->users);
603 pippijn 1.4 s->callback.eob (s);
604 pippijn 1.1 s->flags |= SF_EOB;
605     /* convert P10 style EOB to ircnet/ratbox style */
606     LIST_FOREACH (n, s->children.head)
607     {
608     s2 = static_cast<server_t *> (n->data);
609     if (s2->flags & SF_EOB2)
610     handle_eob (s2);
611     }
612     /* Reseed RNG now we have a little more data to seed with */
613     if (s->uplink == me.me)
614 pippijn 1.4 init_gen_rand (gen_rand32 () ^ ((NOW << 20) + cnt.user + (cnt.chanuser << 12)) ^ (cnt.chan << 17) ^ ~cnt.bin);
615 pippijn 1.1 }
616    
617     /* Received a message from a user, check if they are flooding
618     * Returns true if the message should be ignored.
619     * u - user sending the message
620     * t - target of the message (to be used in warning the user, may be NULL
621     * to use the server name)
622     */
623     int
624     floodcheck (user_t *u, user_t *t)
625     {
626     char *from;
627 pippijn 1.4 static time_t last_ignore_notice = 0;
628 pippijn 1.1
629     if (t == NULL)
630     from = me.name;
631     else if (t->server == me.me)
632     from = t->nick;
633     else
634     {
635     slog (LG_ERROR, "BUG: tried to floodcheck message to non-service %s", t->nick);
636     return 0;
637     }
638    
639     /* Check if we match a services ignore */
640 pippijn 1.4 if (svsignore_find(u))
641     {
642     if (u->msgs == 0 && last_ignore_notice != NOW)
643     {
644     /* tell them once per session, don't flood */
645     u->msgs = 1;
646     last_ignore_notice = NOW;
647     notice (from, u->nick, _("You are on services ignore. You may not use any service."));
648     }
649     return 1;
650     }
651    
652 pippijn 1.1 if (config_options.flood_msgs)
653     {
654     /* check if they're being ignored */
655     if (u->offenses > 10)
656     {
657     if ((NOW - u->lastmsg) > 30)
658     {
659     u->offenses -= 10;
660     u->lastmsg = NOW;
661     u->msgs = 0;
662     }
663     else
664     return 1;
665     }
666    
667     if ((NOW - u->lastmsg) > config_options.flood_time)
668     {
669     u->lastmsg = NOW;
670     u->msgs = 0;
671     }
672    
673     u->msgs++;
674    
675     if (u->msgs > config_options.flood_msgs)
676     {
677     /* they're flooding. */
678     /* perhaps allowed to? -- jilles */
679     if (has_priv_user (u, PRIV_FLOOD))
680     {
681     u->msgs = 0;
682     return 0;
683     }
684     if (!u->offenses)
685     {
686     /* ignore them the first time */
687     u->lastmsg = NOW;
688     u->msgs = 0;
689     u->offenses = 11;
690    
691     /* ok to use nick here, notice() will
692     * change it to UID if necessary -- jilles */
693     notice (from, u->nick, "You have triggered services flood protection.");
694     notice (from, u->nick, "This is your first offense. You will be ignored for 30 seconds.");
695    
696     snoop ("FLOOD: \2%s\2", u->nick);
697    
698     return 1;
699     }
700    
701     if (u->offenses == 1)
702     {
703     /* ignore them the second time */
704     u->lastmsg = NOW;
705     u->msgs = 0;
706     u->offenses = 12;
707    
708     notice (from, u->nick, "You have triggered services flood protection.");
709     notice (from, u->nick, "This is your last warning. You will be ignored for 30 seconds.");
710    
711     snoop ("FLOOD: \2%s\2", u->nick);
712    
713     return 1;
714     }
715    
716     if (u->offenses == 2)
717     {
718     kline_t *k;
719    
720     /* kline them the third time */
721 pippijn 1.9 k = kline_t::create ("*", u->host, "ten minute ban: flooding services", 600);
722 pippijn 1.1 k->setby = sstrdup (chansvs.nick);
723    
724     snoop ("FLOOD:KLINE: \2%s\2", u->nick);
725    
726     return 1;
727     }
728     }
729     }
730    
731     return 0;
732     }
733    
734     bool
735     should_reg_umode (user_t *u)
736     {
737     mynick_t *mn;
738    
739     if (nicksvs.me == NULL || nicksvs.no_nick_ownership || u->myuser == NULL || u->myuser->flags & MU_WAITAUTH)
740     return false;
741 pippijn 1.9 mn = mynick_t::find (u->nick);
742 pippijn 1.1 return mn != NULL && mn->owner == u->myuser;
743     }