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