introduce unreg_privmsg_func, fix minor autoinvite issue, alter account-finding on...
[srvx.git] / src / proto-p10.c
1 /* proto-p10.c - IRC protocol output
2  * Copyright 2000-2004 srvx Development Team
3  *
4  * This file is part of srvx.
5  *
6  * srvx is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with srvx; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
19  */
20
21 #include "proto-common.c"
22
23 /* Full commands. */
24 #define CMD_ACCOUNT             "ACCOUNT"
25 #define CMD_ADMIN               "ADMIN"
26 #define CMD_ASLL                "ASLL"
27 #define CMD_AWAY                "AWAY"
28 #define CMD_BURST               "BURST"
29 #define CMD_CLEARMODE           "CLEARMODE"
30 #define CMD_CLOSE               "CLOSE"
31 #define CMD_CNOTICE             "CNOTICE"
32 #define CMD_CONNECT             "CONNECT"
33 #define CMD_CPRIVMSG            "CPRIVMSG"
34 #define CMD_CREATE              "CREATE"
35 #define CMD_DESTRUCT            "DESTRUCT"
36 #define CMD_DESYNCH             "DESYNCH"
37 #define CMD_DIE                 "DIE"
38 #define CMD_DNS                 "DNS"
39 #define CMD_EOB                 "END_OF_BURST"
40 #define CMD_EOB_ACK             "EOB_ACK"
41 #define CMD_ERROR               "ERROR"
42 #define CMD_GET                 "GET"
43 #define CMD_GLINE               "GLINE"
44 #define CMD_HASH                "HASH"
45 #define CMD_HELP                "HELP"
46 #define CMD_INFO                "INFO"
47 #define CMD_INVITE              "INVITE"
48 #define CMD_ISON                "ISON"
49 #define CMD_JOIN                "JOIN"
50 #define CMD_JUPE                "JUPE"
51 #define CMD_KICK                "KICK"
52 #define CMD_KILL                "KILL"
53 #define CMD_LINKS               "LINKS"
54 #define CMD_LIST                "LIST"
55 #define CMD_LUSERS              "LUSERS"
56 #define CMD_MAP                 "MAP"
57 #define CMD_MODE                "MODE"
58 #define CMD_MOTD                "MOTD"
59 #define CMD_NAMES               "NAMES"
60 #define CMD_NICK                "NICK"
61 #define CMD_NOTICE              "NOTICE"
62 #define CMD_OPER                "OPER"
63 #define CMD_OPMODE              "OPMODE"
64 #define CMD_PART                "PART"
65 #define CMD_PASS                "PASS"
66 #define CMD_PING                "PING"
67 #define CMD_PONG                "PONG"
68 #define CMD_POST                "POST"
69 #define CMD_PRIVMSG             "PRIVMSG"
70 #define CMD_PRIVS               "PRIVS"
71 #define CMD_PROTO               "PROTO"
72 #define CMD_QUIT                "QUIT"
73 #define CMD_REHASH              "REHASH"
74 #define CMD_RESET               "RESET"
75 #define CMD_RESTART             "RESTART"
76 #define CMD_RPING               "RPING"
77 #define CMD_RPONG               "RPONG"
78 #define CMD_SERVER              "SERVER"
79 #define CMD_SERVLIST            "SERVLIST"
80 #define CMD_SERVSET             "SERVSET"
81 #define CMD_SET                 "SET"
82 #define CMD_SETTIME             "SETTIME"
83 #define CMD_SILENCE             "SILENCE"
84 #define CMD_SQUERY              "SQUERY"
85 #define CMD_SQUIT               "SQUIT"
86 #define CMD_STATS               "STATS"
87 #define CMD_SVSNICK             "SVSNICK"
88 #define CMD_TIME                "TIME"
89 #define CMD_TOPIC               "TOPIC"
90 #define CMD_TRACE               "TRACE"
91 #define CMD_UPING               "UPING"
92 #define CMD_USER                "USER"
93 #define CMD_USERHOST            "USERHOST"
94 #define CMD_USERIP              "USERIP"
95 #define CMD_VERSION             "VERSION"
96 #define CMD_WALLCHOPS           "WALLCHOPS"
97 #define CMD_WALLOPS             "WALLOPS"
98 #define CMD_WALLUSERS           "WALLUSERS"
99 #define CMD_WALLVOICES          "WALLVOICES"
100 #define CMD_WHO                 "WHO"
101 #define CMD_WHOIS               "WHOIS"
102 #define CMD_WHOWAS              "WHOWAS"
103
104 /* Tokenized commands. */
105 #define TOK_ACCOUNT             "AC"
106 #define TOK_ADMIN               "AD"
107 #define TOK_ASLL                "LL"
108 #define TOK_AWAY                "A"
109 #define TOK_BURST               "B"
110 #define TOK_CLEARMODE           "CM"
111 #define TOK_CLOSE               "CLOSE"
112 #define TOK_CNOTICE             "CN"
113 #define TOK_CONNECT             "CO"
114 #define TOK_CPRIVMSG            "CP"
115 #define TOK_CREATE              "C"
116 #define TOK_DESTRUCT            "DE"
117 #define TOK_DESYNCH             "DS"
118 #define TOK_DIE                 "DIE"
119 #define TOK_DNS                 "DNS"
120 #define TOK_EOB                 "EB"
121 #define TOK_EOB_ACK             "EA"
122 #define TOK_ERROR               "Y"
123 #define TOK_GET                 "GET"
124 #define TOK_GLINE               "GL"
125 #define TOK_HASH                "HASH"
126 #define TOK_HELP                "HELP"
127 #define TOK_INFO                "F"
128 #define TOK_INVITE              "I"
129 #define TOK_ISON                "ISON"
130 #define TOK_JOIN                "J"
131 #define TOK_JUPE                "JU"
132 #define TOK_KICK                "K"
133 #define TOK_KILL                "D"
134 #define TOK_LINKS               "LI"
135 #define TOK_LIST                "LIST"
136 #define TOK_LUSERS              "LU"
137 #define TOK_MAP                 "MAP"
138 #define TOK_MODE                "M"
139 #define TOK_MOTD                "MO"
140 #define TOK_NAMES               "E"
141 #define TOK_NICK                "N"
142 #define TOK_NOTICE              "O"
143 #define TOK_OPER                "OPER"
144 #define TOK_OPMODE              "OM"
145 #define TOK_PART                "L"
146 #define TOK_PASS                "PA"
147 #define TOK_PING                "G"
148 #define TOK_PONG                "Z"
149 #define TOK_POST                "POST"
150 #define TOK_PRIVMSG             "P"
151 #define TOK_PRIVS               "PRIVS"
152 #define TOK_PROTO               "PROTO"
153 #define TOK_QUIT                "Q"
154 #define TOK_REHASH              "REHASH"
155 #define TOK_RESET               "RESET"
156 #define TOK_RESTART             "RESTART"
157 #define TOK_RPING               "RI"
158 #define TOK_RPONG               "RO"
159 #define TOK_SERVER              "S"
160 #define TOK_SERVLIST            "SERVSET"
161 #define TOK_SERVSET             "SERVSET"
162 #define TOK_SET                 "SET"
163 #define TOK_SETTIME             "SE"
164 #define TOK_SILENCE             "U"
165 #define TOK_SQUERY              "SQUERY"
166 #define TOK_SQUIT               "SQ"
167 #define TOK_STATS               "R"
168 #define TOK_SVSNICK             "SN"
169 #define TOK_TIME                "TI"
170 #define TOK_TOPIC               "T"
171 #define TOK_TRACE               "TR"
172 #define TOK_UPING               "UP"
173 #define TOK_USER                "USER"
174 #define TOK_USERHOST            "USERHOST"
175 #define TOK_USERIP              "USERIP"
176 #define TOK_VERSION             "V"
177 #define TOK_WALLCHOPS           "WC"
178 #define TOK_WALLOPS             "WA"
179 #define TOK_WALLUSERS           "WU"
180 #define TOK_WALLVOICES          "WV"
181 #define TOK_WHO                 "H"
182 #define TOK_WHOIS               "W"
183 #define TOK_WHOWAS              "X"
184
185 /* Protocol messages; aliased to full commands or tokens depending
186    on compile-time configuration. ircu prefers tokens WITH THE
187    EXCEPTION OF THE SERVER AND PASS COMMANDS, which cannot be
188    tokenized, because clients' (ie. a linking server) commands are
189    only checked against the full command list.
190 */
191 #if defined(ENABLE_TOKENS)
192 #define TYPE(NAME)              TOK_ ## NAME
193 #else /* !ENABLE_TOKENS */
194 #define TYPE(NAME)              CMD_ ## NAME
195 #endif /* ENABLE_TOKENS */
196
197 #define P10_ACCOUNT             TYPE(ACCOUNT)
198 #define P10_ADMIN               TYPE(ADMIN)
199 #define P10_ASLL                TYPE(ASLL)
200 #define P10_AWAY                TYPE(AWAY)
201 #define P10_BURST               TYPE(BURST)
202 #define P10_CLEARMODE           TYPE(CLEARMODE)
203 #define P10_CLOSE               TYPE(CLOSE)
204 #define P10_CNOTICE             TYPE(CNOTICE)
205 #define P10_CONNECT             TYPE(CONNECT)
206 #define P10_CPRIVMSG            TYPE(CPRIVMSG)
207 #define P10_CREATE              TYPE(CREATE)
208 #define P10_DESTRUCT            TYPE(DESTRUCT)
209 #define P10_DESYNCH             TYPE(DESYNCH)
210 #define P10_DIE                 TYPE(DIE)
211 #define P10_DNS                 TYPE(DNS)
212 #define P10_EOB                 TYPE(EOB)
213 #define P10_EOB_ACK             TYPE(EOB_ACK)
214 #define P10_ERROR               TYPE(ERROR)
215 #define P10_GET                 TYPE(GET)
216 #define P10_GLINE               TYPE(GLINE)
217 #define P10_HASH                TYPE(HASH)
218 #define P10_HELP                TYPE(HELP)
219 #define P10_INFO                TYPE(INFO)
220 #define P10_INVITE              TYPE(INVITE)
221 #define P10_ISON                TYPE(ISON)
222 #define P10_JOIN                TYPE(JOIN)
223 #define P10_JUPE                TYPE(JUPE)
224 #define P10_KICK                TYPE(KICK)
225 #define P10_KILL                TYPE(KILL)
226 #define P10_LINKS               TYPE(LINKS)
227 #define P10_LIST                TYPE(LIST)
228 #define P10_LUSERS              TYPE(LUSERS)
229 #define P10_MAP                 TYPE(MAP)
230 #define P10_MODE                TYPE(MODE)
231 #define P10_MOTD                TYPE(MOTD)
232 #define P10_NAMES               TYPE(NAMES)
233 #define P10_NICK                TYPE(NICK)
234 #define P10_NOTICE              TYPE(NOTICE)
235 #define P10_OPER                TYPE(OPER)
236 #define P10_OPMODE              TYPE(OPMODE)
237 #define P10_PART                TYPE(PART)
238 #define P10_PASS                CMD_PASS
239 #define P10_PING                TYPE(PING)
240 #define P10_PONG                TYPE(PONG)
241 #define P10_POST                TYPE(POST)
242 #define P10_PRIVMSG             TYPE(PRIVMSG)
243 #define P10_PRIVS               TYPE(PRIVS)
244 #define P10_PROTO               TYPE(PROTO)
245 #define P10_QUIT                TYPE(QUIT)
246 #define P10_REHASH              TYPE(REHASH)
247 #define P10_RESET               TYPE(RESET)
248 #define P10_RESTART             TYPE(RESTART)
249 #define P10_RPING               TYPE(RPING)
250 #define P10_RPONG               TYPE(RPONG)
251 #define P10_SERVER              CMD_SERVER
252 #define P10_SERVLIST            TYPE(SERVLIST)
253 #define P10_SERVSET             TYPE(SERVSET)
254 #define P10_SET                 TYPE(SET)
255 #define P10_SETTIME             TYPE(SETTIME)
256 #define P10_SILENCE             TYPE(SILENCE)
257 #define P10_SQUERY              TYPE(SQUERY)
258 #define P10_SQUIT               TYPE(SQUIT)
259 #define P10_STATS               TYPE(STATS)
260 #define P10_SVSNICK             TYPE(SVSNICK)
261 #define P10_TIME                TYPE(TIME)
262 #define P10_TOPIC               TYPE(TOPIC)
263 #define P10_TRACE               TYPE(TRACE)
264 #define P10_UPING               TYPE(UPING)
265 #define P10_USER                TYPE(USER)
266 #define P10_USERHOST            TYPE(USERHOST)
267 #define P10_USERIP              TYPE(USERIP)
268 #define P10_VERSION             TYPE(VERSION)
269 #define P10_WALLCHOPS           TYPE(WALLCHOPS)
270 #define P10_WALLOPS             TYPE(WALLOPS)
271 #define P10_WALLUSERS           TYPE(WALLUSERS)
272 #define P10_WALLVOICES          TYPE(WALLVOICES)
273 #define P10_WHO                 TYPE(WHO)
274 #define P10_WHOIS               TYPE(WHOIS)
275 #define P10_WHOWAS              TYPE(WHOWAS)
276
277 /* Servers claiming to have a boot or link time before PREHISTORY
278  * trigger errors to the log.  We hope no server has been running
279  * constantly since September 1994.  :)
280  */
281 #define PREHISTORY 780000000
282
283 static struct server *servers_num[64*64];
284 static privmsg_func_t *privmsg_funcs;
285 static unsigned int num_privmsg_funcs;
286 static privmsg_func_t *notice_funcs;
287 static unsigned int num_notice_funcs;
288 static struct dict *unbursted_channels;
289
290 char *his_servername;
291 char *his_servercomment;
292
293 static struct userNode *AddUser(struct server* uplink, const char *nick, const char *ident, const char *hostname, const char *modes, const char *numeric, const char *userinfo, time_t timestamp, const char *realip);
294
295 /* Numerics can be XYY, XYYY, or XXYYY; with X's identifying the
296  * server and Y's indentifying the client on that server. */
297 struct server*
298 GetServerN(const char *numeric)
299 {
300     switch (strlen(numeric)) {
301     default:
302         return servers_num[base64toint(numeric, 2)];
303     case 4:
304     case 3:
305     case 1:
306         return servers_num[base64toint(numeric, 1)];
307     case 0:
308         return NULL;
309     }
310 }
311
312 struct userNode*
313 GetUserN(const char *numeric) /* using numeric */
314 {
315     struct userNode *un;
316     struct server *s;
317     int n, slen, ulen;
318
319     switch (strlen(numeric)) {
320     default:
321         log_module(MAIN_LOG, LOG_WARNING, "GetUserN(%s): numeric too long!", numeric);
322         return NULL;
323     case 5: slen = 2; ulen = 3; break;
324     case 4: slen = 1; ulen = 3; break;
325     case 3: slen = 1; ulen = 2; break;
326     case 2: case 1: case 0:
327         log_module(MAIN_LOG, LOG_WARNING, "GetUserN(%s): numeric too short!", numeric);
328         return NULL;
329     }
330     if (!(s = servers_num[base64toint(numeric, slen)])) {
331         log_module(MAIN_LOG, LOG_WARNING, "GetUserN(%s): couldn't find server (len=%d)!", numeric, slen);
332         return NULL;
333     }
334     n = base64toint(numeric+slen, ulen) & s->num_mask;
335     if (!(un = s->users[n])) {
336         log_module(MAIN_LOG, LOG_WARNING, "GetUserN(%s) couldn't find user!", numeric);
337     }
338     return un;
339 }
340
341 static void
342 privmsg_user_helper(struct userNode *un, void *data)
343 {
344     struct privmsg_desc *pd = data;
345     unsigned int num = un->num_local;
346     if (!pd->is_notice) {
347         if ((num < num_privmsg_funcs) && privmsg_funcs[num]) {
348             privmsg_funcs[num](pd->user, un, pd->text, pd->is_qualified);
349         }
350     } else {
351         if ((num < num_notice_funcs) && notice_funcs[num]) {
352             notice_funcs[num](pd->user, un, pd->text, pd->is_qualified);
353         }
354     }
355 }
356
357 void
358 irc_server(struct server *srv)
359 {
360     char extranum[COMBO_NUMERIC_LEN+1];
361
362     inttobase64(extranum, srv->num_mask, (srv->numeric[1] || (srv->num_mask >= 64*64)) ? 3 : 2);
363     if (srv == self) {
364         /* The +s, ignored by Run's ircu, means "service" to Undernet's ircu */
365         putsock(P10_SERVER " %s %d %li %li J10 %s%s +s :%s",
366                 srv->name, srv->hops+1, srv->boot, srv->link, srv->numeric, extranum, srv->description);
367     } else {
368         putsock("%s " P10_SERVER " %s %d %li %li %c10 %s%s +s :%s",
369                 self->numeric, srv->name, srv->hops+1, srv->boot, srv->link, (srv->self_burst ? 'J' : 'P'), srv->numeric, extranum, srv->description);
370     }
371 }
372
373 void
374 irc_user(struct userNode *user)
375 {
376     char b64ip[7];
377     if (!user)
378         return;
379     inttobase64(b64ip, ntohl(user->ip.s_addr), 6);
380     if (user->modes) {
381         int modelen;
382         char modes[32];
383
384         modelen = 0;
385         if (IsOper(user))
386             modes[modelen++] = 'o';
387         if (IsInvisible(user))
388             modes[modelen++] = 'i';
389         if (IsWallOp(user))
390             modes[modelen++] = 'w';
391         if (IsService(user))
392             modes[modelen++] = 'k';
393         if (IsServNotice(user))
394             modes[modelen++] = 's';
395         if (IsDeaf(user))
396             modes[modelen++] = 'd';
397         if (IsGlobal(user))
398             modes[modelen++] = 'g';
399         if (IsHelperIrcu(user))
400             modes[modelen++] = 'h';
401         if (IsHiddenHost(user))
402             modes[modelen++] = 'x';
403         modes[modelen] = 0;
404
405         /* we don't need to put the + in modes because it's in the format string. */
406         putsock("%s " P10_NICK " %s %d %li %s %s +%s %s %s :%s",
407                 user->uplink->numeric, user->nick, user->uplink->hops+1, user->timestamp, user->ident, user->hostname, modes, b64ip, user->numeric, user->info);
408     } else {
409         putsock("%s " P10_NICK " %s %d %li %s %s %s %s :%s",
410                 user->uplink->numeric, user->nick, user->uplink->hops+1, user->timestamp, user->ident, user->hostname, b64ip, user->numeric, user->info);
411     }
412 }
413
414 void
415 irc_account(struct userNode *user, const char *stamp)
416 {
417     putsock("%s " P10_ACCOUNT " %s %s", self->numeric, user->numeric, stamp);
418 }
419
420 void
421 irc_regnick(UNUSED_ARG(struct userNode *user))
422 {
423     /* no operation here */
424 }
425
426 void
427 irc_nick(struct userNode *user, UNUSED_ARG(const char *old_nick))
428 {
429     putsock("%s " P10_NICK " %s "FMT_TIME_T, user->numeric, user->nick, now);
430 }
431
432 void
433 irc_fetchtopic(struct userNode *from, const char *to)
434 {
435     if (!from || !to)
436         return;
437     putsock("%s " P10_TOPIC " %s", from->numeric, to);
438 }
439
440 void
441 irc_squit(struct server *srv, const char *message, const char *service_message)
442 {
443     if (!service_message)
444         service_message = message;
445
446     /* Are we leaving the network? */
447     if (srv == self && cManager.uplink->state == CONNECTED) {
448         unsigned int i;
449
450         /* Quit all clients linked to me. */
451         for (i = 0; i <= self->num_mask; i++) {
452             if (!self->users[i])
453                 continue;
454             irc_quit(self->users[i], service_message);
455         }
456     }
457
458     putsock("%s " P10_SQUIT " %s %d :%s", self->numeric, srv->name, 0, message);
459
460     if (srv == self) {
461         /* Force a reconnect to the currently selected server. */
462         cManager.uplink->tries = 0;
463         log_module(MAIN_LOG, LOG_INFO, "Squitting from uplink: %s", message);
464         close_socket();
465     }
466 }
467
468 void
469 irc_wallchops(struct userNode *from, const char *to, const char *message)
470 {
471     putsock("%s " P10_WALLCHOPS " %s :%s", from->numeric, to, message);
472 }
473
474 void
475 irc_notice(struct userNode *from, const char *to, const char *message)
476 {
477     putsock("%s " P10_NOTICE " %s :%s", from->numeric, to, message);
478 }
479
480 void
481 irc_notice_user(struct userNode *from, struct userNode *to, const char *message)
482 {
483     putsock("%s " P10_NOTICE " %s :%s", from->numeric, to->numeric, message);
484 }
485
486 void
487 irc_privmsg(struct userNode *from, const char *to, const char *message)
488 {
489     putsock("%s " P10_PRIVMSG " %s :%s", from->numeric, to, message);
490 }
491
492 void
493 irc_eob(void)
494 {
495     putsock("%s " P10_EOB, self->numeric);
496 }
497
498 void
499 irc_eob_ack(void)
500 {
501     putsock("%s " P10_EOB_ACK, self->numeric);
502 }
503
504 void
505 irc_ping(const char *payload)
506 {
507     putsock("%s " P10_PING " :%s", self->numeric, payload);
508 }
509
510 void
511 irc_pong(const char *who, const char *data)
512 {
513     putsock("%s " P10_PONG " %s :%s", self->numeric, who, data);
514 }
515
516 void
517 irc_pass(const char *passwd)
518 {
519     putsock(P10_PASS " :%s", passwd);
520 }
521
522 void
523 irc_introduce(const char *passwd)
524 {
525     void timed_send_ping(void *data);
526
527     self->self_burst = self->burst = 1;
528     irc_pass(passwd);
529     irc_server(self);
530     burst_length = 0;
531     timeq_add(now + ping_freq, timed_send_ping, 0);
532 }
533
534 void
535 irc_gline(struct server *srv, struct gline *gline)
536 {
537     putsock("%s " P10_GLINE " %s +%s %ld :%s",
538             self->numeric, (srv ? srv->numeric : "*"), gline->target, gline->expires-now, gline->reason);
539 }
540
541 void
542 irc_settime(const char *srv_name_mask, time_t new_time)
543 {
544     ioset_set_time(new_time);
545     if (!strcmp(srv_name_mask, "*"))
546         srv_name_mask = "";
547     putsock("%s " P10_SETTIME " " FMT_TIME_T " %s", self->numeric, new_time, srv_name_mask);
548 }
549
550 void
551 irc_ungline(const char *mask)
552 {
553     putsock("%s " P10_GLINE " * -%s", self->numeric, mask);
554 }
555
556 static void
557 irc_burst(struct chanNode *chan)
558 {
559     char burst_line[512];
560     int pos, base_len, len;
561     struct modeNode *mn;
562     struct banNode *bn;
563     long last_mode=-1;
564     unsigned int n;
565
566     if (!chan->members.used)
567         return;
568     base_len = sprintf(burst_line, "%s " P10_BURST " %s " FMT_TIME_T " ",
569                        self->numeric, chan->name, chan->timestamp);
570     len = irc_make_chanmode(chan, burst_line+base_len);
571     pos = base_len + len;
572     if (len)
573         burst_line[pos++] = ' ';
574
575     /* dump the users */
576     for (n=0; n<chan->members.used; n++) {
577         mn = chan->members.list[n];
578         if (pos > 500) {
579             burst_line[pos-1] = 0; /* -1 to back up over the space or comma */
580             putsock("%s", burst_line);
581             pos = base_len;
582             last_mode = -1;
583         }
584         memcpy(burst_line+pos, mn->user->numeric, strlen(mn->user->numeric));
585         pos += strlen(mn->user->numeric);
586         if (mn->modes && (mn->modes != last_mode)) {
587             last_mode = mn->modes;
588             burst_line[pos++] = ':';
589             if (last_mode & MODE_CHANOP)
590                 burst_line[pos++] = 'o';
591             if (last_mode & MODE_VOICE)
592                 burst_line[pos++] = 'v';
593         }
594         if ((n+1)<chan->members.used)
595             burst_line[pos++] = ',';
596     }
597     if (chan->banlist.used) {
598         /* dump the bans */
599         if (pos+2+strlen(chan->banlist.list[0]->ban) > 505) {
600             burst_line[pos-1] = 0;
601             putsock("%s", burst_line);
602             pos = base_len;
603         } else {
604             burst_line[pos++] = ' ';
605         }
606
607         burst_line[pos++] = ':';
608         burst_line[pos++] = '%';
609         base_len = pos;
610         for (n=0; n<chan->banlist.used; n++) {
611             bn = chan->banlist.list[n];
612             len = strlen(bn->ban);
613             if (pos+len+1 > 510) {
614                 burst_line[pos-1] = 0; /* -1 to back up over the space or comma */
615                 putsock("%s", burst_line);
616                 pos = base_len;
617             }
618             memcpy(burst_line+pos, bn->ban, len);
619             pos += len;
620             burst_line[pos++] = ' ';
621         }
622     }
623     /* print the last line */
624     burst_line[pos] = 0;
625     putsock("%s", burst_line);
626 }
627
628 void
629 irc_quit(struct userNode *user, const char *message)
630 {
631     putsock("%s " P10_QUIT " :%s", user->numeric, message);
632 }
633
634 void
635 irc_error(const char *to, const char *message)
636 {
637     if (to) {
638         putsock("%s " P10_ERROR " :%s", to, message);
639     } else {
640         putsock(":%s " P10_ERROR " :%s", self->name, message);
641     }
642 }
643
644 void
645 irc_kill(struct userNode *from, struct userNode *target, const char *message)
646 {
647     if (from) {
648         putsock("%s " P10_KILL " %s :%s!%s (%s)",
649                 from->numeric, target->numeric, self->name, from->nick, message);
650     } else {
651         putsock("%s " P10_KILL " %s :%s (%s)",
652                 self->numeric, target->numeric, self->name, message);
653     }
654 }
655
656 void
657 irc_mode(struct userNode *from, struct chanNode *target, const char *modes)
658 {
659     putsock("%s " P10_MODE " %s %s "FMT_TIME_T,
660             (from ? from->numeric : self->numeric),
661             target->name, modes, target->timestamp);
662 }
663
664 void
665 irc_invite(struct userNode *from, struct userNode *who, struct chanNode *to)
666 {
667     putsock("%s " P10_INVITE " %s %s", from->numeric, who->nick, to->name);
668 }
669
670 void
671 irc_join(struct userNode *who, struct chanNode *what)
672 {
673     if (what->members.used == 1) {
674         putsock("%s " P10_CREATE " %s %lu",
675                 who->numeric, what->name, what->timestamp);
676     } else {
677         putsock("%s " P10_JOIN " %s %lu", who->numeric, what->name, what->timestamp);
678     }
679 }
680
681 void
682 irc_kick(struct userNode *who, struct userNode *target, struct chanNode *channel, const char *msg)
683 {
684     const char *numeric;
685     struct modeNode *mn = GetUserMode(channel, who);
686     numeric = (mn && (mn->modes & MODE_CHANOP)) ? who->numeric : self->numeric;
687     putsock("%s " P10_KICK " %s %s :%s",
688             numeric, channel->name, target->numeric, msg);
689 }
690
691 void
692 irc_stats(struct userNode *from, struct server *target, char type)
693 {
694     putsock("%s " P10_STATS " %c :%s", from->numeric, type, target->numeric);
695 }
696
697 void
698 irc_svsnick(struct userNode *from, struct userNode *target, const char *newnick)
699 {
700     putsock("%s " P10_SVSNICK " %s %s "FMT_TIME_T, from->uplink->numeric, target->numeric, newnick, now);
701 }
702
703 void
704 irc_part(struct userNode *who, struct chanNode *what, const char *reason)
705 {
706     if (reason) {
707         putsock("%s " P10_PART " %s :%s", who->numeric, what->name, reason);
708     } else {
709         putsock("%s " P10_PART " %s", who->numeric, what->name);
710     }
711 }
712
713 void
714 irc_topic(struct userNode *who, struct chanNode *what, const char *topic)
715 {
716     putsock("%s " P10_TOPIC " %s :%s", who->numeric, what->name, topic);
717 }
718
719 void
720 irc_raw(const char *what)
721 {
722     putsock("%s", what);
723 }
724
725 void
726 irc_numeric(struct userNode *user, unsigned int num, const char *format, ...)
727 {
728     va_list arg_list;
729     char buffer[MAXLEN];
730     va_start(arg_list, format);
731     vsnprintf(buffer, MAXLEN-2, format, arg_list);
732     buffer[MAXLEN-1] = 0;
733     putsock(":%s %03d %s %s", self->name, num, user->nick, buffer);
734 }
735
736 static void send_burst(void);
737
738 static void
739 change_nicklen(int new_nicklen)
740 {
741     unsigned int nn;
742     char new_nick[NICKLEN+1];
743     struct userNode *user;
744
745     nicklen = new_nicklen;
746     /* fix up any users we have here */
747     for (nn=0; nn<=self->num_mask; nn++) {
748         if (!(user = self->users[nn]))
749             continue;
750         safestrncpy(new_nick, user->nick, sizeof(new_nick));
751         new_nick[nicklen] = 0;
752         NickChange(user, new_nick, 1);
753     }
754 }
755
756 static CMD_FUNC(cmd_server)
757 {
758     struct server *srv;
759     const char *str;
760
761     if (argc < 8)
762         return 0;
763     if (origin) {
764         /* another server introduced us */
765         srv = AddServer(GetServerH(origin), argv[1], atoi(argv[2]), atoi(argv[3]), atoi(argv[4]), argv[6], argv[argc-1]);
766         if (!srv)
767             return 0;
768         srv->self_burst = argv[5][0] == 'J';
769         srv->burst = 1;
770     } else {
771         /* this must be our uplink */
772         srv = self->uplink = AddServer(self, argv[1], atoi(argv[2]), atoi(argv[3]), atoi(argv[4]), argv[6], argv[argc-1]);
773         if (!srv)
774             return 0;
775         srv->self_burst = argv[5][0] == 'J';
776         srv->burst = 1;
777         if ((argv[7][0] == '+') && !force_n2k) {
778             log_module(MAIN_LOG, LOG_WARNING, "Got Undernet-style SERVER message but \"force_n2k\" not on.");
779         }
780         send_burst();
781     }
782
783     /* Fix up our timestamps if necessary. */
784     if (srv->boot <= PREHISTORY) {
785         /* Server from the mists of time.. */
786         if (srv->hops == 1) {
787             log_module(MAIN_LOG, LOG_ERROR, "Server %s claims to have booted at time "FMT_TIME_T".  This is absurd.", srv->name, srv->boot);
788         }
789     } else if ((str = conf_get_data("server/reliable_clock", RECDB_QSTRING))
790                && enabled_string(str)) {
791         /* If we have a reliable clock, we just keep our current time. */
792     } else {
793         if (srv->boot <= self->boot) {
794             /* The other server is older than us.  Accept their timestamp.
795              * Alternately, we are same age, but we accept their time
796              * since we are linking to them. */
797             self->boot = srv->boot;
798             ioset_set_time(srv->link);
799         }
800     }
801     if (srv == self->uplink) {
802         extern time_t burst_begin;
803         burst_begin = now;
804     }
805     return 1;
806 }
807
808 static CMD_FUNC(cmd_eob)
809 {
810     struct server *sender;
811     dict_iterator_t it;
812     unsigned int ii;
813
814     if (!(sender = GetServerH(origin)))
815         return 0;
816     if (sender == self->uplink) {
817         cManager.uplink->state = CONNECTED;
818         for (it = dict_first(unbursted_channels); it; it = iter_next(it))
819             irc_burst(iter_data(it));
820         dict_delete(unbursted_channels);
821         unbursted_channels = NULL;
822         irc_eob();
823         irc_eob_ack();
824     }
825     sender->self_burst = 0;
826     recalc_bursts(sender);
827     for (ii=0; ii<slf_used; ii++)
828         slf_list[ii](sender);
829     return 1;
830 }
831
832 static CMD_FUNC(cmd_eob_ack)
833 {
834     extern time_t burst_begin;
835
836     if (GetServerH(origin) == self->uplink) {
837         burst_length = now - burst_begin;
838         self->self_burst = self->burst = 0;
839     }
840     cManager.uplink->state = CONNECTED;
841     return 1;
842 }
843
844 static CMD_FUNC(cmd_ping)
845 {
846     struct server *srv;
847     struct userNode *un;
848
849     if(argc > 3)
850         irc_pong(argv[2], argv[1]);
851     else if ((srv = GetServerH(origin)))
852         irc_pong(self->name, srv->numeric);
853     else if ((un = GetUserH(origin)))
854         irc_pong(self->name, un->numeric);
855     else
856         irc_pong(self->name, origin);
857
858     timeq_del(0, timed_send_ping, 0, TIMEQ_IGNORE_WHEN|TIMEQ_IGNORE_DATA);
859     timeq_del(0, timed_ping_timeout, 0, TIMEQ_IGNORE_WHEN|TIMEQ_IGNORE_DATA);
860     timeq_add(now + ping_freq, timed_send_ping, 0);
861     received_ping();
862     return 1;
863 }
864
865 static CMD_FUNC(cmd_error_nick)
866 {
867     /* Go back to original IRC length .. and try to reconnect :/ */
868     change_nicklen(9);
869     irc_squit(self, "Got erroneous nickname, truncating nicks.", NULL);
870     return 1;
871 }
872
873 struct create_desc {
874     struct userNode *user;
875     time_t when;
876 };
877
878 static void
879 join_helper(struct chanNode *chan, void *data)
880 {
881     struct create_desc *cd = data;
882     AddChannelUser(cd->user, chan);
883 }
884
885 static void
886 create_helper(char *name, void *data)
887 {
888     struct create_desc *cd = data;
889
890     if (!strcmp(name, "0")) {
891         while (cd->user->channels.used > 0)
892             DelChannelUser(cd->user, cd->user->channels.list[0]->channel, 0, 0);
893         return;
894     }
895
896     AddChannelUser(cd->user, AddChannel(name, cd->when, NULL, NULL));
897 }
898
899 static CMD_FUNC(cmd_create)
900 {
901     struct create_desc cd;
902     struct userNode *user;
903
904     if ((argc < 3) || !(user = GetUserH(origin)))
905         return 0;
906     cd.user = user;
907     cd.when = atoi(argv[2]);
908     parse_foreach(argv[1], join_helper, create_helper, NULL, NULL, &cd);
909     return 1;
910 }
911
912 static CMD_FUNC(cmd_join)
913 {
914     struct create_desc cd;
915
916     if (!(cd.user = GetUserH(origin)))
917         return 0;
918     if (argc < 2)
919         return 0;
920     else if (argc < 3)
921         cd.when = now;
922     else
923         cd.when = atoi(argv[2]);
924     parse_foreach(argv[1], join_helper, create_helper, NULL, NULL, &cd);
925     return 1;
926 }
927
928 static CMD_FUNC(cmd_pong)
929 {
930     if (argc < 3)
931         return 0;
932     if (!strcmp(argv[2], self->name)) {
933         timeq_del(0, timed_send_ping, 0, TIMEQ_IGNORE_WHEN|TIMEQ_IGNORE_DATA);
934         timeq_del(0, timed_ping_timeout, 0, TIMEQ_IGNORE_WHEN|TIMEQ_IGNORE_DATA);
935         timeq_add(now + ping_freq, timed_send_ping, 0);
936         received_ping();
937     }
938     return 1;
939 }
940
941 static CMD_FUNC(cmd_nick)
942 {
943     struct userNode *user;
944     if ((user = GetUserH(origin))) {
945         /* nick change (since the source is a user numeric) */
946         if (argc < 2)
947             return 0;
948         NickChange(user, argv[1], 1);
949     } else {
950         struct server *serv;
951         char modes[MAXLEN];
952         /* new nick */
953         if (argc < 9)
954             return 0;
955         serv = GetServerH(origin);
956         if (argc > 9)
957             unsplit_string(argv+6, argc-9, modes);
958         else
959             strcpy(modes, "+");
960         AddUser(serv, argv[1], argv[4], argv[5], modes, argv[argc-2], argv[argc-1], atoi(argv[3]), argv[argc-3]);
961     }
962     return 1;
963 }
964
965 static CMD_FUNC(cmd_account)
966 {
967     struct userNode *user;
968
969     if ((argc < 3) || !origin || !GetServerH(origin))
970         return 0; /* Origin must be server. */
971     user = GetUserN(argv[1]);
972     if (!user)
973         return 1; /* A QUIT probably passed the ACCOUNT. */
974     call_account_func(user, argv[2]);
975     return 1;
976 }
977
978 static CMD_FUNC(cmd_burst)
979 {
980     extern int rel_age;
981     char modes[MAXLEN], *members = "", *banlist = NULL;
982     unsigned int next = 3, res = 1;
983     struct chanNode *cNode;
984     struct userNode *un;
985     struct modeNode *mNode;
986     long mode;
987     char *user, *end, sep;
988     time_t in_timestamp;
989
990     if (argc < 3)
991         return 0;
992     modes[0] = 0;
993     while (next < argc) {
994         switch (argv[next][0]) {
995         case '+': {
996             const char *pos;
997             int n_modes;
998             for (pos=argv[next], n_modes = 1; *pos; pos++)
999                 if ((*pos == 'k') || (*pos == 'l'))
1000                     n_modes++;
1001             unsplit_string(argv+next, n_modes, modes);
1002             next += n_modes;
1003             break;
1004         }
1005         case '%': banlist = argv[next++]+1; break;
1006         default: members = argv[next++]; break;
1007         }
1008     }
1009
1010     in_timestamp = atoi(argv[2]);
1011     if ((cNode = dict_find(unbursted_channels, argv[1], NULL))) {
1012         cNode->timestamp = in_timestamp;
1013         dict_remove(unbursted_channels, cNode->name);
1014         irc_burst(cNode);
1015     }
1016     cNode = AddChannel(argv[1], in_timestamp, modes, banlist);
1017
1018     /* Burst channel members in now. */
1019     for (user = members, sep = *members, mode = 0; sep; user = end) {
1020         for (end = user + 3; isalnum(*end) || *end == '[' || *end == ']'; end++) ;
1021         sep = *end++; end[-1] = 0;
1022         if (sep == ':') {
1023             mode = 0;
1024             while ((sep = *end++)) {
1025                 if (sep == 'o')
1026                     mode |= MODE_CHANOP;
1027                 else if (sep == 'v')
1028                     mode |= MODE_VOICE;
1029                 else
1030                     break;
1031             }
1032             if (rel_age < 0)
1033                 mode = 0;
1034         }
1035         if (!(un = GetUserN(user))) {
1036             res = 0;
1037             continue;
1038         }
1039         if ((mNode = AddChannelUser(un, cNode)))
1040             mNode->modes = mode;
1041     }
1042
1043     return res;
1044 }
1045
1046 static CMD_FUNC(cmd_mode)
1047 {
1048     struct chanNode *cn;
1049     struct userNode *un;
1050
1051     if (argc < 3)
1052         return 0;
1053     if (!IsChannelName(argv[1])) {
1054         un = GetUserH(argv[1]);
1055         if (!un) {
1056             log_module(MAIN_LOG, LOG_ERROR, "Unable to find user %s whose mode is changing.", argv[1]);
1057             return 0;
1058         }
1059         mod_usermode(un, argv[2]);
1060         return 1;
1061     }
1062
1063     if (!(cn = GetChannel(argv[1]))) {
1064         log_module(MAIN_LOG, LOG_ERROR, "Unable to find channel %s whose mode is changing.", argv[1]);
1065         return 0;
1066     }
1067     if ((un = GetUserH(origin))) {
1068         struct modeNode *mn;
1069         /* Update idle time for person setting the mode */
1070         if ((mn = GetUserMode(cn, un)))
1071             mn->idle_since = now;
1072     } else {
1073         /* If it came from a server, reset timestamp to re-sync. */
1074         cn->timestamp = atoi(argv[argc-1]);
1075     }
1076
1077     return mod_chanmode(un, cn, argv+2, argc-2, MCP_ALLOW_OVB|MCP_FROM_SERVER|(un ? MC_NOTIFY : 0));
1078 }
1079
1080 static CMD_FUNC(cmd_opmode)
1081 {
1082     struct chanNode *cn;
1083     struct userNode *un;
1084
1085     if (argc < 3)
1086         return 0;
1087
1088     if (!(cn = GetChannel(argv[1]))) {
1089         log_module(MAIN_LOG, LOG_ERROR, "Unable to find channel %s whose mode is changing.", argv[1]);
1090         return 0;
1091     }
1092     if (!(un = GetUserH(origin))) {
1093         log_module(MAIN_LOG, LOG_ERROR, "Unable to find user %s requesting OPMODE.", origin);
1094         return 0;
1095     }
1096     if (!IsOper(un)) {
1097         log_module(MAIN_LOG, LOG_ERROR, "Non-privileged user %s using OPMODE.", un->nick);
1098         return 0;
1099     }
1100
1101     return mod_chanmode(un, cn, argv+2, argc-2, MCP_ALLOW_OVB|MCP_FROM_SERVER); /* do NOT announce opmode locally */
1102 }
1103
1104 static int clear_chanmode(struct chanNode *channel, const char *modes);
1105
1106 static CMD_FUNC(cmd_clearmode)
1107 {
1108     struct chanNode *cn;
1109     struct userNode *un;
1110
1111     if (argc < 3)
1112         return 0;
1113
1114     if (!(cn = GetChannel(argv[1]))) {
1115         log_module(MAIN_LOG, LOG_ERROR, "Unable to find channel %s whose mode is changing.", argv[1]);
1116         return 0;
1117     }
1118     if (!(un = GetUserH(origin))) {
1119         log_module(MAIN_LOG, LOG_ERROR, "Unable to find user %s requesting CLEARMODE.", origin);
1120         return 0;
1121     }
1122     if (!IsOper(un)) {
1123         log_module(MAIN_LOG, LOG_ERROR, "Non-privileged user %s using CLEARMODE.", un->nick);
1124         return 0;
1125     }
1126
1127     return clear_chanmode(cn, argv[2]);
1128 }
1129
1130 static CMD_FUNC(cmd_topic)
1131 {
1132     struct chanNode *cn;
1133     time_t chan_ts, topic_ts;
1134
1135     if (argc < 3)
1136         return 0;
1137     if (!(cn = GetChannel(argv[1]))) {
1138         log_module(MAIN_LOG, LOG_ERROR, "Unable to find channel %s whose topic is being set", argv[1]);
1139         return 0;
1140     }
1141     if (argc >= 5) {
1142         /* Looks like an Asuka style topic burst. */
1143         chan_ts = atoi(argv[2]);
1144         topic_ts = atoi(argv[3]);
1145     } else {
1146         chan_ts = cn->timestamp;
1147         topic_ts = now;
1148     }
1149     SetChannelTopic(cn, GetUserH(origin), argv[argc-1], 0);
1150     cn->topic_time = topic_ts;
1151     return 1;
1152 }
1153
1154 static CMD_FUNC(cmd_num_topic)
1155 {
1156     static struct chanNode *cn;
1157
1158     if (!argv[0])
1159         return 0; /* huh? */
1160     if (argv[2]) {
1161         cn = GetChannel(argv[2]);
1162         if (!cn) {
1163             log_module(MAIN_LOG, LOG_ERROR, "Unable to find channel %s in topic reply", argv[2]);
1164             return 0;
1165         }
1166     } else
1167         return 0;
1168
1169     switch (atoi(argv[0])) {
1170     case 331:
1171         cn->topic_time = 0;
1172         break;  /* no topic */
1173     case 332:
1174         if (argc < 4)
1175             return 0;
1176         safestrncpy(cn->topic, unsplit_string(argv+3, argc-3, NULL), sizeof(cn->topic));
1177         break;
1178     case 333:
1179         if (argc < 5)
1180             return 0;
1181         safestrncpy(cn->topic_nick, argv[3], sizeof(cn->topic_nick));
1182         cn->topic_time = atoi(argv[4]);
1183         break;
1184     default:
1185         return 0; /* should never happen */
1186     }
1187     return 1;
1188 }
1189
1190 static CMD_FUNC(cmd_num_gline)
1191 {
1192     if (argc < 6)
1193         return 0;
1194     gline_add(origin, argv[3], atoi(argv[4])-now, argv[5], now, 0);
1195     return 1;
1196 }
1197
1198 static CMD_FUNC(cmd_quit)
1199 {
1200     struct userNode *user;
1201     if (argc < 2)
1202         return 0;
1203     /* Sometimes we get a KILL then a QUIT or the like, so we don't want to
1204      * call DelUser unless we have the user in our grasp. */
1205     if ((user = GetUserH(origin)))
1206         DelUser(user, NULL, false, argv[1]);
1207     return 1;
1208 }
1209
1210 static CMD_FUNC(cmd_kill)
1211 {
1212     struct userNode *user;
1213     if (argc < 2)
1214         return 0;
1215     user = GetUserN(argv[1]);
1216     if (!user) {
1217         /* If we get a KILL for a non-existent user, it could be a
1218          * Ghost response to a KILL we sent out earlier.  So we only
1219          * whine if the target is local.
1220          */
1221         if (!strncmp(argv[1], self->numeric, strlen(self->numeric)))
1222             log_module(MAIN_LOG, LOG_ERROR, "Unable to find kill victim %s", argv[1]);
1223         return 0;
1224     }
1225
1226     if (IsLocal(user) && IsService(user)) {
1227         /* TODO: rate limit this so silly things don't happen. */
1228         ReintroduceUser(user);
1229         return 1;
1230     }
1231
1232     DelUser(user, NULL, false, argv[2]);
1233     return 1;
1234 }
1235
1236 static CMD_FUNC(cmd_part)
1237 {
1238     struct userNode *user;
1239
1240     if (argc < 2)
1241         return 0;
1242     user = GetUserH(origin);
1243     if (!user)
1244         return 0;
1245     parse_foreach(argv[1], part_helper, NULL, NULL, NULL, user);
1246     return 1;
1247 }
1248
1249 static CMD_FUNC(cmd_kick)
1250 {
1251     if (argc < 3)
1252         return 0;
1253     ChannelUserKicked(GetUserH(origin), GetUserN(argv[2]), GetChannel(argv[1]));
1254     return 1;
1255 }
1256
1257 static CMD_FUNC(cmd_squit)
1258 {
1259     struct server *server;
1260
1261     if (argc < 4)
1262         return 0;
1263     if (!(server = GetServerH(argv[1])))
1264         return 0;
1265
1266     if (server == self->uplink) {
1267         /* Force a reconnect to the currently selected server. */
1268         cManager.uplink->tries = 0;
1269         log_module(MAIN_LOG, LOG_INFO, "Squitting from uplink: %s", argv[3]);
1270         close_socket();
1271         return 1;
1272     }
1273
1274     DelServer(server, 0, argv[3]);
1275     return 1;
1276 }
1277
1278 static CMD_FUNC(cmd_privmsg)
1279 {
1280     struct privmsg_desc pd;
1281     if (argc != 3)
1282         return 0;
1283     pd.user = GetUserH(origin);
1284     if (!pd.user || (IsGagged(pd.user) && !IsOper(pd.user)))
1285         return 1;
1286     pd.is_notice = 0;
1287     pd.text = argv[2];
1288     parse_foreach(argv[1], privmsg_chan_helper, NULL, privmsg_user_helper, privmsg_invalid, &pd);
1289     return 1;
1290 }
1291
1292 static CMD_FUNC(cmd_notice)
1293 {
1294     struct privmsg_desc pd;
1295     if (argc != 3)
1296         return 0;
1297     pd.user = GetUserH(origin);
1298     if (!pd.user || (IsGagged(pd.user) && !IsOper(pd.user)))
1299         return 1;
1300     pd.is_notice = 1;
1301     pd.text = argv[2];
1302     parse_foreach(argv[1], privmsg_chan_helper, NULL, privmsg_user_helper, privmsg_invalid, &pd);
1303     return 1;
1304 }
1305
1306 static CMD_FUNC(cmd_away)
1307 {
1308     struct userNode *uNode;
1309
1310     uNode = GetUserH(origin);
1311     if (!uNode)
1312         return 1;
1313     if (argc < 2)
1314         uNode->modes &= ~FLAGS_AWAY;
1315     else
1316         uNode->modes |= FLAGS_AWAY;
1317     return 1;
1318 }
1319
1320 static CMD_FUNC(cmd_gline)
1321 {
1322     if (argc < 3)
1323         return 0;
1324     if (argv[2][0] == '+') {
1325         if (argc < 5)
1326             return 0;
1327         gline_add(origin, argv[2]+1, strtoul(argv[3], NULL, 0), argv[argc-1], now, 0);
1328         return 1;
1329     } else if (argv[2][0] == '-') {
1330         gline_remove(argv[2]+1, 0);
1331         return 1;
1332     } else
1333         return 0;
1334 }
1335
1336 static CMD_FUNC(cmd_svsnick)
1337 {
1338     struct userNode *target, *dest;
1339     if ((argc < 4)
1340         || !(target = GetUserN(argv[1]))
1341         || !IsLocal(target)
1342         || (dest = GetUserH(argv[2])))
1343         return 0;
1344     NickChange(target, argv[2], 0);
1345     return 1;
1346 }
1347
1348 static oper_func_t *of_list;
1349 static unsigned int of_size = 0, of_used = 0;
1350
1351 void
1352 free_user(struct userNode *user)
1353 {
1354     free(user->nick);
1355     free(user);
1356 }
1357
1358 static void
1359 parse_cleanup(void)
1360 {
1361     unsigned int nn;
1362     free(of_list);
1363     free(privmsg_funcs);
1364     free(notice_funcs);
1365     free(mcf_list);
1366     dict_delete(irc_func_dict);
1367     for (nn=0; nn<dead_users.used; nn++)
1368         free_user(dead_users.list[nn]);
1369     userList_clean(&dead_users);
1370 }
1371
1372 static void
1373 p10_conf_reload(void) {
1374     hidden_host_suffix = conf_get_data("server/hidden_host", RECDB_QSTRING);
1375 }
1376
1377 static void
1378 remove_unbursted_channel(struct chanNode *cNode) {
1379     if (unbursted_channels)
1380         dict_remove(unbursted_channels, cNode->name);
1381 }
1382
1383 void
1384 init_parse(void)
1385 {
1386     const char *str, *desc;
1387     int numnick, usermask, max_users;
1388     char numer[COMBO_NUMERIC_LEN+1];
1389     extern char *his_servername;
1390     extern char *his_servercomment;
1391
1392     /* read config items */
1393     str = conf_get_data("server/ping_freq", RECDB_QSTRING);
1394     ping_freq = str ? ParseInterval(str) : 120;
1395     str = conf_get_data("server/ping_timeout", RECDB_QSTRING);
1396     ping_timeout = str ? ParseInterval(str) : 30;
1397     str = conf_get_data("server/force_n2k", RECDB_QSTRING);
1398     force_n2k = str ? enabled_string(str) : 1;
1399     str = conf_get_data("server/numeric", RECDB_QSTRING);
1400     if (!str) {
1401         log_module(MAIN_LOG, LOG_ERROR, "No server/numeric entry in config file.");
1402         exit(1);
1403     }
1404     numnick = atoi(str);
1405     str = conf_get_data("server/max_users", RECDB_QSTRING);
1406     max_users = str ? atoi(str) : 4096;
1407     for (usermask = 4; usermask < max_users; usermask <<= 1) ;
1408     usermask--;
1409     if ((numnick < 64) && (usermask < 4096) && !force_n2k)
1410         inttobase64(numer, (numnick << 12) + (usermask & 0x00fff), 3);
1411     else
1412         inttobase64(numer, (numnick << 18) + (usermask & 0x3ffff), 5);
1413     str = conf_get_data("server/his_servername", RECDB_QSTRING);
1414     his_servername = str ? strdup(str) : NULL;
1415     str = conf_get_data("server/his_servercomment", RECDB_QSTRING);
1416     his_servercomment = str ? strdup(str) : NULL;
1417     str = conf_get_data("server/hostname", RECDB_QSTRING);
1418     desc = conf_get_data("server/description", RECDB_QSTRING);
1419     if (!str || !desc) {
1420         log_module(MAIN_LOG, LOG_ERROR, "No server/hostname entry in config file.");
1421         exit(1);
1422     }
1423     self = AddServer(NULL, str, 0, boot_time, now, numer, desc);
1424     conf_register_reload(p10_conf_reload);
1425
1426     irc_func_dict = dict_new();
1427     dict_insert(irc_func_dict, CMD_BURST, cmd_burst);
1428     dict_insert(irc_func_dict, TOK_BURST, cmd_burst);
1429     dict_insert(irc_func_dict, CMD_CREATE, cmd_create);
1430     dict_insert(irc_func_dict, TOK_CREATE, cmd_create);
1431     dict_insert(irc_func_dict, CMD_EOB, cmd_eob);
1432     dict_insert(irc_func_dict, TOK_EOB, cmd_eob);
1433     dict_insert(irc_func_dict, CMD_EOB_ACK, cmd_eob_ack);
1434     dict_insert(irc_func_dict, TOK_EOB_ACK, cmd_eob_ack);
1435     dict_insert(irc_func_dict, CMD_MODE, cmd_mode);
1436     dict_insert(irc_func_dict, TOK_MODE, cmd_mode);
1437     dict_insert(irc_func_dict, CMD_NICK, cmd_nick);
1438     dict_insert(irc_func_dict, TOK_NICK, cmd_nick);
1439     dict_insert(irc_func_dict, CMD_ACCOUNT, cmd_account);
1440     dict_insert(irc_func_dict, TOK_ACCOUNT, cmd_account);
1441     dict_insert(irc_func_dict, CMD_PASS, cmd_pass);
1442     dict_insert(irc_func_dict, TOK_PASS, cmd_pass);
1443     dict_insert(irc_func_dict, CMD_PING, cmd_ping);
1444     dict_insert(irc_func_dict, TOK_PING, cmd_ping);
1445     dict_insert(irc_func_dict, CMD_PRIVMSG, cmd_privmsg);
1446     dict_insert(irc_func_dict, TOK_PRIVMSG, cmd_privmsg);
1447     dict_insert(irc_func_dict, CMD_PONG, cmd_pong);
1448     dict_insert(irc_func_dict, TOK_PONG, cmd_pong);
1449     dict_insert(irc_func_dict, CMD_QUIT, cmd_quit);
1450     dict_insert(irc_func_dict, TOK_QUIT, cmd_quit);
1451     dict_insert(irc_func_dict, CMD_SERVER, cmd_server);
1452     dict_insert(irc_func_dict, TOK_SERVER, cmd_server);
1453     dict_insert(irc_func_dict, CMD_JOIN, cmd_join);
1454     dict_insert(irc_func_dict, TOK_JOIN, cmd_join);
1455     dict_insert(irc_func_dict, CMD_PART, cmd_part);
1456     dict_insert(irc_func_dict, TOK_PART, cmd_part);
1457     dict_insert(irc_func_dict, CMD_ERROR, cmd_error);
1458     dict_insert(irc_func_dict, TOK_ERROR, cmd_error);
1459     dict_insert(irc_func_dict, CMD_TOPIC, cmd_topic);
1460     dict_insert(irc_func_dict, TOK_TOPIC, cmd_topic);
1461     dict_insert(irc_func_dict, CMD_AWAY, cmd_away);
1462     dict_insert(irc_func_dict, TOK_AWAY, cmd_away);
1463     dict_insert(irc_func_dict, CMD_SILENCE, cmd_dummy);
1464     dict_insert(irc_func_dict, TOK_SILENCE, cmd_dummy);
1465     dict_insert(irc_func_dict, CMD_KICK, cmd_kick);
1466     dict_insert(irc_func_dict, TOK_KICK, cmd_kick);
1467     dict_insert(irc_func_dict, CMD_SQUIT, cmd_squit);
1468     dict_insert(irc_func_dict, TOK_SQUIT, cmd_squit);
1469     dict_insert(irc_func_dict, CMD_KILL, cmd_kill);
1470     dict_insert(irc_func_dict, TOK_KILL, cmd_kill);
1471     dict_insert(irc_func_dict, CMD_NOTICE, cmd_notice);
1472     dict_insert(irc_func_dict, TOK_NOTICE, cmd_notice);
1473     dict_insert(irc_func_dict, CMD_STATS, cmd_stats);
1474     dict_insert(irc_func_dict, TOK_STATS, cmd_stats);
1475     dict_insert(irc_func_dict, CMD_SVSNICK, cmd_svsnick);
1476     dict_insert(irc_func_dict, TOK_SVSNICK, cmd_svsnick);
1477     dict_insert(irc_func_dict, CMD_WHOIS, cmd_whois);
1478     dict_insert(irc_func_dict, TOK_WHOIS, cmd_whois);
1479     dict_insert(irc_func_dict, CMD_GLINE, cmd_gline);
1480     dict_insert(irc_func_dict, TOK_GLINE, cmd_gline);
1481     dict_insert(irc_func_dict, CMD_OPMODE, cmd_opmode);
1482     dict_insert(irc_func_dict, TOK_OPMODE, cmd_opmode);
1483     dict_insert(irc_func_dict, CMD_CLEARMODE, cmd_clearmode);
1484     dict_insert(irc_func_dict, TOK_CLEARMODE, cmd_clearmode);
1485     dict_insert(irc_func_dict, CMD_VERSION, cmd_version);
1486     dict_insert(irc_func_dict, TOK_VERSION, cmd_version);
1487     dict_insert(irc_func_dict, CMD_ADMIN, cmd_admin);
1488     dict_insert(irc_func_dict, TOK_ADMIN, cmd_admin);
1489
1490     /* In P10, DESTRUCT doesn't do anything except be broadcast to servers.
1491      * Apparently to obliterate channels from any servers that think they
1492      * exist?
1493      */
1494     dict_insert(irc_func_dict, CMD_DESTRUCT, cmd_dummy);
1495     dict_insert(irc_func_dict, TOK_DESTRUCT, cmd_dummy);
1496     /* Ignore invites */
1497     dict_insert(irc_func_dict, CMD_INVITE, cmd_dummy);
1498     dict_insert(irc_func_dict, TOK_INVITE, cmd_dummy);
1499     /* DESYNCH is just informational, so ignore it */
1500     dict_insert(irc_func_dict, CMD_DESYNCH, cmd_dummy);
1501     dict_insert(irc_func_dict, TOK_DESYNCH, cmd_dummy);
1502     /* Ignore channel operator notices. */
1503     dict_insert(irc_func_dict, CMD_WALLCHOPS, cmd_dummy);
1504     dict_insert(irc_func_dict, TOK_WALLCHOPS, cmd_dummy);
1505     dict_insert(irc_func_dict, CMD_WALLVOICES, cmd_dummy);
1506     dict_insert(irc_func_dict, TOK_WALLVOICES, cmd_dummy);
1507     /* Ignore opers being silly. */
1508     dict_insert(irc_func_dict, CMD_WALLOPS, cmd_dummy);
1509     dict_insert(irc_func_dict, TOK_WALLOPS, cmd_dummy);
1510     /* We have reliable clock!  Always!  Wraaa! */
1511     dict_insert(irc_func_dict, CMD_SETTIME, cmd_dummy);
1512     dict_insert(irc_func_dict, TOK_SETTIME, cmd_dummy);
1513     /* handle topics */
1514     dict_insert(irc_func_dict, "331", cmd_num_topic);
1515     dict_insert(irc_func_dict, "332", cmd_num_topic);
1516     dict_insert(irc_func_dict, "333", cmd_num_topic);
1517     dict_insert(irc_func_dict, "432", cmd_error_nick); /* Erroneus [sic] nickname */
1518     /* ban list resetting */
1519     /* "stats g" responses */
1520     dict_insert(irc_func_dict, "247", cmd_num_gline);
1521     dict_insert(irc_func_dict, "219", cmd_dummy); /* "End of /STATS report" */
1522     /* other numeric responses we might get */
1523     dict_insert(irc_func_dict, "401", cmd_dummy); /* target left network */
1524     dict_insert(irc_func_dict, "403", cmd_dummy); /* no such channel */
1525     dict_insert(irc_func_dict, "404", cmd_dummy); /* cannot send to channel */
1526     dict_insert(irc_func_dict, "441", cmd_dummy); /* target isn't on that channel */
1527     dict_insert(irc_func_dict, "442", cmd_dummy); /* you aren't on that channel */
1528     dict_insert(irc_func_dict, "443", cmd_dummy); /* is already on channel (after invite?) */
1529     dict_insert(irc_func_dict, "461", cmd_dummy); /* Not enough parameters (after TOPIC w/ 0 args) */
1530     dict_insert(irc_func_dict, "467", cmd_dummy); /* Channel key already set */
1531
1532     num_privmsg_funcs = 16;
1533     privmsg_funcs = malloc(sizeof(privmsg_func_t)*num_privmsg_funcs);
1534     memset(privmsg_funcs, 0, sizeof(privmsg_func_t)*num_privmsg_funcs);
1535
1536     num_notice_funcs = 16;
1537     notice_funcs = malloc(sizeof(privmsg_func_t)*num_notice_funcs);
1538     memset(notice_funcs, 0, sizeof(privmsg_func_t)*num_notice_funcs);
1539
1540     userList_init(&dead_users);
1541     reg_del_channel_func(remove_unbursted_channel);
1542     reg_exit_func(parse_cleanup);
1543 }
1544
1545 int
1546 parse_line(char *line, int recursive)
1547 {
1548     char *argv[MAXNUMPARAMS], *origin;
1549     int argc, cmd, res=0;
1550     cmd_func_t *func;
1551
1552     argc = split_line(line, true, MAXNUMPARAMS, argv);
1553     cmd = self->uplink || !argv[0][1] || !argv[0][2];
1554     if (argc > cmd) {
1555         if (cmd) {
1556             if (argv[0][0] == ':') {
1557                 origin = argv[0]+1;
1558             } else if (!argv[0][1] || !argv[0][2]) {
1559                 struct server *sNode = GetServerN(argv[0]);
1560                 origin = sNode ? sNode->name : 0;
1561             } else {
1562                 struct userNode *uNode = GetUserN(argv[0]);
1563                 origin = uNode ? uNode->nick : 0;
1564             }
1565         } else
1566             origin = 0;
1567         if ((func = dict_find(irc_func_dict, argv[cmd], NULL)))
1568             res = func(origin, argc-cmd, argv+cmd);
1569     }
1570     if (!res) {
1571         log_module(MAIN_LOG, LOG_ERROR, "PARSE ERROR on line: %s", unsplit_string(argv, argc, NULL));
1572     } else if (!recursive) {
1573         unsigned int i;
1574         for (i=0; i<dead_users.used; i++)
1575             free_user(dead_users.list[i]);
1576         dead_users.used = 0;
1577     }
1578     return res;
1579 }
1580
1581 static void
1582 parse_foreach(char *target_list, foreach_chanfunc cf, foreach_nonchan nc, foreach_userfunc uf, foreach_nonuser nu, void *data)
1583 {
1584     char *j, old;
1585     do {
1586         j = target_list;
1587         while (*j != 0 && *j != ',')
1588             j++;
1589         old = *j;
1590         *j = 0;
1591         if (IsChannelName(target_list)
1592             || (target_list[0] == '0' && target_list[1] == '\0')) {
1593             struct chanNode *chan = GetChannel(target_list);
1594             if (chan) {
1595                 if (cf)
1596                     cf(chan, data);
1597             } else {
1598                 if (nc)
1599                     nc(target_list, data);
1600             }
1601         } else {
1602             struct userNode *user;
1603             struct privmsg_desc *pd = data;
1604
1605             pd->is_qualified = 0;
1606             if (*target_list == '@') {
1607                 user = NULL;
1608             } else if (strchr(target_list, '@')) {
1609                 struct server *server;
1610
1611                 pd->is_qualified = 1;
1612                 user = GetUserH(strtok(target_list, "@"));
1613                 server = GetServerH(strtok(NULL, "@"));
1614
1615                 if (user && (user->uplink != server)) {
1616                     /* Don't attempt to index into any arrays
1617                        using a user's numeric on another server. */
1618                     user = NULL;
1619                 }
1620             } else {
1621                 user = GetUserN(target_list);
1622             }
1623
1624             if (user) {
1625                 if (uf)
1626                     uf(user, data);
1627             } else {
1628                 if (nu)
1629                     nu(target_list, data);
1630             }
1631         }
1632         target_list = j+1;
1633     } while (old == ',');
1634 }
1635
1636 static int
1637 get_local_numeric(void)
1638 {
1639     static unsigned int next_numeric = 0;
1640     if (self->clients > self->num_mask)
1641         return -1;
1642     while (self->users[next_numeric])
1643         if (++next_numeric > self->num_mask)
1644             next_numeric = 0;
1645     return next_numeric;
1646 }
1647
1648 static void
1649 make_numeric(struct server *svr, int local_num, char *outbuf)
1650 {
1651     int slen, llen;
1652
1653     if (force_n2k || svr->numeric[1]) {
1654         slen = 2;
1655         llen = 3;
1656     } else {
1657         slen = 1;
1658         llen = (local_num < 64*64) ? 2 : 3;
1659     }
1660     strncpy(outbuf, svr->numeric, slen);
1661     inttobase64(outbuf+slen, local_num, llen);
1662     outbuf[slen+llen] = 0;
1663 }
1664
1665 struct server *
1666 AddServer(struct server *uplink, const char *name, int hops, time_t boot, time_t link, const char *numeric, const char *description)
1667 {
1668     struct server* sNode;
1669     int slen, mlen;
1670
1671     if ((sNode = GetServerN(numeric))) {
1672         /* This means we're trying to re-add an existant server.
1673          * To be safe, we should forget the previous incarnation.
1674          * (And all its linked servers.)
1675          *
1676          * It usually only happens in replays when the original
1677          * had a ping timeout and the replay didn't (because
1678          * replaying a ping timeout invariably gets things wrong).
1679          */
1680         DelServer(sNode, 0, NULL);
1681     }
1682
1683     switch (strlen(numeric)) {
1684     case 5: slen = 2; mlen = 3; break;
1685     case 4: slen = 1; mlen = 3; break;
1686     case 3: slen = 1; mlen = 2; break;
1687     default:
1688         log_module(MAIN_LOG, LOG_ERROR, "AddServer(\"%s\", \"%s\", ...): Numeric %s has invalid length.", uplink->name, name, numeric);
1689         return NULL;
1690     }
1691
1692     sNode = calloc(1, sizeof(*sNode));
1693     sNode->uplink = uplink;
1694     safestrncpy(sNode->name, name, sizeof(sNode->name));
1695     sNode->num_mask = base64toint(numeric+slen, mlen);
1696     sNode->hops = hops;
1697     sNode->boot = boot;
1698     sNode->link = link;
1699     strncpy(sNode->numeric, numeric, slen);
1700     safestrncpy(sNode->description, description, sizeof(sNode->description));
1701     sNode->users = calloc(sNode->num_mask+1, sizeof(*sNode->users));
1702     serverList_init(&sNode->children);
1703     if (sNode->uplink) {
1704         /* uplink may be NULL if we're just building ourself */
1705         serverList_append(&sNode->uplink->children, sNode);
1706     }
1707     servers_num[base64toint(numeric, slen)] = sNode;
1708     dict_insert(servers, sNode->name, sNode);
1709     return sNode;
1710 }
1711
1712 void DelServer(struct server* serv, int announce, const char *message)
1713 {
1714     unsigned int i;
1715
1716     /* If we receive an ERROR command before the SERVER
1717      * command a NULL server can be passed */
1718     if (!serv)
1719         return;
1720
1721     /* Hrm, what's the right way to SQUIT some other server?
1722      * (This code is only to handle killing juped servers.) */
1723     if (announce && (serv->uplink == self) && (serv != self->uplink))
1724         irc_squit(serv, message, NULL);
1725
1726     /* must recursively remove servers linked to this one first */
1727     for (i=serv->children.used;i>0;)
1728         if (serv->children.list[--i] != self)
1729             DelServer(serv->children.list[i], false, NULL);
1730
1731     /* clean up server's user hash tables */
1732     for (i=0;i<=serv->num_mask;i++)
1733         if (serv->users[i])
1734             DelUser(serv->users[i], NULL, false, "server delinked");
1735
1736     /* delete server */
1737     if (serv->uplink)
1738         serverList_remove(&serv->uplink->children, serv);
1739     if (serv == self->uplink)
1740         self->uplink = NULL;
1741     servers_num[base64toint(serv->numeric, strlen(serv->numeric))] = NULL;
1742     dict_remove(servers, serv->name);
1743     serverList_clean(&serv->children);
1744     free(serv->users);
1745     free(serv);
1746 }
1747
1748 struct userNode *
1749 AddService(const char *nick, const char *desc, const char *hostname)
1750 {
1751     char numeric[COMBO_NUMERIC_LEN+1];
1752     int local_num = get_local_numeric();
1753     time_t timestamp = now;
1754     struct userNode *old_user = GetUserH(nick);
1755
1756     if (old_user) {
1757         if (IsLocal(old_user))
1758             return old_user;
1759         timestamp = old_user->timestamp - 1;
1760     }
1761     if (local_num == -1) {
1762         log_module(MAIN_LOG, LOG_ERROR, "Unable to allocate numnick for service %s", nick);
1763         return 0;
1764     }
1765     if (!hostname)
1766         hostname = self->name;
1767     make_numeric(self, local_num, numeric);
1768     return AddUser(self, nick, nick, hostname, "+oik", numeric, desc, now, "AAAAAA");
1769 }
1770
1771 struct userNode *
1772 AddClone(const char *nick, const char *ident, const char *hostname, const char *desc)
1773 {
1774     char numeric[COMBO_NUMERIC_LEN+1];
1775     int local_num = get_local_numeric();
1776     time_t timestamp = now;
1777     struct userNode *old_user = GetUserH(nick);
1778
1779     if (old_user) {
1780         if (IsLocal(old_user))
1781             return old_user;
1782         timestamp = old_user->timestamp - 1;
1783     }
1784     if (local_num == -1) {
1785         log_module(MAIN_LOG, LOG_ERROR, "Unable to allocate numnick for clone %s", nick);
1786         return 0;
1787     }
1788     make_numeric(self, local_num, numeric);
1789     return AddUser(self, nick, ident, hostname, "+i", numeric, desc, timestamp, "AAAAAA");
1790 }
1791
1792 int
1793 is_valid_nick(const char *nick) {
1794     /* IRC has some of The Most Fucked-Up ideas about character sets
1795      * in the world.. */
1796     if (!isalpha(*nick) && !strchr("{|}~[\\]^_`", *nick))
1797         return 0;
1798     for (++nick; *nick; ++nick)
1799         if (!isalnum(*nick) && !strchr("{|}~[\\]^-_`", *nick))
1800             return 0;
1801     if (strlen(nick) > nicklen)
1802         return 0;
1803     return 1;
1804 }
1805
1806 static struct userNode*
1807 AddUser(struct server* uplink, const char *nick, const char *ident, const char *hostname, const char *modes, const char *numeric, const char *userinfo, time_t timestamp, const char *realip)
1808 {
1809     struct userNode *oldUser, *uNode;
1810     unsigned int n, ignore_user;
1811
1812     if ((strlen(numeric) < 3) || (strlen(numeric) > 5)) {
1813         log_module(MAIN_LOG, LOG_WARNING, "AddUser(%p, %s, ...): numeric %s wrong length!", uplink, nick, numeric);
1814         return NULL;
1815     }
1816
1817     if (!uplink) {
1818         log_module(MAIN_LOG, LOG_WARNING, "AddUser(%p, %s, ...): server for numeric %s doesn't exist!", uplink, nick, numeric);
1819         return NULL;
1820     }
1821
1822     if (uplink != GetServerN(numeric)) {
1823         log_module(MAIN_LOG, LOG_WARNING, "AddUser(%p, %s, ...): server for numeric %s differs from nominal uplink %s.", uplink, nick, numeric, uplink->name);
1824         return NULL;
1825     }
1826
1827     if (!is_valid_nick(nick)) {
1828         log_module(MAIN_LOG, LOG_WARNING, "AddUser(%p, %s, ...): invalid nickname detected.", uplink, nick);
1829         return NULL;
1830     }
1831
1832     ignore_user = 0;
1833     if ((oldUser = GetUserH(nick))) {
1834         if (IsLocal(oldUser) && (IsService(oldUser) || IsPersistent(oldUser))) {
1835             /* The service should collide the new user off. */
1836             oldUser->timestamp = timestamp - 1;
1837             irc_user(oldUser);
1838         }
1839         if (oldUser->timestamp > timestamp) {
1840             /* "Old" user is really newer; remove them */
1841             DelUser(oldUser, 0, 1, "Overruled by older nick");
1842         } else {
1843             /* User being added is too new; do not add them to
1844              * clients, but do add them to the server's list, since it
1845              * will send a KILL and QUIT soon. */
1846             ignore_user = 1;
1847         }
1848     }
1849
1850     /* create new usernode and set all values */
1851     uNode = calloc(1, sizeof(*uNode));
1852     uNode->nick = strdup(nick);
1853     safestrncpy(uNode->ident, ident, sizeof(uNode->ident));
1854     safestrncpy(uNode->info, userinfo, sizeof(uNode->info));
1855     safestrncpy(uNode->hostname, hostname, sizeof(uNode->hostname));
1856     safestrncpy(uNode->numeric, numeric, sizeof(uNode->numeric));
1857     uNode->ip.s_addr = htonl(base64toint(realip, 6));
1858     uNode->timestamp = timestamp;
1859     modeList_init(&uNode->channels);
1860     uNode->uplink = uplink;
1861     if (++uNode->uplink->clients > uNode->uplink->max_clients) {
1862         uNode->uplink->max_clients = uNode->uplink->clients;
1863     }
1864     uNode->num_local = base64toint(numeric+strlen(uNode->uplink->numeric), 3) & uNode->uplink->num_mask;
1865     uNode->uplink->users[uNode->num_local] = uNode;
1866     mod_usermode(uNode, modes);
1867     if (ignore_user)
1868         return uNode;
1869
1870     dict_insert(clients, uNode->nick, uNode);
1871     if (dict_size(clients) > max_clients) {
1872         max_clients = dict_size(clients);
1873         max_clients_time = now;
1874     }
1875     if (IsLocal(uNode))
1876         irc_user(uNode);
1877     for (n=0; n<nuf_used; n++)
1878         if (nuf_list[n](uNode))
1879             break;
1880     return uNode;
1881 }
1882
1883 /* removes user from it's server's hash table and nick hash table */
1884 void
1885 DelUser(struct userNode* user, struct userNode *killer, int announce, const char *why)
1886 {
1887     unsigned int n;
1888
1889     /* mark them as dead, in case anybody cares */
1890     user->dead = 1;
1891
1892     /* remove user from all channels */
1893     while (user->channels.used > 0)
1894         DelChannelUser(user, user->channels.list[user->channels.used-1]->channel, false, 0);
1895
1896     /* Call these in reverse order so ChanServ can update presence
1897        information before NickServ nukes the handle_info. */
1898     for (n = duf_used; n > 0; )
1899         duf_list[--n](user, killer, why);
1900
1901     user->uplink->clients--;
1902     user->uplink->users[user->num_local] = NULL;
1903     if (IsOper(user))
1904         userList_remove(&curr_opers, user);
1905     /* remove from global dictionary, but not if after a collide */
1906     if (user == dict_find(clients, user->nick, NULL))
1907         dict_remove(clients, user->nick);
1908
1909     if (IsInvisible(user))
1910         invis_clients--;
1911
1912     if (announce) {
1913         if (IsLocal(user))
1914             irc_quit(user, why);
1915         else
1916             irc_kill(killer, user, why);
1917     }
1918
1919     modeList_clean(&user->channels);
1920     /* We don't free them, in case we try to privmsg them or something
1921      * (like when a stupid oper kills themself).  We just put them onto
1922      * a list of clients that get freed after processing each line.
1923      */
1924     if (dead_users.size)
1925         userList_append(&dead_users, user);
1926     else
1927         free_user(user);
1928 }
1929
1930 void mod_usermode(struct userNode *user, const char *mode_change) {
1931     static void call_oper_funcs(struct userNode *user);
1932     int add = 1;
1933     const char *word = mode_change;
1934
1935     if (!user || !mode_change)
1936         return;
1937     while (*word != ' ' && *word) word++;\
1938     while (*word == ' ') word++; \
1939     while (1) {
1940 #define do_user_mode(FLAG) do { if (add) user->modes |= FLAG; else user->modes &= ~FLAG; } while (0)
1941         switch (*mode_change++) {
1942         case 0: case ' ': return;
1943         case '+': add = 1; break;
1944         case '-': add = 0; break;
1945         case 'o':
1946             do_user_mode(FLAGS_OPER);
1947             if (add) {
1948                 userList_append(&curr_opers, user);
1949                 call_oper_funcs(user);
1950             } else {
1951                 userList_remove(&curr_opers, user);
1952             }
1953             break;
1954         case 'O': do_user_mode(FLAGS_LOCOP); break;
1955         case 'i': do_user_mode(FLAGS_INVISIBLE);
1956             if (add)
1957                 invis_clients++;
1958             else
1959                 invis_clients--;
1960             break;
1961         case 'w': do_user_mode(FLAGS_WALLOP); break;
1962         case 's': do_user_mode(FLAGS_SERVNOTICE); break;
1963         case 'd': do_user_mode(FLAGS_DEAF); break;
1964         case 'k': do_user_mode(FLAGS_SERVICE); break;
1965         case 'g': do_user_mode(FLAGS_GLOBAL); break;
1966         case 'h': do_user_mode(FLAGS_HELPER); break;
1967         case 'x': do_user_mode(FLAGS_HIDDEN_HOST); break;
1968         case 'r':
1969             if (*word) {
1970                 char tag[MAXLEN];
1971                 unsigned int ii;
1972                 for (ii=0; (*word != ' ') && (*word != '\0'); )
1973                     tag[ii++] = *word++;
1974                 tag[ii] = 0;
1975                 while (*word == ' ')
1976                     word++;
1977                 call_account_func(user, tag);
1978             }
1979             break;
1980         }
1981 #undef do_user_mode
1982     }
1983 }
1984
1985 struct mod_chanmode *
1986 mod_chanmode_parse(struct chanNode *channel, char **modes, unsigned int argc, unsigned int flags)
1987 {
1988     struct mod_chanmode *change;
1989     unsigned int ii, in_arg, ch_arg, add;
1990
1991     if (argc == 0)
1992         return NULL;
1993     if (!(change = mod_chanmode_alloc(argc - 1)))
1994         return NULL;
1995
1996     for (ii = ch_arg = 0, in_arg = add = 1;
1997          (modes[0][ii] != '\0') && (modes[0][ii] != ' ');
1998          ++ii) {
1999         switch (modes[0][ii]) {
2000         case '+':
2001             add = 1;
2002             break;
2003         case '-':
2004             add = 0;
2005             break;
2006 #define do_chan_mode(FLAG) do { if (add) change->modes_set |= (FLAG), change->modes_clear &= ~(FLAG); else change->modes_clear |= (FLAG), change->modes_set &= ~(FLAG); } while(0)
2007         case 'C': do_chan_mode(MODE_NOCTCPS); break;
2008         case 'D': do_chan_mode(MODE_DELAYJOINS); break;
2009         case 'c': do_chan_mode(MODE_NOCOLORS); break;
2010         case 'i': do_chan_mode(MODE_INVITEONLY); break;
2011         case 'm': do_chan_mode(MODE_MODERATED); break;
2012         case 'n': do_chan_mode(MODE_NOPRIVMSGS); break;
2013         case 'p': do_chan_mode(MODE_PRIVATE); break;
2014         case 'r': do_chan_mode(MODE_REGONLY); break;
2015         case 's': do_chan_mode(MODE_SECRET); break;
2016         case 't': do_chan_mode(MODE_TOPICLIMIT); break;
2017 #undef do_chan_mode
2018         case 'l':
2019             if (add) {
2020                 if (in_arg >= argc)
2021                     goto error;
2022                 change->modes_set |= MODE_LIMIT;
2023                 change->new_limit = atoi(modes[in_arg++]);
2024             } else {
2025                 change->modes_set &= ~MODE_LIMIT;
2026                 change->modes_clear |= MODE_LIMIT;
2027             }
2028             break;
2029         case 'k':
2030             if (add) {
2031                 if (in_arg >= argc)
2032                     goto error;
2033                 change->modes_set |= MODE_KEY;
2034                 safestrncpy(change->new_key, modes[in_arg++], sizeof(change->new_key));
2035             } else {
2036                 change->modes_clear |= MODE_KEY;
2037                 if (!(flags & MCP_KEY_FREE)) {
2038                     if (in_arg >= argc)
2039                         goto error;
2040                     in_arg++;
2041                 }
2042             }
2043             break;
2044         case 'b':
2045             if (!(flags & MCP_ALLOW_OVB))
2046                 goto error;
2047             if (in_arg >= argc)
2048                 goto error;
2049             change->args[ch_arg].mode = MODE_BAN;
2050             if (!add)
2051                 change->args[ch_arg].mode |= MODE_REMOVE;
2052             change->args[ch_arg++].hostmask = modes[in_arg++];
2053             break;
2054         case 'o': case 'v':
2055         {
2056             struct userNode *victim;
2057             if (!(flags & MCP_ALLOW_OVB))
2058                 goto error;
2059             if (in_arg >= argc)
2060                 goto error;
2061             change->args[ch_arg].mode = (modes[0][ii] == 'o') ? MODE_CHANOP : MODE_VOICE;
2062             if (!add)
2063                 change->args[ch_arg].mode |= MODE_REMOVE;
2064             if (flags & MCP_FROM_SERVER)
2065                 victim = GetUserN(modes[in_arg++]);
2066             else
2067                 victim = GetUserH(modes[in_arg++]);
2068             if (!victim)
2069                 continue;
2070             if ((change->args[ch_arg].member = GetUserMode(channel, victim)))
2071                 ch_arg++;
2072             break;
2073         }
2074         default:
2075             if (!(flags & MCP_FROM_SERVER))
2076                 goto error;
2077             break;
2078         }
2079     }
2080     change->argc = ch_arg; /* in case any turned out to be ignored */
2081     if (change->modes_set & MODE_SECRET) {
2082         change->modes_set &= ~(MODE_PRIVATE);
2083         change->modes_clear |= MODE_PRIVATE;
2084     } else if (change->modes_set & MODE_PRIVATE) {
2085         change->modes_set &= ~(MODE_SECRET);
2086         change->modes_clear |= MODE_SECRET;
2087     }
2088     return change;
2089   error:
2090     mod_chanmode_free(change);
2091     return NULL;
2092 }
2093
2094 struct chanmode_buffer {
2095     char modes[MAXLEN];
2096     char args[MAXLEN];
2097     struct chanNode *channel;
2098     struct userNode *actor;
2099     unsigned int modes_used;
2100     unsigned int args_used;
2101     size_t chname_len;
2102     unsigned int is_add : 1;
2103     unsigned int is_chanop : 1;
2104 };
2105
2106 static void
2107 mod_chanmode_append(struct chanmode_buffer *buf, char ch, const char *arg)
2108 {
2109     size_t arg_len = strlen(arg);
2110     if (buf->modes_used > (MAXMODEPARAMS) ||
2111         buf->modes_used + buf->args_used + buf->chname_len + arg_len > 450) {
2112         memcpy(buf->modes + buf->modes_used, buf->args, buf->args_used);
2113         buf->modes[buf->modes_used + buf->args_used] = '\0';
2114         irc_mode((buf->is_chanop ? buf->actor : NULL), buf->channel, buf->modes);
2115         buf->modes[0] = buf->is_add ? '+' : '-';
2116         buf->modes_used = 1;
2117         buf->args_used = 0;
2118     }
2119     buf->modes[buf->modes_used++] = ch;
2120     buf->args[buf->args_used++] = ' ';
2121     memcpy(buf->args + buf->args_used, arg, arg_len);
2122     buf->args_used += arg_len;
2123 }
2124
2125 void
2126 mod_chanmode_announce(struct userNode *who, struct chanNode *channel, struct mod_chanmode *change)
2127 {
2128     struct chanmode_buffer chbuf;
2129     unsigned int arg;
2130     struct modeNode *mn;
2131     char int_buff[32], mode = '\0';
2132
2133     assert(change->argc <= change->alloc_argc);
2134     memset(&chbuf, 0, sizeof(chbuf));
2135     chbuf.channel = channel;
2136     chbuf.actor = who;
2137     chbuf.chname_len = strlen(channel->name);
2138     if ((mn = GetUserMode(channel, who)) && (mn->modes & MODE_CHANOP))
2139         chbuf.is_chanop = 1;
2140
2141     /* First remove modes */
2142     chbuf.is_add = 0;
2143     if (change->modes_clear) {
2144         if (mode != '-')
2145             chbuf.modes[chbuf.modes_used++] = mode = '-';
2146 #define DO_MODE_CHAR(BIT, CHAR) if (change->modes_clear & MODE_##BIT) chbuf.modes[chbuf.modes_used++] = CHAR
2147         DO_MODE_CHAR(PRIVATE, 'p');
2148         DO_MODE_CHAR(SECRET, 's');
2149         DO_MODE_CHAR(MODERATED, 'm');
2150         DO_MODE_CHAR(TOPICLIMIT, 't');
2151         DO_MODE_CHAR(INVITEONLY, 'i');
2152         DO_MODE_CHAR(NOPRIVMSGS, 'n');
2153         DO_MODE_CHAR(LIMIT, 'l');
2154         DO_MODE_CHAR(DELAYJOINS, 'D');
2155         DO_MODE_CHAR(REGONLY, 'r');
2156         DO_MODE_CHAR(NOCOLORS, 'c');
2157         DO_MODE_CHAR(NOCTCPS, 'C');
2158 #undef DO_MODE_CHAR
2159         if (change->modes_clear & channel->modes & MODE_KEY)
2160             mod_chanmode_append(&chbuf, 'k', channel->key);
2161     }
2162     for (arg = 0; arg < change->argc; ++arg) {
2163         if (!(change->args[arg].mode & MODE_REMOVE))
2164             continue;
2165         if (mode != '-')
2166             chbuf.modes[chbuf.modes_used++] = mode = '-';
2167         switch (change->args[arg].mode & ~MODE_REMOVE) {
2168         case MODE_BAN:
2169             mod_chanmode_append(&chbuf, 'b', change->args[arg].hostmask);
2170             break;
2171         default:
2172             if (change->args[arg].mode & MODE_CHANOP)
2173                 mod_chanmode_append(&chbuf, 'o', change->args[arg].member->user->numeric);
2174             if (change->args[arg].mode & MODE_VOICE)
2175                 mod_chanmode_append(&chbuf, 'v', change->args[arg].member->user->numeric);
2176             break;
2177         }
2178     }
2179
2180     /* Then set them */
2181     chbuf.is_add = 1;
2182     if (change->modes_set) {
2183         if (mode != '+')
2184             chbuf.modes[chbuf.modes_used++] = mode = '+';
2185 #define DO_MODE_CHAR(BIT, CHAR) if (change->modes_set & MODE_##BIT) chbuf.modes[chbuf.modes_used++] = CHAR
2186         DO_MODE_CHAR(PRIVATE, 'p');
2187         DO_MODE_CHAR(SECRET, 's');
2188         DO_MODE_CHAR(MODERATED, 'm');
2189         DO_MODE_CHAR(TOPICLIMIT, 't');
2190         DO_MODE_CHAR(INVITEONLY, 'i');
2191         DO_MODE_CHAR(NOPRIVMSGS, 'n');
2192         DO_MODE_CHAR(DELAYJOINS, 'D');
2193         DO_MODE_CHAR(REGONLY, 'r');
2194         DO_MODE_CHAR(NOCOLORS, 'c');
2195         DO_MODE_CHAR(NOCTCPS, 'C');
2196 #undef DO_MODE_CHAR
2197         if(change->modes_set & MODE_KEY)
2198             mod_chanmode_append(&chbuf, 'k', change->new_key);
2199         if(change->modes_set & MODE_LIMIT) {
2200             sprintf(int_buff, "%d", change->new_limit);
2201             mod_chanmode_append(&chbuf, 'l', int_buff);
2202         }
2203     }
2204     for (arg = 0; arg < change->argc; ++arg) {
2205         if (change->args[arg].mode & MODE_REMOVE)
2206             continue;
2207         if (mode != '+')
2208             chbuf.modes[chbuf.modes_used++] = mode = '+';
2209         switch (change->args[arg].mode) {
2210         case MODE_BAN:
2211             mod_chanmode_append(&chbuf, 'b', change->args[arg].hostmask);
2212             break;
2213         default:
2214             if (change->args[arg].mode & MODE_CHANOP)
2215                 mod_chanmode_append(&chbuf, 'o', change->args[arg].member->user->numeric);
2216             if (change->args[arg].mode & MODE_VOICE)
2217                 mod_chanmode_append(&chbuf, 'v', change->args[arg].member->user->numeric);
2218             break;
2219         }
2220     }
2221
2222     /* Flush the buffer and apply changes locally */
2223     if (chbuf.modes_used > 0) {
2224         memcpy(chbuf.modes + chbuf.modes_used, chbuf.args, chbuf.args_used);
2225         chbuf.modes[chbuf.modes_used + chbuf.args_used] = '\0';
2226         irc_mode((chbuf.is_chanop ? chbuf.actor : NULL), chbuf.channel, chbuf.modes);
2227     }
2228     mod_chanmode_apply(who, channel, change);
2229 }
2230
2231 char *
2232 mod_chanmode_format(struct mod_chanmode *change, char *outbuff)
2233 {
2234     unsigned int used = 0;
2235     assert(change->argc <= change->alloc_argc);
2236     if (change->modes_clear) {
2237         outbuff[used++] = '-';
2238 #define DO_MODE_CHAR(BIT, CHAR) if (change->modes_clear & MODE_##BIT) outbuff[used++] = CHAR
2239         DO_MODE_CHAR(PRIVATE, 'p');
2240         DO_MODE_CHAR(SECRET, 's');
2241         DO_MODE_CHAR(MODERATED, 'm');
2242         DO_MODE_CHAR(TOPICLIMIT, 't');
2243         DO_MODE_CHAR(INVITEONLY, 'i');
2244         DO_MODE_CHAR(NOPRIVMSGS, 'n');
2245         DO_MODE_CHAR(LIMIT, 'l');
2246         DO_MODE_CHAR(KEY, 'k');
2247         DO_MODE_CHAR(DELAYJOINS, 'D');
2248         DO_MODE_CHAR(REGONLY, 'r');
2249         DO_MODE_CHAR(NOCOLORS, 'c');
2250         DO_MODE_CHAR(NOCTCPS, 'C');
2251 #undef DO_MODE_CHAR
2252     }
2253     if (change->modes_set) {
2254         outbuff[used++] = '+';
2255 #define DO_MODE_CHAR(BIT, CHAR) if (change->modes_set & MODE_##BIT) outbuff[used++] = CHAR
2256         DO_MODE_CHAR(PRIVATE, 'p');
2257         DO_MODE_CHAR(SECRET, 's');
2258         DO_MODE_CHAR(MODERATED, 'm');
2259         DO_MODE_CHAR(TOPICLIMIT, 't');
2260         DO_MODE_CHAR(INVITEONLY, 'i');
2261         DO_MODE_CHAR(NOPRIVMSGS, 'n');
2262         DO_MODE_CHAR(DELAYJOINS, 'D');
2263         DO_MODE_CHAR(REGONLY, 'r');
2264         DO_MODE_CHAR(NOCOLORS, 'c');
2265         DO_MODE_CHAR(NOCTCPS, 'C');
2266 #undef DO_MODE_CHAR
2267         switch (change->modes_set & (MODE_KEY|MODE_LIMIT)) {
2268         case MODE_KEY|MODE_LIMIT:
2269             used += sprintf(outbuff+used, "lk %d %s", change->new_limit, change->new_key);
2270             break;
2271         case MODE_KEY:
2272             used += sprintf(outbuff+used, "k %s", change->new_key);
2273             break;
2274         case MODE_LIMIT:
2275             used += sprintf(outbuff+used, "l %d", change->new_limit);
2276             break;
2277         }
2278     }
2279     outbuff[used] = 0;
2280     return outbuff;
2281 }
2282
2283 static int
2284 clear_chanmode(struct chanNode *channel, const char *modes)
2285 {
2286     unsigned int remove;
2287
2288     for (remove = 0; *modes; modes++) {
2289         switch (*modes) {
2290         case 'o': remove |= MODE_CHANOP; break;
2291         case 'v': remove |= MODE_VOICE; break;
2292         case 'p': remove |= MODE_PRIVATE; break;
2293         case 's': remove |= MODE_SECRET; break;
2294         case 'm': remove |= MODE_MODERATED; break;
2295         case 't': remove |= MODE_TOPICLIMIT; break;
2296         case 'i': remove |= MODE_INVITEONLY; break;
2297         case 'n': remove |= MODE_NOPRIVMSGS; break;
2298         case 'k':
2299             remove |= MODE_KEY;
2300             channel->key[0] = '\0';
2301             break;
2302         case 'l':
2303             remove |= MODE_LIMIT;
2304             channel->limit = 0;
2305             break;
2306         case 'b': remove |= MODE_BAN; break;
2307         case 'D': remove |= MODE_DELAYJOINS; break;
2308         case 'r': remove |= MODE_REGONLY; break;
2309         case 'c': remove |= MODE_NOCOLORS;
2310         case 'C': remove |= MODE_NOCTCPS; break;
2311         }
2312     }
2313
2314     if (!remove)
2315         return 1;
2316
2317     /* Remove simple modes. */
2318     channel->modes &= ~remove;
2319
2320     /* If removing bans, kill 'em all. */
2321     if ((remove & MODE_BAN) && channel->banlist.used) {
2322         unsigned int i;
2323         for (i=0; i<channel->banlist.used; i++)
2324             free(channel->banlist.list[i]);
2325         channel->banlist.used = 0;
2326     }
2327
2328     /* Remove member modes. */
2329     if ((remove & (MODE_CHANOP | MODE_VOICE)) && channel->members.used) {
2330         int mask = ~(remove & (MODE_CHANOP | MODE_VOICE));
2331         unsigned int i;
2332
2333         for (i = 0; i < channel->members.used; i++)
2334             channel->members.list[i]->modes &= mask;
2335     }
2336
2337     return 1;
2338 }
2339
2340 void
2341 reg_privmsg_func(struct userNode *user, privmsg_func_t handler)
2342 {
2343     unsigned int numeric = user->num_local;
2344     if (numeric >= num_privmsg_funcs) {
2345         int newnum = numeric + 8;
2346         privmsg_funcs = realloc(privmsg_funcs, newnum*sizeof(privmsg_func_t));
2347         memset(privmsg_funcs+num_privmsg_funcs, 0, (newnum-num_privmsg_funcs)*sizeof(privmsg_func_t));
2348         num_privmsg_funcs = newnum;
2349     }
2350     if (privmsg_funcs[numeric])
2351         log_module(MAIN_LOG, LOG_WARNING, "re-registering new privmsg handler for numeric %d", numeric);
2352     privmsg_funcs[numeric] = handler;
2353 }
2354
2355 void
2356 unreg_privmsg_func(struct userNode *user, privmsg_func_t handler)
2357 {
2358     unsigned int x;
2359
2360     if (!user || handler)
2361       return; /* this really only works with users */
2362
2363     memset(privmsg_funcs+user->num_local, 0, sizeof(privmsg_func_t));
2364
2365     for (x = user->num_local+1; x < num_privmsg_funcs; x++) 
2366        memmove(privmsg_funcs+x-1, privmsg_funcs+x, sizeof(privmsg_func_t));
2367     
2368     privmsg_funcs = realloc(privmsg_funcs, num_privmsg_funcs*sizeof(privmsg_func_t)); 
2369     num_privmsg_funcs--;
2370 }
2371
2372
2373 void
2374 reg_notice_func(struct userNode *user, privmsg_func_t handler)
2375 {
2376     unsigned int numeric = user->num_local;
2377     if (numeric >= num_notice_funcs) {
2378         int newnum = numeric + 8;
2379         notice_funcs = realloc(notice_funcs, newnum*sizeof(privmsg_func_t));
2380         memset(notice_funcs+num_notice_funcs, 0, (newnum-num_notice_funcs)*sizeof(privmsg_func_t));
2381         num_notice_funcs = newnum;
2382     }
2383     if (notice_funcs[numeric])
2384         log_module(MAIN_LOG, LOG_WARNING, "re-registering new notice handler for numeric %d", numeric);
2385     notice_funcs[numeric] = handler;
2386 }
2387
2388 void
2389 unreg_notice_func(struct userNode *user, privmsg_func_t handler)
2390 {
2391     unsigned int x;
2392
2393     if (!user || handler)
2394           return; /* this really only works with users */
2395
2396     memset(notice_funcs+user->num_local, 0, sizeof(privmsg_func_t));
2397
2398     for (x = user->num_local+1; x < num_notice_funcs; x++)
2399        memmove(notice_funcs+x-1, notice_funcs+x, sizeof(privmsg_func_t));
2400
2401     memset(notice_funcs+user->num_local, 0, sizeof(privmsg_func_t));
2402     notice_funcs = realloc(notice_funcs, num_notice_funcs*sizeof(privmsg_func_t));
2403     num_notice_funcs--;
2404 }
2405
2406 void
2407 reg_oper_func(oper_func_t handler)
2408 {
2409     if (of_used == of_size) {
2410         if (of_size) {
2411             of_size <<= 1;
2412             of_list = realloc(of_list, of_size*sizeof(oper_func_t));
2413         } else {
2414             of_size = 8;
2415             of_list = malloc(of_size*sizeof(oper_func_t));
2416         }
2417     }
2418     of_list[of_used++] = handler;
2419 }
2420
2421 static void
2422 call_oper_funcs(struct userNode *user)
2423 {
2424     unsigned int n;
2425     if (IsLocal(user))
2426         return;
2427     for (n=0; n<of_used; n++)
2428         of_list[n](user);
2429 }
2430
2431 static void
2432 send_burst(void)
2433 {
2434     unsigned int i, hop, max_hop=1;
2435     dict_iterator_t it;
2436
2437     /* burst (juped) servers, closest first (except self, which is sent already) */
2438     for (i=0; i<ArrayLength(servers_num); i++)
2439         if (servers_num[i] && servers_num[i]->hops > max_hop)
2440             max_hop = servers_num[i]->hops;
2441     for (hop=1; hop<=max_hop; hop++) {
2442         for (i=0;i<ArrayLength(servers_num);i++) {
2443             if (servers_num[i]
2444                 && (servers_num[i]->hops == hop)
2445                 && (servers_num[i] != self->uplink))
2446                 irc_server(servers_num[i]);
2447         }
2448     }
2449
2450     /* burst local nicks */
2451     for (i=0; i<=self->num_mask; i++)
2452         if (self->users[i])
2453             irc_user(self->users[i]);
2454
2455     /* build dict of unbursted channel names (just copy existing channels) */
2456     unbursted_channels = dict_new();
2457     for (it = dict_first(channels); it; it = iter_next(it))
2458         dict_insert(unbursted_channels, iter_key(it), iter_data(it));
2459 }