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