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