[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) { |
---|
[5c90890] | 451 | GList *all_users = g_hash_table_get_values(irc->nick_user_hash); |
---|
| 452 | irc_send_who(irc, (GSList *) all_users, "**"); |
---|
| 453 | g_list_free(all_users); |
---|
[5ebff60] | 454 | } else if ((ic = irc_channel_by_name(irc, channel))) { |
---|
| 455 | irc_send_who(irc, ic->users, channel); |
---|
| 456 | } else if ((iu = irc_user_by_name(irc, channel))) { |
---|
[a72af0d] | 457 | /* Tiny hack! */ |
---|
[5ebff60] | 458 | GSList *l = g_slist_append(NULL, iu); |
---|
| 459 | irc_send_who(irc, l, channel); |
---|
| 460 | g_slist_free(l); |
---|
| 461 | } else { |
---|
| 462 | irc_send_num(irc, 403, "%s :No such channel", channel); |
---|
[a72af0d] | 463 | } |
---|
[2f53ada] | 464 | } |
---|
| 465 | |
---|
[5ebff60] | 466 | static void irc_cmd_privmsg(irc_t *irc, char **cmd) |
---|
[b919363] | 467 | { |
---|
[280c56a] | 468 | irc_channel_t *ic; |
---|
| 469 | irc_user_t *iu; |
---|
[5ebff60] | 470 | |
---|
| 471 | if (!cmd[2]) { |
---|
| 472 | irc_send_num(irc, 412, ":No text to send"); |
---|
[24b8bbb] | 473 | return; |
---|
[280c56a] | 474 | } |
---|
[5ebff60] | 475 | |
---|
[24b8bbb] | 476 | /* Don't treat CTCP actions as real CTCPs, just convert them right now. */ |
---|
[5ebff60] | 477 | if (g_strncasecmp(cmd[2], "\001ACTION", 7) == 0) { |
---|
[24b8bbb] | 478 | cmd[2] += 4; |
---|
[5ebff60] | 479 | memcpy(cmd[2], "/me", 3); |
---|
| 480 | if (cmd[2][strlen(cmd[2]) - 1] == '\001') { |
---|
| 481 | cmd[2][strlen(cmd[2]) - 1] = '\0'; |
---|
| 482 | } |
---|
[24b8bbb] | 483 | } |
---|
[5ebff60] | 484 | |
---|
| 485 | if (irc_channel_name_ok(cmd[1]) && |
---|
| 486 | (ic = irc_channel_by_name(irc, cmd[1]))) { |
---|
| 487 | if (cmd[2][0] == '\001') { |
---|
[a7dbf45] | 488 | /* CTCPs to channels? Nah. Maybe later. */ |
---|
[5ebff60] | 489 | } else if (ic->f->privmsg) { |
---|
| 490 | ic->f->privmsg(ic, cmd[2]); |
---|
[a7dbf45] | 491 | } |
---|
[5ebff60] | 492 | } else if ((iu = irc_user_by_name(irc, cmd[1]))) { |
---|
| 493 | if (cmd[2][0] == '\001') { |
---|
[24b8bbb] | 494 | char **ctcp; |
---|
[5ebff60] | 495 | |
---|
| 496 | if (iu->f->ctcp == NULL) { |
---|
[24b8bbb] | 497 | return; |
---|
[5ebff60] | 498 | } |
---|
| 499 | if (cmd[2][strlen(cmd[2]) - 1] == '\001') { |
---|
| 500 | cmd[2][strlen(cmd[2]) - 1] = '\0'; |
---|
| 501 | } |
---|
| 502 | |
---|
| 503 | ctcp = split_command_parts(cmd[2] + 1, 0); |
---|
| 504 | iu->f->ctcp(iu, ctcp); |
---|
| 505 | } else if (iu->f->privmsg) { |
---|
[92c8d41] | 506 | iu->last_channel = NULL; |
---|
[5ebff60] | 507 | iu->f->privmsg(iu, cmd[2]); |
---|
[bce78c8] | 508 | } |
---|
[5ebff60] | 509 | } else { |
---|
| 510 | irc_send_num(irc, 401, "%s :No such nick/channel", cmd[1]); |
---|
[b919363] | 511 | } |
---|
[280c56a] | 512 | } |
---|
| 513 | |
---|
[5ebff60] | 514 | static void irc_cmd_notice(irc_t *irc, char **cmd) |
---|
[d7f8500] | 515 | { |
---|
[bbc69f7] | 516 | irc_user_t *iu; |
---|
[5ebff60] | 517 | |
---|
| 518 | if (!cmd[2]) { |
---|
| 519 | irc_send_num(irc, 412, ":No text to send"); |
---|
[d7f8500] | 520 | return; |
---|
| 521 | } |
---|
[5ebff60] | 522 | |
---|
[d7f8500] | 523 | /* At least for now just echo. IIRC some IRC clients use self-notices |
---|
| 524 | for lag checks, so try to support that. */ |
---|
[5ebff60] | 525 | if (nick_cmp(NULL, cmd[1], irc->user->nick) == 0) { |
---|
[9767d03] | 526 | irc_send_msg(irc->user, "NOTICE", irc->user->nick, cmd[2], NULL); |
---|
[5ebff60] | 527 | } else if ((iu = irc_user_by_name(irc, cmd[1]))) { |
---|
| 528 | iu->f->privmsg(iu, cmd[2]); |
---|
| 529 | } |
---|
[d7f8500] | 530 | } |
---|
| 531 | |
---|
[5ebff60] | 532 | static void irc_cmd_nickserv(irc_t *irc, char **cmd) |
---|
[d860a8d] | 533 | { |
---|
| 534 | /* [SH] This aliases the NickServ command to PRIVMSG root */ |
---|
| 535 | /* [TV] This aliases the NS command to PRIVMSG root as well */ |
---|
[5ebff60] | 536 | root_command(irc, cmd + 1); |
---|
[d860a8d] | 537 | } |
---|
| 538 | |
---|
[5ebff60] | 539 | static void irc_cmd_oper_hack(irc_t *irc, char **cmd); |
---|
[280c56a] | 540 | |
---|
[5ebff60] | 541 | static void irc_cmd_oper(irc_t *irc, char **cmd) |
---|
[280c56a] | 542 | { |
---|
[060d066] | 543 | /* Very non-standard evil but useful/secure hack, see below. */ |
---|
[5ebff60] | 544 | if (irc->status & OPER_HACK_ANY) { |
---|
| 545 | return irc_cmd_oper_hack(irc, cmd); |
---|
[280c56a] | 546 | } |
---|
[5ebff60] | 547 | |
---|
| 548 | if (global.conf->oper_pass && |
---|
| 549 | (strncmp(global.conf->oper_pass, "md5:", 4) == 0 ? |
---|
| 550 | md5_verify_password(cmd[2], global.conf->oper_pass + 4) == 0 : |
---|
| 551 | strcmp(cmd[2], global.conf->oper_pass) == 0)) { |
---|
| 552 | irc_umode_set(irc, "+o", 1); |
---|
| 553 | irc_send_num(irc, 381, ":Password accepted"); |
---|
| 554 | } else { |
---|
| 555 | irc_send_num(irc, 491, ":Incorrect password"); |
---|
[280c56a] | 556 | } |
---|
| 557 | } |
---|
| 558 | |
---|
[5ebff60] | 559 | static void irc_cmd_oper_hack(irc_t *irc, char **cmd) |
---|
[060d066] | 560 | { |
---|
[5ebff60] | 561 | char *password = g_strjoinv(" ", cmd + 2); |
---|
| 562 | |
---|
[060d066] | 563 | /* /OPER can now also be used to enter IM/identify passwords without |
---|
| 564 | echoing. It's a hack but the extra password security is worth it. */ |
---|
[5ebff60] | 565 | if (irc->status & OPER_HACK_ACCOUNT_PASSWORD) { |
---|
[060d066] | 566 | account_t *a; |
---|
[5ebff60] | 567 | |
---|
| 568 | for (a = irc->b->accounts; a; a = a->next) { |
---|
| 569 | if (strcmp(a->pass, PASSWORD_PENDING) == 0) { |
---|
| 570 | set_setstr(&a->set, "password", password); |
---|
| 571 | irc_rootmsg(irc, "Password added to IM account " |
---|
| 572 | "%s", a->tag); |
---|
[060d066] | 573 | /* The IRC client may expect this. 491 suggests the OPER |
---|
| 574 | password was wrong, so the client won't expect a +o. |
---|
| 575 | It may however repeat the password prompt. We'll see. */ |
---|
[5ebff60] | 576 | irc_send_num(irc, 491, ":Password added to IM account " |
---|
| 577 | "%s", a->tag); |
---|
[060d066] | 578 | } |
---|
[5ebff60] | 579 | } |
---|
| 580 | } else if (irc->status & OPER_HACK_IDENTIFY) { |
---|
[fda194f] | 581 | char *send_cmd[] = { "identify", password, NULL, NULL }; |
---|
| 582 | irc->status &= ~OPER_HACK_IDENTIFY; |
---|
[5ebff60] | 583 | if (irc->status & OPER_HACK_IDENTIFY_NOLOAD) { |
---|
[fda194f] | 584 | send_cmd[1] = "-noload"; |
---|
| 585 | send_cmd[2] = password; |
---|
[5ebff60] | 586 | } else if (irc->status & OPER_HACK_IDENTIFY_FORCE) { |
---|
[fda194f] | 587 | send_cmd[1] = "-force"; |
---|
| 588 | send_cmd[2] = password; |
---|
| 589 | } |
---|
[5ebff60] | 590 | irc_send_num(irc, 491, ":Trying to identify"); |
---|
| 591 | root_command(irc, send_cmd); |
---|
| 592 | } else if (irc->status & OPER_HACK_REGISTER) { |
---|
[060d066] | 593 | char *send_cmd[] = { "register", password, NULL }; |
---|
[5ebff60] | 594 | irc_send_num(irc, 491, ":Trying to identify"); |
---|
| 595 | root_command(irc, send_cmd); |
---|
[060d066] | 596 | } |
---|
[5ebff60] | 597 | |
---|
[060d066] | 598 | irc->status &= ~OPER_HACK_ANY; |
---|
[5ebff60] | 599 | g_free(password); |
---|
[060d066] | 600 | } |
---|
| 601 | |
---|
[5ebff60] | 602 | static void irc_cmd_invite(irc_t *irc, char **cmd) |
---|
[280c56a] | 603 | { |
---|
[66b9e36a] | 604 | irc_channel_t *ic; |
---|
| 605 | irc_user_t *iu; |
---|
[5ebff60] | 606 | |
---|
| 607 | if ((iu = irc_user_by_name(irc, cmd[1])) == NULL) { |
---|
| 608 | irc_send_num(irc, 401, "%s :No such nick", cmd[1]); |
---|
[66b9e36a] | 609 | return; |
---|
[5ebff60] | 610 | } else if ((ic = irc_channel_by_name(irc, cmd[2])) == NULL) { |
---|
| 611 | irc_send_num(irc, 403, "%s :No such channel", cmd[2]); |
---|
[66b9e36a] | 612 | return; |
---|
| 613 | } |
---|
[5ebff60] | 614 | |
---|
| 615 | if (!ic->f->invite) { |
---|
| 616 | irc_send_num(irc, 482, "%s :Can't invite people here", cmd[2]); |
---|
| 617 | } else if (ic->f->invite(ic, iu)) { |
---|
| 618 | irc_send_num(irc, 341, "%s %s", iu->nick, ic->name); |
---|
| 619 | } |
---|
[0298d11] | 620 | } |
---|
| 621 | |
---|
[5ebff60] | 622 | static void irc_cmd_kick(irc_t *irc, char **cmd) |
---|
[7821ee8] | 623 | { |
---|
| 624 | irc_channel_t *ic; |
---|
| 625 | irc_user_t *iu; |
---|
[5ebff60] | 626 | |
---|
| 627 | if ((iu = irc_user_by_name(irc, cmd[2])) == NULL) { |
---|
| 628 | irc_send_num(irc, 401, "%s :No such nick", cmd[2]); |
---|
[7821ee8] | 629 | return; |
---|
[5ebff60] | 630 | } else if ((ic = irc_channel_by_name(irc, cmd[1])) == NULL) { |
---|
| 631 | irc_send_num(irc, 403, "%s :No such channel", cmd[1]); |
---|
[7821ee8] | 632 | return; |
---|
[5ebff60] | 633 | } else if (!ic->f->kick) { |
---|
| 634 | irc_send_num(irc, 482, "%s :Can't kick people here", cmd[1]); |
---|
[7821ee8] | 635 | return; |
---|
| 636 | } |
---|
[5ebff60] | 637 | |
---|
| 638 | ic->f->kick(ic, iu, cmd[3] ? cmd[3] : NULL); |
---|
[7821ee8] | 639 | } |
---|
| 640 | |
---|
[5ebff60] | 641 | static void irc_cmd_userhost(irc_t *irc, char **cmd) |
---|
[0298d11] | 642 | { |
---|
| 643 | int i; |
---|
[5ebff60] | 644 | |
---|
[0298d11] | 645 | /* [TV] Usable USERHOST-implementation according to |
---|
[5ebff60] | 646 | RFC1459. Without this, mIRC shows an error |
---|
| 647 | while connecting, and the used way of rejecting |
---|
| 648 | breaks standards. |
---|
[0298d11] | 649 | */ |
---|
[5ebff60] | 650 | |
---|
| 651 | for (i = 1; cmd[i]; i++) { |
---|
| 652 | irc_user_t *iu = irc_user_by_name(irc, cmd[i]); |
---|
| 653 | |
---|
| 654 | if (iu) { |
---|
| 655 | irc_send_num(irc, 302, ":%s=%c%s@%s", iu->nick, |
---|
| 656 | irc_user_get_away(iu) ? '-' : '+', |
---|
| 657 | iu->user, iu->host); |
---|
| 658 | } |
---|
[003a12b] | 659 | } |
---|
[0298d11] | 660 | } |
---|
| 661 | |
---|
[5ebff60] | 662 | static void irc_cmd_ison(irc_t *irc, char **cmd) |
---|
[0298d11] | 663 | { |
---|
[b4e4b95] | 664 | char buff[IRC_MAX_LINE]; |
---|
[0298d11] | 665 | int lenleft, i; |
---|
[5ebff60] | 666 | |
---|
[0298d11] | 667 | buff[0] = '\0'; |
---|
[5ebff60] | 668 | |
---|
[0298d11] | 669 | /* [SH] Leave room for : and \0 */ |
---|
| 670 | lenleft = IRC_MAX_LINE - 2; |
---|
[5ebff60] | 671 | |
---|
| 672 | for (i = 1; cmd[i]; i++) { |
---|
[42616d1] | 673 | char *this, *next; |
---|
[5ebff60] | 674 | |
---|
[42616d1] | 675 | this = cmd[i]; |
---|
[5ebff60] | 676 | while (*this) { |
---|
[003a12b] | 677 | irc_user_t *iu; |
---|
[5ebff60] | 678 | |
---|
| 679 | if ((next = strchr(this, ' '))) { |
---|
[42616d1] | 680 | *next = 0; |
---|
[5ebff60] | 681 | } |
---|
| 682 | |
---|
| 683 | if ((iu = irc_user_by_name(irc, this)) && |
---|
| 684 | iu->bu && iu->bu->flags & BEE_USER_ONLINE) { |
---|
| 685 | lenleft -= strlen(iu->nick) + 1; |
---|
| 686 | |
---|
| 687 | if (lenleft < 0) { |
---|
[42616d1] | 688 | break; |
---|
[5ebff60] | 689 | } |
---|
| 690 | |
---|
| 691 | strcat(buff, iu->nick); |
---|
| 692 | strcat(buff, " "); |
---|
[0298d11] | 693 | } |
---|
[5ebff60] | 694 | |
---|
| 695 | if (next) { |
---|
[42616d1] | 696 | *next = ' '; |
---|
| 697 | this = next + 1; |
---|
[5ebff60] | 698 | } else { |
---|
[42616d1] | 699 | break; |
---|
[5ebff60] | 700 | } |
---|
[0298d11] | 701 | } |
---|
[5ebff60] | 702 | |
---|
[42616d1] | 703 | /* *sigh* */ |
---|
[5ebff60] | 704 | if (lenleft < 0) { |
---|
[42616d1] | 705 | break; |
---|
[5ebff60] | 706 | } |
---|
| 707 | } |
---|
| 708 | |
---|
| 709 | if (strlen(buff) > 0) { |
---|
| 710 | buff[strlen(buff) - 1] = '\0'; |
---|
[0298d11] | 711 | } |
---|
[5ebff60] | 712 | |
---|
| 713 | irc_send_num(irc, 303, ":%s", buff); |
---|
[0298d11] | 714 | } |
---|
| 715 | |
---|
[5ebff60] | 716 | static void irc_cmd_watch(irc_t *irc, char **cmd) |
---|
[0298d11] | 717 | { |
---|
| 718 | int i; |
---|
[5ebff60] | 719 | |
---|
[0298d11] | 720 | /* Obviously we could also mark a user structure as being |
---|
| 721 | watched, but what if the WATCH command is sent right |
---|
| 722 | after connecting? The user won't exist yet then... */ |
---|
[5ebff60] | 723 | for (i = 1; cmd[i]; i++) { |
---|
[0298d11] | 724 | char *nick; |
---|
[003a12b] | 725 | irc_user_t *iu; |
---|
[5ebff60] | 726 | |
---|
| 727 | if (!cmd[i][0] || !cmd[i][1]) { |
---|
[0298d11] | 728 | break; |
---|
| 729 | } |
---|
[5ebff60] | 730 | |
---|
| 731 | nick = g_strdup(cmd[i] + 1); |
---|
| 732 | nick_lc(irc, nick); |
---|
| 733 | |
---|
| 734 | iu = irc_user_by_name(irc, nick); |
---|
| 735 | |
---|
| 736 | if (cmd[i][0] == '+') { |
---|
| 737 | if (!g_hash_table_lookup(irc->watches, nick)) { |
---|
| 738 | g_hash_table_insert(irc->watches, nick, nick); |
---|
| 739 | } |
---|
| 740 | |
---|
| 741 | if (iu && iu->bu && iu->bu->flags & BEE_USER_ONLINE) { |
---|
| 742 | irc_send_num(irc, 604, "%s %s %s %d :%s", iu->nick, iu->user, |
---|
| 743 | iu->host, (int) time(NULL), "is online"); |
---|
| 744 | } else { |
---|
| 745 | irc_send_num(irc, 605, "%s %s %s %d :%s", nick, "*", "*", |
---|
| 746 | (int) time(NULL), "is offline"); |
---|
| 747 | } |
---|
| 748 | } else if (cmd[i][0] == '-') { |
---|
[0298d11] | 749 | gpointer okey, ovalue; |
---|
[5ebff60] | 750 | |
---|
| 751 | if (g_hash_table_lookup_extended(irc->watches, nick, &okey, &ovalue)) { |
---|
| 752 | g_hash_table_remove(irc->watches, okey); |
---|
| 753 | g_free(okey); |
---|
| 754 | |
---|
| 755 | irc_send_num(irc, 602, "%s %s %s %d :%s", nick, "*", "*", 0, "Stopped watching"); |
---|
[0298d11] | 756 | } |
---|
| 757 | } |
---|
| 758 | } |
---|
| 759 | } |
---|
| 760 | |
---|
[5ebff60] | 761 | static void irc_cmd_topic(irc_t *irc, char **cmd) |
---|
[0298d11] | 762 | { |
---|
[5ebff60] | 763 | irc_channel_t *ic = irc_channel_by_name(irc, cmd[1]); |
---|
[4469e7e] | 764 | const char *new = cmd[2]; |
---|
[5ebff60] | 765 | |
---|
| 766 | if (ic == NULL) { |
---|
| 767 | irc_send_num(irc, 403, "%s :No such channel", cmd[1]); |
---|
| 768 | } else if (new) { |
---|
| 769 | if (ic->f->topic == NULL) { |
---|
| 770 | irc_send_num(irc, 482, "%s :Can't change this channel's topic", ic->name); |
---|
| 771 | } else if (ic->f->topic(ic, new)) { |
---|
| 772 | irc_send_topic(ic, TRUE); |
---|
| 773 | } |
---|
| 774 | } else { |
---|
| 775 | irc_send_topic(ic, FALSE); |
---|
[50e1776] | 776 | } |
---|
[0298d11] | 777 | } |
---|
| 778 | |
---|
[5ebff60] | 779 | static void irc_cmd_away(irc_t *irc, char **cmd) |
---|
[0298d11] | 780 | { |
---|
[5ebff60] | 781 | if (cmd[1] && *cmd[1]) { |
---|
| 782 | char away[strlen(cmd[1]) + 1]; |
---|
[0298d11] | 783 | int i, j; |
---|
[5ebff60] | 784 | |
---|
[0298d11] | 785 | /* Copy away string, but skip control chars. Mainly because |
---|
| 786 | Jabber really doesn't like them. */ |
---|
[5ebff60] | 787 | for (i = j = 0; cmd[1][i]; i++) { |
---|
| 788 | if ((unsigned char) (away[j] = cmd[1][i]) >= ' ') { |
---|
| 789 | j++; |
---|
| 790 | } |
---|
| 791 | } |
---|
[81186cab] | 792 | away[j] = '\0'; |
---|
[5ebff60] | 793 | |
---|
| 794 | irc_send_num(irc, 306, ":You're now away: %s", away); |
---|
| 795 | set_setstr(&irc->b->set, "away", away); |
---|
| 796 | } else { |
---|
| 797 | irc_send_num(irc, 305, ":Welcome back"); |
---|
| 798 | set_setstr(&irc->b->set, "away", NULL); |
---|
[0298d11] | 799 | } |
---|
| 800 | } |
---|
| 801 | |
---|
[5ebff60] | 802 | static void irc_cmd_list(irc_t *irc, char **cmd) |
---|
[23d6165] | 803 | { |
---|
| 804 | GSList *l; |
---|
[5ebff60] | 805 | |
---|
| 806 | for (l = irc->channels; l; l = l->next) { |
---|
[23d6165] | 807 | irc_channel_t *ic = l->data; |
---|
[5ebff60] | 808 | |
---|
| 809 | irc_send_num(irc, 322, "%s %d :%s", |
---|
| 810 | ic->name, g_slist_length(ic->users), ic->topic ? : ""); |
---|
[23d6165] | 811 | } |
---|
[5ebff60] | 812 | irc_send_num(irc, 323, ":%s", "End of /LIST"); |
---|
[23d6165] | 813 | } |
---|
| 814 | |
---|
[5ebff60] | 815 | static void irc_cmd_version(irc_t *irc, char **cmd) |
---|
[82898af] | 816 | { |
---|
[2e78f75] | 817 | irc_send_num(irc, 351, "%s-%s. %s :", |
---|
| 818 | PACKAGE, BITLBEE_VERSION, irc->root->host); |
---|
[82898af] | 819 | } |
---|
| 820 | |
---|
[5ebff60] | 821 | static void irc_cmd_completions(irc_t *irc, char **cmd) |
---|
[0298d11] | 822 | { |
---|
| 823 | help_t *h; |
---|
| 824 | set_t *s; |
---|
| 825 | int i; |
---|
[5ebff60] | 826 | |
---|
[9767d03] | 827 | irc_send_msg_raw(irc->root, "NOTICE", irc->user->nick, "COMPLETIONS OK"); |
---|
[5ebff60] | 828 | |
---|
| 829 | for (i = 0; root_commands[i].command; i++) { |
---|
| 830 | irc_send_msg_f(irc->root, "NOTICE", irc->user->nick, "COMPLETIONS %s", root_commands[i].command); |
---|
| 831 | } |
---|
| 832 | |
---|
| 833 | for (h = global.help; h; h = h->next) { |
---|
| 834 | irc_send_msg_f(irc->root, "NOTICE", irc->user->nick, "COMPLETIONS help %s", h->title); |
---|
| 835 | } |
---|
| 836 | |
---|
| 837 | for (s = irc->b->set; s; s = s->next) { |
---|
| 838 | irc_send_msg_f(irc->root, "NOTICE", irc->user->nick, "COMPLETIONS set %s", s->key); |
---|
| 839 | } |
---|
| 840 | |
---|
[9767d03] | 841 | irc_send_msg_raw(irc->root, "NOTICE", irc->user->nick, "COMPLETIONS END"); |
---|
[0298d11] | 842 | } |
---|
| 843 | |
---|
[5ebff60] | 844 | static void irc_cmd_rehash(irc_t *irc, char **cmd) |
---|
[f4a5940] | 845 | { |
---|
[5ebff60] | 846 | if (global.conf->runmode == RUNMODE_INETD) { |
---|
| 847 | ipc_master_cmd_rehash(NULL, NULL); |
---|
| 848 | } else { |
---|
| 849 | ipc_to_master(cmd); |
---|
| 850 | } |
---|
| 851 | |
---|
| 852 | irc_send_num(irc, 382, "%s :Rehashing", global.conf_file); |
---|
[f4a5940] | 853 | } |
---|
| 854 | |
---|
[0298d11] | 855 | static const command_t irc_commands[] = { |
---|
[0ef1c92] | 856 | { "cap", 1, irc_cmd_cap, 0 }, |
---|
[a199d33] | 857 | { "pass", 1, irc_cmd_pass, 0 }, |
---|
[d179fd90] | 858 | { "proxy", 5, irc_cmd_proxy, IRC_CMD_PRE_LOGIN }, |
---|
[0298d11] | 859 | { "user", 4, irc_cmd_user, IRC_CMD_PRE_LOGIN }, |
---|
| 860 | { "nick", 1, irc_cmd_nick, 0 }, |
---|
| 861 | { "quit", 0, irc_cmd_quit, 0 }, |
---|
| 862 | { "ping", 0, irc_cmd_ping, 0 }, |
---|
[3923003] | 863 | { "pong", 0, irc_cmd_pong, IRC_CMD_LOGGED_IN }, |
---|
[b9e020a] | 864 | { "join", 1, irc_cmd_join, IRC_CMD_LOGGED_IN }, |
---|
| 865 | { "names", 1, irc_cmd_names, IRC_CMD_LOGGED_IN }, |
---|
| 866 | { "part", 1, irc_cmd_part, IRC_CMD_LOGGED_IN }, |
---|
[b95932e] | 867 | { "whois", 1, irc_cmd_whois, IRC_CMD_LOGGED_IN }, |
---|
| 868 | { "whowas", 1, irc_cmd_whowas, IRC_CMD_LOGGED_IN }, |
---|
[9b69eb7] | 869 | { "motd", 0, irc_cmd_motd, IRC_CMD_LOGGED_IN }, |
---|
[b919363] | 870 | { "mode", 1, irc_cmd_mode, IRC_CMD_LOGGED_IN }, |
---|
[2f53ada] | 871 | { "who", 0, irc_cmd_who, IRC_CMD_LOGGED_IN }, |
---|
[280c56a] | 872 | { "privmsg", 1, irc_cmd_privmsg, IRC_CMD_LOGGED_IN }, |
---|
[d7f8500] | 873 | { "notice", 1, irc_cmd_notice, IRC_CMD_LOGGED_IN }, |
---|
[d860a8d] | 874 | { "nickserv", 1, irc_cmd_nickserv, IRC_CMD_LOGGED_IN }, |
---|
| 875 | { "ns", 1, irc_cmd_nickserv, IRC_CMD_LOGGED_IN }, |
---|
[81186cab] | 876 | { "away", 0, irc_cmd_away, IRC_CMD_LOGGED_IN }, |
---|
[d7d677d] | 877 | { "version", 0, irc_cmd_version, IRC_CMD_LOGGED_IN }, |
---|
| 878 | { "completions", 0, irc_cmd_completions, IRC_CMD_LOGGED_IN }, |
---|
[0298d11] | 879 | { "userhost", 1, irc_cmd_userhost, IRC_CMD_LOGGED_IN }, |
---|
| 880 | { "ison", 1, irc_cmd_ison, IRC_CMD_LOGGED_IN }, |
---|
| 881 | { "watch", 1, irc_cmd_watch, IRC_CMD_LOGGED_IN }, |
---|
[003a12b] | 882 | { "invite", 2, irc_cmd_invite, IRC_CMD_LOGGED_IN }, |
---|
[7821ee8] | 883 | { "kick", 2, irc_cmd_kick, IRC_CMD_LOGGED_IN }, |
---|
[4469e7e] | 884 | { "topic", 1, irc_cmd_topic, IRC_CMD_LOGGED_IN }, |
---|
[003a12b] | 885 | { "oper", 2, irc_cmd_oper, IRC_CMD_LOGGED_IN }, |
---|
[23d6165] | 886 | { "list", 0, irc_cmd_list, IRC_CMD_LOGGED_IN }, |
---|
[0431ea1] | 887 | { "die", 0, NULL, IRC_CMD_OPER_ONLY | IRC_CMD_TO_MASTER }, |
---|
[565a1ea] | 888 | { "deaf", 0, NULL, IRC_CMD_OPER_ONLY | IRC_CMD_TO_MASTER }, |
---|
[48721c3] | 889 | { "wallops", 1, NULL, IRC_CMD_OPER_ONLY | IRC_CMD_TO_MASTER }, |
---|
[dfc8a46] | 890 | { "wall", 1, NULL, IRC_CMD_OPER_ONLY | IRC_CMD_TO_MASTER }, |
---|
[f4a5940] | 891 | { "rehash", 0, irc_cmd_rehash, IRC_CMD_OPER_ONLY }, |
---|
[54879ab] | 892 | { "restart", 0, NULL, IRC_CMD_OPER_ONLY | IRC_CMD_TO_MASTER }, |
---|
[48721c3] | 893 | { "kill", 2, NULL, IRC_CMD_OPER_ONLY | IRC_CMD_TO_MASTER }, |
---|
[58b63de6] | 894 | { "authenticate", 1, irc_cmd_authenticate, 0 }, |
---|
[0298d11] | 895 | { NULL } |
---|
| 896 | }; |
---|
| 897 | |
---|
[5ebff60] | 898 | void irc_exec(irc_t *irc, char *cmd[]) |
---|
| 899 | { |
---|
[f1d38f2] | 900 | int i, n_arg; |
---|
[5ebff60] | 901 | |
---|
| 902 | if (!cmd[0]) { |
---|
[f73b969] | 903 | return; |
---|
[5ebff60] | 904 | } |
---|
| 905 | |
---|
| 906 | for (i = 0; irc_commands[i].command; i++) { |
---|
| 907 | if (g_strcasecmp(irc_commands[i].command, cmd[0]) == 0) { |
---|
[f1d38f2] | 908 | /* There should be no typo in the next line: */ |
---|
[5ebff60] | 909 | for (n_arg = 0; cmd[n_arg]; n_arg++) { |
---|
[25c4c78] | 910 | ; |
---|
[edf9657] | 911 | } |
---|
[5ebff60] | 912 | n_arg--; |
---|
| 913 | |
---|
| 914 | if (irc_commands[i].flags & IRC_CMD_PRE_LOGIN && irc->status & USTATUS_LOGGED_IN) { |
---|
| 915 | irc_send_num(irc, 462, ":Only allowed before logging in"); |
---|
| 916 | } else if (irc_commands[i].flags & IRC_CMD_LOGGED_IN && !(irc->status & USTATUS_LOGGED_IN)) { |
---|
| 917 | irc_send_num(irc, 451, ":Register first"); |
---|
| 918 | } else if (irc_commands[i].flags & IRC_CMD_OPER_ONLY && !strchr(irc->umode, 'o')) { |
---|
| 919 | irc_send_num(irc, 481, ":Permission denied - You're not an IRC operator"); |
---|
| 920 | } else if (n_arg < irc_commands[i].required_parameters) { |
---|
| 921 | irc_send_num(irc, 461, "%s :Need more parameters", cmd[0]); |
---|
| 922 | } else if (irc_commands[i].flags & IRC_CMD_TO_MASTER) { |
---|
[5424c76] | 923 | /* IPC doesn't make sense in inetd mode, |
---|
| 924 | but the function will catch that. */ |
---|
[5ebff60] | 925 | ipc_to_master(cmd); |
---|
| 926 | } else { |
---|
| 927 | irc_commands[i].execute(irc, cmd); |
---|
[f73b969] | 928 | } |
---|
[5ebff60] | 929 | |
---|
[2f13222] | 930 | return; |
---|
[0298d11] | 931 | } |
---|
[5ebff60] | 932 | } |
---|
| 933 | |
---|
| 934 | if (irc->status & USTATUS_LOGGED_IN) { |
---|
| 935 | irc_send_num(irc, 421, "%s :Unknown command", cmd[0]); |
---|
| 936 | } |
---|
[0298d11] | 937 | } |
---|