[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; |
---|
[a72af0d] | 304 | irc_user_t *iu; |
---|
[2f53ada] | 305 | |
---|
| 306 | if( !channel || *channel == '0' || *channel == '*' || !*channel ) |
---|
| 307 | irc_send_who( irc, irc->users, "**" ); |
---|
| 308 | else if( ( ic = irc_channel_by_name( irc, channel ) ) ) |
---|
| 309 | irc_send_who( irc, ic->users, channel ); |
---|
[a72af0d] | 310 | else if( ( iu = irc_user_by_name( irc, channel ) ) ) |
---|
| 311 | { |
---|
| 312 | /* Tiny hack! */ |
---|
| 313 | GSList *l = g_slist_append( NULL, iu ); |
---|
| 314 | irc_send_who( irc, l, channel ); |
---|
| 315 | g_slist_free( l ); |
---|
| 316 | } |
---|
[2f53ada] | 317 | else |
---|
| 318 | irc_send_num( irc, 403, "%s :No such channel", channel ); |
---|
| 319 | } |
---|
| 320 | |
---|
[280c56a] | 321 | static void irc_cmd_privmsg( irc_t *irc, char **cmd ) |
---|
[b919363] | 322 | { |
---|
[280c56a] | 323 | irc_channel_t *ic; |
---|
| 324 | irc_user_t *iu; |
---|
| 325 | |
---|
| 326 | if( !cmd[2] ) |
---|
[b919363] | 327 | { |
---|
[280c56a] | 328 | irc_send_num( irc, 412, ":No text to send" ); |
---|
[24b8bbb] | 329 | return; |
---|
[280c56a] | 330 | } |
---|
[24b8bbb] | 331 | |
---|
| 332 | /* Don't treat CTCP actions as real CTCPs, just convert them right now. */ |
---|
| 333 | if( g_strncasecmp( cmd[2], "\001ACTION", 7 ) == 0 ) |
---|
| 334 | { |
---|
| 335 | cmd[2] += 4; |
---|
[e1f3f94] | 336 | memcpy( cmd[2], "/me", 3 ); |
---|
[24b8bbb] | 337 | if( cmd[2][strlen(cmd[2])-1] == '\001' ) |
---|
| 338 | cmd[2][strlen(cmd[2])-1] = '\0'; |
---|
| 339 | } |
---|
| 340 | |
---|
| 341 | if( irc_channel_name_ok( cmd[1] ) && |
---|
| 342 | ( ic = irc_channel_by_name( irc, cmd[1] ) ) ) |
---|
[280c56a] | 343 | { |
---|
[a7dbf45] | 344 | if( cmd[2][0] == '\001' ) |
---|
| 345 | { |
---|
| 346 | /* CTCPs to channels? Nah. Maybe later. */ |
---|
| 347 | } |
---|
| 348 | else if( ic->f->privmsg ) |
---|
[280c56a] | 349 | ic->f->privmsg( ic, cmd[2] ); |
---|
| 350 | } |
---|
| 351 | else if( ( iu = irc_user_by_name( irc, cmd[1] ) ) ) |
---|
| 352 | { |
---|
[24b8bbb] | 353 | if( cmd[2][0] == '\001' ) |
---|
| 354 | { |
---|
| 355 | char **ctcp; |
---|
| 356 | |
---|
| 357 | if( iu->f->ctcp == NULL ) |
---|
| 358 | return; |
---|
| 359 | if( cmd[2][strlen(cmd[2])-1] == '\001' ) |
---|
| 360 | cmd[2][strlen(cmd[2])-1] = '\0'; |
---|
| 361 | |
---|
| 362 | ctcp = split_command_parts( cmd[2] + 1 ); |
---|
| 363 | iu->f->ctcp( iu, ctcp ); |
---|
| 364 | } |
---|
| 365 | else if( iu->f->privmsg ) |
---|
[bce78c8] | 366 | { |
---|
[92c8d41] | 367 | iu->last_channel = NULL; |
---|
[280c56a] | 368 | iu->f->privmsg( iu, cmd[2] ); |
---|
[bce78c8] | 369 | } |
---|
[b919363] | 370 | } |
---|
| 371 | else |
---|
| 372 | { |
---|
[280c56a] | 373 | irc_send_num( irc, 401, "%s :No such nick/channel", cmd[1] ); |
---|
[b919363] | 374 | } |
---|
[280c56a] | 375 | } |
---|
| 376 | |
---|
[d7f8500] | 377 | static void irc_cmd_notice( irc_t *irc, char **cmd ) |
---|
| 378 | { |
---|
| 379 | if( !cmd[2] ) |
---|
| 380 | { |
---|
| 381 | irc_send_num( irc, 412, ":No text to send" ); |
---|
| 382 | return; |
---|
| 383 | } |
---|
| 384 | |
---|
| 385 | /* At least for now just echo. IIRC some IRC clients use self-notices |
---|
| 386 | for lag checks, so try to support that. */ |
---|
| 387 | if( nick_cmp( cmd[1], irc->user->nick ) == 0 ) |
---|
| 388 | irc_send_msg( irc->user, "NOTICE", irc->user->nick, cmd[2], NULL ); |
---|
| 389 | } |
---|
| 390 | |
---|
[d860a8d] | 391 | static void irc_cmd_nickserv( irc_t *irc, char **cmd ) |
---|
| 392 | { |
---|
| 393 | /* [SH] This aliases the NickServ command to PRIVMSG root */ |
---|
| 394 | /* [TV] This aliases the NS command to PRIVMSG root as well */ |
---|
| 395 | root_command( irc, cmd + 1 ); |
---|
| 396 | } |
---|
| 397 | |
---|
[280c56a] | 398 | |
---|
| 399 | |
---|
| 400 | static void irc_cmd_oper( irc_t *irc, char **cmd ) |
---|
| 401 | { |
---|
| 402 | if( global.conf->oper_pass && |
---|
| 403 | ( strncmp( global.conf->oper_pass, "md5:", 4 ) == 0 ? |
---|
| 404 | md5_verify_password( cmd[2], global.conf->oper_pass + 4 ) == 0 : |
---|
| 405 | strcmp( cmd[2], global.conf->oper_pass ) == 0 ) ) |
---|
| 406 | { |
---|
| 407 | irc_umode_set( irc, "+o", 1 ); |
---|
| 408 | irc_send_num( irc, 381, ":Password accepted" ); |
---|
| 409 | } |
---|
| 410 | else |
---|
| 411 | { |
---|
| 412 | irc_send_num( irc, 432, ":Incorrect password" ); |
---|
| 413 | } |
---|
| 414 | } |
---|
| 415 | |
---|
| 416 | static void irc_cmd_invite( irc_t *irc, char **cmd ) |
---|
| 417 | { |
---|
[66b9e36a] | 418 | irc_channel_t *ic; |
---|
| 419 | irc_user_t *iu; |
---|
[280c56a] | 420 | |
---|
[66b9e36a] | 421 | if( ( iu = irc_user_by_name( irc, cmd[1] ) ) == NULL ) |
---|
| 422 | { |
---|
| 423 | irc_send_num( irc, 401, "%s :No such nick", cmd[1] ); |
---|
| 424 | return; |
---|
| 425 | } |
---|
| 426 | else if( ( ic = irc_channel_by_name( irc, cmd[2] ) ) == NULL ) |
---|
| 427 | { |
---|
| 428 | irc_send_num( irc, 403, "%s :No such channel", cmd[2] ); |
---|
| 429 | return; |
---|
| 430 | } |
---|
[280c56a] | 431 | |
---|
[46d215d] | 432 | if( !ic->f->invite ) |
---|
[66b9e36a] | 433 | irc_send_num( irc, 482, "%s :Can't invite people here", cmd[2] ); |
---|
[46d215d] | 434 | else if( ic->f->invite( ic, iu ) ) |
---|
| 435 | irc_send_num( irc, 341, "%s %s", iu->nick, ic->name ); |
---|
[0298d11] | 436 | } |
---|
| 437 | |
---|
[f73b969] | 438 | static void irc_cmd_userhost( irc_t *irc, char **cmd ) |
---|
[0298d11] | 439 | { |
---|
| 440 | int i; |
---|
| 441 | |
---|
| 442 | /* [TV] Usable USERHOST-implementation according to |
---|
| 443 | RFC1459. Without this, mIRC shows an error |
---|
| 444 | while connecting, and the used way of rejecting |
---|
| 445 | breaks standards. |
---|
| 446 | */ |
---|
| 447 | |
---|
| 448 | for( i = 1; cmd[i]; i ++ ) |
---|
[003a12b] | 449 | { |
---|
| 450 | irc_user_t *iu = irc_user_by_name( irc, cmd[i] ); |
---|
| 451 | |
---|
| 452 | if( iu ) |
---|
| 453 | irc_send_num( irc, 302, ":%s=%c%s@%s", iu->nick, |
---|
| 454 | irc_user_get_away( iu ) ? '-' : '+', |
---|
| 455 | iu->user, iu->host ); |
---|
| 456 | } |
---|
[0298d11] | 457 | } |
---|
| 458 | |
---|
[f73b969] | 459 | static void irc_cmd_ison( irc_t *irc, char **cmd ) |
---|
[0298d11] | 460 | { |
---|
[b4e4b95] | 461 | char buff[IRC_MAX_LINE]; |
---|
[0298d11] | 462 | int lenleft, i; |
---|
| 463 | |
---|
| 464 | buff[0] = '\0'; |
---|
| 465 | |
---|
| 466 | /* [SH] Leave room for : and \0 */ |
---|
| 467 | lenleft = IRC_MAX_LINE - 2; |
---|
| 468 | |
---|
| 469 | for( i = 1; cmd[i]; i ++ ) |
---|
| 470 | { |
---|
[42616d1] | 471 | char *this, *next; |
---|
| 472 | |
---|
| 473 | this = cmd[i]; |
---|
| 474 | while( *this ) |
---|
[0298d11] | 475 | { |
---|
[003a12b] | 476 | irc_user_t *iu; |
---|
| 477 | |
---|
[42616d1] | 478 | if( ( next = strchr( this, ' ' ) ) ) |
---|
| 479 | *next = 0; |
---|
[0298d11] | 480 | |
---|
[003a12b] | 481 | if( ( iu = irc_user_by_name( irc, this ) ) && |
---|
| 482 | iu->bu && iu->bu->flags & BEE_USER_ONLINE ) |
---|
[0298d11] | 483 | { |
---|
[003a12b] | 484 | lenleft -= strlen( iu->nick ) + 1; |
---|
[42616d1] | 485 | |
---|
| 486 | if( lenleft < 0 ) |
---|
| 487 | break; |
---|
| 488 | |
---|
[003a12b] | 489 | strcat( buff, iu->nick ); |
---|
[42616d1] | 490 | strcat( buff, " " ); |
---|
[0298d11] | 491 | } |
---|
| 492 | |
---|
[42616d1] | 493 | if( next ) |
---|
| 494 | { |
---|
| 495 | *next = ' '; |
---|
| 496 | this = next + 1; |
---|
| 497 | } |
---|
| 498 | else |
---|
| 499 | { |
---|
| 500 | break; |
---|
| 501 | } |
---|
[0298d11] | 502 | } |
---|
[42616d1] | 503 | |
---|
| 504 | /* *sigh* */ |
---|
| 505 | if( lenleft < 0 ) |
---|
| 506 | break; |
---|
[0298d11] | 507 | } |
---|
| 508 | |
---|
| 509 | if( strlen( buff ) > 0 ) |
---|
| 510 | buff[strlen(buff)-1] = '\0'; |
---|
| 511 | |
---|
[3ddb7477] | 512 | irc_send_num( irc, 303, ":%s", buff ); |
---|
[0298d11] | 513 | } |
---|
| 514 | |
---|
[f73b969] | 515 | static void irc_cmd_watch( irc_t *irc, char **cmd ) |
---|
[0298d11] | 516 | { |
---|
| 517 | int i; |
---|
| 518 | |
---|
| 519 | /* Obviously we could also mark a user structure as being |
---|
| 520 | watched, but what if the WATCH command is sent right |
---|
| 521 | after connecting? The user won't exist yet then... */ |
---|
| 522 | for( i = 1; cmd[i]; i ++ ) |
---|
| 523 | { |
---|
| 524 | char *nick; |
---|
[003a12b] | 525 | irc_user_t *iu; |
---|
[0298d11] | 526 | |
---|
| 527 | if( !cmd[i][0] || !cmd[i][1] ) |
---|
| 528 | break; |
---|
| 529 | |
---|
| 530 | nick = g_strdup( cmd[i] + 1 ); |
---|
| 531 | nick_lc( nick ); |
---|
| 532 | |
---|
[003a12b] | 533 | iu = irc_user_by_name( irc, nick ); |
---|
[0298d11] | 534 | |
---|
| 535 | if( cmd[i][0] == '+' ) |
---|
| 536 | { |
---|
| 537 | if( !g_hash_table_lookup( irc->watches, nick ) ) |
---|
| 538 | g_hash_table_insert( irc->watches, nick, nick ); |
---|
| 539 | |
---|
[003a12b] | 540 | if( iu && iu->bu && iu->bu->flags & BEE_USER_ONLINE ) |
---|
| 541 | irc_send_num( irc, 604, "%s %s %s %d :%s", iu->nick, iu->user, |
---|
| 542 | iu->host, (int) time( NULL ), "is online" ); |
---|
[0298d11] | 543 | else |
---|
[003a12b] | 544 | irc_send_num( irc, 605, "%s %s %s %d :%s", nick, "*", "*", |
---|
| 545 | (int) time( NULL ), "is offline" ); |
---|
[0298d11] | 546 | } |
---|
| 547 | else if( cmd[i][0] == '-' ) |
---|
| 548 | { |
---|
| 549 | gpointer okey, ovalue; |
---|
| 550 | |
---|
| 551 | if( g_hash_table_lookup_extended( irc->watches, nick, &okey, &ovalue ) ) |
---|
| 552 | { |
---|
| 553 | g_hash_table_remove( irc->watches, okey ); |
---|
[e59b4f6] | 554 | g_free( okey ); |
---|
[0298d11] | 555 | |
---|
[3ddb7477] | 556 | irc_send_num( irc, 602, "%s %s %s %d :%s", nick, "*", "*", 0, "Stopped watching" ); |
---|
[0298d11] | 557 | } |
---|
| 558 | } |
---|
| 559 | } |
---|
| 560 | } |
---|
| 561 | |
---|
[f73b969] | 562 | static void irc_cmd_topic( irc_t *irc, char **cmd ) |
---|
[0298d11] | 563 | { |
---|
[4469e7e] | 564 | irc_channel_t *ic = irc_channel_by_name( irc, cmd[1] ); |
---|
| 565 | const char *new = cmd[2]; |
---|
[50e1776] | 566 | |
---|
[4469e7e] | 567 | if( ic == NULL ) |
---|
[50e1776] | 568 | { |
---|
[4469e7e] | 569 | irc_send_num( irc, 403, "%s :No such channel", cmd[1] ); |
---|
| 570 | } |
---|
| 571 | else if( new ) |
---|
| 572 | { |
---|
| 573 | if( ic->f->topic == NULL ) |
---|
| 574 | irc_send_num( irc, 482, "%s :Can't change this channel's topic", ic->name ); |
---|
| 575 | else if( ic->f->topic( ic, new ) ) |
---|
| 576 | irc_send_topic( ic, TRUE ); |
---|
[50e1776] | 577 | } |
---|
[0298d11] | 578 | else |
---|
[50e1776] | 579 | { |
---|
[4469e7e] | 580 | irc_send_topic( ic, FALSE ); |
---|
[50e1776] | 581 | } |
---|
[0298d11] | 582 | } |
---|
| 583 | |
---|
[f73b969] | 584 | static void irc_cmd_away( irc_t *irc, char **cmd ) |
---|
[0298d11] | 585 | { |
---|
[81186cab] | 586 | if( cmd[1] && *cmd[1] ) |
---|
[0298d11] | 587 | { |
---|
[81186cab] | 588 | char away[strlen(cmd[1])+1]; |
---|
[0298d11] | 589 | int i, j; |
---|
| 590 | |
---|
| 591 | /* Copy away string, but skip control chars. Mainly because |
---|
| 592 | Jabber really doesn't like them. */ |
---|
[81186cab] | 593 | for( i = j = 0; cmd[1][i]; i ++ ) |
---|
| 594 | if( ( away[j] = cmd[1][i] ) >= ' ' ) |
---|
[0298d11] | 595 | j ++; |
---|
[81186cab] | 596 | away[j] = '\0'; |
---|
[0298d11] | 597 | |
---|
[81186cab] | 598 | irc_send_num( irc, 306, ":You're now away: %s", away ); |
---|
[4a9fd5f] | 599 | set_setstr( &irc->b->set, "away", away ); |
---|
[0298d11] | 600 | } |
---|
| 601 | else |
---|
| 602 | { |
---|
[3ddb7477] | 603 | irc_send_num( irc, 305, ":Welcome back" ); |
---|
[4a9fd5f] | 604 | set_setstr( &irc->b->set, "away", NULL ); |
---|
[0298d11] | 605 | } |
---|
| 606 | } |
---|
| 607 | |
---|
[23d6165] | 608 | static void irc_cmd_list( irc_t *irc, char **cmd ) |
---|
| 609 | { |
---|
| 610 | GSList *l; |
---|
| 611 | |
---|
| 612 | for( l = irc->channels; l; l = l->next ) |
---|
| 613 | { |
---|
| 614 | irc_channel_t *ic = l->data; |
---|
| 615 | |
---|
| 616 | irc_send_num( irc, 322, "%s %d :%s", |
---|
| 617 | ic->name, g_slist_length( ic->users ), ic->topic ? : "" ); |
---|
| 618 | } |
---|
| 619 | irc_send_num( irc, 323, ":%s", "End of /LIST" ); |
---|
| 620 | } |
---|
| 621 | |
---|
[82898af] | 622 | static void irc_cmd_version( irc_t *irc, char **cmd ) |
---|
| 623 | { |
---|
[d7d677d] | 624 | irc_send_num( irc, 351, "bitlbee-%s. %s :%s/%s ", |
---|
| 625 | BITLBEE_VERSION, irc->root->host, ARCH, CPU ); |
---|
[82898af] | 626 | } |
---|
| 627 | |
---|
[f73b969] | 628 | static void irc_cmd_completions( irc_t *irc, char **cmd ) |
---|
[0298d11] | 629 | { |
---|
| 630 | help_t *h; |
---|
| 631 | set_t *s; |
---|
| 632 | int i; |
---|
| 633 | |
---|
[d7d677d] | 634 | irc_send_msg_raw( irc->root, "NOTICE", irc->user->nick, "COMPLETIONS OK" ); |
---|
[0298d11] | 635 | |
---|
[8358691] | 636 | for( i = 0; root_commands[i].command; i ++ ) |
---|
| 637 | irc_send_msg_f( irc->root, "NOTICE", irc->user->nick, "COMPLETIONS %s", root_commands[i].command ); |
---|
[0298d11] | 638 | |
---|
| 639 | for( h = global.help; h; h = h->next ) |
---|
[d7d677d] | 640 | irc_send_msg_f( irc->root, "NOTICE", irc->user->nick, "COMPLETIONS help %s", h->title ); |
---|
[0298d11] | 641 | |
---|
[d7d677d] | 642 | for( s = irc->b->set; s; s = s->next ) |
---|
| 643 | irc_send_msg_f( irc->root, "NOTICE", irc->user->nick, "COMPLETIONS set %s", s->key ); |
---|
[0298d11] | 644 | |
---|
[d7d677d] | 645 | irc_send_msg_raw( irc->root, "NOTICE", irc->user->nick, "COMPLETIONS END" ); |
---|
[0298d11] | 646 | } |
---|
| 647 | |
---|
[f73b969] | 648 | static void irc_cmd_rehash( irc_t *irc, char **cmd ) |
---|
[f4a5940] | 649 | { |
---|
[5424c76] | 650 | if( global.conf->runmode == RUNMODE_INETD ) |
---|
| 651 | ipc_master_cmd_rehash( NULL, NULL ); |
---|
| 652 | else |
---|
| 653 | ipc_to_master( cmd ); |
---|
[f4a5940] | 654 | |
---|
[3ddb7477] | 655 | irc_send_num( irc, 382, "%s :Rehashing", global.conf_file ); |
---|
[f4a5940] | 656 | } |
---|
| 657 | |
---|
[0298d11] | 658 | static const command_t irc_commands[] = { |
---|
[a199d33] | 659 | { "pass", 1, irc_cmd_pass, 0 }, |
---|
[0298d11] | 660 | { "user", 4, irc_cmd_user, IRC_CMD_PRE_LOGIN }, |
---|
| 661 | { "nick", 1, irc_cmd_nick, 0 }, |
---|
| 662 | { "quit", 0, irc_cmd_quit, 0 }, |
---|
| 663 | { "ping", 0, irc_cmd_ping, 0 }, |
---|
[3923003] | 664 | { "pong", 0, irc_cmd_pong, IRC_CMD_LOGGED_IN }, |
---|
[b9e020a] | 665 | { "join", 1, irc_cmd_join, IRC_CMD_LOGGED_IN }, |
---|
| 666 | { "names", 1, irc_cmd_names, IRC_CMD_LOGGED_IN }, |
---|
| 667 | { "part", 1, irc_cmd_part, IRC_CMD_LOGGED_IN }, |
---|
[b95932e] | 668 | { "whois", 1, irc_cmd_whois, IRC_CMD_LOGGED_IN }, |
---|
| 669 | { "whowas", 1, irc_cmd_whowas, IRC_CMD_LOGGED_IN }, |
---|
[9b69eb7] | 670 | { "motd", 0, irc_cmd_motd, IRC_CMD_LOGGED_IN }, |
---|
[b919363] | 671 | { "mode", 1, irc_cmd_mode, IRC_CMD_LOGGED_IN }, |
---|
[2f53ada] | 672 | { "who", 0, irc_cmd_who, IRC_CMD_LOGGED_IN }, |
---|
[280c56a] | 673 | { "privmsg", 1, irc_cmd_privmsg, IRC_CMD_LOGGED_IN }, |
---|
[d7f8500] | 674 | { "notice", 1, irc_cmd_notice, IRC_CMD_LOGGED_IN }, |
---|
[d860a8d] | 675 | { "nickserv", 1, irc_cmd_nickserv, IRC_CMD_LOGGED_IN }, |
---|
| 676 | { "ns", 1, irc_cmd_nickserv, IRC_CMD_LOGGED_IN }, |
---|
[81186cab] | 677 | { "away", 0, irc_cmd_away, IRC_CMD_LOGGED_IN }, |
---|
[d7d677d] | 678 | { "version", 0, irc_cmd_version, IRC_CMD_LOGGED_IN }, |
---|
| 679 | { "completions", 0, irc_cmd_completions, IRC_CMD_LOGGED_IN }, |
---|
[0298d11] | 680 | { "userhost", 1, irc_cmd_userhost, IRC_CMD_LOGGED_IN }, |
---|
| 681 | { "ison", 1, irc_cmd_ison, IRC_CMD_LOGGED_IN }, |
---|
| 682 | { "watch", 1, irc_cmd_watch, IRC_CMD_LOGGED_IN }, |
---|
[003a12b] | 683 | { "invite", 2, irc_cmd_invite, IRC_CMD_LOGGED_IN }, |
---|
[4469e7e] | 684 | { "topic", 1, irc_cmd_topic, IRC_CMD_LOGGED_IN }, |
---|
[003a12b] | 685 | { "oper", 2, irc_cmd_oper, IRC_CMD_LOGGED_IN }, |
---|
[23d6165] | 686 | { "list", 0, irc_cmd_list, IRC_CMD_LOGGED_IN }, |
---|
[0431ea1] | 687 | { "die", 0, NULL, IRC_CMD_OPER_ONLY | IRC_CMD_TO_MASTER }, |
---|
[565a1ea] | 688 | { "deaf", 0, NULL, IRC_CMD_OPER_ONLY | IRC_CMD_TO_MASTER }, |
---|
[48721c3] | 689 | { "wallops", 1, NULL, IRC_CMD_OPER_ONLY | IRC_CMD_TO_MASTER }, |
---|
[dfc8a46] | 690 | { "wall", 1, NULL, IRC_CMD_OPER_ONLY | IRC_CMD_TO_MASTER }, |
---|
[f4a5940] | 691 | { "rehash", 0, irc_cmd_rehash, IRC_CMD_OPER_ONLY }, |
---|
[54879ab] | 692 | { "restart", 0, NULL, IRC_CMD_OPER_ONLY | IRC_CMD_TO_MASTER }, |
---|
[48721c3] | 693 | { "kill", 2, NULL, IRC_CMD_OPER_ONLY | IRC_CMD_TO_MASTER }, |
---|
[0298d11] | 694 | { NULL } |
---|
| 695 | }; |
---|
| 696 | |
---|
[f73b969] | 697 | void irc_exec( irc_t *irc, char *cmd[] ) |
---|
[0298d11] | 698 | { |
---|
[f1d38f2] | 699 | int i, n_arg; |
---|
[0298d11] | 700 | |
---|
| 701 | if( !cmd[0] ) |
---|
[f73b969] | 702 | return; |
---|
[0298d11] | 703 | |
---|
| 704 | for( i = 0; irc_commands[i].command; i++ ) |
---|
| 705 | if( g_strcasecmp( irc_commands[i].command, cmd[0] ) == 0 ) |
---|
| 706 | { |
---|
[f1d38f2] | 707 | /* There should be no typo in the next line: */ |
---|
| 708 | for( n_arg = 0; cmd[n_arg]; n_arg ++ ); n_arg --; |
---|
| 709 | |
---|
[79e826a] | 710 | if( irc_commands[i].flags & IRC_CMD_PRE_LOGIN && irc->status & USTATUS_LOGGED_IN ) |
---|
[edf9657] | 711 | { |
---|
[3ddb7477] | 712 | irc_send_num( irc, 462, ":Only allowed before logging in" ); |
---|
[edf9657] | 713 | } |
---|
[3af70b0] | 714 | else if( irc_commands[i].flags & IRC_CMD_LOGGED_IN && !( irc->status & USTATUS_LOGGED_IN ) ) |
---|
[edf9657] | 715 | { |
---|
[3ddb7477] | 716 | irc_send_num( irc, 451, ":Register first" ); |
---|
[edf9657] | 717 | } |
---|
[f73b969] | 718 | else if( irc_commands[i].flags & IRC_CMD_OPER_ONLY && !strchr( irc->umode, 'o' ) ) |
---|
[edf9657] | 719 | { |
---|
[3ddb7477] | 720 | irc_send_num( irc, 481, ":Permission denied - You're not an IRC operator" ); |
---|
[edf9657] | 721 | } |
---|
[f1d38f2] | 722 | else if( n_arg < irc_commands[i].required_parameters ) |
---|
[f73b969] | 723 | { |
---|
[3ddb7477] | 724 | irc_send_num( irc, 461, "%s :Need more parameters", cmd[0] ); |
---|
[f73b969] | 725 | } |
---|
| 726 | else if( irc_commands[i].flags & IRC_CMD_TO_MASTER ) |
---|
| 727 | { |
---|
[5424c76] | 728 | /* IPC doesn't make sense in inetd mode, |
---|
| 729 | but the function will catch that. */ |
---|
[0431ea1] | 730 | ipc_to_master( cmd ); |
---|
[f73b969] | 731 | } |
---|
[0431ea1] | 732 | else |
---|
[f73b969] | 733 | { |
---|
| 734 | irc_commands[i].execute( irc, cmd ); |
---|
| 735 | } |
---|
| 736 | |
---|
[2f13222] | 737 | return; |
---|
[0298d11] | 738 | } |
---|
[2f13222] | 739 | |
---|
| 740 | if( irc->status >= USTATUS_LOGGED_IN ) |
---|
[3ddb7477] | 741 | irc_send_num( irc, 421, "%s :Unknown command", cmd[0] ); |
---|
[0298d11] | 742 | } |
---|