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