[0298d11] | 1 | /********************************************************************\ |
---|
| 2 | * BitlBee -- An IRC to other IM-networks gateway * |
---|
| 3 | * * |
---|
[3ddb7477] | 4 | * Copyright 2002-2010 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; |
---|
| 22 | if not, write to the Free Software Foundation, Inc., 59 Temple Place, |
---|
| 23 | Suite 330, Boston, MA 02111-1307 USA |
---|
| 24 | */ |
---|
| 25 | |
---|
| 26 | #define BITLBEE_CORE |
---|
| 27 | #include "bitlbee.h" |
---|
[0431ea1] | 28 | #include "ipc.h" |
---|
[0298d11] | 29 | |
---|
[f73b969] | 30 | static void irc_cmd_pass( irc_t *irc, char **cmd ) |
---|
[0298d11] | 31 | { |
---|
[a199d33] | 32 | if( irc->status & USTATUS_LOGGED_IN ) |
---|
| 33 | { |
---|
| 34 | char *send_cmd[] = { "identify", cmd[1], NULL }; |
---|
| 35 | |
---|
| 36 | /* We're already logged in, this client seems to send the PASS |
---|
| 37 | command last. (Possibly it won't send it at all if it turns |
---|
| 38 | out we don't require it, which will break this feature.) |
---|
| 39 | Try to identify using the given password. */ |
---|
[fb117aee] | 40 | return root_command( irc, send_cmd ); |
---|
[a199d33] | 41 | } |
---|
| 42 | /* Handling in pre-logged-in state, first see if this server is |
---|
| 43 | password-protected: */ |
---|
| 44 | else if( global.conf->auth_pass && |
---|
[c029350] | 45 | ( strncmp( global.conf->auth_pass, "md5:", 4 ) == 0 ? |
---|
| 46 | md5_verify_password( cmd[1], global.conf->auth_pass + 4 ) == 0 : |
---|
| 47 | strcmp( cmd[1], global.conf->auth_pass ) == 0 ) ) |
---|
[0298d11] | 48 | { |
---|
[79e826a] | 49 | irc->status |= USTATUS_AUTHORIZED; |
---|
[de3e100] | 50 | irc_check_login( irc ); |
---|
[0298d11] | 51 | } |
---|
[a199d33] | 52 | else if( global.conf->auth_pass ) |
---|
[0298d11] | 53 | { |
---|
[3ddb7477] | 54 | irc_send_num( irc, 464, ":Incorrect password" ); |
---|
[0298d11] | 55 | } |
---|
[a199d33] | 56 | else |
---|
| 57 | { |
---|
| 58 | /* Remember the password and try to identify after USER/NICK. */ |
---|
[70f69ecc] | 59 | irc_setpass( irc, cmd[1] ); |
---|
[a199d33] | 60 | irc_check_login( irc ); |
---|
| 61 | } |
---|
[0298d11] | 62 | } |
---|
| 63 | |
---|
[f73b969] | 64 | static void irc_cmd_user( irc_t *irc, char **cmd ) |
---|
[0298d11] | 65 | { |
---|
[3ddb7477] | 66 | irc->user->user = g_strdup( cmd[1] ); |
---|
| 67 | irc->user->fullname = g_strdup( cmd[4] ); |
---|
[edf9657] | 68 | |
---|
| 69 | irc_check_login( irc ); |
---|
[0298d11] | 70 | } |
---|
| 71 | |
---|
[f73b969] | 72 | static void irc_cmd_nick( irc_t *irc, char **cmd ) |
---|
[0298d11] | 73 | { |
---|
[9a9b520] | 74 | irc_user_t *iu; |
---|
| 75 | |
---|
| 76 | if( ( iu = irc_user_by_name( irc, cmd[1] ) ) && iu != irc->user ) |
---|
[0298d11] | 77 | { |
---|
[9a9b520] | 78 | irc_send_num( irc, 433, "%s :This nick is already in use", cmd[1] ); |
---|
[0298d11] | 79 | } |
---|
| 80 | else if( !nick_ok( cmd[1] ) ) |
---|
| 81 | { |
---|
| 82 | /* [SH] Invalid characters. */ |
---|
[9a9b520] | 83 | irc_send_num( irc, 432, "%s :This nick contains invalid characters", cmd[1] ); |
---|
[0298d11] | 84 | } |
---|
[9a9b520] | 85 | else if( irc->status & USTATUS_LOGGED_IN ) |
---|
[ffa1173] | 86 | { |
---|
| 87 | if( irc->status & USTATUS_IDENTIFIED ) |
---|
| 88 | { |
---|
| 89 | irc_setpass( irc, NULL ); |
---|
| 90 | irc->status &= ~USTATUS_IDENTIFIED; |
---|
| 91 | irc_umode_set( irc, "-R", 1 ); |
---|
[92cb8c4] | 92 | irc_usermsg( irc, "Changing nicks resets your identify status. " |
---|
| 93 | "Re-identify or register a new account if you want " |
---|
| 94 | "your configuration to be saved. See \x02help " |
---|
| 95 | "nick_changes\x02." ); |
---|
[ffa1173] | 96 | } |
---|
| 97 | |
---|
| 98 | irc_user_set_nick( irc->user, cmd[1] ); |
---|
| 99 | } |
---|
[0298d11] | 100 | else |
---|
| 101 | { |
---|
[9a9b520] | 102 | g_free( irc->user->nick ); |
---|
[3ddb7477] | 103 | irc->user->nick = g_strdup( cmd[1] ); |
---|
[edf9657] | 104 | |
---|
| 105 | irc_check_login( irc ); |
---|
[0298d11] | 106 | } |
---|
| 107 | } |
---|
| 108 | |
---|
[f73b969] | 109 | static void irc_cmd_quit( irc_t *irc, char **cmd ) |
---|
[0298d11] | 110 | { |
---|
[f73b969] | 111 | if( cmd[1] && *cmd[1] ) |
---|
| 112 | irc_abort( irc, 0, "Quit: %s", cmd[1] ); |
---|
| 113 | else |
---|
| 114 | irc_abort( irc, 0, "Leaving..." ); |
---|
[0298d11] | 115 | } |
---|
| 116 | |
---|
[f73b969] | 117 | static void irc_cmd_ping( irc_t *irc, char **cmd ) |
---|
[0298d11] | 118 | { |
---|
[ebaebfe] | 119 | irc_write( irc, ":%s PONG %s :%s", irc->root->host, |
---|
| 120 | irc->root->host, cmd[1]?cmd[1]:irc->root->host ); |
---|
[0298d11] | 121 | } |
---|
| 122 | |
---|
[3923003] | 123 | static void irc_cmd_pong( irc_t *irc, char **cmd ) |
---|
| 124 | { |
---|
| 125 | /* We could check the value we get back from the user, but in |
---|
| 126 | fact we don't care, we're just happy s/he's still alive. */ |
---|
| 127 | irc->last_pong = gettime(); |
---|
| 128 | irc->pinging = 0; |
---|
| 129 | } |
---|
| 130 | |
---|
[b9e020a] | 131 | static void irc_cmd_join( irc_t *irc, char **cmd ) |
---|
| 132 | { |
---|
[58646d9] | 133 | char *comma, *s = cmd[1]; |
---|
[b9e020a] | 134 | |
---|
[58646d9] | 135 | while( s ) |
---|
[57119e8] | 136 | { |
---|
[58646d9] | 137 | irc_channel_t *ic; |
---|
| 138 | |
---|
| 139 | if( ( comma = strchr( s, ',' ) ) ) |
---|
| 140 | *comma = '\0'; |
---|
| 141 | |
---|
| 142 | if( ( ic = irc_channel_by_name( irc, s ) ) == NULL ) |
---|
[324c378] | 143 | { |
---|
[58646d9] | 144 | ic = irc_channel_new( irc, s ); |
---|
[324c378] | 145 | |
---|
| 146 | if( strcmp( set_getstr( &ic->set, "type" ), "control" ) != 0 ) |
---|
| 147 | { |
---|
| 148 | /* Autoconfiguration is for control channels only ATM. */ |
---|
| 149 | } |
---|
| 150 | else if( bee_group_by_name( ic->irc->b, ic->name + 1, FALSE ) ) |
---|
| 151 | { |
---|
| 152 | set_setstr( &ic->set, "group", ic->name + 1 ); |
---|
| 153 | set_setstr( &ic->set, "fill_by", "group" ); |
---|
| 154 | } |
---|
| 155 | else if( set_setstr( &ic->set, "protocol", ic->name + 1 ) ) |
---|
| 156 | { |
---|
| 157 | set_setstr( &ic->set, "fill_by", "protocol" ); |
---|
| 158 | } |
---|
| 159 | else if( set_setstr( &ic->set, "account", ic->name + 1 ) ) |
---|
| 160 | { |
---|
| 161 | set_setstr( &ic->set, "fill_by", "account" ); |
---|
| 162 | } |
---|
| 163 | else |
---|
| 164 | { |
---|
| 165 | bee_irc_channel_update( ic->irc, ic, NULL ); |
---|
| 166 | } |
---|
| 167 | } |
---|
[58646d9] | 168 | |
---|
| 169 | if( ic == NULL ) |
---|
| 170 | { |
---|
| 171 | irc_send_num( irc, 479, "%s :Invalid channel name", s ); |
---|
| 172 | goto next; |
---|
| 173 | } |
---|
| 174 | |
---|
| 175 | if( ic->flags & IRC_CHANNEL_JOINED ) |
---|
| 176 | /* Dude, you're already there... |
---|
| 177 | RFC doesn't have any reply for that though? */ |
---|
| 178 | goto next; |
---|
| 179 | |
---|
| 180 | if( ic->f->join && !ic->f->join( ic ) ) |
---|
| 181 | /* The story is: FALSE either means the handler |
---|
| 182 | showed an error message, or is doing some work |
---|
| 183 | before the join should be confirmed. (In the |
---|
| 184 | latter case, the caller should take care of that |
---|
| 185 | confirmation.) TRUE means all's good, let the |
---|
| 186 | user join the channel right away. */ |
---|
| 187 | goto next; |
---|
| 188 | |
---|
| 189 | irc_channel_add_user( ic, irc->user ); |
---|
| 190 | |
---|
| 191 | next: |
---|
| 192 | if( comma ) |
---|
| 193 | { |
---|
| 194 | s = comma + 1; |
---|
| 195 | *comma = ','; |
---|
| 196 | } |
---|
| 197 | else |
---|
| 198 | break; |
---|
[57119e8] | 199 | } |
---|
[b9e020a] | 200 | } |
---|
| 201 | |
---|
| 202 | static void irc_cmd_names( irc_t *irc, char **cmd ) |
---|
| 203 | { |
---|
| 204 | irc_channel_t *ic; |
---|
| 205 | |
---|
| 206 | if( cmd[1] && ( ic = irc_channel_by_name( irc, cmd[1] ) ) ) |
---|
| 207 | irc_send_names( ic ); |
---|
| 208 | /* With no args, we should show /names of all chans. Make the code |
---|
| 209 | below work well if necessary. |
---|
| 210 | else |
---|
| 211 | { |
---|
| 212 | GSList *l; |
---|
| 213 | |
---|
| 214 | for( l = irc->channels; l; l = l->next ) |
---|
| 215 | irc_send_names( l->data ); |
---|
| 216 | } |
---|
| 217 | */ |
---|
| 218 | } |
---|
| 219 | |
---|
| 220 | static void irc_cmd_part( irc_t *irc, char **cmd ) |
---|
| 221 | { |
---|
| 222 | irc_channel_t *ic; |
---|
| 223 | |
---|
| 224 | if( ( ic = irc_channel_by_name( irc, cmd[1] ) ) == NULL ) |
---|
| 225 | { |
---|
| 226 | irc_send_num( irc, 403, "%s :No such channel", cmd[1] ); |
---|
| 227 | } |
---|
[b1af3e8] | 228 | else if( irc_channel_del_user( ic, irc->user, IRC_CDU_PART, cmd[2] ) ) |
---|
[47fae0f] | 229 | { |
---|
| 230 | if( ic->f->part ) |
---|
| 231 | ic->f->part( ic, NULL ); |
---|
| 232 | } |
---|
| 233 | else |
---|
[b9e020a] | 234 | { |
---|
| 235 | irc_send_num( irc, 442, "%s :You're not on that channel", cmd[1] ); |
---|
| 236 | } |
---|
| 237 | } |
---|
| 238 | |
---|
[b95932e] | 239 | static void irc_cmd_whois( irc_t *irc, char **cmd ) |
---|
| 240 | { |
---|
| 241 | char *nick = cmd[1]; |
---|
[280c56a] | 242 | irc_user_t *iu = irc_user_by_name( irc, nick ); |
---|
[b95932e] | 243 | |
---|
| 244 | if( iu ) |
---|
| 245 | irc_send_whois( iu ); |
---|
| 246 | else |
---|
| 247 | irc_send_num( irc, 401, "%s :Nick does not exist", nick ); |
---|
| 248 | } |
---|
| 249 | |
---|
| 250 | static void irc_cmd_whowas( irc_t *irc, char **cmd ) |
---|
| 251 | { |
---|
| 252 | /* For some reason irssi tries a whowas when whois fails. We can |
---|
| 253 | ignore this, but then the user never gets a "user not found" |
---|
| 254 | message from irssi which is a bit annoying. So just respond |
---|
| 255 | with not-found and irssi users will get better error messages */ |
---|
| 256 | |
---|
| 257 | irc_send_num( irc, 406, "%s :Nick does not exist", cmd[1] ); |
---|
| 258 | irc_send_num( irc, 369, "%s :End of WHOWAS", cmd[1] ); |
---|
| 259 | } |
---|
| 260 | |
---|
[9b69eb7] | 261 | static void irc_cmd_motd( irc_t *irc, char **cmd ) |
---|
| 262 | { |
---|
| 263 | irc_send_motd( irc ); |
---|
| 264 | } |
---|
| 265 | |
---|
[f73b969] | 266 | static void irc_cmd_mode( irc_t *irc, char **cmd ) |
---|
[0298d11] | 267 | { |
---|
[b919363] | 268 | if( irc_channel_name_ok( cmd[1] ) ) |
---|
[0298d11] | 269 | { |
---|
[b919363] | 270 | irc_channel_t *ic; |
---|
| 271 | |
---|
| 272 | if( ( ic = irc_channel_by_name( irc, cmd[1] ) ) == NULL ) |
---|
| 273 | irc_send_num( irc, 403, "%s :No such channel", cmd[1] ); |
---|
| 274 | else if( cmd[2] ) |
---|
[0298d11] | 275 | { |
---|
| 276 | if( *cmd[2] == '+' || *cmd[2] == '-' ) |
---|
[3ddb7477] | 277 | irc_send_num( irc, 477, "%s :Can't change channel modes", cmd[1] ); |
---|
[0298d11] | 278 | else if( *cmd[2] == 'b' ) |
---|
[3ddb7477] | 279 | irc_send_num( irc, 368, "%s :No bans possible", cmd[1] ); |
---|
[0298d11] | 280 | } |
---|
| 281 | else |
---|
[b919363] | 282 | irc_send_num( irc, 324, "%s +%s", cmd[1], ic->mode ); |
---|
[0298d11] | 283 | } |
---|
| 284 | else |
---|
| 285 | { |
---|
[b919363] | 286 | if( nick_cmp( cmd[1], irc->user->nick ) == 0 ) |
---|
[0298d11] | 287 | { |
---|
| 288 | if( cmd[2] ) |
---|
| 289 | irc_umode_set( irc, cmd[2], 0 ); |
---|
[2f13222] | 290 | else |
---|
[3ddb7477] | 291 | irc_send_num( irc, 221, "+%s", irc->umode ); |
---|
[0298d11] | 292 | } |
---|
| 293 | else |
---|
[3ddb7477] | 294 | irc_send_num( irc, 502, ":Don't touch their modes" ); |
---|
[0298d11] | 295 | } |
---|
| 296 | } |
---|
| 297 | |
---|
[2f53ada] | 298 | static void irc_cmd_who( irc_t *irc, char **cmd ) |
---|
| 299 | { |
---|
| 300 | char *channel = cmd[1]; |
---|
| 301 | irc_channel_t *ic; |
---|
| 302 | |
---|
| 303 | if( !channel || *channel == '0' || *channel == '*' || !*channel ) |
---|
| 304 | irc_send_who( irc, irc->users, "**" ); |
---|
| 305 | else if( ( ic = irc_channel_by_name( irc, channel ) ) ) |
---|
| 306 | irc_send_who( irc, ic->users, channel ); |
---|
| 307 | else |
---|
| 308 | irc_send_num( irc, 403, "%s :No such channel", channel ); |
---|
| 309 | } |
---|
| 310 | |
---|
[280c56a] | 311 | static void irc_cmd_privmsg( irc_t *irc, char **cmd ) |
---|
[b919363] | 312 | { |
---|
[280c56a] | 313 | irc_channel_t *ic; |
---|
| 314 | irc_user_t *iu; |
---|
| 315 | |
---|
| 316 | if( !cmd[2] ) |
---|
[b919363] | 317 | { |
---|
[280c56a] | 318 | irc_send_num( irc, 412, ":No text to send" ); |
---|
[24b8bbb] | 319 | return; |
---|
[280c56a] | 320 | } |
---|
[24b8bbb] | 321 | |
---|
| 322 | /* Don't treat CTCP actions as real CTCPs, just convert them right now. */ |
---|
| 323 | if( g_strncasecmp( cmd[2], "\001ACTION", 7 ) == 0 ) |
---|
| 324 | { |
---|
| 325 | cmd[2] += 4; |
---|
[e1f3f94] | 326 | memcpy( cmd[2], "/me", 3 ); |
---|
[24b8bbb] | 327 | if( cmd[2][strlen(cmd[2])-1] == '\001' ) |
---|
| 328 | cmd[2][strlen(cmd[2])-1] = '\0'; |
---|
| 329 | } |
---|
| 330 | |
---|
| 331 | if( irc_channel_name_ok( cmd[1] ) && |
---|
| 332 | ( ic = irc_channel_by_name( irc, cmd[1] ) ) ) |
---|
[280c56a] | 333 | { |
---|
| 334 | if( ic->f->privmsg ) |
---|
| 335 | ic->f->privmsg( ic, cmd[2] ); |
---|
| 336 | } |
---|
| 337 | else if( ( iu = irc_user_by_name( irc, cmd[1] ) ) ) |
---|
| 338 | { |
---|
[24b8bbb] | 339 | if( cmd[2][0] == '\001' ) |
---|
| 340 | { |
---|
| 341 | char **ctcp; |
---|
| 342 | |
---|
| 343 | if( iu->f->ctcp == NULL ) |
---|
| 344 | return; |
---|
| 345 | if( cmd[2][strlen(cmd[2])-1] == '\001' ) |
---|
| 346 | cmd[2][strlen(cmd[2])-1] = '\0'; |
---|
| 347 | |
---|
| 348 | ctcp = split_command_parts( cmd[2] + 1 ); |
---|
| 349 | iu->f->ctcp( iu, ctcp ); |
---|
| 350 | } |
---|
| 351 | else if( iu->f->privmsg ) |
---|
[bce78c8] | 352 | { |
---|
[92c8d41] | 353 | iu->last_channel = NULL; |
---|
[280c56a] | 354 | iu->f->privmsg( iu, cmd[2] ); |
---|
[bce78c8] | 355 | } |
---|
[b919363] | 356 | } |
---|
| 357 | else |
---|
| 358 | { |
---|
[280c56a] | 359 | irc_send_num( irc, 401, "%s :No such nick/channel", cmd[1] ); |
---|
[b919363] | 360 | } |
---|
[280c56a] | 361 | } |
---|
| 362 | |
---|
[d7f8500] | 363 | static void irc_cmd_notice( irc_t *irc, char **cmd ) |
---|
| 364 | { |
---|
| 365 | if( !cmd[2] ) |
---|
| 366 | { |
---|
| 367 | irc_send_num( irc, 412, ":No text to send" ); |
---|
| 368 | return; |
---|
| 369 | } |
---|
| 370 | |
---|
| 371 | /* At least for now just echo. IIRC some IRC clients use self-notices |
---|
| 372 | for lag checks, so try to support that. */ |
---|
| 373 | if( nick_cmp( cmd[1], irc->user->nick ) == 0 ) |
---|
| 374 | irc_send_msg( irc->user, "NOTICE", irc->user->nick, cmd[2], NULL ); |
---|
| 375 | } |
---|
| 376 | |
---|
[d860a8d] | 377 | static void irc_cmd_nickserv( irc_t *irc, char **cmd ) |
---|
| 378 | { |
---|
| 379 | /* [SH] This aliases the NickServ command to PRIVMSG root */ |
---|
| 380 | /* [TV] This aliases the NS command to PRIVMSG root as well */ |
---|
| 381 | root_command( irc, cmd + 1 ); |
---|
| 382 | } |
---|
| 383 | |
---|
[280c56a] | 384 | |
---|
| 385 | |
---|
| 386 | static void irc_cmd_oper( irc_t *irc, char **cmd ) |
---|
| 387 | { |
---|
| 388 | if( global.conf->oper_pass && |
---|
| 389 | ( strncmp( global.conf->oper_pass, "md5:", 4 ) == 0 ? |
---|
| 390 | md5_verify_password( cmd[2], global.conf->oper_pass + 4 ) == 0 : |
---|
| 391 | strcmp( cmd[2], global.conf->oper_pass ) == 0 ) ) |
---|
| 392 | { |
---|
| 393 | irc_umode_set( irc, "+o", 1 ); |
---|
| 394 | irc_send_num( irc, 381, ":Password accepted" ); |
---|
| 395 | } |
---|
| 396 | else |
---|
| 397 | { |
---|
| 398 | irc_send_num( irc, 432, ":Incorrect password" ); |
---|
| 399 | } |
---|
| 400 | } |
---|
| 401 | |
---|
| 402 | static void irc_cmd_invite( irc_t *irc, char **cmd ) |
---|
| 403 | { |
---|
[66b9e36a] | 404 | irc_channel_t *ic; |
---|
| 405 | irc_user_t *iu; |
---|
[280c56a] | 406 | |
---|
[66b9e36a] | 407 | if( ( iu = irc_user_by_name( irc, cmd[1] ) ) == NULL ) |
---|
| 408 | { |
---|
| 409 | irc_send_num( irc, 401, "%s :No such nick", cmd[1] ); |
---|
| 410 | return; |
---|
| 411 | } |
---|
| 412 | else if( ( ic = irc_channel_by_name( irc, cmd[2] ) ) == NULL ) |
---|
| 413 | { |
---|
| 414 | irc_send_num( irc, 403, "%s :No such channel", cmd[2] ); |
---|
| 415 | return; |
---|
| 416 | } |
---|
[280c56a] | 417 | |
---|
[46d215d] | 418 | if( !ic->f->invite ) |
---|
[66b9e36a] | 419 | irc_send_num( irc, 482, "%s :Can't invite people here", cmd[2] ); |
---|
[46d215d] | 420 | else if( ic->f->invite( ic, iu ) ) |
---|
| 421 | irc_send_num( irc, 341, "%s %s", iu->nick, ic->name ); |
---|
[0298d11] | 422 | } |
---|
| 423 | |
---|
[f73b969] | 424 | static void irc_cmd_userhost( irc_t *irc, char **cmd ) |
---|
[0298d11] | 425 | { |
---|
| 426 | int i; |
---|
| 427 | |
---|
| 428 | /* [TV] Usable USERHOST-implementation according to |
---|
| 429 | RFC1459. Without this, mIRC shows an error |
---|
| 430 | while connecting, and the used way of rejecting |
---|
| 431 | breaks standards. |
---|
| 432 | */ |
---|
| 433 | |
---|
| 434 | for( i = 1; cmd[i]; i ++ ) |
---|
[003a12b] | 435 | { |
---|
| 436 | irc_user_t *iu = irc_user_by_name( irc, cmd[i] ); |
---|
| 437 | |
---|
| 438 | if( iu ) |
---|
| 439 | irc_send_num( irc, 302, ":%s=%c%s@%s", iu->nick, |
---|
| 440 | irc_user_get_away( iu ) ? '-' : '+', |
---|
| 441 | iu->user, iu->host ); |
---|
| 442 | } |
---|
[0298d11] | 443 | } |
---|
| 444 | |
---|
[f73b969] | 445 | static void irc_cmd_ison( irc_t *irc, char **cmd ) |
---|
[0298d11] | 446 | { |
---|
[b4e4b95] | 447 | char buff[IRC_MAX_LINE]; |
---|
[0298d11] | 448 | int lenleft, i; |
---|
| 449 | |
---|
| 450 | buff[0] = '\0'; |
---|
| 451 | |
---|
| 452 | /* [SH] Leave room for : and \0 */ |
---|
| 453 | lenleft = IRC_MAX_LINE - 2; |
---|
| 454 | |
---|
| 455 | for( i = 1; cmd[i]; i ++ ) |
---|
| 456 | { |
---|
[42616d1] | 457 | char *this, *next; |
---|
| 458 | |
---|
| 459 | this = cmd[i]; |
---|
| 460 | while( *this ) |
---|
[0298d11] | 461 | { |
---|
[003a12b] | 462 | irc_user_t *iu; |
---|
| 463 | |
---|
[42616d1] | 464 | if( ( next = strchr( this, ' ' ) ) ) |
---|
| 465 | *next = 0; |
---|
[0298d11] | 466 | |
---|
[003a12b] | 467 | if( ( iu = irc_user_by_name( irc, this ) ) && |
---|
| 468 | iu->bu && iu->bu->flags & BEE_USER_ONLINE ) |
---|
[0298d11] | 469 | { |
---|
[003a12b] | 470 | lenleft -= strlen( iu->nick ) + 1; |
---|
[42616d1] | 471 | |
---|
| 472 | if( lenleft < 0 ) |
---|
| 473 | break; |
---|
| 474 | |
---|
[003a12b] | 475 | strcat( buff, iu->nick ); |
---|
[42616d1] | 476 | strcat( buff, " " ); |
---|
[0298d11] | 477 | } |
---|
| 478 | |
---|
[42616d1] | 479 | if( next ) |
---|
| 480 | { |
---|
| 481 | *next = ' '; |
---|
| 482 | this = next + 1; |
---|
| 483 | } |
---|
| 484 | else |
---|
| 485 | { |
---|
| 486 | break; |
---|
| 487 | } |
---|
[0298d11] | 488 | } |
---|
[42616d1] | 489 | |
---|
| 490 | /* *sigh* */ |
---|
| 491 | if( lenleft < 0 ) |
---|
| 492 | break; |
---|
[0298d11] | 493 | } |
---|
| 494 | |
---|
| 495 | if( strlen( buff ) > 0 ) |
---|
| 496 | buff[strlen(buff)-1] = '\0'; |
---|
| 497 | |
---|
[3ddb7477] | 498 | irc_send_num( irc, 303, ":%s", buff ); |
---|
[0298d11] | 499 | } |
---|
| 500 | |
---|
[f73b969] | 501 | static void irc_cmd_watch( irc_t *irc, char **cmd ) |
---|
[0298d11] | 502 | { |
---|
| 503 | int i; |
---|
| 504 | |
---|
| 505 | /* Obviously we could also mark a user structure as being |
---|
| 506 | watched, but what if the WATCH command is sent right |
---|
| 507 | after connecting? The user won't exist yet then... */ |
---|
| 508 | for( i = 1; cmd[i]; i ++ ) |
---|
| 509 | { |
---|
| 510 | char *nick; |
---|
[003a12b] | 511 | irc_user_t *iu; |
---|
[0298d11] | 512 | |
---|
| 513 | if( !cmd[i][0] || !cmd[i][1] ) |
---|
| 514 | break; |
---|
| 515 | |
---|
| 516 | nick = g_strdup( cmd[i] + 1 ); |
---|
| 517 | nick_lc( nick ); |
---|
| 518 | |
---|
[003a12b] | 519 | iu = irc_user_by_name( irc, nick ); |
---|
[0298d11] | 520 | |
---|
| 521 | if( cmd[i][0] == '+' ) |
---|
| 522 | { |
---|
| 523 | if( !g_hash_table_lookup( irc->watches, nick ) ) |
---|
| 524 | g_hash_table_insert( irc->watches, nick, nick ); |
---|
| 525 | |
---|
[003a12b] | 526 | if( iu && iu->bu && iu->bu->flags & BEE_USER_ONLINE ) |
---|
| 527 | irc_send_num( irc, 604, "%s %s %s %d :%s", iu->nick, iu->user, |
---|
| 528 | iu->host, (int) time( NULL ), "is online" ); |
---|
[0298d11] | 529 | else |
---|
[003a12b] | 530 | irc_send_num( irc, 605, "%s %s %s %d :%s", nick, "*", "*", |
---|
| 531 | (int) time( NULL ), "is offline" ); |
---|
[0298d11] | 532 | } |
---|
| 533 | else if( cmd[i][0] == '-' ) |
---|
| 534 | { |
---|
| 535 | gpointer okey, ovalue; |
---|
| 536 | |
---|
| 537 | if( g_hash_table_lookup_extended( irc->watches, nick, &okey, &ovalue ) ) |
---|
| 538 | { |
---|
| 539 | g_hash_table_remove( irc->watches, okey ); |
---|
[e59b4f6] | 540 | g_free( okey ); |
---|
[0298d11] | 541 | |
---|
[3ddb7477] | 542 | irc_send_num( irc, 602, "%s %s %s %d :%s", nick, "*", "*", 0, "Stopped watching" ); |
---|
[0298d11] | 543 | } |
---|
| 544 | } |
---|
| 545 | } |
---|
| 546 | } |
---|
| 547 | |
---|
[f73b969] | 548 | static void irc_cmd_topic( irc_t *irc, char **cmd ) |
---|
[0298d11] | 549 | { |
---|
[4469e7e] | 550 | irc_channel_t *ic = irc_channel_by_name( irc, cmd[1] ); |
---|
| 551 | const char *new = cmd[2]; |
---|
[50e1776] | 552 | |
---|
[4469e7e] | 553 | if( ic == NULL ) |
---|
[50e1776] | 554 | { |
---|
[4469e7e] | 555 | irc_send_num( irc, 403, "%s :No such channel", cmd[1] ); |
---|
| 556 | } |
---|
| 557 | else if( new ) |
---|
| 558 | { |
---|
| 559 | if( ic->f->topic == NULL ) |
---|
| 560 | irc_send_num( irc, 482, "%s :Can't change this channel's topic", ic->name ); |
---|
| 561 | else if( ic->f->topic( ic, new ) ) |
---|
| 562 | irc_send_topic( ic, TRUE ); |
---|
[50e1776] | 563 | } |
---|
[0298d11] | 564 | else |
---|
[50e1776] | 565 | { |
---|
[4469e7e] | 566 | irc_send_topic( ic, FALSE ); |
---|
[50e1776] | 567 | } |
---|
[0298d11] | 568 | } |
---|
| 569 | |
---|
[f73b969] | 570 | static void irc_cmd_away( irc_t *irc, char **cmd ) |
---|
[0298d11] | 571 | { |
---|
[81186cab] | 572 | if( cmd[1] && *cmd[1] ) |
---|
[0298d11] | 573 | { |
---|
[81186cab] | 574 | char away[strlen(cmd[1])+1]; |
---|
[0298d11] | 575 | int i, j; |
---|
| 576 | |
---|
| 577 | /* Copy away string, but skip control chars. Mainly because |
---|
| 578 | Jabber really doesn't like them. */ |
---|
[81186cab] | 579 | for( i = j = 0; cmd[1][i]; i ++ ) |
---|
| 580 | if( ( away[j] = cmd[1][i] ) >= ' ' ) |
---|
[0298d11] | 581 | j ++; |
---|
[81186cab] | 582 | away[j] = '\0'; |
---|
[0298d11] | 583 | |
---|
[81186cab] | 584 | irc_send_num( irc, 306, ":You're now away: %s", away ); |
---|
[4a9fd5f] | 585 | set_setstr( &irc->b->set, "away", away ); |
---|
[0298d11] | 586 | } |
---|
| 587 | else |
---|
| 588 | { |
---|
[3ddb7477] | 589 | irc_send_num( irc, 305, ":Welcome back" ); |
---|
[4a9fd5f] | 590 | set_setstr( &irc->b->set, "away", NULL ); |
---|
[0298d11] | 591 | } |
---|
| 592 | } |
---|
| 593 | |
---|
[82898af] | 594 | static void irc_cmd_version( irc_t *irc, char **cmd ) |
---|
| 595 | { |
---|
[d7d677d] | 596 | irc_send_num( irc, 351, "bitlbee-%s. %s :%s/%s ", |
---|
| 597 | BITLBEE_VERSION, irc->root->host, ARCH, CPU ); |
---|
[82898af] | 598 | } |
---|
| 599 | |
---|
[f73b969] | 600 | static void irc_cmd_completions( irc_t *irc, char **cmd ) |
---|
[0298d11] | 601 | { |
---|
| 602 | help_t *h; |
---|
| 603 | set_t *s; |
---|
| 604 | int i; |
---|
| 605 | |
---|
[d7d677d] | 606 | irc_send_msg_raw( irc->root, "NOTICE", irc->user->nick, "COMPLETIONS OK" ); |
---|
[0298d11] | 607 | |
---|
| 608 | for( i = 0; commands[i].command; i ++ ) |
---|
[d7d677d] | 609 | irc_send_msg_f( irc->root, "NOTICE", irc->user->nick, "COMPLETIONS %s", commands[i].command ); |
---|
[0298d11] | 610 | |
---|
| 611 | for( h = global.help; h; h = h->next ) |
---|
[d7d677d] | 612 | irc_send_msg_f( irc->root, "NOTICE", irc->user->nick, "COMPLETIONS help %s", h->title ); |
---|
[0298d11] | 613 | |
---|
[d7d677d] | 614 | for( s = irc->b->set; s; s = s->next ) |
---|
| 615 | irc_send_msg_f( irc->root, "NOTICE", irc->user->nick, "COMPLETIONS set %s", s->key ); |
---|
[0298d11] | 616 | |
---|
[d7d677d] | 617 | irc_send_msg_raw( irc->root, "NOTICE", irc->user->nick, "COMPLETIONS END" ); |
---|
[0298d11] | 618 | } |
---|
| 619 | |
---|
[f73b969] | 620 | static void irc_cmd_rehash( irc_t *irc, char **cmd ) |
---|
[f4a5940] | 621 | { |
---|
[5424c76] | 622 | if( global.conf->runmode == RUNMODE_INETD ) |
---|
| 623 | ipc_master_cmd_rehash( NULL, NULL ); |
---|
| 624 | else |
---|
| 625 | ipc_to_master( cmd ); |
---|
[f4a5940] | 626 | |
---|
[3ddb7477] | 627 | irc_send_num( irc, 382, "%s :Rehashing", global.conf_file ); |
---|
[f4a5940] | 628 | } |
---|
| 629 | |
---|
[0298d11] | 630 | static const command_t irc_commands[] = { |
---|
[a199d33] | 631 | { "pass", 1, irc_cmd_pass, 0 }, |
---|
[0298d11] | 632 | { "user", 4, irc_cmd_user, IRC_CMD_PRE_LOGIN }, |
---|
| 633 | { "nick", 1, irc_cmd_nick, 0 }, |
---|
| 634 | { "quit", 0, irc_cmd_quit, 0 }, |
---|
| 635 | { "ping", 0, irc_cmd_ping, 0 }, |
---|
[3923003] | 636 | { "pong", 0, irc_cmd_pong, IRC_CMD_LOGGED_IN }, |
---|
[b9e020a] | 637 | { "join", 1, irc_cmd_join, IRC_CMD_LOGGED_IN }, |
---|
| 638 | { "names", 1, irc_cmd_names, IRC_CMD_LOGGED_IN }, |
---|
| 639 | { "part", 1, irc_cmd_part, IRC_CMD_LOGGED_IN }, |
---|
[b95932e] | 640 | { "whois", 1, irc_cmd_whois, IRC_CMD_LOGGED_IN }, |
---|
| 641 | { "whowas", 1, irc_cmd_whowas, IRC_CMD_LOGGED_IN }, |
---|
[9b69eb7] | 642 | { "motd", 0, irc_cmd_motd, IRC_CMD_LOGGED_IN }, |
---|
[b919363] | 643 | { "mode", 1, irc_cmd_mode, IRC_CMD_LOGGED_IN }, |
---|
[2f53ada] | 644 | { "who", 0, irc_cmd_who, IRC_CMD_LOGGED_IN }, |
---|
[280c56a] | 645 | { "privmsg", 1, irc_cmd_privmsg, IRC_CMD_LOGGED_IN }, |
---|
[d7f8500] | 646 | { "notice", 1, irc_cmd_notice, IRC_CMD_LOGGED_IN }, |
---|
[d860a8d] | 647 | { "nickserv", 1, irc_cmd_nickserv, IRC_CMD_LOGGED_IN }, |
---|
| 648 | { "ns", 1, irc_cmd_nickserv, IRC_CMD_LOGGED_IN }, |
---|
[81186cab] | 649 | { "away", 0, irc_cmd_away, IRC_CMD_LOGGED_IN }, |
---|
[d7d677d] | 650 | { "version", 0, irc_cmd_version, IRC_CMD_LOGGED_IN }, |
---|
| 651 | { "completions", 0, irc_cmd_completions, IRC_CMD_LOGGED_IN }, |
---|
[0298d11] | 652 | { "userhost", 1, irc_cmd_userhost, IRC_CMD_LOGGED_IN }, |
---|
| 653 | { "ison", 1, irc_cmd_ison, IRC_CMD_LOGGED_IN }, |
---|
| 654 | { "watch", 1, irc_cmd_watch, IRC_CMD_LOGGED_IN }, |
---|
[003a12b] | 655 | { "invite", 2, irc_cmd_invite, IRC_CMD_LOGGED_IN }, |
---|
[4469e7e] | 656 | { "topic", 1, irc_cmd_topic, IRC_CMD_LOGGED_IN }, |
---|
[003a12b] | 657 | { "oper", 2, irc_cmd_oper, IRC_CMD_LOGGED_IN }, |
---|
[0431ea1] | 658 | { "die", 0, NULL, IRC_CMD_OPER_ONLY | IRC_CMD_TO_MASTER }, |
---|
[565a1ea] | 659 | { "deaf", 0, NULL, IRC_CMD_OPER_ONLY | IRC_CMD_TO_MASTER }, |
---|
[48721c3] | 660 | { "wallops", 1, NULL, IRC_CMD_OPER_ONLY | IRC_CMD_TO_MASTER }, |
---|
[dfc8a46] | 661 | { "wall", 1, NULL, IRC_CMD_OPER_ONLY | IRC_CMD_TO_MASTER }, |
---|
[f4a5940] | 662 | { "rehash", 0, irc_cmd_rehash, IRC_CMD_OPER_ONLY }, |
---|
[54879ab] | 663 | { "restart", 0, NULL, IRC_CMD_OPER_ONLY | IRC_CMD_TO_MASTER }, |
---|
[48721c3] | 664 | { "kill", 2, NULL, IRC_CMD_OPER_ONLY | IRC_CMD_TO_MASTER }, |
---|
[0298d11] | 665 | { NULL } |
---|
| 666 | }; |
---|
| 667 | |
---|
[f73b969] | 668 | void irc_exec( irc_t *irc, char *cmd[] ) |
---|
[0298d11] | 669 | { |
---|
[f1d38f2] | 670 | int i, n_arg; |
---|
[0298d11] | 671 | |
---|
| 672 | if( !cmd[0] ) |
---|
[f73b969] | 673 | return; |
---|
[0298d11] | 674 | |
---|
| 675 | for( i = 0; irc_commands[i].command; i++ ) |
---|
| 676 | if( g_strcasecmp( irc_commands[i].command, cmd[0] ) == 0 ) |
---|
| 677 | { |
---|
[f1d38f2] | 678 | /* There should be no typo in the next line: */ |
---|
| 679 | for( n_arg = 0; cmd[n_arg]; n_arg ++ ); n_arg --; |
---|
| 680 | |
---|
[79e826a] | 681 | if( irc_commands[i].flags & IRC_CMD_PRE_LOGIN && irc->status & USTATUS_LOGGED_IN ) |
---|
[edf9657] | 682 | { |
---|
[3ddb7477] | 683 | irc_send_num( irc, 462, ":Only allowed before logging in" ); |
---|
[edf9657] | 684 | } |
---|
[3af70b0] | 685 | else if( irc_commands[i].flags & IRC_CMD_LOGGED_IN && !( irc->status & USTATUS_LOGGED_IN ) ) |
---|
[edf9657] | 686 | { |
---|
[3ddb7477] | 687 | irc_send_num( irc, 451, ":Register first" ); |
---|
[edf9657] | 688 | } |
---|
[f73b969] | 689 | else if( irc_commands[i].flags & IRC_CMD_OPER_ONLY && !strchr( irc->umode, 'o' ) ) |
---|
[edf9657] | 690 | { |
---|
[3ddb7477] | 691 | irc_send_num( irc, 481, ":Permission denied - You're not an IRC operator" ); |
---|
[edf9657] | 692 | } |
---|
[f1d38f2] | 693 | else if( n_arg < irc_commands[i].required_parameters ) |
---|
[f73b969] | 694 | { |
---|
[3ddb7477] | 695 | irc_send_num( irc, 461, "%s :Need more parameters", cmd[0] ); |
---|
[f73b969] | 696 | } |
---|
| 697 | else if( irc_commands[i].flags & IRC_CMD_TO_MASTER ) |
---|
| 698 | { |
---|
[5424c76] | 699 | /* IPC doesn't make sense in inetd mode, |
---|
| 700 | but the function will catch that. */ |
---|
[0431ea1] | 701 | ipc_to_master( cmd ); |
---|
[f73b969] | 702 | } |
---|
[0431ea1] | 703 | else |
---|
[f73b969] | 704 | { |
---|
| 705 | irc_commands[i].execute( irc, cmd ); |
---|
| 706 | } |
---|
| 707 | |
---|
[2f13222] | 708 | return; |
---|
[0298d11] | 709 | } |
---|
[2f13222] | 710 | |
---|
| 711 | if( irc->status >= USTATUS_LOGGED_IN ) |
---|
[3ddb7477] | 712 | irc_send_num( irc, 421, "%s :Unknown command", cmd[0] ); |
---|
[0298d11] | 713 | } |
---|