[5ebff60] | 1 | /********************************************************************\ |
---|
[0298d11] | 2 | * BitlBee -- An IRC to other IM-networks gateway * |
---|
| 3 | * * |
---|
[0e788f5] | 4 | * Copyright 2002-2012 Wilmer van der Gaast and others * |
---|
[0298d11] | 5 | \********************************************************************/ |
---|
| 6 | |
---|
| 7 | /* IRC commands */ |
---|
| 8 | |
---|
| 9 | /* |
---|
| 10 | This program is free software; you can redistribute it and/or modify |
---|
| 11 | it under the terms of the GNU General Public License as published by |
---|
| 12 | the Free Software Foundation; either version 2 of the License, or |
---|
| 13 | (at your option) any later version. |
---|
| 14 | |
---|
| 15 | This program is distributed in the hope that it will be useful, |
---|
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 18 | GNU General Public License for more details. |
---|
| 19 | |
---|
| 20 | You should have received a copy of the GNU General Public License with |
---|
| 21 | the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL; |
---|
[6f10697] | 22 | if not, write to the Free Software Foundation, Inc., 51 Franklin St., |
---|
| 23 | Fifth Floor, Boston, MA 02110-1301 USA |
---|
[0298d11] | 24 | */ |
---|
| 25 | |
---|
| 26 | #define BITLBEE_CORE |
---|
| 27 | #include "bitlbee.h" |
---|
[b097945] | 28 | #include "canohost.h" |
---|
[674a01d] | 29 | #include "help.h" |
---|
[0431ea1] | 30 | #include "ipc.h" |
---|
[58b63de6] | 31 | #include "base64.h" |
---|
[0298d11] | 32 | |
---|
[5ebff60] | 33 | static void irc_cmd_pass(irc_t *irc, char **cmd) |
---|
[0298d11] | 34 | { |
---|
[5ebff60] | 35 | if (irc->status & USTATUS_LOGGED_IN) { |
---|
[a199d33] | 36 | char *send_cmd[] = { "identify", cmd[1], NULL }; |
---|
[5ebff60] | 37 | |
---|
[a199d33] | 38 | /* We're already logged in, this client seems to send the PASS |
---|
| 39 | command last. (Possibly it won't send it at all if it turns |
---|
| 40 | out we don't require it, which will break this feature.) |
---|
| 41 | Try to identify using the given password. */ |
---|
[5ebff60] | 42 | root_command(irc, send_cmd); |
---|
[daae10f] | 43 | return; |
---|
[a199d33] | 44 | } |
---|
| 45 | /* Handling in pre-logged-in state, first see if this server is |
---|
| 46 | password-protected: */ |
---|
[5ebff60] | 47 | else if (global.conf->auth_pass && |
---|
| 48 | (strncmp(global.conf->auth_pass, "md5:", 4) == 0 ? |
---|
| 49 | md5_verify_password(cmd[1], global.conf->auth_pass + 4) == 0 : |
---|
| 50 | strcmp(cmd[1], global.conf->auth_pass) == 0)) { |
---|
[79e826a] | 51 | irc->status |= USTATUS_AUTHORIZED; |
---|
[5ebff60] | 52 | irc_check_login(irc); |
---|
| 53 | } else if (global.conf->auth_pass) { |
---|
| 54 | irc_send_num(irc, 464, ":Incorrect password"); |
---|
| 55 | } else { |
---|
[a199d33] | 56 | /* Remember the password and try to identify after USER/NICK. */ |
---|
[5ebff60] | 57 | irc_setpass(irc, cmd[1]); |
---|
| 58 | irc_check_login(irc); |
---|
[a199d33] | 59 | } |
---|
[0298d11] | 60 | } |
---|
| 61 | |
---|
[d179fd90] | 62 | /* http://www.haproxy.org/download/1.8/doc/proxy-protocol.txt |
---|
| 63 | |
---|
| 64 | This isn't actually IRC, it's used by for example stunnel4 to indicate |
---|
| 65 | the origin of the secured counterpart of the connection. It'll go wrong |
---|
| 66 | with arguments starting with : like for example "::1" but I guess I'm |
---|
| 67 | okay with that. */ |
---|
| 68 | static void irc_cmd_proxy(irc_t *irc, char **cmd) |
---|
| 69 | { |
---|
| 70 | struct addrinfo hints, *ai; |
---|
| 71 | struct sockaddr_storage sock; |
---|
| 72 | socklen_t socklen = sizeof(sock); |
---|
| 73 | |
---|
| 74 | if (getpeername(irc->fd, (struct sockaddr*) &sock, &socklen) != 0) { |
---|
| 75 | return; |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | ipv64_normalise_mapped(&sock, &socklen); |
---|
| 79 | |
---|
| 80 | /* Only accept PROXY "command" on localhost sockets. */ |
---|
| 81 | if (!((sock.ss_family == AF_INET && |
---|
| 82 | ntohl(((struct sockaddr_in*)&sock)->sin_addr.s_addr) == INADDR_LOOPBACK) || |
---|
| 83 | (sock.ss_family == AF_INET6 && |
---|
| 84 | IN6_IS_ADDR_LOOPBACK(&((struct sockaddr_in6*)&sock)->sin6_addr)))) { |
---|
| 85 | return; |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | /* And only once. Do this with a pretty dumb regex-match for |
---|
| 89 | now, maybe better to use some sort of flag.. */ |
---|
| 90 | if (!g_regex_match_simple("^(ip6-)?localhost(.(localdomain.?)?)?$", irc->user->host, 0, 0)) { |
---|
| 91 | return; |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | memset(&hints, 0, sizeof(hints)); |
---|
| 95 | hints.ai_flags = AI_NUMERICHOST; |
---|
| 96 | if (getaddrinfo(cmd[2], NULL, &hints, &ai) != 0) { |
---|
| 97 | return; |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | irc_set_hosts(irc, ai->ai_addr, ai->ai_addrlen); |
---|
| 101 | freeaddrinfo(ai); |
---|
| 102 | } |
---|
| 103 | |
---|
[58b63de6] | 104 | static gboolean irc_sasl_plain_parse(char *input, char **user, char **pass) |
---|
| 105 | { |
---|
| 106 | int i, part, len; |
---|
| 107 | guint8 *decoded; |
---|
[c4e61db] | 108 | char *parts[3]; |
---|
[58b63de6] | 109 | |
---|
| 110 | /* bitlbee's base64_decode wrapper adds an extra null terminator at the end */ |
---|
| 111 | len = base64_decode(input, &decoded); |
---|
| 112 | |
---|
| 113 | /* this loop splits the decoded string into the parts array, like this: |
---|
| 114 | "username\0username\0password" -> {"username", "username", "password"} */ |
---|
| 115 | |
---|
| 116 | for (i = 0, part = 0; i < len && part < 3; part++) { |
---|
| 117 | /* set each of parts[] to point to the beginning of a string */ |
---|
| 118 | parts[part] = (char *) decoded + i; |
---|
| 119 | |
---|
| 120 | /* move the cursor forward to the next null terminator*/ |
---|
| 121 | i += strlen(parts[part]) + 1; |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | /* sanity checks */ |
---|
[c788e15] | 125 | if (part != 3 || i != (len + 1) || (parts[0][0] && strcmp(parts[0], parts[1]) != 0)) { |
---|
[58b63de6] | 126 | g_free(decoded); |
---|
| 127 | return FALSE; |
---|
| 128 | } else { |
---|
[c788e15] | 129 | *user = g_strdup(parts[1]); |
---|
[58b63de6] | 130 | *pass = g_strdup(parts[2]); |
---|
| 131 | g_free(decoded); |
---|
| 132 | return TRUE; |
---|
| 133 | } |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | static gboolean irc_sasl_check_pass(irc_t *irc, char *user, char *pass) |
---|
| 137 | { |
---|
| 138 | storage_status_t status; |
---|
| 139 | |
---|
| 140 | /* just check the password here to be able to reply with useful numerics |
---|
| 141 | * the actual identification will be handled later */ |
---|
[8e6ecfe] | 142 | status = auth_check_pass(irc, user, pass); |
---|
[58b63de6] | 143 | |
---|
| 144 | if (status == STORAGE_OK) { |
---|
| 145 | if (!irc->user->nick) { |
---|
| 146 | /* set the nick here so we have it for the following numeric */ |
---|
| 147 | irc->user->nick = g_strdup(user); |
---|
| 148 | } |
---|
[4528a52] | 149 | irc_send_num(irc, 900, "%s!%s@%s %s :You are now logged in as %s", |
---|
| 150 | irc->user->nick, irc->user->user, irc->user->host, |
---|
| 151 | irc->user->nick, irc->user->nick); |
---|
[58b63de6] | 152 | irc_send_num(irc, 903, ":Password accepted"); |
---|
| 153 | return TRUE; |
---|
| 154 | |
---|
| 155 | } else if (status == STORAGE_INVALID_PASSWORD) { |
---|
| 156 | irc_send_num(irc, 904, ":Incorrect password"); |
---|
| 157 | } else if (status == STORAGE_NO_SUCH_USER) { |
---|
| 158 | irc_send_num(irc, 904, ":The nick is (probably) not registered"); |
---|
| 159 | } else { |
---|
| 160 | irc_send_num(irc, 904, ":Unknown SASL authentication error"); |
---|
| 161 | } |
---|
| 162 | |
---|
| 163 | return FALSE; |
---|
| 164 | } |
---|
| 165 | |
---|
| 166 | static void irc_cmd_authenticate(irc_t *irc, char **cmd) |
---|
| 167 | { |
---|
| 168 | /* require the CAP to be enabled, and don't allow authentication before server password */ |
---|
| 169 | if (!(irc->caps & CAP_SASL) || |
---|
| 170 | (global.conf->authmode == AUTHMODE_CLOSED && !(irc->status & USTATUS_AUTHORIZED))) { |
---|
| 171 | return; |
---|
| 172 | } |
---|
| 173 | |
---|
| 174 | if (irc->status & USTATUS_SASL_PLAIN_PENDING) { |
---|
| 175 | char *user, *pass; |
---|
| 176 | |
---|
| 177 | irc->status &= ~USTATUS_SASL_PLAIN_PENDING; |
---|
| 178 | |
---|
| 179 | if (!irc_sasl_plain_parse(cmd[1], &user, &pass)) { |
---|
| 180 | irc_send_num(irc, 904, ":SASL authentication failed"); |
---|
| 181 | return; |
---|
| 182 | } |
---|
| 183 | |
---|
| 184 | /* let's not support the nick != user case |
---|
| 185 | * if NICK is received after SASL, it will just fail after registration */ |
---|
| 186 | if (user && irc->user->nick && strcmp(user, irc->user->nick) != 0) { |
---|
| 187 | irc_send_num(irc, 902, ":Your SASL username does not match your nickname"); |
---|
| 188 | |
---|
| 189 | } else if (irc_sasl_check_pass(irc, user, pass)) { |
---|
| 190 | /* and here we do the same thing as the PASS command*/ |
---|
| 191 | if (irc->status & USTATUS_LOGGED_IN) { |
---|
| 192 | char *send_cmd[] = { "identify", pass, NULL }; |
---|
| 193 | root_command(irc, send_cmd); |
---|
| 194 | } else { |
---|
| 195 | /* no check_login here - wait for CAP END */ |
---|
| 196 | irc_setpass(irc, pass); |
---|
| 197 | } |
---|
| 198 | } |
---|
| 199 | |
---|
| 200 | g_free(user); |
---|
| 201 | g_free(pass); |
---|
| 202 | |
---|
| 203 | } else if (irc->status & USTATUS_IDENTIFIED) { |
---|
| 204 | irc_send_num(irc, 907, ":You have already authenticated"); |
---|
| 205 | |
---|
| 206 | } else if (strcmp(cmd[1], "*") == 0) { |
---|
| 207 | irc_send_num(irc, 906, ":SASL authentication aborted"); |
---|
| 208 | irc->status &= ~USTATUS_SASL_PLAIN_PENDING; |
---|
| 209 | |
---|
| 210 | } else if (g_strcasecmp(cmd[1], "PLAIN") == 0) { |
---|
| 211 | irc_write(irc, "AUTHENTICATE +"); |
---|
| 212 | irc->status |= USTATUS_SASL_PLAIN_PENDING; |
---|
| 213 | |
---|
| 214 | } else { |
---|
| 215 | irc_send_num(irc, 908, "PLAIN :is the available SASL mechanism"); |
---|
| 216 | irc_send_num(irc, 904, ":SASL authentication failed"); |
---|
| 217 | irc->status &= ~USTATUS_SASL_PLAIN_PENDING; |
---|
| 218 | } |
---|
| 219 | } |
---|
| 220 | |
---|
[5ebff60] | 221 | static void irc_cmd_user(irc_t *irc, char **cmd) |
---|
[0298d11] | 222 | { |
---|
[5ebff60] | 223 | irc->user->user = g_strdup(cmd[1]); |
---|
| 224 | irc->user->fullname = g_strdup(cmd[4]); |
---|
| 225 | |
---|
| 226 | irc_check_login(irc); |
---|
[0298d11] | 227 | } |
---|
| 228 | |
---|
[5ebff60] | 229 | static void irc_cmd_nick(irc_t *irc, char **cmd) |
---|
[0298d11] | 230 | { |
---|
[9a9b520] | 231 | irc_user_t *iu; |
---|
[5ebff60] | 232 | |
---|
| 233 | if ((iu = irc_user_by_name(irc, cmd[1])) && iu != irc->user) { |
---|
| 234 | irc_send_num(irc, 433, "%s :This nick is already in use", cmd[1]); |
---|
| 235 | } else if (!nick_ok(NULL, cmd[1])) { |
---|
[0298d11] | 236 | /* [SH] Invalid characters. */ |
---|
[5ebff60] | 237 | irc_send_num(irc, 432, "%s :This nick contains invalid characters", cmd[1]); |
---|
| 238 | } else if (irc->status & USTATUS_LOGGED_IN) { |
---|
[3558fea] | 239 | /* WATCH OUT: iu from the first if reused here to check if the |
---|
| 240 | new nickname is the same (other than case, possibly). If it |
---|
| 241 | is, no need to reset identify-status. */ |
---|
[5ebff60] | 242 | if ((irc->status & USTATUS_IDENTIFIED) && iu != irc->user) { |
---|
| 243 | irc_setpass(irc, NULL); |
---|
[ffa1173] | 244 | irc->status &= ~USTATUS_IDENTIFIED; |
---|
[5ebff60] | 245 | irc_umode_set(irc, "-R", 1); |
---|
[58b63de6] | 246 | |
---|
| 247 | if (irc->caps & CAP_SASL) { |
---|
| 248 | irc_send_num(irc, 901, "%s!%s@%s :You are now logged out", |
---|
| 249 | irc->user->nick, irc->user->user, irc->user->host); |
---|
| 250 | } |
---|
| 251 | |
---|
[5ebff60] | 252 | irc_rootmsg(irc, "Changing nicks resets your identify status. " |
---|
| 253 | "Re-identify or register a new account if you want " |
---|
| 254 | "your configuration to be saved. See \x02help " |
---|
| 255 | "nick_changes\x02."); |
---|
[ffa1173] | 256 | } |
---|
[5ebff60] | 257 | |
---|
| 258 | if (strcmp(cmd[1], irc->user->nick) != 0) { |
---|
| 259 | irc_user_set_nick(irc->user, cmd[1]); |
---|
| 260 | } |
---|
| 261 | } else { |
---|
| 262 | g_free(irc->user->nick); |
---|
| 263 | irc->user->nick = g_strdup(cmd[1]); |
---|
| 264 | |
---|
| 265 | irc_check_login(irc); |
---|
[0298d11] | 266 | } |
---|
| 267 | } |
---|
| 268 | |
---|
[5ebff60] | 269 | static void irc_cmd_quit(irc_t *irc, char **cmd) |
---|
[0298d11] | 270 | { |
---|
[5ebff60] | 271 | if (cmd[1] && *cmd[1]) { |
---|
| 272 | irc_abort(irc, 0, "Quit: %s", cmd[1]); |
---|
| 273 | } else { |
---|
| 274 | irc_abort(irc, 0, "Leaving..."); |
---|
| 275 | } |
---|
[0298d11] | 276 | } |
---|
| 277 | |
---|
[5ebff60] | 278 | static void irc_cmd_ping(irc_t *irc, char **cmd) |
---|
[0298d11] | 279 | { |
---|
[5ebff60] | 280 | irc_write(irc, ":%s PONG %s :%s", irc->root->host, |
---|
| 281 | irc->root->host, cmd[1] ? cmd[1] : irc->root->host); |
---|
[0298d11] | 282 | } |
---|
| 283 | |
---|
[5ebff60] | 284 | static void irc_cmd_pong(irc_t *irc, char **cmd) |
---|
[3923003] | 285 | { |
---|
| 286 | /* We could check the value we get back from the user, but in |
---|
| 287 | fact we don't care, we're just happy s/he's still alive. */ |
---|
| 288 | irc->last_pong = gettime(); |
---|
| 289 | irc->pinging = 0; |
---|
| 290 | } |
---|
| 291 | |
---|
[5ebff60] | 292 | static void irc_cmd_join(irc_t *irc, char **cmd) |
---|
[b9e020a] | 293 | { |
---|
[58646d9] | 294 | char *comma, *s = cmd[1]; |
---|
[5ebff60] | 295 | |
---|
| 296 | while (s) { |
---|
[58646d9] | 297 | irc_channel_t *ic; |
---|
[5ebff60] | 298 | |
---|
| 299 | if ((comma = strchr(s, ','))) { |
---|
[58646d9] | 300 | *comma = '\0'; |
---|
[5ebff60] | 301 | } |
---|
| 302 | |
---|
| 303 | if ((ic = irc_channel_by_name(irc, s)) == NULL && |
---|
| 304 | (ic = irc_channel_new(irc, s))) { |
---|
| 305 | if (strcmp(set_getstr(&ic->set, "type"), "control") != 0) { |
---|
[324c378] | 306 | /* Autoconfiguration is for control channels only ATM. */ |
---|
[5ebff60] | 307 | } else if (bee_group_by_name(ic->irc->b, ic->name + 1, FALSE)) { |
---|
| 308 | set_setstr(&ic->set, "group", ic->name + 1); |
---|
| 309 | set_setstr(&ic->set, "fill_by", "group"); |
---|
| 310 | } else if (set_setstr(&ic->set, "protocol", ic->name + 1)) { |
---|
| 311 | set_setstr(&ic->set, "fill_by", "protocol"); |
---|
| 312 | } else if (set_setstr(&ic->set, "account", ic->name + 1)) { |
---|
| 313 | set_setstr(&ic->set, "fill_by", "account"); |
---|
| 314 | } else { |
---|
[13fa2db] | 315 | /* The set commands above will run this already, |
---|
| 316 | but if we didn't hit any, we have to fill the |
---|
| 317 | channel with the default population. */ |
---|
[5ebff60] | 318 | bee_irc_channel_update(ic->irc, ic, NULL); |
---|
[324c378] | 319 | } |
---|
[5ebff60] | 320 | } else if (ic == NULL) { |
---|
| 321 | irc_send_num(irc, 479, "%s :Invalid channel name", s); |
---|
[58646d9] | 322 | goto next; |
---|
| 323 | } |
---|
[5ebff60] | 324 | |
---|
| 325 | if (ic->flags & IRC_CHANNEL_JOINED) { |
---|
[58646d9] | 326 | /* Dude, you're already there... |
---|
| 327 | RFC doesn't have any reply for that though? */ |
---|
| 328 | goto next; |
---|
[5ebff60] | 329 | } |
---|
| 330 | |
---|
| 331 | if (ic->f->join && !ic->f->join(ic)) { |
---|
[58646d9] | 332 | /* The story is: FALSE either means the handler |
---|
| 333 | showed an error message, or is doing some work |
---|
| 334 | before the join should be confirmed. (In the |
---|
| 335 | latter case, the caller should take care of that |
---|
| 336 | confirmation.) TRUE means all's good, let the |
---|
| 337 | user join the channel right away. */ |
---|
| 338 | goto next; |
---|
[5ebff60] | 339 | } |
---|
| 340 | |
---|
| 341 | irc_channel_add_user(ic, irc->user); |
---|
| 342 | |
---|
[58646d9] | 343 | next: |
---|
[5ebff60] | 344 | if (comma) { |
---|
[58646d9] | 345 | s = comma + 1; |
---|
| 346 | *comma = ','; |
---|
[5ebff60] | 347 | } else { |
---|
[58646d9] | 348 | break; |
---|
[5ebff60] | 349 | } |
---|
[57119e8] | 350 | } |
---|
[b9e020a] | 351 | } |
---|
| 352 | |
---|
[5ebff60] | 353 | static void irc_cmd_names(irc_t *irc, char **cmd) |
---|
[b9e020a] | 354 | { |
---|
| 355 | irc_channel_t *ic; |
---|
[5ebff60] | 356 | |
---|
| 357 | if (cmd[1] && (ic = irc_channel_by_name(irc, cmd[1]))) { |
---|
| 358 | irc_send_names(ic); |
---|
| 359 | } |
---|
[b9e020a] | 360 | /* With no args, we should show /names of all chans. Make the code |
---|
| 361 | below work well if necessary. |
---|
| 362 | else |
---|
| 363 | { |
---|
[5ebff60] | 364 | GSList *l; |
---|
| 365 | |
---|
| 366 | for( l = irc->channels; l; l = l->next ) |
---|
| 367 | irc_send_names( l->data ); |
---|
[b9e020a] | 368 | } |
---|
| 369 | */ |
---|
| 370 | } |
---|
| 371 | |
---|
[5ebff60] | 372 | static void irc_cmd_part(irc_t *irc, char **cmd) |
---|
[b9e020a] | 373 | { |
---|
| 374 | irc_channel_t *ic; |
---|
[5ebff60] | 375 | |
---|
| 376 | if ((ic = irc_channel_by_name(irc, cmd[1])) == NULL) { |
---|
| 377 | irc_send_num(irc, 403, "%s :No such channel", cmd[1]); |
---|
| 378 | } else if (irc_channel_del_user(ic, irc->user, IRC_CDU_PART, cmd[2])) { |
---|
| 379 | if (ic->f->part) { |
---|
| 380 | ic->f->part(ic, NULL); |
---|
| 381 | } |
---|
| 382 | } else { |
---|
| 383 | irc_send_num(irc, 442, "%s :You're not on that channel", cmd[1]); |
---|
[b9e020a] | 384 | } |
---|
| 385 | } |
---|
| 386 | |
---|
[5ebff60] | 387 | static void irc_cmd_whois(irc_t *irc, char **cmd) |
---|
[b95932e] | 388 | { |
---|
| 389 | char *nick = cmd[1]; |
---|
[5ebff60] | 390 | irc_user_t *iu = irc_user_by_name(irc, nick); |
---|
| 391 | |
---|
| 392 | if (iu) { |
---|
| 393 | irc_send_whois(iu); |
---|
| 394 | } else { |
---|
| 395 | irc_send_num(irc, 401, "%s :Nick does not exist", nick); |
---|
| 396 | } |
---|
[b95932e] | 397 | } |
---|
| 398 | |
---|
[5ebff60] | 399 | static void irc_cmd_whowas(irc_t *irc, char **cmd) |
---|
[b95932e] | 400 | { |
---|
| 401 | /* For some reason irssi tries a whowas when whois fails. We can |
---|
| 402 | ignore this, but then the user never gets a "user not found" |
---|
| 403 | message from irssi which is a bit annoying. So just respond |
---|
| 404 | with not-found and irssi users will get better error messages */ |
---|
[5ebff60] | 405 | |
---|
| 406 | irc_send_num(irc, 406, "%s :Nick does not exist", cmd[1]); |
---|
| 407 | irc_send_num(irc, 369, "%s :End of WHOWAS", cmd[1]); |
---|
[b95932e] | 408 | } |
---|
| 409 | |
---|
[5ebff60] | 410 | static void irc_cmd_motd(irc_t *irc, char **cmd) |
---|
[9b69eb7] | 411 | { |
---|
[5ebff60] | 412 | irc_send_motd(irc); |
---|
[9b69eb7] | 413 | } |
---|
| 414 | |
---|
[5ebff60] | 415 | static void irc_cmd_mode(irc_t *irc, char **cmd) |
---|
[0298d11] | 416 | { |
---|
[5ebff60] | 417 | if (irc_channel_name_ok(cmd[1])) { |
---|
[b919363] | 418 | irc_channel_t *ic; |
---|
[5ebff60] | 419 | |
---|
| 420 | if ((ic = irc_channel_by_name(irc, cmd[1])) == NULL) { |
---|
| 421 | irc_send_num(irc, 403, "%s :No such channel", cmd[1]); |
---|
| 422 | } else if (cmd[2]) { |
---|
| 423 | if (*cmd[2] == '+' || *cmd[2] == '-') { |
---|
| 424 | irc_send_num(irc, 477, "%s :Can't change channel modes", cmd[1]); |
---|
| 425 | } else if (*cmd[2] == 'b') { |
---|
| 426 | irc_send_num(irc, 368, "%s :No bans possible", cmd[1]); |
---|
| 427 | } |
---|
| 428 | } else { |
---|
| 429 | irc_send_num(irc, 324, "%s +%s", cmd[1], ic->mode); |
---|
[0298d11] | 430 | } |
---|
[5ebff60] | 431 | } else { |
---|
| 432 | if (nick_cmp(NULL, cmd[1], irc->user->nick) == 0) { |
---|
| 433 | if (cmd[2]) { |
---|
| 434 | irc_umode_set(irc, cmd[2], 0); |
---|
| 435 | } else { |
---|
| 436 | irc_send_num(irc, 221, "+%s", irc->umode); |
---|
| 437 | } |
---|
| 438 | } else { |
---|
| 439 | irc_send_num(irc, 502, ":Don't touch their modes"); |
---|
[0298d11] | 440 | } |
---|
| 441 | } |
---|
| 442 | } |
---|
| 443 | |
---|
[5ebff60] | 444 | static void irc_cmd_who(irc_t *irc, char **cmd) |
---|
[2f53ada] | 445 | { |
---|
| 446 | char *channel = cmd[1]; |
---|
| 447 | irc_channel_t *ic; |
---|
[a72af0d] | 448 | irc_user_t *iu; |
---|
[5ebff60] | 449 | |
---|
| 450 | if (!channel || *channel == '0' || *channel == '*' || !*channel) { |
---|
| 451 | irc_send_who(irc, irc->users, "**"); |
---|
| 452 | } else if ((ic = irc_channel_by_name(irc, channel))) { |
---|
| 453 | irc_send_who(irc, ic->users, channel); |
---|
| 454 | } else if ((iu = irc_user_by_name(irc, channel))) { |
---|
[a72af0d] | 455 | /* Tiny hack! */ |
---|
[5ebff60] | 456 | GSList *l = g_slist_append(NULL, iu); |
---|
| 457 | irc_send_who(irc, l, channel); |
---|
| 458 | g_slist_free(l); |
---|
| 459 | } else { |
---|
| 460 | irc_send_num(irc, 403, "%s :No such channel", channel); |
---|
[a72af0d] | 461 | } |
---|
[2f53ada] | 462 | } |
---|
| 463 | |
---|
[5ebff60] | 464 | static void irc_cmd_privmsg(irc_t *irc, char **cmd) |
---|
[b919363] | 465 | { |
---|
[280c56a] | 466 | irc_channel_t *ic; |
---|
| 467 | irc_user_t *iu; |
---|
[5ebff60] | 468 | |
---|
| 469 | if (!cmd[2]) { |
---|
| 470 | irc_send_num(irc, 412, ":No text to send"); |
---|
[24b8bbb] | 471 | return; |
---|
[280c56a] | 472 | } |
---|
[5ebff60] | 473 | |
---|
[24b8bbb] | 474 | /* Don't treat CTCP actions as real CTCPs, just convert them right now. */ |
---|
[5ebff60] | 475 | if (g_strncasecmp(cmd[2], "\001ACTION", 7) == 0) { |
---|
[24b8bbb] | 476 | cmd[2] += 4; |
---|
[5ebff60] | 477 | memcpy(cmd[2], "/me", 3); |
---|
| 478 | if (cmd[2][strlen(cmd[2]) - 1] == '\001') { |
---|
| 479 | cmd[2][strlen(cmd[2]) - 1] = '\0'; |
---|
| 480 | } |
---|
[24b8bbb] | 481 | } |
---|
[5ebff60] | 482 | |
---|
| 483 | if (irc_channel_name_ok(cmd[1]) && |
---|
| 484 | (ic = irc_channel_by_name(irc, cmd[1]))) { |
---|
| 485 | if (cmd[2][0] == '\001') { |
---|
[a7dbf45] | 486 | /* CTCPs to channels? Nah. Maybe later. */ |
---|
[5ebff60] | 487 | } else if (ic->f->privmsg) { |
---|
| 488 | ic->f->privmsg(ic, cmd[2]); |
---|
[a7dbf45] | 489 | } |
---|
[5ebff60] | 490 | } else if ((iu = irc_user_by_name(irc, cmd[1]))) { |
---|
| 491 | if (cmd[2][0] == '\001') { |
---|
[24b8bbb] | 492 | char **ctcp; |
---|
[5ebff60] | 493 | |
---|
| 494 | if (iu->f->ctcp == NULL) { |
---|
[24b8bbb] | 495 | return; |
---|
[5ebff60] | 496 | } |
---|
| 497 | if (cmd[2][strlen(cmd[2]) - 1] == '\001') { |
---|
| 498 | cmd[2][strlen(cmd[2]) - 1] = '\0'; |
---|
| 499 | } |
---|
| 500 | |
---|
| 501 | ctcp = split_command_parts(cmd[2] + 1, 0); |
---|
| 502 | iu->f->ctcp(iu, ctcp); |
---|
| 503 | } else if (iu->f->privmsg) { |
---|
[92c8d41] | 504 | iu->last_channel = NULL; |
---|
[5ebff60] | 505 | iu->f->privmsg(iu, cmd[2]); |
---|
[bce78c8] | 506 | } |
---|
[5ebff60] | 507 | } else { |
---|
| 508 | irc_send_num(irc, 401, "%s :No such nick/channel", cmd[1]); |
---|
[b919363] | 509 | } |
---|
[280c56a] | 510 | } |
---|
| 511 | |
---|
[5ebff60] | 512 | static void irc_cmd_notice(irc_t *irc, char **cmd) |
---|
[d7f8500] | 513 | { |
---|
[bbc69f7] | 514 | irc_user_t *iu; |
---|
[5ebff60] | 515 | |
---|
| 516 | if (!cmd[2]) { |
---|
| 517 | irc_send_num(irc, 412, ":No text to send"); |
---|
[d7f8500] | 518 | return; |
---|
| 519 | } |
---|
[5ebff60] | 520 | |
---|
[d7f8500] | 521 | /* At least for now just echo. IIRC some IRC clients use self-notices |
---|
| 522 | for lag checks, so try to support that. */ |
---|
[5ebff60] | 523 | if (nick_cmp(NULL, cmd[1], irc->user->nick) == 0) { |
---|
| 524 | irc_send_msg(irc->user, "NOTICE", irc->user->nick, cmd[2], NULL); |
---|
| 525 | } else if ((iu = irc_user_by_name(irc, cmd[1]))) { |
---|
| 526 | iu->f->privmsg(iu, cmd[2]); |
---|
| 527 | } |
---|
[d7f8500] | 528 | } |
---|
| 529 | |
---|
[5ebff60] | 530 | static void irc_cmd_nickserv(irc_t *irc, char **cmd) |
---|
[d860a8d] | 531 | { |
---|
| 532 | /* [SH] This aliases the NickServ command to PRIVMSG root */ |
---|
| 533 | /* [TV] This aliases the NS command to PRIVMSG root as well */ |
---|
[5ebff60] | 534 | root_command(irc, cmd + 1); |
---|
[d860a8d] | 535 | } |
---|
| 536 | |
---|
[5ebff60] | 537 | static void irc_cmd_oper_hack(irc_t *irc, char **cmd); |
---|
[280c56a] | 538 | |
---|
[5ebff60] | 539 | static void irc_cmd_oper(irc_t *irc, char **cmd) |
---|
[280c56a] | 540 | { |
---|
[060d066] | 541 | /* Very non-standard evil but useful/secure hack, see below. */ |
---|
[5ebff60] | 542 | if (irc->status & OPER_HACK_ANY) { |
---|
| 543 | return irc_cmd_oper_hack(irc, cmd); |
---|
[280c56a] | 544 | } |
---|
[5ebff60] | 545 | |
---|
| 546 | if (global.conf->oper_pass && |
---|
| 547 | (strncmp(global.conf->oper_pass, "md5:", 4) == 0 ? |
---|
| 548 | md5_verify_password(cmd[2], global.conf->oper_pass + 4) == 0 : |
---|
| 549 | strcmp(cmd[2], global.conf->oper_pass) == 0)) { |
---|
| 550 | irc_umode_set(irc, "+o", 1); |
---|
| 551 | irc_send_num(irc, 381, ":Password accepted"); |
---|
| 552 | } else { |
---|
| 553 | irc_send_num(irc, 491, ":Incorrect password"); |
---|
[280c56a] | 554 | } |
---|
| 555 | } |
---|
| 556 | |
---|
[5ebff60] | 557 | static void irc_cmd_oper_hack(irc_t *irc, char **cmd) |
---|
[060d066] | 558 | { |
---|
[5ebff60] | 559 | char *password = g_strjoinv(" ", cmd + 2); |
---|
| 560 | |
---|
[060d066] | 561 | /* /OPER can now also be used to enter IM/identify passwords without |
---|
| 562 | echoing. It's a hack but the extra password security is worth it. */ |
---|
[5ebff60] | 563 | if (irc->status & OPER_HACK_ACCOUNT_PASSWORD) { |
---|
[060d066] | 564 | account_t *a; |
---|
[5ebff60] | 565 | |
---|
| 566 | for (a = irc->b->accounts; a; a = a->next) { |
---|
| 567 | if (strcmp(a->pass, PASSWORD_PENDING) == 0) { |
---|
| 568 | set_setstr(&a->set, "password", password); |
---|
| 569 | irc_rootmsg(irc, "Password added to IM account " |
---|
| 570 | "%s", a->tag); |
---|
[060d066] | 571 | /* The IRC client may expect this. 491 suggests the OPER |
---|
| 572 | password was wrong, so the client won't expect a +o. |
---|
| 573 | It may however repeat the password prompt. We'll see. */ |
---|
[5ebff60] | 574 | irc_send_num(irc, 491, ":Password added to IM account " |
---|
| 575 | "%s", a->tag); |
---|
[060d066] | 576 | } |
---|
[5ebff60] | 577 | } |
---|
| 578 | } else if (irc->status & OPER_HACK_IDENTIFY) { |
---|
[fda194f] | 579 | char *send_cmd[] = { "identify", password, NULL, NULL }; |
---|
| 580 | irc->status &= ~OPER_HACK_IDENTIFY; |
---|
[5ebff60] | 581 | if (irc->status & OPER_HACK_IDENTIFY_NOLOAD) { |
---|
[fda194f] | 582 | send_cmd[1] = "-noload"; |
---|
| 583 | send_cmd[2] = password; |
---|
[5ebff60] | 584 | } else if (irc->status & OPER_HACK_IDENTIFY_FORCE) { |
---|
[fda194f] | 585 | send_cmd[1] = "-force"; |
---|
| 586 | send_cmd[2] = password; |
---|
| 587 | } |
---|
[5ebff60] | 588 | irc_send_num(irc, 491, ":Trying to identify"); |
---|
| 589 | root_command(irc, send_cmd); |
---|
| 590 | } else if (irc->status & OPER_HACK_REGISTER) { |
---|
[060d066] | 591 | char *send_cmd[] = { "register", password, NULL }; |
---|
[5ebff60] | 592 | irc_send_num(irc, 491, ":Trying to identify"); |
---|
| 593 | root_command(irc, send_cmd); |
---|
[060d066] | 594 | } |
---|
[5ebff60] | 595 | |
---|
[060d066] | 596 | irc->status &= ~OPER_HACK_ANY; |
---|
[5ebff60] | 597 | g_free(password); |
---|
[060d066] | 598 | } |
---|
| 599 | |
---|
[5ebff60] | 600 | static void irc_cmd_invite(irc_t *irc, char **cmd) |
---|
[280c56a] | 601 | { |
---|
[66b9e36a] | 602 | irc_channel_t *ic; |
---|
| 603 | irc_user_t *iu; |
---|
[5ebff60] | 604 | |
---|
| 605 | if ((iu = irc_user_by_name(irc, cmd[1])) == NULL) { |
---|
| 606 | irc_send_num(irc, 401, "%s :No such nick", cmd[1]); |
---|
[66b9e36a] | 607 | return; |
---|
[5ebff60] | 608 | } else if ((ic = irc_channel_by_name(irc, cmd[2])) == NULL) { |
---|
| 609 | irc_send_num(irc, 403, "%s :No such channel", cmd[2]); |
---|
[66b9e36a] | 610 | return; |
---|
| 611 | } |
---|
[5ebff60] | 612 | |
---|
| 613 | if (!ic->f->invite) { |
---|
| 614 | irc_send_num(irc, 482, "%s :Can't invite people here", cmd[2]); |
---|
| 615 | } else if (ic->f->invite(ic, iu)) { |
---|
| 616 | irc_send_num(irc, 341, "%s %s", iu->nick, ic->name); |
---|
| 617 | } |
---|
[0298d11] | 618 | } |
---|
| 619 | |
---|
[5ebff60] | 620 | static void irc_cmd_kick(irc_t *irc, char **cmd) |
---|
[7821ee8] | 621 | { |
---|
| 622 | irc_channel_t *ic; |
---|
| 623 | irc_user_t *iu; |
---|
[5ebff60] | 624 | |
---|
| 625 | if ((iu = irc_user_by_name(irc, cmd[2])) == NULL) { |
---|
| 626 | irc_send_num(irc, 401, "%s :No such nick", cmd[2]); |
---|
[7821ee8] | 627 | return; |
---|
[5ebff60] | 628 | } else if ((ic = irc_channel_by_name(irc, cmd[1])) == NULL) { |
---|
| 629 | irc_send_num(irc, 403, "%s :No such channel", cmd[1]); |
---|
[7821ee8] | 630 | return; |
---|
[5ebff60] | 631 | } else if (!ic->f->kick) { |
---|
| 632 | irc_send_num(irc, 482, "%s :Can't kick people here", cmd[1]); |
---|
[7821ee8] | 633 | return; |
---|
| 634 | } |
---|
[5ebff60] | 635 | |
---|
| 636 | ic->f->kick(ic, iu, cmd[3] ? cmd[3] : NULL); |
---|
[7821ee8] | 637 | } |
---|
| 638 | |
---|
[5ebff60] | 639 | static void irc_cmd_userhost(irc_t *irc, char **cmd) |
---|
[0298d11] | 640 | { |
---|
| 641 | int i; |
---|
[5ebff60] | 642 | |
---|
[0298d11] | 643 | /* [TV] Usable USERHOST-implementation according to |
---|
[5ebff60] | 644 | RFC1459. Without this, mIRC shows an error |
---|
| 645 | while connecting, and the used way of rejecting |
---|
| 646 | breaks standards. |
---|
[0298d11] | 647 | */ |
---|
[5ebff60] | 648 | |
---|
| 649 | for (i = 1; cmd[i]; i++) { |
---|
| 650 | irc_user_t *iu = irc_user_by_name(irc, cmd[i]); |
---|
| 651 | |
---|
| 652 | if (iu) { |
---|
| 653 | irc_send_num(irc, 302, ":%s=%c%s@%s", iu->nick, |
---|
| 654 | irc_user_get_away(iu) ? '-' : '+', |
---|
| 655 | iu->user, iu->host); |
---|
| 656 | } |
---|
[003a12b] | 657 | } |
---|
[0298d11] | 658 | } |
---|
| 659 | |
---|
[5ebff60] | 660 | static void irc_cmd_ison(irc_t *irc, char **cmd) |
---|
[0298d11] | 661 | { |
---|
[b4e4b95] | 662 | char buff[IRC_MAX_LINE]; |
---|
[0298d11] | 663 | int lenleft, i; |
---|
[5ebff60] | 664 | |
---|
[0298d11] | 665 | buff[0] = '\0'; |
---|
[5ebff60] | 666 | |
---|
[0298d11] | 667 | /* [SH] Leave room for : and \0 */ |
---|
| 668 | lenleft = IRC_MAX_LINE - 2; |
---|
[5ebff60] | 669 | |
---|
| 670 | for (i = 1; cmd[i]; i++) { |
---|
[42616d1] | 671 | char *this, *next; |
---|
[5ebff60] | 672 | |
---|
[42616d1] | 673 | this = cmd[i]; |
---|
[5ebff60] | 674 | while (*this) { |
---|
[003a12b] | 675 | irc_user_t *iu; |
---|
[5ebff60] | 676 | |
---|
| 677 | if ((next = strchr(this, ' '))) { |
---|
[42616d1] | 678 | *next = 0; |
---|
[5ebff60] | 679 | } |
---|
| 680 | |
---|
| 681 | if ((iu = irc_user_by_name(irc, this)) && |
---|
| 682 | iu->bu && iu->bu->flags & BEE_USER_ONLINE) { |
---|
| 683 | lenleft -= strlen(iu->nick) + 1; |
---|
| 684 | |
---|
| 685 | if (lenleft < 0) { |
---|
[42616d1] | 686 | break; |
---|
[5ebff60] | 687 | } |
---|
| 688 | |
---|
| 689 | strcat(buff, iu->nick); |
---|
| 690 | strcat(buff, " "); |
---|
[0298d11] | 691 | } |
---|
[5ebff60] | 692 | |
---|
| 693 | if (next) { |
---|
[42616d1] | 694 | *next = ' '; |
---|
| 695 | this = next + 1; |
---|
[5ebff60] | 696 | } else { |
---|
[42616d1] | 697 | break; |
---|
[5ebff60] | 698 | } |
---|
[0298d11] | 699 | } |
---|
[5ebff60] | 700 | |
---|
[42616d1] | 701 | /* *sigh* */ |
---|
[5ebff60] | 702 | if (lenleft < 0) { |
---|
[42616d1] | 703 | break; |
---|
[5ebff60] | 704 | } |
---|
| 705 | } |
---|
| 706 | |
---|
| 707 | if (strlen(buff) > 0) { |
---|
| 708 | buff[strlen(buff) - 1] = '\0'; |
---|
[0298d11] | 709 | } |
---|
[5ebff60] | 710 | |
---|
| 711 | irc_send_num(irc, 303, ":%s", buff); |
---|
[0298d11] | 712 | } |
---|
| 713 | |
---|
[5ebff60] | 714 | static void irc_cmd_watch(irc_t *irc, char **cmd) |
---|
[0298d11] | 715 | { |
---|
| 716 | int i; |
---|
[5ebff60] | 717 | |
---|
[0298d11] | 718 | /* Obviously we could also mark a user structure as being |
---|
| 719 | watched, but what if the WATCH command is sent right |
---|
| 720 | after connecting? The user won't exist yet then... */ |
---|
[5ebff60] | 721 | for (i = 1; cmd[i]; i++) { |
---|
[0298d11] | 722 | char *nick; |
---|
[003a12b] | 723 | irc_user_t *iu; |
---|
[5ebff60] | 724 | |
---|
| 725 | if (!cmd[i][0] || !cmd[i][1]) { |
---|
[0298d11] | 726 | break; |
---|
| 727 | } |
---|
[5ebff60] | 728 | |
---|
| 729 | nick = g_strdup(cmd[i] + 1); |
---|
| 730 | nick_lc(irc, nick); |
---|
| 731 | |
---|
| 732 | iu = irc_user_by_name(irc, nick); |
---|
| 733 | |
---|
| 734 | if (cmd[i][0] == '+') { |
---|
| 735 | if (!g_hash_table_lookup(irc->watches, nick)) { |
---|
| 736 | g_hash_table_insert(irc->watches, nick, nick); |
---|
| 737 | } |
---|
| 738 | |
---|
| 739 | if (iu && iu->bu && iu->bu->flags & BEE_USER_ONLINE) { |
---|
| 740 | irc_send_num(irc, 604, "%s %s %s %d :%s", iu->nick, iu->user, |
---|
| 741 | iu->host, (int) time(NULL), "is online"); |
---|
| 742 | } else { |
---|
| 743 | irc_send_num(irc, 605, "%s %s %s %d :%s", nick, "*", "*", |
---|
| 744 | (int) time(NULL), "is offline"); |
---|
| 745 | } |
---|
| 746 | } else if (cmd[i][0] == '-') { |
---|
[0298d11] | 747 | gpointer okey, ovalue; |
---|
[5ebff60] | 748 | |
---|
| 749 | if (g_hash_table_lookup_extended(irc->watches, nick, &okey, &ovalue)) { |
---|
| 750 | g_hash_table_remove(irc->watches, okey); |
---|
| 751 | g_free(okey); |
---|
| 752 | |
---|
| 753 | irc_send_num(irc, 602, "%s %s %s %d :%s", nick, "*", "*", 0, "Stopped watching"); |
---|
[0298d11] | 754 | } |
---|
| 755 | } |
---|
| 756 | } |
---|
| 757 | } |
---|
| 758 | |
---|
[5ebff60] | 759 | static void irc_cmd_topic(irc_t *irc, char **cmd) |
---|
[0298d11] | 760 | { |
---|
[5ebff60] | 761 | irc_channel_t *ic = irc_channel_by_name(irc, cmd[1]); |
---|
[4469e7e] | 762 | const char *new = cmd[2]; |
---|
[5ebff60] | 763 | |
---|
| 764 | if (ic == NULL) { |
---|
| 765 | irc_send_num(irc, 403, "%s :No such channel", cmd[1]); |
---|
| 766 | } else if (new) { |
---|
| 767 | if (ic->f->topic == NULL) { |
---|
| 768 | irc_send_num(irc, 482, "%s :Can't change this channel's topic", ic->name); |
---|
| 769 | } else if (ic->f->topic(ic, new)) { |
---|
| 770 | irc_send_topic(ic, TRUE); |
---|
| 771 | } |
---|
| 772 | } else { |
---|
| 773 | irc_send_topic(ic, FALSE); |
---|
[50e1776] | 774 | } |
---|
[0298d11] | 775 | } |
---|
| 776 | |
---|
[5ebff60] | 777 | static void irc_cmd_away(irc_t *irc, char **cmd) |
---|
[0298d11] | 778 | { |
---|
[5ebff60] | 779 | if (cmd[1] && *cmd[1]) { |
---|
| 780 | char away[strlen(cmd[1]) + 1]; |
---|
[0298d11] | 781 | int i, j; |
---|
[5ebff60] | 782 | |
---|
[0298d11] | 783 | /* Copy away string, but skip control chars. Mainly because |
---|
| 784 | Jabber really doesn't like them. */ |
---|
[5ebff60] | 785 | for (i = j = 0; cmd[1][i]; i++) { |
---|
| 786 | if ((unsigned char) (away[j] = cmd[1][i]) >= ' ') { |
---|
| 787 | j++; |
---|
| 788 | } |
---|
| 789 | } |
---|
[81186cab] | 790 | away[j] = '\0'; |
---|
[5ebff60] | 791 | |
---|
| 792 | irc_send_num(irc, 306, ":You're now away: %s", away); |
---|
| 793 | set_setstr(&irc->b->set, "away", away); |
---|
| 794 | } else { |
---|
| 795 | irc_send_num(irc, 305, ":Welcome back"); |
---|
| 796 | set_setstr(&irc->b->set, "away", NULL); |
---|
[0298d11] | 797 | } |
---|
| 798 | } |
---|
| 799 | |
---|
[5ebff60] | 800 | static void irc_cmd_list(irc_t *irc, char **cmd) |
---|
[23d6165] | 801 | { |
---|
| 802 | GSList *l; |
---|
[5ebff60] | 803 | |
---|
| 804 | for (l = irc->channels; l; l = l->next) { |
---|
[23d6165] | 805 | irc_channel_t *ic = l->data; |
---|
[5ebff60] | 806 | |
---|
| 807 | irc_send_num(irc, 322, "%s %d :%s", |
---|
| 808 | ic->name, g_slist_length(ic->users), ic->topic ? : ""); |
---|
[23d6165] | 809 | } |
---|
[5ebff60] | 810 | irc_send_num(irc, 323, ":%s", "End of /LIST"); |
---|
[23d6165] | 811 | } |
---|
| 812 | |
---|
[5ebff60] | 813 | static void irc_cmd_version(irc_t *irc, char **cmd) |
---|
[82898af] | 814 | { |
---|
[2e78f75] | 815 | irc_send_num(irc, 351, "%s-%s. %s :", |
---|
| 816 | PACKAGE, BITLBEE_VERSION, irc->root->host); |
---|
[82898af] | 817 | } |
---|
| 818 | |
---|
[5ebff60] | 819 | static void irc_cmd_completions(irc_t *irc, char **cmd) |
---|
[0298d11] | 820 | { |
---|
| 821 | help_t *h; |
---|
| 822 | set_t *s; |
---|
| 823 | int i; |
---|
[5ebff60] | 824 | |
---|
| 825 | irc_send_msg_raw(irc->root, "NOTICE", irc->user->nick, "COMPLETIONS OK"); |
---|
| 826 | |
---|
| 827 | for (i = 0; root_commands[i].command; i++) { |
---|
| 828 | irc_send_msg_f(irc->root, "NOTICE", irc->user->nick, "COMPLETIONS %s", root_commands[i].command); |
---|
| 829 | } |
---|
| 830 | |
---|
| 831 | for (h = global.help; h; h = h->next) { |
---|
| 832 | irc_send_msg_f(irc->root, "NOTICE", irc->user->nick, "COMPLETIONS help %s", h->title); |
---|
| 833 | } |
---|
| 834 | |
---|
| 835 | for (s = irc->b->set; s; s = s->next) { |
---|
| 836 | irc_send_msg_f(irc->root, "NOTICE", irc->user->nick, "COMPLETIONS set %s", s->key); |
---|
| 837 | } |
---|
| 838 | |
---|
| 839 | irc_send_msg_raw(irc->root, "NOTICE", irc->user->nick, "COMPLETIONS END"); |
---|
[0298d11] | 840 | } |
---|
| 841 | |
---|
[5ebff60] | 842 | static void irc_cmd_rehash(irc_t *irc, char **cmd) |
---|
[f4a5940] | 843 | { |
---|
[5ebff60] | 844 | if (global.conf->runmode == RUNMODE_INETD) { |
---|
| 845 | ipc_master_cmd_rehash(NULL, NULL); |
---|
| 846 | } else { |
---|
| 847 | ipc_to_master(cmd); |
---|
| 848 | } |
---|
| 849 | |
---|
| 850 | irc_send_num(irc, 382, "%s :Rehashing", global.conf_file); |
---|
[f4a5940] | 851 | } |
---|
| 852 | |
---|
[0298d11] | 853 | static const command_t irc_commands[] = { |
---|
[0ef1c92] | 854 | { "cap", 1, irc_cmd_cap, 0 }, |
---|
[a199d33] | 855 | { "pass", 1, irc_cmd_pass, 0 }, |
---|
[d179fd90] | 856 | { "proxy", 5, irc_cmd_proxy, IRC_CMD_PRE_LOGIN }, |
---|
[0298d11] | 857 | { "user", 4, irc_cmd_user, IRC_CMD_PRE_LOGIN }, |
---|
| 858 | { "nick", 1, irc_cmd_nick, 0 }, |
---|
| 859 | { "quit", 0, irc_cmd_quit, 0 }, |
---|
| 860 | { "ping", 0, irc_cmd_ping, 0 }, |
---|
[3923003] | 861 | { "pong", 0, irc_cmd_pong, IRC_CMD_LOGGED_IN }, |
---|
[b9e020a] | 862 | { "join", 1, irc_cmd_join, IRC_CMD_LOGGED_IN }, |
---|
| 863 | { "names", 1, irc_cmd_names, IRC_CMD_LOGGED_IN }, |
---|
| 864 | { "part", 1, irc_cmd_part, IRC_CMD_LOGGED_IN }, |
---|
[b95932e] | 865 | { "whois", 1, irc_cmd_whois, IRC_CMD_LOGGED_IN }, |
---|
| 866 | { "whowas", 1, irc_cmd_whowas, IRC_CMD_LOGGED_IN }, |
---|
[9b69eb7] | 867 | { "motd", 0, irc_cmd_motd, IRC_CMD_LOGGED_IN }, |
---|
[b919363] | 868 | { "mode", 1, irc_cmd_mode, IRC_CMD_LOGGED_IN }, |
---|
[2f53ada] | 869 | { "who", 0, irc_cmd_who, IRC_CMD_LOGGED_IN }, |
---|
[280c56a] | 870 | { "privmsg", 1, irc_cmd_privmsg, IRC_CMD_LOGGED_IN }, |
---|
[d7f8500] | 871 | { "notice", 1, irc_cmd_notice, IRC_CMD_LOGGED_IN }, |
---|
[d860a8d] | 872 | { "nickserv", 1, irc_cmd_nickserv, IRC_CMD_LOGGED_IN }, |
---|
| 873 | { "ns", 1, irc_cmd_nickserv, IRC_CMD_LOGGED_IN }, |
---|
[81186cab] | 874 | { "away", 0, irc_cmd_away, IRC_CMD_LOGGED_IN }, |
---|
[d7d677d] | 875 | { "version", 0, irc_cmd_version, IRC_CMD_LOGGED_IN }, |
---|
| 876 | { "completions", 0, irc_cmd_completions, IRC_CMD_LOGGED_IN }, |
---|
[0298d11] | 877 | { "userhost", 1, irc_cmd_userhost, IRC_CMD_LOGGED_IN }, |
---|
| 878 | { "ison", 1, irc_cmd_ison, IRC_CMD_LOGGED_IN }, |
---|
| 879 | { "watch", 1, irc_cmd_watch, IRC_CMD_LOGGED_IN }, |
---|
[003a12b] | 880 | { "invite", 2, irc_cmd_invite, IRC_CMD_LOGGED_IN }, |
---|
[7821ee8] | 881 | { "kick", 2, irc_cmd_kick, IRC_CMD_LOGGED_IN }, |
---|
[4469e7e] | 882 | { "topic", 1, irc_cmd_topic, IRC_CMD_LOGGED_IN }, |
---|
[003a12b] | 883 | { "oper", 2, irc_cmd_oper, IRC_CMD_LOGGED_IN }, |
---|
[23d6165] | 884 | { "list", 0, irc_cmd_list, IRC_CMD_LOGGED_IN }, |
---|
[0431ea1] | 885 | { "die", 0, NULL, IRC_CMD_OPER_ONLY | IRC_CMD_TO_MASTER }, |
---|
[565a1ea] | 886 | { "deaf", 0, NULL, IRC_CMD_OPER_ONLY | IRC_CMD_TO_MASTER }, |
---|
[48721c3] | 887 | { "wallops", 1, NULL, IRC_CMD_OPER_ONLY | IRC_CMD_TO_MASTER }, |
---|
[dfc8a46] | 888 | { "wall", 1, NULL, IRC_CMD_OPER_ONLY | IRC_CMD_TO_MASTER }, |
---|
[f4a5940] | 889 | { "rehash", 0, irc_cmd_rehash, IRC_CMD_OPER_ONLY }, |
---|
[54879ab] | 890 | { "restart", 0, NULL, IRC_CMD_OPER_ONLY | IRC_CMD_TO_MASTER }, |
---|
[48721c3] | 891 | { "kill", 2, NULL, IRC_CMD_OPER_ONLY | IRC_CMD_TO_MASTER }, |
---|
[58b63de6] | 892 | { "authenticate", 1, irc_cmd_authenticate, 0 }, |
---|
[0298d11] | 893 | { NULL } |
---|
| 894 | }; |
---|
| 895 | |
---|
[5ebff60] | 896 | void irc_exec(irc_t *irc, char *cmd[]) |
---|
| 897 | { |
---|
[f1d38f2] | 898 | int i, n_arg; |
---|
[5ebff60] | 899 | |
---|
| 900 | if (!cmd[0]) { |
---|
[f73b969] | 901 | return; |
---|
[5ebff60] | 902 | } |
---|
| 903 | |
---|
| 904 | for (i = 0; irc_commands[i].command; i++) { |
---|
| 905 | if (g_strcasecmp(irc_commands[i].command, cmd[0]) == 0) { |
---|
[f1d38f2] | 906 | /* There should be no typo in the next line: */ |
---|
[5ebff60] | 907 | for (n_arg = 0; cmd[n_arg]; n_arg++) { |
---|
[25c4c78] | 908 | ; |
---|
[edf9657] | 909 | } |
---|
[5ebff60] | 910 | n_arg--; |
---|
| 911 | |
---|
| 912 | if (irc_commands[i].flags & IRC_CMD_PRE_LOGIN && irc->status & USTATUS_LOGGED_IN) { |
---|
| 913 | irc_send_num(irc, 462, ":Only allowed before logging in"); |
---|
| 914 | } else if (irc_commands[i].flags & IRC_CMD_LOGGED_IN && !(irc->status & USTATUS_LOGGED_IN)) { |
---|
| 915 | irc_send_num(irc, 451, ":Register first"); |
---|
| 916 | } else if (irc_commands[i].flags & IRC_CMD_OPER_ONLY && !strchr(irc->umode, 'o')) { |
---|
| 917 | irc_send_num(irc, 481, ":Permission denied - You're not an IRC operator"); |
---|
| 918 | } else if (n_arg < irc_commands[i].required_parameters) { |
---|
| 919 | irc_send_num(irc, 461, "%s :Need more parameters", cmd[0]); |
---|
| 920 | } else if (irc_commands[i].flags & IRC_CMD_TO_MASTER) { |
---|
[5424c76] | 921 | /* IPC doesn't make sense in inetd mode, |
---|
| 922 | but the function will catch that. */ |
---|
[5ebff60] | 923 | ipc_to_master(cmd); |
---|
| 924 | } else { |
---|
| 925 | irc_commands[i].execute(irc, cmd); |
---|
[f73b969] | 926 | } |
---|
[5ebff60] | 927 | |
---|
[2f13222] | 928 | return; |
---|
[0298d11] | 929 | } |
---|
[5ebff60] | 930 | } |
---|
| 931 | |
---|
| 932 | if (irc->status & USTATUS_LOGGED_IN) { |
---|
| 933 | irc_send_num(irc, 421, "%s :Unknown command", cmd[0]); |
---|
| 934 | } |
---|
[0298d11] | 935 | } |
---|