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