ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/ermyth/src/ptasks.C
Revision: 1.10
Committed: Sun Sep 16 18:54:45 2007 UTC (16 years, 9 months ago) by pippijn
Content type: text/plain
Branch: MAIN
Changes since 1.9: +7 -2 lines
Log Message:
#defines to enum

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