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