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