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