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