basic off-channel support; restructuring how part functions are handled
[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     base_len = sprintf(burst_line, "%s " P10_BURST " %s " FMT_TIME_T " ",
567                        self->numeric, chan->name, chan->timestamp);
568     len = irc_make_chanmode(chan, burst_line+base_len);
569     pos = base_len + len;
570     if (len)
571         burst_line[pos++] = ' ';
572
573     /* dump the users */
574     for (n=0; n<chan->members.used; n++) {
575         mn = chan->members.list[n];
576         if (pos > 500) {
577             burst_line[pos-1] = 0; /* -1 to back up over the space or comma */
578             putsock("%s", burst_line);
579             pos = base_len;
580             last_mode = -1;
581         }
582         memcpy(burst_line+pos, mn->user->numeric, strlen(mn->user->numeric));
583         pos += strlen(mn->user->numeric);
584         if (mn->modes && (mn->modes != last_mode)) {
585             last_mode = mn->modes;
586             burst_line[pos++] = ':';
587             if (last_mode & MODE_CHANOP)
588                 burst_line[pos++] = 'o';
589             if (last_mode & MODE_VOICE)
590                 burst_line[pos++] = 'v';
591         }
592         if ((n+1)<chan->members.used)
593             burst_line[pos++] = ',';
594     }
595     if (chan->banlist.used) {
596         /* dump the bans */
597         if (pos+2+strlen(chan->banlist.list[0]->ban) > 505) {
598             burst_line[pos-1] = 0;
599             putsock("%s", burst_line);
600             pos = base_len;
601         } else {
602             burst_line[pos++] = ' ';
603         }
604
605         burst_line[pos++] = ':';
606         burst_line[pos++] = '%';
607         base_len = pos;
608         for (n=0; n<chan->banlist.used; n++) {
609             bn = chan->banlist.list[n];
610             len = strlen(bn->ban);
611             if (pos+len+1 > 510) {
612                 burst_line[pos-1] = 0; /* -1 to back up over the space or comma */
613                 putsock("%s", burst_line);
614                 pos = base_len;
615             }
616             memcpy(burst_line+pos, bn->ban, len);
617             pos += len;
618             burst_line[pos++] = ' ';
619         }
620     }
621     /* print the last line */
622     burst_line[pos] = 0;
623     putsock("%s", burst_line);
624 }
625
626 void
627 irc_quit(struct userNode *user, const char *message)
628 {
629     putsock("%s " P10_QUIT " :%s", user->numeric, message);
630 }
631
632 void
633 irc_error(const char *to, const char *message)
634 {
635     if (to) {
636         putsock("%s " P10_ERROR " :%s", to, message);
637     } else {
638         putsock(":%s " P10_ERROR " :%s", self->name, message);
639     }
640 }
641
642 void
643 irc_kill(struct userNode *from, struct userNode *target, const char *message)
644 {
645     if (from) {
646         putsock("%s " P10_KILL " %s :%s!%s (%s)",
647                 from->numeric, target->numeric, self->name, from->nick, message);
648     } else {
649         putsock("%s " P10_KILL " %s :%s (%s)",
650                 self->numeric, target->numeric, self->name, message);
651     }
652 }
653
654 void
655 irc_mode(struct userNode *from, struct chanNode *target, const char *modes)
656 {
657     putsock("%s " P10_MODE " %s %s "FMT_TIME_T,
658             (from ? from->numeric : self->numeric),
659             target->name, modes, target->timestamp);
660 }
661
662 void
663 irc_invite(struct userNode *from, struct userNode *who, struct chanNode *to)
664 {
665     putsock("%s " P10_INVITE " %s %s", from->numeric, who->nick, to->name);
666 }
667
668 void
669 irc_join(struct userNode *who, struct chanNode *what)
670 {
671     if (what->members.used == 1) {
672         putsock("%s " P10_CREATE " %s %lu",
673                 who->numeric, what->name, what->timestamp);
674     } else {
675         putsock("%s " P10_JOIN " %s %lu", who->numeric, what->name, what->timestamp);
676     }
677 }
678
679 void
680 irc_kick(struct userNode *who, struct userNode *target, struct chanNode *channel, const char *msg)
681 {
682     const char *numeric;
683     struct modeNode *mn = GetUserMode(channel, who);
684     numeric = ((!mn && off_channel) || (mn->modes & MODE_CHANOP)) ? who->numeric : self->numeric;
685     putsock("%s " P10_KICK " %s %s :%s",
686             numeric, channel->name, target->numeric, msg);
687 }
688
689 void
690 irc_stats(struct userNode *from, struct server *target, char type)
691 {
692     putsock("%s " P10_STATS " %c :%s", from->numeric, type, target->numeric);
693 }
694
695 void
696 irc_svsnick(struct userNode *from, struct userNode *target, const char *newnick)
697 {
698     putsock("%s " P10_SVSNICK " %s %s "FMT_TIME_T, from->uplink->numeric, target->numeric, newnick, now);
699 }
700
701 void
702 irc_part(struct userNode *who, struct chanNode *what, const char *reason)
703 {
704     if (reason) {
705         putsock("%s " P10_PART " %s :%s", who->numeric, what->name, reason);
706     } else {
707         putsock("%s " P10_PART " %s", who->numeric, what->name);
708     }
709 }
710
711 void
712 irc_topic(struct userNode *who, struct chanNode *what, const char *topic)
713 {
714     putsock("%s " P10_TOPIC " %s :%s", who->numeric, what->name, topic);
715 }
716
717 void
718 irc_raw(const char *what)
719 {
720     putsock("%s", what);
721 }
722
723 void
724 irc_numeric(struct userNode *user, unsigned int num, const char *format, ...)
725 {
726     va_list arg_list;
727     char buffer[MAXLEN];
728     va_start(arg_list, format);
729     vsnprintf(buffer, MAXLEN-2, format, arg_list);
730     buffer[MAXLEN-1] = 0;
731     putsock(":%s %03d %s %s", self->name, num, user->nick, buffer);
732 }
733
734 static void send_burst(void);
735
736 static void
737 change_nicklen(int new_nicklen)
738 {
739     unsigned int nn;
740     char new_nick[NICKLEN+1];
741     struct userNode *user;
742
743     nicklen = new_nicklen;
744     /* fix up any users we have here */
745     for (nn=0; nn<=self->num_mask; nn++) {
746         if (!(user = self->users[nn]))
747             continue;
748         safestrncpy(new_nick, user->nick, sizeof(new_nick));
749         new_nick[nicklen] = 0;
750         NickChange(user, new_nick, 1);
751     }
752 }
753
754 static CMD_FUNC(cmd_server)
755 {
756     struct server *srv;
757     const char *str;
758
759     if (argc < 8)
760         return 0;
761     if (origin) {
762         /* another server introduced us */
763         srv = AddServer(GetServerH(origin), argv[1], atoi(argv[2]), atoi(argv[3]), atoi(argv[4]), argv[6], argv[argc-1]);
764         if (!srv)
765             return 0;
766         srv->self_burst = argv[5][0] == 'J';
767         srv->burst = 1;
768     } else {
769         /* this must be our uplink */
770         srv = self->uplink = AddServer(self, argv[1], atoi(argv[2]), atoi(argv[3]), atoi(argv[4]), argv[6], argv[argc-1]);
771         if (!srv)
772             return 0;
773         srv->self_burst = argv[5][0] == 'J';
774         srv->burst = 1;
775         if ((argv[7][0] == '+') && !force_n2k) {
776             log_module(MAIN_LOG, LOG_WARNING, "Got Undernet-style SERVER message but \"force_n2k\" not on.");
777         }
778         send_burst();
779     }
780
781     /* Fix up our timestamps if necessary. */
782     if (srv->boot <= PREHISTORY) {
783         /* Server from the mists of time.. */
784         if (srv->hops == 1) {
785             log_module(MAIN_LOG, LOG_ERROR, "Server %s claims to have booted at time "FMT_TIME_T".  This is absurd.", srv->name, srv->boot);
786         }
787     } else if ((str = conf_get_data("server/reliable_clock", RECDB_QSTRING))
788                && enabled_string(str)) {
789         /* If we have a reliable clock, we just keep our current time. */
790     } else {
791         if (srv->boot <= self->boot) {
792             /* The other server is older than us.  Accept their timestamp.
793              * Alternately, we are same age, but we accept their time
794              * since we are linking to them. */
795             self->boot = srv->boot;
796             ioset_set_time(srv->link);
797         }
798     }
799     if (srv == self->uplink) {
800         extern time_t burst_begin;
801         burst_begin = now;
802     }
803     return 1;
804 }
805
806 static CMD_FUNC(cmd_eob)
807 {
808     struct server *sender;
809     dict_iterator_t it;
810     unsigned int ii;
811
812     if (!(sender = GetServerH(origin)))
813         return 0;
814     if (sender == self->uplink) {
815         cManager.uplink->state = CONNECTED;
816         for (it = dict_first(unbursted_channels); it; it = iter_next(it))
817             irc_burst(iter_data(it));
818         dict_delete(unbursted_channels);
819         unbursted_channels = NULL;
820         irc_eob();
821         irc_eob_ack();
822     }
823     sender->self_burst = 0;
824     recalc_bursts(sender);
825     for (ii=0; ii<slf_used; ii++)
826         slf_list[ii](sender);
827     return 1;
828 }
829
830 static CMD_FUNC(cmd_eob_ack)
831 {
832     extern time_t burst_begin;
833
834     if (GetServerH(origin) == self->uplink) {
835         burst_length = now - burst_begin;
836         self->self_burst = self->burst = 0;
837     }
838     cManager.uplink->state = CONNECTED;
839     return 1;
840 }
841
842 static CMD_FUNC(cmd_ping)
843 {
844     struct server *srv;
845     struct userNode *un;
846
847     if(argc > 3)
848         irc_pong(argv[2], argv[1]);
849     else if ((srv = GetServerH(origin)))
850         irc_pong(self->name, srv->numeric);
851     else if ((un = GetUserH(origin)))
852         irc_pong(self->name, un->numeric);
853     else
854         irc_pong(self->name, origin);
855
856     timeq_del(0, timed_send_ping, 0, TIMEQ_IGNORE_WHEN|TIMEQ_IGNORE_DATA);
857     timeq_del(0, timed_ping_timeout, 0, TIMEQ_IGNORE_WHEN|TIMEQ_IGNORE_DATA);
858     timeq_add(now + ping_freq, timed_send_ping, 0);
859     received_ping();
860     return 1;
861 }
862
863 static CMD_FUNC(cmd_error_nick)
864 {
865     /* Go back to original IRC length .. and try to reconnect :/ */
866     change_nicklen(9);
867     irc_squit(self, "Got erroneous nickname, truncating nicks.", NULL);
868     return 1;
869 }
870
871 struct create_desc {
872     struct userNode *user;
873     time_t when;
874 };
875
876 static void
877 join_helper(struct chanNode *chan, void *data)
878 {
879     struct create_desc *cd = data;
880     AddChannelUser(cd->user, chan);
881 }
882
883 static void
884 create_helper(char *name, void *data)
885 {
886     struct create_desc *cd = data;
887
888     if (!strcmp(name, "0")) {
889         while (cd->user->channels.used > 0)
890             DelChannelUser(cd->user, cd->user->channels.list[0]->channel, 0, 0);
891         return;
892     }
893
894     AddChannelUser(cd->user, AddChannel(name, cd->when, NULL, NULL));
895 }
896
897 static CMD_FUNC(cmd_create)
898 {
899     struct create_desc cd;
900     struct userNode *user;
901
902     if ((argc < 3) || !(user = GetUserH(origin)))
903         return 0;
904     cd.user = user;
905     cd.when = atoi(argv[2]);
906     parse_foreach(argv[1], join_helper, create_helper, NULL, NULL, &cd);
907     return 1;
908 }
909
910 static CMD_FUNC(cmd_join)
911 {
912     struct create_desc cd;
913
914     if (!(cd.user = GetUserH(origin)))
915         return 0;
916     if (argc < 2)
917         return 0;
918     else if (argc < 3)
919         cd.when = now;
920     else
921         cd.when = atoi(argv[2]);
922     parse_foreach(argv[1], join_helper, create_helper, NULL, NULL, &cd);
923     return 1;
924 }
925
926 static CMD_FUNC(cmd_pong)
927 {
928     if (argc < 3)
929         return 0;
930     if (!strcmp(argv[2], self->name)) {
931         timeq_del(0, timed_send_ping, 0, TIMEQ_IGNORE_WHEN|TIMEQ_IGNORE_DATA);
932         timeq_del(0, timed_ping_timeout, 0, TIMEQ_IGNORE_WHEN|TIMEQ_IGNORE_DATA);
933         timeq_add(now + ping_freq, timed_send_ping, 0);
934         received_ping();
935     }
936     return 1;
937 }
938
939 static CMD_FUNC(cmd_nick)
940 {
941     struct userNode *user;
942     if ((user = GetUserH(origin))) {
943         /* nick change (since the source is a user numeric) */
944         if (argc < 2)
945             return 0;
946         NickChange(user, argv[1], 1);
947     } else {
948         struct server *serv;
949         char modes[MAXLEN];
950         /* new nick */
951         if (argc < 9)
952             return 0;
953         serv = GetServerH(origin);
954         if (argc > 9)
955             unsplit_string(argv+6, argc-9, modes);
956         else
957             strcpy(modes, "+");
958         AddUser(serv, argv[1], argv[4], argv[5], modes, argv[argc-2], argv[argc-1], atoi(argv[3]), argv[argc-3]);
959     }
960     return 1;
961 }
962
963 static CMD_FUNC(cmd_account)
964 {
965     struct userNode *user;
966
967     if ((argc < 3) || !origin || !GetServerH(origin))
968         return 0; /* Origin must be server. */
969     user = GetUserN(argv[1]);
970     if (!user)
971         return 1; /* A QUIT probably passed the ACCOUNT. */
972     call_account_func(user, argv[2]);
973     return 1;
974 }
975
976 static CMD_FUNC(cmd_burst)
977 {
978     extern int rel_age;
979     char modes[MAXLEN], *members = "", *banlist = NULL;
980     unsigned int next = 3, res = 1;
981     struct chanNode *cNode;
982     struct userNode *un;
983     struct modeNode *mNode;
984     long mode;
985     char *user, *end, sep;
986     time_t in_timestamp;
987
988     if (argc < 3)
989         return 0;
990     modes[0] = 0;
991     while (next < argc) {
992         switch (argv[next][0]) {
993         case '+': {
994             const char *pos;
995             int n_modes;
996             for (pos=argv[next], n_modes = 1; *pos; pos++)
997                 if ((*pos == 'k') || (*pos == 'l'))
998                     n_modes++;
999             unsplit_string(argv+next, n_modes, modes);
1000             next += n_modes;
1001             break;
1002         }
1003         case '%': banlist = argv[next++]+1; break;
1004         default: members = argv[next++]; break;
1005         }
1006     }
1007
1008     in_timestamp = atoi(argv[2]);
1009     if ((cNode = dict_find(unbursted_channels, argv[1], NULL))) {
1010         cNode->timestamp = in_timestamp;
1011         dict_remove(unbursted_channels, cNode->name);
1012         irc_burst(cNode);
1013     }
1014     cNode = AddChannel(argv[1], in_timestamp, modes, banlist);
1015
1016     /* Burst channel members in now. */
1017     for (user = members, sep = *members, mode = 0; sep; user = end) {
1018         for (end = user + 3; isalnum(*end) || *end == '[' || *end == ']'; end++) ;
1019         sep = *end++; end[-1] = 0;
1020         if (sep == ':') {
1021             mode = 0;
1022             while ((sep = *end++)) {
1023                 if (sep == 'o')
1024                     mode |= MODE_CHANOP;
1025                 else if (sep == 'v')
1026                     mode |= MODE_VOICE;
1027                 else
1028                     break;
1029             }
1030             if (rel_age < 0)
1031                 mode = 0;
1032         }
1033         if (!(un = GetUserN(user))) {
1034             res = 0;
1035             continue;
1036         }
1037         if ((mNode = AddChannelUser(un, cNode)))
1038             mNode->modes = mode;
1039     }
1040
1041     return res;
1042 }
1043
1044 static CMD_FUNC(cmd_mode)
1045 {
1046     struct chanNode *cn;
1047     struct userNode *un;
1048
1049     if (argc < 3)
1050         return 0;
1051     if (!IsChannelName(argv[1])) {
1052         un = GetUserH(argv[1]);
1053         if (!un) {
1054             log_module(MAIN_LOG, LOG_ERROR, "Unable to find user %s whose mode is changing.", argv[1]);
1055             return 0;
1056         }
1057         mod_usermode(un, argv[2]);
1058         return 1;
1059     }
1060
1061     if (!(cn = GetChannel(argv[1]))) {
1062         log_module(MAIN_LOG, LOG_ERROR, "Unable to find channel %s whose mode is changing.", argv[1]);
1063         return 0;
1064     }
1065     if ((un = GetUserH(origin))) {
1066         struct modeNode *mn;
1067         /* Update idle time for person setting the mode */
1068         if ((mn = GetUserMode(cn, un)))
1069             mn->idle_since = now;
1070     } else {
1071         /* If it came from a server, reset timestamp to re-sync. */
1072         cn->timestamp = atoi(argv[argc-1]);
1073     }
1074
1075     return mod_chanmode(un, cn, argv+2, argc-2, MCP_ALLOW_OVB|MCP_FROM_SERVER|(un ? MC_NOTIFY : 0));
1076 }
1077
1078 static CMD_FUNC(cmd_opmode)
1079 {
1080     struct chanNode *cn;
1081     struct userNode *un;
1082
1083     if (argc < 3)
1084         return 0;
1085
1086     if (!(cn = GetChannel(argv[1]))) {
1087         log_module(MAIN_LOG, LOG_ERROR, "Unable to find channel %s whose mode is changing.", argv[1]);
1088         return 0;
1089     }
1090     if (!(un = GetUserH(origin))) {
1091         log_module(MAIN_LOG, LOG_ERROR, "Unable to find user %s requesting OPMODE.", origin);
1092         return 0;
1093     }
1094     if (!IsOper(un)) {
1095         log_module(MAIN_LOG, LOG_ERROR, "Non-privileged user %s using OPMODE.", un->nick);
1096         return 0;
1097     }
1098
1099     return mod_chanmode(un, cn, argv+2, argc-2, MCP_ALLOW_OVB|MCP_FROM_SERVER); /* do NOT announce opmode locally */
1100 }
1101
1102 static int clear_chanmode(struct chanNode *channel, const char *modes);
1103
1104 static CMD_FUNC(cmd_clearmode)
1105 {
1106     struct chanNode *cn;
1107     struct userNode *un;
1108
1109     if (argc < 3)
1110         return 0;
1111
1112     if (!(cn = GetChannel(argv[1]))) {
1113         log_module(MAIN_LOG, LOG_ERROR, "Unable to find channel %s whose mode is changing.", argv[1]);
1114         return 0;
1115     }
1116     if (!(un = GetUserH(origin))) {
1117         log_module(MAIN_LOG, LOG_ERROR, "Unable to find user %s requesting CLEARMODE.", origin);
1118         return 0;
1119     }
1120     if (!IsOper(un)) {
1121         log_module(MAIN_LOG, LOG_ERROR, "Non-privileged user %s using CLEARMODE.", un->nick);
1122         return 0;
1123     }
1124
1125     return clear_chanmode(cn, argv[2]);
1126 }
1127
1128 static CMD_FUNC(cmd_topic)
1129 {
1130     struct chanNode *cn;
1131     time_t chan_ts, topic_ts;
1132
1133     if (argc < 3)
1134         return 0;
1135     if (!(cn = GetChannel(argv[1]))) {
1136         log_module(MAIN_LOG, LOG_ERROR, "Unable to find channel %s whose topic is being set", argv[1]);
1137         return 0;
1138     }
1139     if (argc >= 5) {
1140         /* Looks like an Asuka style topic burst. */
1141         chan_ts = atoi(argv[2]);
1142         topic_ts = atoi(argv[3]);
1143     } else {
1144         chan_ts = cn->timestamp;
1145         topic_ts = now;
1146     }
1147     SetChannelTopic(cn, GetUserH(origin), argv[argc-1], 0);
1148     cn->topic_time = topic_ts;
1149     return 1;
1150 }
1151
1152 static CMD_FUNC(cmd_num_topic)
1153 {
1154     static struct chanNode *cn;
1155
1156     if (!argv[0])
1157         return 0; /* huh? */
1158     if (argv[2]) {
1159         cn = GetChannel(argv[2]);
1160         if (!cn) {
1161             log_module(MAIN_LOG, LOG_ERROR, "Unable to find channel %s in topic reply", argv[2]);
1162             return 0;
1163         }
1164     } else
1165         return 0;
1166
1167     switch (atoi(argv[0])) {
1168     case 331:
1169         cn->topic_time = 0;
1170         break;  /* no topic */
1171     case 332:
1172         if (argc < 4)
1173             return 0;
1174         safestrncpy(cn->topic, unsplit_string(argv+3, argc-3, NULL), sizeof(cn->topic));
1175         break;
1176     case 333:
1177         if (argc < 5)
1178             return 0;
1179         safestrncpy(cn->topic_nick, argv[3], sizeof(cn->topic_nick));
1180         cn->topic_time = atoi(argv[4]);
1181         break;
1182     default:
1183         return 0; /* should never happen */
1184     }
1185     return 1;
1186 }
1187
1188 static CMD_FUNC(cmd_num_gline)
1189 {
1190     if (argc < 6)
1191         return 0;
1192     gline_add(origin, argv[3], atoi(argv[4])-now, argv[5], now, 0);
1193     return 1;
1194 }
1195
1196 static CMD_FUNC(cmd_quit)
1197 {
1198     struct userNode *user;
1199     if (argc < 2)
1200         return 0;
1201     /* Sometimes we get a KILL then a QUIT or the like, so we don't want to
1202      * call DelUser unless we have the user in our grasp. */
1203     if ((user = GetUserH(origin)))
1204         DelUser(user, NULL, false, argv[1]);
1205     return 1;
1206 }
1207
1208 static CMD_FUNC(cmd_kill)
1209 {
1210     struct userNode *user;
1211     if (argc < 2)
1212         return 0;
1213     user = GetUserN(argv[1]);
1214     if (!user) {
1215         /* If we get a KILL for a non-existent user, it could be a
1216          * Ghost response to a KILL we sent out earlier.  So we only
1217          * whine if the target is local.
1218          */
1219         if (!strncmp(argv[1], self->numeric, strlen(self->numeric)))
1220             log_module(MAIN_LOG, LOG_ERROR, "Unable to find kill victim %s", argv[1]);
1221         return 0;
1222     }
1223
1224     if (IsLocal(user) && IsService(user)) {
1225         /* TODO: rate limit this so silly things don't happen. */
1226         ReintroduceUser(user);
1227         return 1;
1228     }
1229
1230     DelUser(user, NULL, false, argv[2]);
1231     return 1;
1232 }
1233
1234 static CMD_FUNC(cmd_part)
1235 {
1236     struct userNode *user;
1237
1238     if (argc < 2)
1239         return 0;
1240     user = GetUserH(origin);
1241     if (!user)
1242         return 0;
1243     parse_foreach(argv[1], part_helper, NULL, NULL, NULL, user);
1244     return 1;
1245 }
1246
1247 static CMD_FUNC(cmd_kick)
1248 {
1249     if (argc < 3)
1250         return 0;
1251     ChannelUserKicked(GetUserH(origin), GetUserN(argv[2]), GetChannel(argv[1]));
1252     return 1;
1253 }
1254
1255 static CMD_FUNC(cmd_squit)
1256 {
1257     struct server *server;
1258
1259     if (argc < 4)
1260         return 0;
1261     if (!(server = GetServerH(argv[1])))
1262         return 0;
1263
1264     if (server == self->uplink) {
1265         /* Force a reconnect to the currently selected server. */
1266         cManager.uplink->tries = 0;
1267         log_module(MAIN_LOG, LOG_INFO, "Squitting from uplink: %s", argv[3]);
1268         close_socket();
1269         return 1;
1270     }
1271
1272     DelServer(server, 0, argv[3]);
1273     return 1;
1274 }
1275
1276 static CMD_FUNC(cmd_privmsg)
1277 {
1278     struct privmsg_desc pd;
1279     if (argc != 3)
1280         return 0;
1281     pd.user = GetUserH(origin);
1282     if (!pd.user || (IsGagged(pd.user) && !IsOper(pd.user)))
1283         return 1;
1284     pd.is_notice = 0;
1285     pd.text = argv[2];
1286     parse_foreach(argv[1], privmsg_chan_helper, NULL, privmsg_user_helper, privmsg_invalid, &pd);
1287     return 1;
1288 }
1289
1290 static CMD_FUNC(cmd_notice)
1291 {
1292     struct privmsg_desc pd;
1293     if (argc != 3)
1294         return 0;
1295     pd.user = GetUserH(origin);
1296     if (!pd.user || (IsGagged(pd.user) && !IsOper(pd.user)))
1297         return 1;
1298     pd.is_notice = 1;
1299     pd.text = argv[2];
1300     parse_foreach(argv[1], privmsg_chan_helper, NULL, privmsg_user_helper, privmsg_invalid, &pd);
1301     return 1;
1302 }
1303
1304 static CMD_FUNC(cmd_away)
1305 {
1306     struct userNode *uNode;
1307
1308     uNode = GetUserH(origin);
1309     if (!uNode)
1310         return 1;
1311     if (argc < 2)
1312         uNode->modes &= ~FLAGS_AWAY;
1313     else
1314         uNode->modes |= FLAGS_AWAY;
1315     return 1;
1316 }
1317
1318 static CMD_FUNC(cmd_gline)
1319 {
1320     if (argc < 3)
1321         return 0;
1322     if (argv[2][0] == '+') {
1323         if (argc < 5)
1324             return 0;
1325         gline_add(origin, argv[2]+1, strtoul(argv[3], NULL, 0), argv[argc-1], now, 0);
1326         return 1;
1327     } else if (argv[2][0] == '-') {
1328         gline_remove(argv[2]+1, 0);
1329         return 1;
1330     } else
1331         return 0;
1332 }
1333
1334 static CMD_FUNC(cmd_svsnick)
1335 {
1336     struct userNode *target, *dest;
1337     if ((argc < 4)
1338         || !(target = GetUserN(argv[1]))
1339         || !IsLocal(target)
1340         || (dest = GetUserH(argv[2])))
1341         return 0;
1342     NickChange(target, argv[2], 0);
1343     return 1;
1344 }
1345
1346 static oper_func_t *of_list;
1347 static unsigned int of_size = 0, of_used = 0;
1348
1349 void
1350 free_user(struct userNode *user)
1351 {
1352     free(user->nick);
1353     free(user);
1354 }
1355
1356 static void
1357 parse_cleanup(void)
1358 {
1359     unsigned int nn;
1360     free(of_list);
1361     free(privmsg_funcs);
1362     free(notice_funcs);
1363     free(mcf_list);
1364     dict_delete(irc_func_dict);
1365     for (nn=0; nn<dead_users.used; nn++)
1366         free_user(dead_users.list[nn]);
1367     userList_clean(&dead_users);
1368 }
1369
1370 static void
1371 p10_conf_reload(void) {
1372     hidden_host_suffix = conf_get_data("server/hidden_host", RECDB_QSTRING);
1373 }
1374
1375 static void
1376 remove_unbursted_channel(struct chanNode *cNode) {
1377     if (unbursted_channels)
1378         dict_remove(unbursted_channels, cNode->name);
1379 }
1380
1381 void
1382 init_parse(void)
1383 {
1384     const char *str, *desc;
1385     int numnick, usermask, max_users;
1386     char numer[COMBO_NUMERIC_LEN+1];
1387     extern char *his_servername;
1388     extern char *his_servercomment;
1389
1390     /* read config items */
1391     str = conf_get_data("server/ping_freq", RECDB_QSTRING);
1392     ping_freq = str ? ParseInterval(str) : 120;
1393     str = conf_get_data("server/ping_timeout", RECDB_QSTRING);
1394     ping_timeout = str ? ParseInterval(str) : 30;
1395     str = conf_get_data("server/force_n2k", RECDB_QSTRING);
1396     force_n2k = str ? enabled_string(str) : 1;
1397     str = conf_get_data("server/numeric", RECDB_QSTRING);
1398     if (!str) {
1399         log_module(MAIN_LOG, LOG_ERROR, "No server/numeric entry in config file.");
1400         exit(1);
1401     }
1402     numnick = atoi(str);
1403     str = conf_get_data("server/max_users", RECDB_QSTRING);
1404     max_users = str ? atoi(str) : 4096;
1405     for (usermask = 4; usermask < max_users; usermask <<= 1) ;
1406     usermask--;
1407     if ((numnick < 64) && (usermask < 4096) && !force_n2k)
1408         inttobase64(numer, (numnick << 12) + (usermask & 0x00fff), 3);
1409     else
1410         inttobase64(numer, (numnick << 18) + (usermask & 0x3ffff), 5);
1411
1412     str = conf_get_data("server/his_servername", RECDB_QSTRING);
1413     his_servername = str ? strdup(str) : NULL;
1414     str = conf_get_data("server/his_servercomment", RECDB_QSTRING);
1415     his_servercomment = str ? strdup(str) : NULL;
1416
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
2139     mn = GetUserMode(channel, who);
2140     if ((!mn && off_channel) || (mn->modes & MODE_CHANOP))
2141         chbuf.is_chanop = 1;
2142
2143     /* First remove modes */
2144     chbuf.is_add = 0;
2145     if (change->modes_clear) {
2146         if (mode != '-')
2147             chbuf.modes[chbuf.modes_used++] = mode = '-';
2148 #define DO_MODE_CHAR(BIT, CHAR) if (change->modes_clear & MODE_##BIT) chbuf.modes[chbuf.modes_used++] = CHAR
2149         DO_MODE_CHAR(PRIVATE, 'p');
2150         DO_MODE_CHAR(SECRET, 's');
2151         DO_MODE_CHAR(MODERATED, 'm');
2152         DO_MODE_CHAR(TOPICLIMIT, 't');
2153         DO_MODE_CHAR(INVITEONLY, 'i');
2154         DO_MODE_CHAR(NOPRIVMSGS, 'n');
2155         DO_MODE_CHAR(LIMIT, 'l');
2156         DO_MODE_CHAR(DELAYJOINS, 'D');
2157         DO_MODE_CHAR(REGONLY, 'r');
2158         DO_MODE_CHAR(NOCOLORS, 'c');
2159         DO_MODE_CHAR(NOCTCPS, 'C');
2160 #undef DO_MODE_CHAR
2161         if (change->modes_clear & channel->modes & MODE_KEY)
2162             mod_chanmode_append(&chbuf, 'k', channel->key);
2163     }
2164     for (arg = 0; arg < change->argc; ++arg) {
2165         if (!(change->args[arg].mode & MODE_REMOVE))
2166             continue;
2167         if (mode != '-')
2168             chbuf.modes[chbuf.modes_used++] = mode = '-';
2169         switch (change->args[arg].mode & ~MODE_REMOVE) {
2170         case MODE_BAN:
2171             mod_chanmode_append(&chbuf, 'b', change->args[arg].hostmask);
2172             break;
2173         default:
2174             if (change->args[arg].mode & MODE_CHANOP)
2175                 mod_chanmode_append(&chbuf, 'o', change->args[arg].member->user->numeric);
2176             if (change->args[arg].mode & MODE_VOICE)
2177                 mod_chanmode_append(&chbuf, 'v', change->args[arg].member->user->numeric);
2178             break;
2179         }
2180     }
2181
2182     /* Then set them */
2183     chbuf.is_add = 1;
2184     if (change->modes_set) {
2185         if (mode != '+')
2186             chbuf.modes[chbuf.modes_used++] = mode = '+';
2187 #define DO_MODE_CHAR(BIT, CHAR) if (change->modes_set & MODE_##BIT) chbuf.modes[chbuf.modes_used++] = CHAR
2188         DO_MODE_CHAR(PRIVATE, 'p');
2189         DO_MODE_CHAR(SECRET, 's');
2190         DO_MODE_CHAR(MODERATED, 'm');
2191         DO_MODE_CHAR(TOPICLIMIT, 't');
2192         DO_MODE_CHAR(INVITEONLY, 'i');
2193         DO_MODE_CHAR(NOPRIVMSGS, 'n');
2194         DO_MODE_CHAR(DELAYJOINS, 'D');
2195         DO_MODE_CHAR(REGONLY, 'r');
2196         DO_MODE_CHAR(NOCOLORS, 'c');
2197         DO_MODE_CHAR(NOCTCPS, 'C');
2198 #undef DO_MODE_CHAR
2199         if(change->modes_set & MODE_KEY)
2200             mod_chanmode_append(&chbuf, 'k', change->new_key);
2201         if(change->modes_set & MODE_LIMIT) {
2202             sprintf(int_buff, "%d", change->new_limit);
2203             mod_chanmode_append(&chbuf, 'l', int_buff);
2204         }
2205     }
2206     for (arg = 0; arg < change->argc; ++arg) {
2207         if (change->args[arg].mode & MODE_REMOVE)
2208             continue;
2209         if (mode != '+')
2210             chbuf.modes[chbuf.modes_used++] = mode = '+';
2211         switch (change->args[arg].mode) {
2212         case MODE_BAN:
2213             mod_chanmode_append(&chbuf, 'b', change->args[arg].hostmask);
2214             break;
2215         default:
2216             if (change->args[arg].mode & MODE_CHANOP)
2217                 mod_chanmode_append(&chbuf, 'o', change->args[arg].member->user->numeric);
2218             if (change->args[arg].mode & MODE_VOICE)
2219                 mod_chanmode_append(&chbuf, 'v', change->args[arg].member->user->numeric);
2220             break;
2221         }
2222     }
2223
2224     /* Flush the buffer and apply changes locally */
2225     if (chbuf.modes_used > 0) {
2226         memcpy(chbuf.modes + chbuf.modes_used, chbuf.args, chbuf.args_used);
2227         chbuf.modes[chbuf.modes_used + chbuf.args_used] = '\0';
2228         irc_mode((chbuf.is_chanop ? chbuf.actor : NULL), chbuf.channel, chbuf.modes);
2229     }
2230     mod_chanmode_apply(who, channel, change);
2231 }
2232
2233 char *
2234 mod_chanmode_format(struct mod_chanmode *change, char *outbuff)
2235 {
2236     unsigned int used = 0;
2237     assert(change->argc <= change->alloc_argc);
2238     if (change->modes_clear) {
2239         outbuff[used++] = '-';
2240 #define DO_MODE_CHAR(BIT, CHAR) if (change->modes_clear & MODE_##BIT) outbuff[used++] = CHAR
2241         DO_MODE_CHAR(PRIVATE, 'p');
2242         DO_MODE_CHAR(SECRET, 's');
2243         DO_MODE_CHAR(MODERATED, 'm');
2244         DO_MODE_CHAR(TOPICLIMIT, 't');
2245         DO_MODE_CHAR(INVITEONLY, 'i');
2246         DO_MODE_CHAR(NOPRIVMSGS, 'n');
2247         DO_MODE_CHAR(LIMIT, 'l');
2248         DO_MODE_CHAR(KEY, 'k');
2249         DO_MODE_CHAR(DELAYJOINS, 'D');
2250         DO_MODE_CHAR(REGONLY, 'r');
2251         DO_MODE_CHAR(NOCOLORS, 'c');
2252         DO_MODE_CHAR(NOCTCPS, 'C');
2253 #undef DO_MODE_CHAR
2254     }
2255     if (change->modes_set) {
2256         outbuff[used++] = '+';
2257 #define DO_MODE_CHAR(BIT, CHAR) if (change->modes_set & MODE_##BIT) outbuff[used++] = CHAR
2258         DO_MODE_CHAR(PRIVATE, 'p');
2259         DO_MODE_CHAR(SECRET, 's');
2260         DO_MODE_CHAR(MODERATED, 'm');
2261         DO_MODE_CHAR(TOPICLIMIT, 't');
2262         DO_MODE_CHAR(INVITEONLY, 'i');
2263         DO_MODE_CHAR(NOPRIVMSGS, 'n');
2264         DO_MODE_CHAR(DELAYJOINS, 'D');
2265         DO_MODE_CHAR(REGONLY, 'r');
2266         DO_MODE_CHAR(NOCOLORS, 'c');
2267         DO_MODE_CHAR(NOCTCPS, 'C');
2268 #undef DO_MODE_CHAR
2269         switch (change->modes_set & (MODE_KEY|MODE_LIMIT)) {
2270         case MODE_KEY|MODE_LIMIT:
2271             used += sprintf(outbuff+used, "lk %d %s", change->new_limit, change->new_key);
2272             break;
2273         case MODE_KEY:
2274             used += sprintf(outbuff+used, "k %s", change->new_key);
2275             break;
2276         case MODE_LIMIT:
2277             used += sprintf(outbuff+used, "l %d", change->new_limit);
2278             break;
2279         }
2280     }
2281     outbuff[used] = 0;
2282     return outbuff;
2283 }
2284
2285 static int
2286 clear_chanmode(struct chanNode *channel, const char *modes)
2287 {
2288     unsigned int remove;
2289
2290     for (remove = 0; *modes; modes++) {
2291         switch (*modes) {
2292         case 'o': remove |= MODE_CHANOP; break;
2293         case 'v': remove |= MODE_VOICE; break;
2294         case 'p': remove |= MODE_PRIVATE; break;
2295         case 's': remove |= MODE_SECRET; break;
2296         case 'm': remove |= MODE_MODERATED; break;
2297         case 't': remove |= MODE_TOPICLIMIT; break;
2298         case 'i': remove |= MODE_INVITEONLY; break;
2299         case 'n': remove |= MODE_NOPRIVMSGS; break;
2300         case 'k':
2301             remove |= MODE_KEY;
2302             channel->key[0] = '\0';
2303             break;
2304         case 'l':
2305             remove |= MODE_LIMIT;
2306             channel->limit = 0;
2307             break;
2308         case 'b': remove |= MODE_BAN; break;
2309         case 'D': remove |= MODE_DELAYJOINS; break;
2310         case 'r': remove |= MODE_REGONLY; break;
2311         case 'c': remove |= MODE_NOCOLORS;
2312         case 'C': remove |= MODE_NOCTCPS; break;
2313         }
2314     }
2315
2316     if (!remove)
2317         return 1;
2318
2319     /* Remove simple modes. */
2320     channel->modes &= ~remove;
2321
2322     /* If removing bans, kill 'em all. */
2323     if ((remove & MODE_BAN) && channel->banlist.used) {
2324         unsigned int i;
2325         for (i=0; i<channel->banlist.used; i++)
2326             free(channel->banlist.list[i]);
2327         channel->banlist.used = 0;
2328     }
2329
2330     /* Remove member modes. */
2331     if ((remove & (MODE_CHANOP | MODE_VOICE)) && channel->members.used) {
2332         int mask = ~(remove & (MODE_CHANOP | MODE_VOICE));
2333         unsigned int i;
2334
2335         for (i = 0; i < channel->members.used; i++)
2336             channel->members.list[i]->modes &= mask;
2337     }
2338
2339     return 1;
2340 }
2341
2342 void
2343 reg_privmsg_func(struct userNode *user, privmsg_func_t handler)
2344 {
2345     unsigned int numeric = user->num_local;
2346     if (numeric >= num_privmsg_funcs) {
2347         int newnum = numeric + 8;
2348         privmsg_funcs = realloc(privmsg_funcs, newnum*sizeof(privmsg_func_t));
2349         memset(privmsg_funcs+num_privmsg_funcs, 0, (newnum-num_privmsg_funcs)*sizeof(privmsg_func_t));
2350         num_privmsg_funcs = newnum;
2351     }
2352     if (privmsg_funcs[numeric])
2353         log_module(MAIN_LOG, LOG_WARNING, "re-registering new privmsg handler for numeric %d", numeric);
2354     privmsg_funcs[numeric] = handler;
2355 }
2356
2357 void
2358 unreg_privmsg_func(struct userNode *user, privmsg_func_t handler)
2359 {
2360     unsigned int x;
2361
2362     if (!user || handler)
2363       return; /* this really only works with users */
2364
2365     memset(privmsg_funcs+user->num_local, 0, sizeof(privmsg_func_t));
2366
2367     for (x = user->num_local+1; x < num_privmsg_funcs; x++) 
2368        memmove(privmsg_funcs+x-1, privmsg_funcs+x, sizeof(privmsg_func_t));
2369     
2370     privmsg_funcs = realloc(privmsg_funcs, num_privmsg_funcs*sizeof(privmsg_func_t)); 
2371     num_privmsg_funcs--;
2372 }
2373
2374
2375 void
2376 reg_notice_func(struct userNode *user, privmsg_func_t handler)
2377 {
2378     unsigned int numeric = user->num_local;
2379     if (numeric >= num_notice_funcs) {
2380         int newnum = numeric + 8;
2381         notice_funcs = realloc(notice_funcs, newnum*sizeof(privmsg_func_t));
2382         memset(notice_funcs+num_notice_funcs, 0, (newnum-num_notice_funcs)*sizeof(privmsg_func_t));
2383         num_notice_funcs = newnum;
2384     }
2385     if (notice_funcs[numeric])
2386         log_module(MAIN_LOG, LOG_WARNING, "re-registering new notice handler for numeric %d", numeric);
2387     notice_funcs[numeric] = handler;
2388 }
2389
2390 void
2391 unreg_notice_func(struct userNode *user, privmsg_func_t handler)
2392 {
2393     unsigned int x;
2394
2395     if (!user || handler)
2396           return; /* this really only works with users */
2397
2398     memset(notice_funcs+user->num_local, 0, sizeof(privmsg_func_t));
2399
2400     for (x = user->num_local+1; x < num_notice_funcs; x++)
2401        memmove(notice_funcs+x-1, notice_funcs+x, sizeof(privmsg_func_t));
2402
2403     memset(notice_funcs+user->num_local, 0, sizeof(privmsg_func_t));
2404     notice_funcs = realloc(notice_funcs, num_notice_funcs*sizeof(privmsg_func_t));
2405     num_notice_funcs--;
2406 }
2407
2408 void
2409 reg_oper_func(oper_func_t handler)
2410 {
2411     if (of_used == of_size) {
2412         if (of_size) {
2413             of_size <<= 1;
2414             of_list = realloc(of_list, of_size*sizeof(oper_func_t));
2415         } else {
2416             of_size = 8;
2417             of_list = malloc(of_size*sizeof(oper_func_t));
2418         }
2419     }
2420     of_list[of_used++] = handler;
2421 }
2422
2423 static void
2424 call_oper_funcs(struct userNode *user)
2425 {
2426     unsigned int n;
2427     if (IsLocal(user))
2428         return;
2429     for (n=0; n<of_used; n++)
2430         of_list[n](user);
2431 }
2432
2433 static void
2434 send_burst(void)
2435 {
2436     unsigned int i, hop, max_hop=1;
2437     dict_iterator_t it;
2438
2439     /* burst (juped) servers, closest first (except self, which is sent already) */
2440     for (i=0; i<ArrayLength(servers_num); i++)
2441         if (servers_num[i] && servers_num[i]->hops > max_hop)
2442             max_hop = servers_num[i]->hops;
2443     for (hop=1; hop<=max_hop; hop++) {
2444         for (i=0;i<ArrayLength(servers_num);i++) {
2445             if (servers_num[i]
2446                 && (servers_num[i]->hops == hop)
2447                 && (servers_num[i] != self->uplink))
2448                 irc_server(servers_num[i]);
2449         }
2450     }
2451
2452     /* burst local nicks */
2453     for (i=0; i<=self->num_mask; i++)
2454         if (self->users[i])
2455             irc_user(self->users[i]);
2456
2457     /* build dict of unbursted channel names (just copy existing channels) */
2458     unbursted_channels = dict_new();
2459     for (it = dict_first(channels); it; it = iter_next(it))
2460         dict_insert(unbursted_channels, iter_key(it), iter_data(it));
2461 }