[b7d3cc34] | 1 | /********************************************************************\ |
---|
| 2 | * BitlBee -- An IRC to other IM-networks gateway * |
---|
| 3 | * * |
---|
| 4 | * Copyright 2002-2004 Wilmer van der Gaast and others * |
---|
| 5 | \********************************************************************/ |
---|
| 6 | |
---|
| 7 | /* The big hairy IRCd part of the project */ |
---|
| 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" |
---|
| 28 | #include "crypting.h" |
---|
[2face62] | 29 | #include "ipc.h" |
---|
[b7d3cc34] | 30 | |
---|
[ba9edaa] | 31 | static gboolean irc_userping( gpointer _irc, int fd, b_input_condition cond ); |
---|
[b7d3cc34] | 32 | |
---|
| 33 | GSList *irc_connection_list = NULL; |
---|
| 34 | |
---|
[0383943] | 35 | static char *passchange( set_t *set, char *value ) |
---|
[c2295f7] | 36 | { |
---|
[0383943] | 37 | irc_t *irc = set->data; |
---|
| 38 | |
---|
[88086db] | 39 | irc_setpass( irc, value ); |
---|
| 40 | irc_usermsg( irc, "Password successfully changed" ); |
---|
| 41 | return NULL; |
---|
[c2295f7] | 42 | } |
---|
| 43 | |
---|
[b7d3cc34] | 44 | irc_t *irc_new( int fd ) |
---|
| 45 | { |
---|
[e4d6271] | 46 | irc_t *irc; |
---|
[e9b755e] | 47 | struct sockaddr_storage sock; |
---|
[7435ccf] | 48 | socklen_t socklen = sizeof( sock ); |
---|
[e4d6271] | 49 | |
---|
| 50 | irc = g_new0( irc_t, 1 ); |
---|
[b7d3cc34] | 51 | |
---|
| 52 | irc->fd = fd; |
---|
[a0d04d6] | 53 | sock_make_nonblocking( irc->fd ); |
---|
| 54 | |
---|
[ba9edaa] | 55 | irc->r_watch_source_id = b_input_add( irc->fd, GAIM_INPUT_READ, bitlbee_io_current_client_read, irc ); |
---|
[b7d3cc34] | 56 | |
---|
| 57 | irc->status = USTATUS_OFFLINE; |
---|
| 58 | irc->last_pong = gettime(); |
---|
| 59 | |
---|
| 60 | irc->userhash = g_hash_table_new( g_str_hash, g_str_equal ); |
---|
| 61 | irc->watches = g_hash_table_new( g_str_hash, g_str_equal ); |
---|
| 62 | |
---|
| 63 | strcpy( irc->umode, UMODE ); |
---|
| 64 | irc->mynick = g_strdup( ROOT_NICK ); |
---|
| 65 | irc->channel = g_strdup( ROOT_CHAN ); |
---|
| 66 | |
---|
| 67 | if( global.conf->hostname ) |
---|
| 68 | { |
---|
| 69 | irc->myhost = g_strdup( global.conf->hostname ); |
---|
| 70 | } |
---|
[7435ccf] | 71 | else if( getsockname( irc->fd, (struct sockaddr*) &sock, &socklen ) == 0 ) |
---|
[b7d3cc34] | 72 | { |
---|
[2231302] | 73 | char buf[NI_MAXHOST+1]; |
---|
[e9b755e] | 74 | |
---|
[2231302] | 75 | if( getnameinfo( (struct sockaddr *) &sock, socklen, buf, |
---|
| 76 | NI_MAXHOST, NULL, -1, 0 ) == 0 ) |
---|
| 77 | { |
---|
| 78 | irc->myhost = g_strdup( ipv6_unwrap( buf ) ); |
---|
| 79 | } |
---|
[b7d3cc34] | 80 | } |
---|
| 81 | |
---|
[7435ccf] | 82 | if( getpeername( irc->fd, (struct sockaddr*) &sock, &socklen ) == 0 ) |
---|
[b7d3cc34] | 83 | { |
---|
[2231302] | 84 | char buf[NI_MAXHOST+1]; |
---|
[e9b755e] | 85 | |
---|
[2231302] | 86 | if( getnameinfo( (struct sockaddr *)&sock, socklen, buf, |
---|
| 87 | NI_MAXHOST, NULL, -1, 0 ) == 0 ) |
---|
| 88 | { |
---|
[2a6ca4f] | 89 | irc->host = g_strdup( ipv6_unwrap( buf ) ); |
---|
[2231302] | 90 | } |
---|
[b7d3cc34] | 91 | } |
---|
| 92 | |
---|
[3e1e11af] | 93 | if( irc->host == NULL ) |
---|
| 94 | irc->host = g_strdup( "localhost.localdomain" ); |
---|
| 95 | if( irc->myhost == NULL ) |
---|
| 96 | irc->myhost = g_strdup( "localhost.localdomain" ); |
---|
| 97 | |
---|
[b7d3cc34] | 98 | if( global.conf->ping_interval > 0 && global.conf->ping_timeout > 0 ) |
---|
[ba9edaa] | 99 | irc->ping_source_id = b_timeout_add( global.conf->ping_interval * 1000, irc_userping, irc ); |
---|
[b7d3cc34] | 100 | |
---|
| 101 | irc_write( irc, ":%s NOTICE AUTH :%s", irc->myhost, "BitlBee-IRCd initialized, please go on" ); |
---|
| 102 | |
---|
| 103 | irc_connection_list = g_slist_append( irc_connection_list, irc ); |
---|
[5a71d9c] | 104 | |
---|
[5c9512f] | 105 | set_add( &irc->set, "auto_connect", "true", set_eval_bool, irc ); |
---|
| 106 | set_add( &irc->set, "auto_reconnect", "false", set_eval_bool, irc ); |
---|
| 107 | set_add( &irc->set, "auto_reconnect_delay", "300", set_eval_int, irc ); |
---|
| 108 | set_add( &irc->set, "buddy_sendbuffer", "false", set_eval_bool, irc ); |
---|
| 109 | set_add( &irc->set, "buddy_sendbuffer_delay", "200", set_eval_int, irc ); |
---|
[8d32b9e] | 110 | set_add( &irc->set, "charset", "utf-8", set_eval_charset, irc ); |
---|
[5c9512f] | 111 | set_add( &irc->set, "debug", "false", set_eval_bool, irc ); |
---|
| 112 | set_add( &irc->set, "default_target", "root", NULL, irc ); |
---|
| 113 | set_add( &irc->set, "display_namechanges", "false", set_eval_bool, irc ); |
---|
| 114 | set_add( &irc->set, "handle_unknown", "root", NULL, irc ); |
---|
[52e6e17] | 115 | set_add( &irc->set, "halfop_buddies", "false", set_eval_halfop_buddies, irc ); |
---|
[5c9512f] | 116 | set_add( &irc->set, "lcnicks", "true", set_eval_bool, irc ); |
---|
[5a71d9c] | 117 | set_add( &irc->set, "op_buddies", "false", set_eval_op_buddies, irc ); |
---|
| 118 | set_add( &irc->set, "op_root", "true", set_eval_op_root, irc ); |
---|
| 119 | set_add( &irc->set, "op_user", "true", set_eval_op_user, irc ); |
---|
[5c9512f] | 120 | set_add( &irc->set, "password", NULL, passchange, irc ); |
---|
| 121 | set_add( &irc->set, "private", "true", set_eval_bool, irc ); |
---|
| 122 | set_add( &irc->set, "query_order", "lifo", NULL, irc ); |
---|
| 123 | set_add( &irc->set, "save_on_quit", "true", set_eval_bool, irc ); |
---|
[1186382] | 124 | set_add( &irc->set, "simulate_netsplit", "true", set_eval_bool, irc ); |
---|
[5c9512f] | 125 | set_add( &irc->set, "strip_html", "true", NULL, irc ); |
---|
| 126 | set_add( &irc->set, "to_char", ": ", set_eval_to_char, irc ); |
---|
| 127 | set_add( &irc->set, "typing_notice", "false", set_eval_bool, irc ); |
---|
[5a71d9c] | 128 | set_add( &irc->set, "voice_buddies", "notaway", set_eval_voice_buddies, irc ); |
---|
[b7d3cc34] | 129 | |
---|
| 130 | conf_loaddefaults( irc ); |
---|
[764c7d1] | 131 | |
---|
| 132 | irc->otr_us = otrl_userstate_create(); |
---|
| 133 | irc->otr_mutex = g_mutex_new(); |
---|
[b7d3cc34] | 134 | |
---|
| 135 | return( irc ); |
---|
| 136 | } |
---|
| 137 | |
---|
[f73b969] | 138 | /* immed=1 makes this function pretty much equal to irc_free(), except that |
---|
| 139 | this one will "log". In case the connection is already broken and we |
---|
| 140 | shouldn't try to write to it. */ |
---|
[fc50d48] | 141 | void irc_abort( irc_t *irc, int immed, char *format, ... ) |
---|
[c1826c6] | 142 | { |
---|
[fc50d48] | 143 | if( format != NULL ) |
---|
| 144 | { |
---|
[f73b969] | 145 | va_list params; |
---|
[fc50d48] | 146 | char *reason; |
---|
| 147 | |
---|
| 148 | va_start( params, format ); |
---|
[f73b969] | 149 | reason = g_strdup_vprintf( format, params ); |
---|
[fc50d48] | 150 | va_end( params ); |
---|
| 151 | |
---|
| 152 | if( !immed ) |
---|
| 153 | irc_write( irc, "ERROR :Closing link: %s", reason ); |
---|
| 154 | |
---|
| 155 | ipc_to_master_str( "OPERMSG :Client exiting: %s@%s [%s]\r\n", |
---|
[f73b969] | 156 | irc->nick ? irc->nick : "(NONE)", irc->host, reason ); |
---|
[fc50d48] | 157 | |
---|
| 158 | g_free( reason ); |
---|
| 159 | } |
---|
| 160 | else |
---|
| 161 | { |
---|
| 162 | if( !immed ) |
---|
| 163 | irc_write( irc, "ERROR :Closing link" ); |
---|
| 164 | |
---|
| 165 | ipc_to_master_str( "OPERMSG :Client exiting: %s@%s [%s]\r\n", |
---|
[f73b969] | 166 | irc->nick ? irc->nick : "(NONE)", irc->host, "No reason given" ); |
---|
[fc50d48] | 167 | } |
---|
| 168 | |
---|
[79e826a] | 169 | irc->status |= USTATUS_SHUTDOWN; |
---|
[fc50d48] | 170 | if( irc->sendbuffer && !immed ) |
---|
[c1826c6] | 171 | { |
---|
[fc50d48] | 172 | /* We won't read from this socket anymore. Instead, we'll connect a timer |
---|
| 173 | to it that should shut down the connection in a second, just in case |
---|
| 174 | bitlbee_.._write doesn't do it first. */ |
---|
| 175 | |
---|
[ba9edaa] | 176 | b_event_remove( irc->r_watch_source_id ); |
---|
| 177 | irc->r_watch_source_id = b_timeout_add( 1000, (b_event_handler) irc_free, irc ); |
---|
[c1826c6] | 178 | } |
---|
| 179 | else |
---|
| 180 | { |
---|
| 181 | irc_free( irc ); |
---|
| 182 | } |
---|
| 183 | } |
---|
| 184 | |
---|
[36fa9bd] | 185 | static gboolean irc_free_hashkey( gpointer key, gpointer value, gpointer data ) |
---|
[b7d3cc34] | 186 | { |
---|
| 187 | g_free( key ); |
---|
| 188 | |
---|
| 189 | return( TRUE ); |
---|
| 190 | } |
---|
| 191 | |
---|
| 192 | /* Because we have no garbage collection, this is quite annoying */ |
---|
| 193 | void irc_free(irc_t * irc) |
---|
| 194 | { |
---|
[5b52a48] | 195 | account_t *account; |
---|
[b7d3cc34] | 196 | user_t *user, *usertmp; |
---|
| 197 | help_t *helpnode, *helpnodetmp; |
---|
| 198 | |
---|
| 199 | log_message( LOGLVL_INFO, "Destroying connection with fd %d", irc->fd ); |
---|
| 200 | |
---|
[d5ccd83] | 201 | if( irc->status & USTATUS_IDENTIFIED && set_getbool( &irc->set, "save_on_quit" ) ) |
---|
[b73ac9c] | 202 | if( storage_save( irc, TRUE ) != STORAGE_OK ) |
---|
[b7d3cc34] | 203 | irc_usermsg( irc, "Error while saving settings!" ); |
---|
| 204 | |
---|
[bd9b00f] | 205 | closesocket( irc->fd ); |
---|
| 206 | |
---|
[b7d3cc34] | 207 | if( irc->ping_source_id > 0 ) |
---|
[ba9edaa] | 208 | b_event_remove( irc->ping_source_id ); |
---|
| 209 | b_event_remove( irc->r_watch_source_id ); |
---|
[b7d3cc34] | 210 | if( irc->w_watch_source_id > 0 ) |
---|
[ba9edaa] | 211 | b_event_remove( irc->w_watch_source_id ); |
---|
[9c62a7c] | 212 | |
---|
[b7d3cc34] | 213 | irc_connection_list = g_slist_remove( irc_connection_list, irc ); |
---|
| 214 | |
---|
[55cc2be3] | 215 | for (account = irc->accounts; account; account = account->next) { |
---|
[0da65d5] | 216 | if (account->ic) { |
---|
[c2fb3809] | 217 | imc_logout(account->ic, TRUE); |
---|
[c99af3a] | 218 | } else if (account->reconnect) { |
---|
[6adcb6c6] | 219 | cancel_auto_reconnect(account); |
---|
[c99af3a] | 220 | } |
---|
[55cc2be3] | 221 | } |
---|
[b7d3cc34] | 222 | |
---|
| 223 | g_free(irc->sendbuffer); |
---|
| 224 | g_free(irc->readbuffer); |
---|
| 225 | |
---|
| 226 | g_free(irc->nick); |
---|
| 227 | g_free(irc->user); |
---|
| 228 | g_free(irc->host); |
---|
| 229 | g_free(irc->realname); |
---|
| 230 | g_free(irc->password); |
---|
| 231 | |
---|
| 232 | g_free(irc->myhost); |
---|
| 233 | g_free(irc->mynick); |
---|
| 234 | |
---|
| 235 | g_free(irc->channel); |
---|
| 236 | |
---|
| 237 | while (irc->queries != NULL) |
---|
| 238 | query_del(irc, irc->queries); |
---|
| 239 | |
---|
[5b52a48] | 240 | while (irc->accounts) |
---|
[0da65d5] | 241 | if (irc->accounts->ic == NULL) |
---|
[f959495] | 242 | account_del(irc, irc->accounts); |
---|
| 243 | else |
---|
| 244 | /* Nasty hack, but account_del() doesn't work in this |
---|
| 245 | case and we don't want infinite loops, do we? ;-) */ |
---|
| 246 | irc->accounts = irc->accounts->next; |
---|
[5b52a48] | 247 | |
---|
| 248 | while (irc->set) |
---|
| 249 | set_del(&irc->set, irc->set->key); |
---|
[b7d3cc34] | 250 | |
---|
| 251 | if (irc->users != NULL) { |
---|
| 252 | user = irc->users; |
---|
| 253 | while (user != NULL) { |
---|
| 254 | g_free(user->nick); |
---|
| 255 | g_free(user->away); |
---|
| 256 | g_free(user->handle); |
---|
| 257 | if(user->user!=user->nick) g_free(user->user); |
---|
| 258 | if(user->host!=user->nick) g_free(user->host); |
---|
| 259 | if(user->realname!=user->nick) g_free(user->realname); |
---|
[ba9edaa] | 260 | b_event_remove(user->sendbuf_timer); |
---|
[b7d3cc34] | 261 | |
---|
| 262 | usertmp = user; |
---|
| 263 | user = user->next; |
---|
| 264 | g_free(usertmp); |
---|
| 265 | } |
---|
| 266 | } |
---|
| 267 | |
---|
[36fa9bd] | 268 | g_hash_table_foreach_remove(irc->userhash, irc_free_hashkey, NULL); |
---|
[b7d3cc34] | 269 | g_hash_table_destroy(irc->userhash); |
---|
| 270 | |
---|
[36fa9bd] | 271 | g_hash_table_foreach_remove(irc->watches, irc_free_hashkey, NULL); |
---|
[b7d3cc34] | 272 | g_hash_table_destroy(irc->watches); |
---|
| 273 | |
---|
| 274 | if (irc->help != NULL) { |
---|
| 275 | helpnode = irc->help; |
---|
| 276 | while (helpnode != NULL) { |
---|
| 277 | g_free(helpnode->string); |
---|
| 278 | |
---|
| 279 | helpnodetmp = helpnode; |
---|
| 280 | helpnode = helpnode->next; |
---|
| 281 | g_free(helpnodetmp); |
---|
| 282 | } |
---|
| 283 | } |
---|
[764c7d1] | 284 | |
---|
| 285 | otrl_userstate_free(irc->otr_us); |
---|
| 286 | g_mutex_free(irc->otr_mutex); |
---|
| 287 | |
---|
[b7d3cc34] | 288 | g_free(irc); |
---|
| 289 | |
---|
[d25f6fc] | 290 | if( global.conf->runmode == RUNMODE_INETD || global.conf->runmode == RUNMODE_FORKDAEMON ) |
---|
[ba9edaa] | 291 | b_main_quit(); |
---|
[b7d3cc34] | 292 | } |
---|
| 293 | |
---|
[7cad7b4] | 294 | /* USE WITH CAUTION! |
---|
| 295 | Sets pass without checking */ |
---|
| 296 | void irc_setpass (irc_t *irc, const char *pass) |
---|
| 297 | { |
---|
[6e1fed7] | 298 | g_free (irc->password); |
---|
[7cad7b4] | 299 | |
---|
| 300 | if (pass) { |
---|
| 301 | irc->password = g_strdup (pass); |
---|
| 302 | } else { |
---|
| 303 | irc->password = NULL; |
---|
| 304 | } |
---|
| 305 | } |
---|
| 306 | |
---|
[f73b969] | 307 | void irc_process( irc_t *irc ) |
---|
[b7d3cc34] | 308 | { |
---|
[e27661d] | 309 | char **lines, *temp, **cmd, *cs; |
---|
[b7d3cc34] | 310 | int i; |
---|
| 311 | |
---|
[de3e100] | 312 | if( irc->readbuffer != NULL ) |
---|
| 313 | { |
---|
| 314 | lines = irc_tokenize( irc->readbuffer ); |
---|
| 315 | |
---|
| 316 | for( i = 0; *lines[i] != '\0'; i ++ ) |
---|
| 317 | { |
---|
[7d31002] | 318 | char conv[IRC_MAX_LINE+1]; |
---|
| 319 | |
---|
[e27661d] | 320 | /* [WvG] Because irc_tokenize splits at every newline, the lines[] list |
---|
| 321 | should end with an empty string. This is why this actually works. |
---|
| 322 | Took me a while to figure out, Maurits. :-P */ |
---|
[de3e100] | 323 | if( lines[i+1] == NULL ) |
---|
| 324 | { |
---|
[b7d3cc34] | 325 | temp = g_strdup( lines[i] ); |
---|
| 326 | g_free( irc->readbuffer ); |
---|
| 327 | irc->readbuffer = temp; |
---|
[de3e100] | 328 | i ++; |
---|
[b7d3cc34] | 329 | break; |
---|
[e27661d] | 330 | } |
---|
| 331 | |
---|
[94d52d64] | 332 | if( ( cs = set_getstr( &irc->set, "charset" ) ) ) |
---|
[e27661d] | 333 | { |
---|
| 334 | conv[IRC_MAX_LINE] = 0; |
---|
[94d52d64] | 335 | if( do_iconv( cs, "UTF-8", lines[i], conv, 0, IRC_MAX_LINE - 2 ) == -1 ) |
---|
| 336 | { |
---|
[fc0cf92] | 337 | /* GLib can do strange things if things are not in the expected charset, |
---|
| 338 | so let's be a little bit paranoid here: */ |
---|
[94d52d64] | 339 | if( irc->status & USTATUS_LOGGED_IN ) |
---|
[fc0cf92] | 340 | { |
---|
[43462708] | 341 | irc_usermsg( irc, "Error: Charset mismatch detected. The charset " |
---|
[94d52d64] | 342 | "setting is currently set to %s, so please make " |
---|
| 343 | "sure your IRC client will send and accept text in " |
---|
| 344 | "that charset, or tell BitlBee which charset to " |
---|
| 345 | "expect by changing the charset setting. See " |
---|
| 346 | "`help set charset' for more information. Your " |
---|
| 347 | "message was ignored.", cs ); |
---|
[fc0cf92] | 348 | *conv = 0; |
---|
| 349 | } |
---|
| 350 | else |
---|
| 351 | { |
---|
| 352 | irc_write( irc, ":%s NOTICE AUTH :%s", irc->myhost, |
---|
| 353 | "Warning: invalid (non-UTF8) characters received at login time." ); |
---|
| 354 | |
---|
| 355 | strncpy( conv, lines[i], IRC_MAX_LINE ); |
---|
| 356 | for( temp = conv; *temp; temp ++ ) |
---|
| 357 | if( *temp & 0x80 ) |
---|
| 358 | *temp = '?'; |
---|
| 359 | } |
---|
[94d52d64] | 360 | } |
---|
| 361 | lines[i] = conv; |
---|
[e27661d] | 362 | } |
---|
[de3e100] | 363 | |
---|
[0431ea1] | 364 | if( ( cmd = irc_parse_line( lines[i] ) ) == NULL ) |
---|
[de3e100] | 365 | continue; |
---|
[f73b969] | 366 | irc_exec( irc, cmd ); |
---|
| 367 | |
---|
| 368 | g_free( cmd ); |
---|
| 369 | |
---|
| 370 | /* Shouldn't really happen, but just in case... */ |
---|
| 371 | if( !g_slist_find( irc_connection_list, irc ) ) |
---|
[de3e100] | 372 | { |
---|
[b7d3cc34] | 373 | g_free( lines ); |
---|
[f73b969] | 374 | return; |
---|
[b7d3cc34] | 375 | } |
---|
| 376 | } |
---|
[de3e100] | 377 | |
---|
| 378 | if( lines[i] != NULL ) |
---|
| 379 | { |
---|
| 380 | g_free( irc->readbuffer ); |
---|
[0431ea1] | 381 | irc->readbuffer = NULL; |
---|
[b7d3cc34] | 382 | } |
---|
[de3e100] | 383 | |
---|
[b7d3cc34] | 384 | g_free( lines ); |
---|
| 385 | } |
---|
| 386 | } |
---|
| 387 | |
---|
[e27661d] | 388 | /* Splits a long string into separate lines. The array is NULL-terminated and, unless the string |
---|
| 389 | contains an incomplete line at the end, ends with an empty string. */ |
---|
[b7d3cc34] | 390 | char **irc_tokenize( char *buffer ) |
---|
| 391 | { |
---|
| 392 | int i, j; |
---|
| 393 | char **lines; |
---|
| 394 | |
---|
| 395 | /* Count the number of elements we're gonna need. */ |
---|
[de3e100] | 396 | for( i = 0, j = 1; buffer[i] != '\0'; i ++ ) |
---|
| 397 | { |
---|
| 398 | if( buffer[i] == '\n' ) |
---|
| 399 | if( buffer[i+1] != '\r' && buffer[i+1] != '\n' ) |
---|
| 400 | j ++; |
---|
[b7d3cc34] | 401 | } |
---|
| 402 | |
---|
| 403 | /* Allocate j+1 elements. */ |
---|
[de3e100] | 404 | lines = g_new( char *, j + 1 ); |
---|
[b7d3cc34] | 405 | |
---|
| 406 | /* NULL terminate our list. */ |
---|
[de3e100] | 407 | lines[j] = NULL; |
---|
[b7d3cc34] | 408 | |
---|
[de3e100] | 409 | lines[0] = buffer; |
---|
[b7d3cc34] | 410 | |
---|
| 411 | /* Split the buffer in several strings, using \r\n as our seperator, where \r is optional. |
---|
| 412 | * Although this is not in the RFC, some braindead ircds (newnet's) use this, so some clients might too. |
---|
| 413 | */ |
---|
[de3e100] | 414 | for( i = 0, j = 0; buffer[i] != '\0'; i ++) |
---|
| 415 | { |
---|
| 416 | if( buffer[i] == '\n' ) |
---|
| 417 | { |
---|
| 418 | buffer[i] = '\0'; |
---|
| 419 | |
---|
| 420 | if( i > 0 && buffer[i-1] == '\r' ) |
---|
| 421 | buffer[i-1] = '\0'; |
---|
| 422 | if( buffer[i+1] != '\r' && buffer[i+1] != '\n' ) |
---|
| 423 | lines[++j] = buffer + i + 1; |
---|
[b7d3cc34] | 424 | } |
---|
| 425 | } |
---|
[de3e100] | 426 | |
---|
| 427 | return( lines ); |
---|
[b7d3cc34] | 428 | } |
---|
| 429 | |
---|
[e27661d] | 430 | /* Split an IRC-style line into little parts/arguments. */ |
---|
[0431ea1] | 431 | char **irc_parse_line( char *line ) |
---|
[b7d3cc34] | 432 | { |
---|
| 433 | int i, j; |
---|
| 434 | char **cmd; |
---|
| 435 | |
---|
| 436 | /* Move the line pointer to the start of the command, skipping spaces and the optional prefix. */ |
---|
[de3e100] | 437 | if( line[0] == ':' ) |
---|
| 438 | { |
---|
| 439 | for( i = 0; line[i] != ' '; i ++ ); |
---|
| 440 | line = line + i; |
---|
[b7d3cc34] | 441 | } |
---|
[de3e100] | 442 | for( i = 0; line[i] == ' '; i ++ ); |
---|
| 443 | line = line + i; |
---|
| 444 | |
---|
[b7d3cc34] | 445 | /* If we're already at the end of the line, return. If not, we're going to need at least one element. */ |
---|
[de3e100] | 446 | if( line[0] == '\0') |
---|
| 447 | return NULL; |
---|
| 448 | |
---|
| 449 | /* Count the number of char **cmd elements we're going to need. */ |
---|
| 450 | j = 1; |
---|
| 451 | for( i = 0; line[i] != '\0'; i ++ ) |
---|
| 452 | { |
---|
| 453 | if( line[i] == ' ' ) |
---|
| 454 | { |
---|
| 455 | j ++; |
---|
[b7d3cc34] | 456 | |
---|
[de3e100] | 457 | if( line[i+1] == ':' ) |
---|
| 458 | break; |
---|
| 459 | } |
---|
[b7d3cc34] | 460 | } |
---|
| 461 | |
---|
| 462 | /* Allocate the space we need. */ |
---|
[de3e100] | 463 | cmd = g_new( char *, j + 1 ); |
---|
| 464 | cmd[j] = NULL; |
---|
[b7d3cc34] | 465 | |
---|
| 466 | /* Do the actual line splitting, format is: |
---|
| 467 | * Input: "PRIVMSG #bitlbee :foo bar" |
---|
| 468 | * Output: cmd[0]=="PRIVMSG", cmd[1]=="#bitlbee", cmd[2]=="foo bar", cmd[3]==NULL |
---|
| 469 | */ |
---|
| 470 | |
---|
[de3e100] | 471 | cmd[0] = line; |
---|
| 472 | for( i = 0, j = 0; line[i] != '\0'; i ++ ) |
---|
[b7d3cc34] | 473 | { |
---|
[de3e100] | 474 | if( line[i] == ' ' ) |
---|
[b7d3cc34] | 475 | { |
---|
[de3e100] | 476 | line[i] = '\0'; |
---|
| 477 | cmd[++j] = line + i + 1; |
---|
[b7d3cc34] | 478 | |
---|
[de3e100] | 479 | if( line[i+1] == ':' ) |
---|
[b7d3cc34] | 480 | { |
---|
[de3e100] | 481 | cmd[j] ++; |
---|
[b7d3cc34] | 482 | break; |
---|
| 483 | } |
---|
| 484 | } |
---|
| 485 | } |
---|
| 486 | |
---|
[de3e100] | 487 | return cmd; |
---|
[b7d3cc34] | 488 | } |
---|
| 489 | |
---|
[e27661d] | 490 | /* Converts such an array back into a command string. Mainly used for the IPC code right now. */ |
---|
[74c119d] | 491 | char *irc_build_line( char **cmd ) |
---|
| 492 | { |
---|
| 493 | int i, len; |
---|
| 494 | char *s; |
---|
[b7d3cc34] | 495 | |
---|
[74c119d] | 496 | if( cmd[0] == NULL ) |
---|
| 497 | return NULL; |
---|
[b7d3cc34] | 498 | |
---|
[74c119d] | 499 | len = 1; |
---|
| 500 | for( i = 0; cmd[i]; i ++ ) |
---|
| 501 | len += strlen( cmd[i] ) + 1; |
---|
| 502 | |
---|
| 503 | if( strchr( cmd[i-1], ' ' ) != NULL ) |
---|
| 504 | len ++; |
---|
| 505 | |
---|
| 506 | s = g_new0( char, len + 1 ); |
---|
| 507 | for( i = 0; cmd[i]; i ++ ) |
---|
[b7d3cc34] | 508 | { |
---|
[74c119d] | 509 | if( cmd[i+1] == NULL && strchr( cmd[i], ' ' ) != NULL ) |
---|
| 510 | strcat( s, ":" ); |
---|
[b7d3cc34] | 511 | |
---|
[74c119d] | 512 | strcat( s, cmd[i] ); |
---|
[b7d3cc34] | 513 | |
---|
[74c119d] | 514 | if( cmd[i+1] ) |
---|
| 515 | strcat( s, " " ); |
---|
[b7d3cc34] | 516 | } |
---|
[74c119d] | 517 | strcat( s, "\r\n" ); |
---|
[b7d3cc34] | 518 | |
---|
[74c119d] | 519 | return s; |
---|
[b7d3cc34] | 520 | } |
---|
| 521 | |
---|
| 522 | void irc_reply( irc_t *irc, int code, char *format, ... ) |
---|
| 523 | { |
---|
| 524 | char text[IRC_MAX_LINE]; |
---|
| 525 | va_list params; |
---|
| 526 | |
---|
| 527 | va_start( params, format ); |
---|
| 528 | g_vsnprintf( text, IRC_MAX_LINE, format, params ); |
---|
| 529 | va_end( params ); |
---|
| 530 | irc_write( irc, ":%s %03d %s %s", irc->myhost, code, irc->nick?irc->nick:"*", text ); |
---|
| 531 | |
---|
| 532 | return; |
---|
| 533 | } |
---|
| 534 | |
---|
| 535 | int irc_usermsg( irc_t *irc, char *format, ... ) |
---|
| 536 | { |
---|
| 537 | char text[1024]; |
---|
| 538 | va_list params; |
---|
| 539 | char is_private = 0; |
---|
| 540 | user_t *u; |
---|
| 541 | |
---|
| 542 | u = user_find( irc, irc->mynick ); |
---|
[dd89a55] | 543 | is_private = u->is_private; |
---|
[b7d3cc34] | 544 | |
---|
| 545 | va_start( params, format ); |
---|
| 546 | g_vsnprintf( text, sizeof( text ), format, params ); |
---|
| 547 | va_end( params ); |
---|
| 548 | |
---|
| 549 | return( irc_msgfrom( irc, u->nick, text ) ); |
---|
| 550 | } |
---|
| 551 | |
---|
| 552 | void irc_write( irc_t *irc, char *format, ... ) |
---|
| 553 | { |
---|
| 554 | va_list params; |
---|
| 555 | |
---|
| 556 | va_start( params, format ); |
---|
| 557 | irc_vawrite( irc, format, params ); |
---|
| 558 | va_end( params ); |
---|
| 559 | |
---|
| 560 | return; |
---|
| 561 | |
---|
| 562 | } |
---|
| 563 | |
---|
| 564 | void irc_vawrite( irc_t *irc, char *format, va_list params ) |
---|
| 565 | { |
---|
| 566 | int size; |
---|
[d783e48] | 567 | char line[IRC_MAX_LINE+1], *cs; |
---|
| 568 | |
---|
[0356ae3] | 569 | /* Don't try to write anything new anymore when shutting down. */ |
---|
[5898ef8] | 570 | if( irc->status & USTATUS_SHUTDOWN ) |
---|
[b7d3cc34] | 571 | return; |
---|
[d783e48] | 572 | |
---|
| 573 | line[IRC_MAX_LINE] = 0; |
---|
| 574 | g_vsnprintf( line, IRC_MAX_LINE - 2, format, params ); |
---|
| 575 | |
---|
[b7d3cc34] | 576 | strip_newlines( line ); |
---|
[5c9512f] | 577 | if( ( cs = set_getstr( &irc->set, "charset" ) ) && ( g_strcasecmp( cs, "utf-8" ) != 0 ) ) |
---|
[d783e48] | 578 | { |
---|
| 579 | char conv[IRC_MAX_LINE+1]; |
---|
| 580 | |
---|
| 581 | conv[IRC_MAX_LINE] = 0; |
---|
| 582 | if( do_iconv( "UTF-8", cs, line, conv, 0, IRC_MAX_LINE - 2 ) != -1 ) |
---|
| 583 | strcpy( line, conv ); |
---|
| 584 | } |
---|
[b7d3cc34] | 585 | strcat( line, "\r\n" ); |
---|
[d783e48] | 586 | |
---|
[a0d04d6] | 587 | if( irc->sendbuffer != NULL ) |
---|
| 588 | { |
---|
[b7d3cc34] | 589 | size = strlen( irc->sendbuffer ) + strlen( line ); |
---|
| 590 | irc->sendbuffer = g_renew ( char, irc->sendbuffer, size + 1 ); |
---|
| 591 | strcpy( ( irc->sendbuffer + strlen( irc->sendbuffer ) ), line ); |
---|
| 592 | } |
---|
[a0d04d6] | 593 | else |
---|
[b7d3cc34] | 594 | { |
---|
[a0d04d6] | 595 | irc->sendbuffer = g_strdup(line); |
---|
[b7d3cc34] | 596 | } |
---|
| 597 | |
---|
[a0d04d6] | 598 | if( irc->w_watch_source_id == 0 ) |
---|
[0356ae3] | 599 | { |
---|
| 600 | /* If the buffer is empty we can probably write, so call the write event handler |
---|
| 601 | immediately. If it returns TRUE, it should be called again, so add the event to |
---|
| 602 | the queue. If it's FALSE, we emptied the buffer and saved ourselves some work |
---|
| 603 | in the event queue. */ |
---|
[bbb6ffb] | 604 | /* Really can't be done as long as the code doesn't do error checking very well: |
---|
| 605 | if( bitlbee_io_current_client_write( irc, irc->fd, GAIM_INPUT_WRITE ) ) */ |
---|
| 606 | |
---|
| 607 | /* So just always do it via the event handler. */ |
---|
| 608 | irc->w_watch_source_id = b_input_add( irc->fd, GAIM_INPUT_WRITE, bitlbee_io_current_client_write, irc ); |
---|
[0356ae3] | 609 | } |
---|
[a0d04d6] | 610 | |
---|
[b7d3cc34] | 611 | return; |
---|
| 612 | } |
---|
| 613 | |
---|
[22d41a2] | 614 | void irc_write_all( int now, char *format, ... ) |
---|
[b7d3cc34] | 615 | { |
---|
| 616 | va_list params; |
---|
| 617 | GSList *temp; |
---|
[22d41a2] | 618 | |
---|
[b7d3cc34] | 619 | va_start( params, format ); |
---|
[22d41a2] | 620 | |
---|
[b7d3cc34] | 621 | temp = irc_connection_list; |
---|
[22d41a2] | 622 | while( temp != NULL ) |
---|
| 623 | { |
---|
| 624 | irc_t *irc = temp->data; |
---|
| 625 | |
---|
| 626 | if( now ) |
---|
| 627 | { |
---|
| 628 | g_free( irc->sendbuffer ); |
---|
| 629 | irc->sendbuffer = g_strdup( "\r\n" ); |
---|
| 630 | } |
---|
[b7d3cc34] | 631 | irc_vawrite( temp->data, format, params ); |
---|
[22d41a2] | 632 | if( now ) |
---|
| 633 | { |
---|
[a0d04d6] | 634 | bitlbee_io_current_client_write( irc, irc->fd, GAIM_INPUT_WRITE ); |
---|
[22d41a2] | 635 | } |
---|
[b7d3cc34] | 636 | temp = temp->next; |
---|
| 637 | } |
---|
[22d41a2] | 638 | |
---|
[b7d3cc34] | 639 | va_end( params ); |
---|
| 640 | return; |
---|
| 641 | } |
---|
| 642 | |
---|
| 643 | void irc_names( irc_t *irc, char *channel ) |
---|
| 644 | { |
---|
[3f9440d] | 645 | user_t *u; |
---|
| 646 | char namelist[385] = ""; |
---|
[0da65d5] | 647 | struct groupchat *c = NULL; |
---|
[5a71d9c] | 648 | char *oo = set_getstr(&irc->set, "op_buddies"); |
---|
| 649 | char *vo = set_getstr(&irc->set, "voice_buddies"); |
---|
[b7d3cc34] | 650 | |
---|
[2f13222] | 651 | /* RFCs say there is no error reply allowed on NAMES, so when the |
---|
[b7d3cc34] | 652 | channel is invalid, just give an empty reply. */ |
---|
| 653 | |
---|
[3f9440d] | 654 | if( g_strcasecmp( channel, irc->channel ) == 0 ) |
---|
[b7d3cc34] | 655 | { |
---|
[3f9440d] | 656 | for( u = irc->users; u; u = u->next ) if( u->online ) |
---|
[b7d3cc34] | 657 | { |
---|
[3f9440d] | 658 | if( strlen( namelist ) + strlen( u->nick ) > sizeof( namelist ) - 4 ) |
---|
[b7d3cc34] | 659 | { |
---|
[3f9440d] | 660 | irc_reply( irc, 353, "= %s :%s", channel, namelist ); |
---|
| 661 | *namelist = 0; |
---|
[b7d3cc34] | 662 | } |
---|
[3f9440d] | 663 | |
---|
[5a71d9c] | 664 | if( u->ic && !u->away && !strcmp(vo, "notaway") ) |
---|
[3f9440d] | 665 | strcat( namelist, "+" ); |
---|
[5a71d9c] | 666 | else if( ( strcmp( u->nick, irc->mynick ) == 0 && set_getbool(&irc->set, "op_root") ) || |
---|
| 667 | ( strcmp( u->nick, irc->nick ) == 0 && set_getbool(&irc->set, "op_user") ) || |
---|
| 668 | ( !u->away && !strcmp(oo, "notaway") ) || |
---|
| 669 | ( u->encrypted>1 && !strcmp(oo, "trusted") ) || |
---|
| 670 | ( u->encrypted && !strcmp(oo, "encrypted") ) ) |
---|
[a93e3c8] | 671 | strcat( namelist, "@" ); |
---|
[3f9440d] | 672 | |
---|
| 673 | strcat( namelist, u->nick ); |
---|
| 674 | strcat( namelist, " " ); |
---|
[b7d3cc34] | 675 | } |
---|
| 676 | } |
---|
[0e7ab64] | 677 | else if( ( c = irc_chat_by_channel( irc, channel ) ) ) |
---|
[b7d3cc34] | 678 | { |
---|
| 679 | GList *l; |
---|
[3f9440d] | 680 | |
---|
| 681 | /* root and the user aren't in the channel userlist but should |
---|
| 682 | show up in /NAMES, so list them first: */ |
---|
[5a71d9c] | 683 | sprintf( namelist, "%s%s %s%s ", set_getbool(&irc->set, "op_root") ? "@" : "", irc->mynick, |
---|
| 684 | set_getbool(&irc->set, "op_user") ? "@" : "", irc->nick ); |
---|
[b7d3cc34] | 685 | |
---|
[5a71d9c] | 686 | /* TODO: Honor op/voice_buddies in chats?! */ |
---|
[0da65d5] | 687 | for( l = c->in_room; l; l = l->next ) if( ( u = user_findhandle( c->ic, l->data ) ) ) |
---|
[3f9440d] | 688 | { |
---|
| 689 | if( strlen( namelist ) + strlen( u->nick ) > sizeof( namelist ) - 4 ) |
---|
| 690 | { |
---|
| 691 | irc_reply( irc, 353, "= %s :%s", channel, namelist ); |
---|
| 692 | *namelist = 0; |
---|
| 693 | } |
---|
| 694 | |
---|
| 695 | strcat( namelist, u->nick ); |
---|
| 696 | strcat( namelist, " " ); |
---|
| 697 | } |
---|
[b7d3cc34] | 698 | } |
---|
| 699 | |
---|
[3f9440d] | 700 | if( *namelist ) |
---|
| 701 | irc_reply( irc, 353, "= %s :%s", channel, namelist ); |
---|
| 702 | |
---|
[b7d3cc34] | 703 | irc_reply( irc, 366, "%s :End of /NAMES list", channel ); |
---|
| 704 | } |
---|
| 705 | |
---|
[edf9657] | 706 | int irc_check_login( irc_t *irc ) |
---|
[b7d3cc34] | 707 | { |
---|
[edf9657] | 708 | if( irc->user && irc->nick ) |
---|
| 709 | { |
---|
[3af70b0] | 710 | if( global.conf->authmode == AUTHMODE_CLOSED && !( irc->status & USTATUS_AUTHORIZED ) ) |
---|
[b7d3cc34] | 711 | { |
---|
[edf9657] | 712 | irc_reply( irc, 464, ":This server is password-protected." ); |
---|
| 713 | return 0; |
---|
[b7d3cc34] | 714 | } |
---|
[edf9657] | 715 | else |
---|
[b7d3cc34] | 716 | { |
---|
[edf9657] | 717 | irc_login( irc ); |
---|
| 718 | return 1; |
---|
[b7d3cc34] | 719 | } |
---|
[edf9657] | 720 | } |
---|
| 721 | else |
---|
| 722 | { |
---|
| 723 | /* More information needed. */ |
---|
| 724 | return 0; |
---|
| 725 | } |
---|
[b7d3cc34] | 726 | } |
---|
| 727 | |
---|
| 728 | void irc_login( irc_t *irc ) |
---|
| 729 | { |
---|
| 730 | user_t *u; |
---|
| 731 | |
---|
| 732 | irc_reply( irc, 1, ":Welcome to the BitlBee gateway, %s", irc->nick ); |
---|
| 733 | irc_reply( irc, 2, ":Host %s is running BitlBee " BITLBEE_VERSION " " ARCH "/" CPU ".", irc->myhost ); |
---|
| 734 | irc_reply( irc, 3, ":%s", IRCD_INFO ); |
---|
[238f828] | 735 | irc_reply( irc, 4, "%s %s %s %s", irc->myhost, BITLBEE_VERSION, UMODES UMODES_PRIV, CMODES ); |
---|
[578d627] | 736 | irc_reply( irc, 5, "PREFIX=(ov)@+ CHANTYPES=#& CHANMODES=,,,%s NICKLEN=%d NETWORK=BitlBee CASEMAPPING=rfc1459 MAXTARGETS=1 WATCH=128 :are supported by this server", CMODES, MAX_NICK_LENGTH - 1 ); |
---|
[b7d3cc34] | 737 | irc_motd( irc ); |
---|
[2f13222] | 738 | irc->umode[0] = '\0'; |
---|
[238f828] | 739 | irc_umode_set( irc, "+" UMODE, 1 ); |
---|
[b7d3cc34] | 740 | |
---|
| 741 | u = user_add( irc, irc->mynick ); |
---|
| 742 | u->host = g_strdup( irc->myhost ); |
---|
| 743 | u->realname = g_strdup( ROOT_FN ); |
---|
| 744 | u->online = 1; |
---|
| 745 | u->send_handler = root_command_string; |
---|
| 746 | u->is_private = 0; /* [SH] The channel is root's personal playground. */ |
---|
| 747 | irc_spawn( irc, u ); |
---|
| 748 | |
---|
| 749 | u = user_add( irc, NS_NICK ); |
---|
| 750 | u->host = g_strdup( irc->myhost ); |
---|
| 751 | u->realname = g_strdup( ROOT_FN ); |
---|
| 752 | u->online = 0; |
---|
| 753 | u->send_handler = root_command_string; |
---|
| 754 | u->is_private = 1; /* [SH] NickServ is not in the channel, so should always /query. */ |
---|
| 755 | |
---|
| 756 | u = user_add( irc, irc->nick ); |
---|
| 757 | u->user = g_strdup( irc->user ); |
---|
| 758 | u->host = g_strdup( irc->host ); |
---|
| 759 | u->realname = g_strdup( irc->realname ); |
---|
| 760 | u->online = 1; |
---|
| 761 | irc_spawn( irc, u ); |
---|
| 762 | |
---|
[5e2615a] | 763 | irc_usermsg( irc, "Welcome to the BitlBee gateway!\n\nIf you've never used BitlBee before, please do read the help information using the \x02help\x02 command. Lots of FAQs are answered there." ); |
---|
[b7d3cc34] | 764 | |
---|
[bd9b00f] | 765 | if( global.conf->runmode == RUNMODE_FORKDAEMON || global.conf->runmode == RUNMODE_DAEMON ) |
---|
[2face62] | 766 | ipc_to_master_str( "CLIENT %s %s :%s\r\n", irc->host, irc->nick, irc->realname ); |
---|
| 767 | |
---|
[79e826a] | 768 | irc->status |= USTATUS_LOGGED_IN; |
---|
[b7d3cc34] | 769 | } |
---|
| 770 | |
---|
| 771 | void irc_motd( irc_t *irc ) |
---|
| 772 | { |
---|
| 773 | int fd; |
---|
| 774 | |
---|
| 775 | fd = open( global.conf->motdfile, O_RDONLY ); |
---|
| 776 | if( fd == -1 ) |
---|
| 777 | { |
---|
| 778 | irc_reply( irc, 422, ":We don't need MOTDs." ); |
---|
| 779 | } |
---|
| 780 | else |
---|
| 781 | { |
---|
| 782 | char linebuf[80]; /* Max. line length for MOTD's is 79 chars. It's what most IRC networks seem to do. */ |
---|
| 783 | char *add, max; |
---|
| 784 | int len; |
---|
| 785 | |
---|
| 786 | linebuf[79] = len = 0; |
---|
| 787 | max = sizeof( linebuf ) - 1; |
---|
| 788 | |
---|
| 789 | irc_reply( irc, 375, ":- %s Message Of The Day - ", irc->myhost ); |
---|
| 790 | while( read( fd, linebuf + len, 1 ) == 1 ) |
---|
| 791 | { |
---|
| 792 | if( linebuf[len] == '\n' || len == max ) |
---|
| 793 | { |
---|
| 794 | linebuf[len] = 0; |
---|
| 795 | irc_reply( irc, 372, ":- %s", linebuf ); |
---|
| 796 | len = 0; |
---|
| 797 | } |
---|
| 798 | else if( linebuf[len] == '%' ) |
---|
| 799 | { |
---|
| 800 | read( fd, linebuf + len, 1 ); |
---|
| 801 | if( linebuf[len] == 'h' ) |
---|
| 802 | add = irc->myhost; |
---|
| 803 | else if( linebuf[len] == 'v' ) |
---|
| 804 | add = BITLBEE_VERSION; |
---|
| 805 | else if( linebuf[len] == 'n' ) |
---|
| 806 | add = irc->nick; |
---|
| 807 | else |
---|
| 808 | add = "%"; |
---|
| 809 | |
---|
| 810 | strncpy( linebuf + len, add, max - len ); |
---|
| 811 | while( linebuf[++len] ); |
---|
| 812 | } |
---|
| 813 | else if( len < max ) |
---|
| 814 | { |
---|
| 815 | len ++; |
---|
| 816 | } |
---|
| 817 | } |
---|
| 818 | irc_reply( irc, 376, ":End of MOTD" ); |
---|
[d990997] | 819 | close( fd ); |
---|
[b7d3cc34] | 820 | } |
---|
| 821 | } |
---|
| 822 | |
---|
| 823 | void irc_topic( irc_t *irc, char *channel ) |
---|
| 824 | { |
---|
[50e1776] | 825 | struct groupchat *c = irc_chat_by_channel( irc, channel ); |
---|
| 826 | |
---|
| 827 | if( c && c->topic ) |
---|
| 828 | irc_reply( irc, 332, "%s :%s", channel, c->topic ); |
---|
| 829 | else if( g_strcasecmp( channel, irc->channel ) == 0 ) |
---|
[b7d3cc34] | 830 | irc_reply( irc, 332, "%s :%s", channel, CONTROL_TOPIC ); |
---|
| 831 | else |
---|
[50e1776] | 832 | irc_reply( irc, 331, "%s :No topic for this channel", channel ); |
---|
[b7d3cc34] | 833 | } |
---|
| 834 | |
---|
[238f828] | 835 | void irc_umode_set( irc_t *irc, char *s, int allow_priv ) |
---|
[b7d3cc34] | 836 | { |
---|
[238f828] | 837 | /* allow_priv: Set to 0 if s contains user input, 1 if you want |
---|
| 838 | to set a "privileged" mode (+o, +R, etc). */ |
---|
[b7d3cc34] | 839 | char m[256], st = 1, *t; |
---|
| 840 | int i; |
---|
[2f13222] | 841 | char changes[512], *p, st2 = 2; |
---|
| 842 | char badflag = 0; |
---|
[b7d3cc34] | 843 | |
---|
| 844 | memset( m, 0, sizeof( m ) ); |
---|
| 845 | |
---|
| 846 | for( t = irc->umode; *t; t ++ ) |
---|
| 847 | m[(int)*t] = 1; |
---|
[2f13222] | 848 | |
---|
| 849 | p = changes; |
---|
[b7d3cc34] | 850 | for( t = s; *t; t ++ ) |
---|
| 851 | { |
---|
| 852 | if( *t == '+' || *t == '-' ) |
---|
| 853 | st = *t == '+'; |
---|
[238f828] | 854 | else if( st == 0 || ( strchr( UMODES, *t ) || ( allow_priv && strchr( UMODES_PRIV, *t ) ) ) ) |
---|
[2f13222] | 855 | { |
---|
| 856 | if( m[(int)*t] != st) |
---|
| 857 | { |
---|
| 858 | if( st != st2 ) |
---|
| 859 | st2 = st, *p++ = st ? '+' : '-'; |
---|
| 860 | *p++ = *t; |
---|
| 861 | } |
---|
[b7d3cc34] | 862 | m[(int)*t] = st; |
---|
[2f13222] | 863 | } |
---|
| 864 | else |
---|
| 865 | badflag = 1; |
---|
[b7d3cc34] | 866 | } |
---|
[2f13222] | 867 | *p = '\0'; |
---|
[b7d3cc34] | 868 | |
---|
| 869 | memset( irc->umode, 0, sizeof( irc->umode ) ); |
---|
| 870 | |
---|
| 871 | for( i = 0; i < 256 && strlen( irc->umode ) < ( sizeof( irc->umode ) - 1 ); i ++ ) |
---|
[238f828] | 872 | if( m[i] ) |
---|
[b7d3cc34] | 873 | irc->umode[strlen(irc->umode)] = i; |
---|
| 874 | |
---|
[2f13222] | 875 | if( badflag ) |
---|
| 876 | irc_reply( irc, 501, ":Unknown MODE flag" ); |
---|
| 877 | /* Deliberately no !user@host on the prefix here */ |
---|
| 878 | if( *changes ) |
---|
| 879 | irc_write( irc, ":%s MODE %s %s", irc->nick, irc->nick, changes ); |
---|
[b7d3cc34] | 880 | } |
---|
| 881 | |
---|
| 882 | void irc_spawn( irc_t *irc, user_t *u ) |
---|
| 883 | { |
---|
| 884 | irc_join( irc, u, irc->channel ); |
---|
| 885 | } |
---|
| 886 | |
---|
| 887 | void irc_join( irc_t *irc, user_t *u, char *channel ) |
---|
| 888 | { |
---|
| 889 | char *nick; |
---|
| 890 | |
---|
| 891 | if( ( g_strcasecmp( channel, irc->channel ) != 0 ) || user_find( irc, irc->nick ) ) |
---|
| 892 | irc_write( irc, ":%s!%s@%s JOIN :%s", u->nick, u->user, u->host, channel ); |
---|
| 893 | |
---|
| 894 | if( nick_cmp( u->nick, irc->nick ) == 0 ) |
---|
| 895 | { |
---|
| 896 | irc_write( irc, ":%s MODE %s +%s", irc->myhost, channel, CMODE ); |
---|
| 897 | irc_names( irc, channel ); |
---|
| 898 | irc_topic( irc, channel ); |
---|
| 899 | } |
---|
| 900 | |
---|
| 901 | nick = g_strdup( u->nick ); |
---|
| 902 | nick_lc( nick ); |
---|
| 903 | if( g_hash_table_lookup( irc->watches, nick ) ) |
---|
| 904 | { |
---|
[fc630f9] | 905 | irc_reply( irc, 600, "%s %s %s %d :%s", u->nick, u->user, u->host, (int) time( NULL ), "logged online" ); |
---|
[b7d3cc34] | 906 | } |
---|
| 907 | g_free( nick ); |
---|
| 908 | } |
---|
| 909 | |
---|
| 910 | void irc_part( irc_t *irc, user_t *u, char *channel ) |
---|
| 911 | { |
---|
| 912 | irc_write( irc, ":%s!%s@%s PART %s :%s", u->nick, u->user, u->host, channel, "" ); |
---|
| 913 | } |
---|
| 914 | |
---|
| 915 | void irc_kick( irc_t *irc, user_t *u, char *channel, user_t *kicker ) |
---|
| 916 | { |
---|
| 917 | irc_write( irc, ":%s!%s@%s KICK %s %s :%s", kicker->nick, kicker->user, kicker->host, channel, u->nick, "" ); |
---|
| 918 | } |
---|
| 919 | |
---|
| 920 | void irc_kill( irc_t *irc, user_t *u ) |
---|
| 921 | { |
---|
[fb62f81f] | 922 | char *nick, *s; |
---|
[0a3c243] | 923 | char reason[128]; |
---|
[fb62f81f] | 924 | |
---|
[1186382] | 925 | if( u->ic && u->ic->flags & OPT_LOGGING_OUT && set_getbool( &irc->set, "simulate_netsplit" ) ) |
---|
[fb62f81f] | 926 | { |
---|
[0da65d5] | 927 | if( u->ic->acc->server ) |
---|
[fb62f81f] | 928 | g_snprintf( reason, sizeof( reason ), "%s %s", irc->myhost, |
---|
[0da65d5] | 929 | u->ic->acc->server ); |
---|
[c2fb3809] | 930 | else if( ( s = strchr( u->ic->acc->user, '@' ) ) ) |
---|
[fb62f81f] | 931 | g_snprintf( reason, sizeof( reason ), "%s %s", irc->myhost, |
---|
| 932 | s + 1 ); |
---|
| 933 | else |
---|
| 934 | g_snprintf( reason, sizeof( reason ), "%s %s.%s", irc->myhost, |
---|
[0da65d5] | 935 | u->ic->acc->prpl->name, irc->myhost ); |
---|
[fb62f81f] | 936 | |
---|
| 937 | /* proto_opt might contain garbage after the : */ |
---|
| 938 | if( ( s = strchr( reason, ':' ) ) ) |
---|
| 939 | *s = 0; |
---|
| 940 | } |
---|
| 941 | else |
---|
| 942 | { |
---|
| 943 | strcpy( reason, "Leaving..." ); |
---|
| 944 | } |
---|
[b7d3cc34] | 945 | |
---|
[fb62f81f] | 946 | irc_write( irc, ":%s!%s@%s QUIT :%s", u->nick, u->user, u->host, reason ); |
---|
[b7d3cc34] | 947 | |
---|
| 948 | nick = g_strdup( u->nick ); |
---|
| 949 | nick_lc( nick ); |
---|
| 950 | if( g_hash_table_lookup( irc->watches, nick ) ) |
---|
| 951 | { |
---|
[fc630f9] | 952 | irc_reply( irc, 601, "%s %s %s %d :%s", u->nick, u->user, u->host, (int) time( NULL ), "logged offline" ); |
---|
[b7d3cc34] | 953 | } |
---|
| 954 | g_free( nick ); |
---|
| 955 | } |
---|
| 956 | |
---|
| 957 | int irc_send( irc_t *irc, char *nick, char *s, int flags ) |
---|
| 958 | { |
---|
[0da65d5] | 959 | struct groupchat *c = NULL; |
---|
[b7d3cc34] | 960 | user_t *u = NULL; |
---|
| 961 | |
---|
[94281ef] | 962 | if( *nick == '#' || *nick == '&' ) |
---|
[b7d3cc34] | 963 | { |
---|
[0e7ab64] | 964 | if( !( c = irc_chat_by_channel( irc, nick ) ) ) |
---|
[b7d3cc34] | 965 | { |
---|
| 966 | irc_reply( irc, 403, "%s :Channel does not exist", nick ); |
---|
| 967 | return( 0 ); |
---|
| 968 | } |
---|
| 969 | } |
---|
| 970 | else |
---|
| 971 | { |
---|
| 972 | u = user_find( irc, nick ); |
---|
| 973 | |
---|
| 974 | if( !u ) |
---|
| 975 | { |
---|
| 976 | if( irc->is_private ) |
---|
| 977 | irc_reply( irc, 401, "%s :Nick does not exist", nick ); |
---|
| 978 | else |
---|
| 979 | irc_usermsg( irc, "Nick `%s' does not exist!", nick ); |
---|
| 980 | return( 0 ); |
---|
| 981 | } |
---|
| 982 | } |
---|
| 983 | |
---|
| 984 | if( *s == 1 && s[strlen(s)-1] == 1 ) |
---|
| 985 | { |
---|
| 986 | if( g_strncasecmp( s + 1, "ACTION", 6 ) == 0 ) |
---|
| 987 | { |
---|
| 988 | if( s[7] == ' ' ) s ++; |
---|
| 989 | s += 3; |
---|
| 990 | *(s++) = '/'; |
---|
| 991 | *(s++) = 'm'; |
---|
| 992 | *(s++) = 'e'; |
---|
| 993 | *(s++) = ' '; |
---|
| 994 | s -= 4; |
---|
| 995 | s[strlen(s)-1] = 0; |
---|
| 996 | } |
---|
| 997 | else if( g_strncasecmp( s + 1, "VERSION", 7 ) == 0 ) |
---|
| 998 | { |
---|
| 999 | u = user_find( irc, irc->mynick ); |
---|
| 1000 | irc_privmsg( irc, u, "NOTICE", irc->nick, "", "\001VERSION BitlBee " BITLBEE_VERSION " " ARCH "/" CPU "\001" ); |
---|
| 1001 | return( 1 ); |
---|
| 1002 | } |
---|
| 1003 | else if( g_strncasecmp( s + 1, "PING", 4 ) == 0 ) |
---|
| 1004 | { |
---|
| 1005 | u = user_find( irc, irc->mynick ); |
---|
| 1006 | irc_privmsg( irc, u, "NOTICE", irc->nick, "", s ); |
---|
| 1007 | return( 1 ); |
---|
| 1008 | } |
---|
| 1009 | else if( g_strncasecmp( s + 1, "TYPING", 6 ) == 0 ) |
---|
| 1010 | { |
---|
[0da65d5] | 1011 | if( u && u->ic && u->ic->acc->prpl->send_typing && strlen( s ) >= 10 ) |
---|
[b7d3cc34] | 1012 | { |
---|
| 1013 | time_t current_typing_notice = time( NULL ); |
---|
| 1014 | |
---|
| 1015 | if( current_typing_notice - u->last_typing_notice >= 5 ) |
---|
| 1016 | { |
---|
[df1fb67] | 1017 | u->ic->acc->prpl->send_typing( u->ic, u->handle, ( s[8] - '0' ) << 8 ); |
---|
[b7d3cc34] | 1018 | u->last_typing_notice = current_typing_notice; |
---|
| 1019 | } |
---|
| 1020 | } |
---|
| 1021 | return( 1 ); |
---|
| 1022 | } |
---|
| 1023 | else |
---|
| 1024 | { |
---|
| 1025 | irc_usermsg( irc, "Non-ACTION CTCP's aren't supported" ); |
---|
| 1026 | return( 0 ); |
---|
| 1027 | } |
---|
| 1028 | } |
---|
| 1029 | |
---|
| 1030 | if( u ) |
---|
| 1031 | { |
---|
| 1032 | /* For the next message, we probably do have to send new notices... */ |
---|
| 1033 | u->last_typing_notice = 0; |
---|
| 1034 | u->is_private = irc->is_private; |
---|
| 1035 | |
---|
| 1036 | if( u->is_private ) |
---|
| 1037 | { |
---|
| 1038 | if( !u->online ) |
---|
| 1039 | irc_reply( irc, 301, "%s :%s", u->nick, "User is offline" ); |
---|
| 1040 | else if( u->away ) |
---|
| 1041 | irc_reply( irc, 301, "%s :%s", u->nick, u->away ); |
---|
| 1042 | } |
---|
| 1043 | |
---|
| 1044 | if( u->send_handler ) |
---|
[f73b969] | 1045 | { |
---|
| 1046 | u->send_handler( irc, u, s, flags ); |
---|
| 1047 | return 1; |
---|
| 1048 | } |
---|
[b7d3cc34] | 1049 | } |
---|
[0da65d5] | 1050 | else if( c && c->ic && c->ic->acc && c->ic->acc->prpl ) |
---|
[b7d3cc34] | 1051 | { |
---|
[84b045d] | 1052 | return( imc_chat_msg( c, s, 0 ) ); |
---|
[b7d3cc34] | 1053 | } |
---|
| 1054 | |
---|
| 1055 | return( 0 ); |
---|
| 1056 | } |
---|
| 1057 | |
---|
[ba9edaa] | 1058 | static gboolean buddy_send_handler_delayed( gpointer data, gint fd, b_input_condition cond ) |
---|
[b7d3cc34] | 1059 | { |
---|
| 1060 | user_t *u = data; |
---|
| 1061 | |
---|
[226fce1] | 1062 | /* Shouldn't happen, but just to be sure. */ |
---|
| 1063 | if( u->sendbuf_len < 2 ) |
---|
| 1064 | return FALSE; |
---|
| 1065 | |
---|
[b7d3cc34] | 1066 | u->sendbuf[u->sendbuf_len-2] = 0; /* Cut off the last newline */ |
---|
[84b045d] | 1067 | imc_buddy_msg( u->ic, u->handle, u->sendbuf, u->sendbuf_flags ); |
---|
[b7d3cc34] | 1068 | |
---|
| 1069 | g_free( u->sendbuf ); |
---|
| 1070 | u->sendbuf = NULL; |
---|
| 1071 | u->sendbuf_len = 0; |
---|
| 1072 | u->sendbuf_timer = 0; |
---|
| 1073 | u->sendbuf_flags = 0; |
---|
| 1074 | |
---|
[ba9edaa] | 1075 | return FALSE; |
---|
[b7d3cc34] | 1076 | } |
---|
| 1077 | |
---|
[f73b969] | 1078 | void buddy_send_handler( irc_t *irc, user_t *u, char *msg, int flags ) |
---|
[b7d3cc34] | 1079 | { |
---|
[0da65d5] | 1080 | if( !u || !u->ic ) return; |
---|
[b7d3cc34] | 1081 | |
---|
[d5ccd83] | 1082 | if( set_getbool( &irc->set, "buddy_sendbuffer" ) && set_getint( &irc->set, "buddy_sendbuffer_delay" ) > 0 ) |
---|
[b7d3cc34] | 1083 | { |
---|
[834ff44] | 1084 | int delay; |
---|
| 1085 | |
---|
[b7d3cc34] | 1086 | if( u->sendbuf_len > 0 && u->sendbuf_flags != flags) |
---|
| 1087 | { |
---|
[226fce1] | 1088 | /* Flush the buffer */ |
---|
[ba9edaa] | 1089 | b_event_remove( u->sendbuf_timer ); |
---|
| 1090 | buddy_send_handler_delayed( u, -1, 0 ); |
---|
[b7d3cc34] | 1091 | } |
---|
| 1092 | |
---|
| 1093 | if( u->sendbuf_len == 0 ) |
---|
| 1094 | { |
---|
| 1095 | u->sendbuf_len = strlen( msg ) + 2; |
---|
[226fce1] | 1096 | u->sendbuf = g_new( char, u->sendbuf_len ); |
---|
[b7d3cc34] | 1097 | u->sendbuf[0] = 0; |
---|
| 1098 | u->sendbuf_flags = flags; |
---|
| 1099 | } |
---|
| 1100 | else |
---|
| 1101 | { |
---|
| 1102 | u->sendbuf_len += strlen( msg ) + 1; |
---|
[226fce1] | 1103 | u->sendbuf = g_renew( char, u->sendbuf, u->sendbuf_len ); |
---|
[b7d3cc34] | 1104 | } |
---|
| 1105 | |
---|
| 1106 | strcat( u->sendbuf, msg ); |
---|
| 1107 | strcat( u->sendbuf, "\n" ); |
---|
| 1108 | |
---|
[5c9512f] | 1109 | delay = set_getint( &irc->set, "buddy_sendbuffer_delay" ); |
---|
[834ff44] | 1110 | if( delay <= 5 ) |
---|
| 1111 | delay *= 1000; |
---|
| 1112 | |
---|
[b7d3cc34] | 1113 | if( u->sendbuf_timer > 0 ) |
---|
[ba9edaa] | 1114 | b_event_remove( u->sendbuf_timer ); |
---|
| 1115 | u->sendbuf_timer = b_timeout_add( delay, buddy_send_handler_delayed, u ); |
---|
[b7d3cc34] | 1116 | } |
---|
| 1117 | else |
---|
| 1118 | { |
---|
[84b045d] | 1119 | imc_buddy_msg( u->ic, u->handle, msg, flags ); |
---|
[b7d3cc34] | 1120 | } |
---|
| 1121 | } |
---|
| 1122 | |
---|
| 1123 | int irc_privmsg( irc_t *irc, user_t *u, char *type, char *to, char *prefix, char *msg ) |
---|
| 1124 | { |
---|
| 1125 | char last = 0; |
---|
| 1126 | char *s = msg, *line = msg; |
---|
| 1127 | |
---|
| 1128 | /* The almighty linesplitter .. woohoo!! */ |
---|
| 1129 | while( !last ) |
---|
| 1130 | { |
---|
| 1131 | if( *s == '\r' && *(s+1) == '\n' ) |
---|
| 1132 | *(s++) = 0; |
---|
| 1133 | if( *s == '\n' ) |
---|
| 1134 | { |
---|
| 1135 | last = s[1] == 0; |
---|
| 1136 | *s = 0; |
---|
| 1137 | } |
---|
| 1138 | else |
---|
| 1139 | { |
---|
| 1140 | last = s[0] == 0; |
---|
| 1141 | } |
---|
| 1142 | if( *s == 0 ) |
---|
| 1143 | { |
---|
| 1144 | if( g_strncasecmp( line, "/me ", 4 ) == 0 && ( !prefix || !*prefix ) && g_strcasecmp( type, "PRIVMSG" ) == 0 ) |
---|
| 1145 | { |
---|
| 1146 | irc_write( irc, ":%s!%s@%s %s %s :\001ACTION %s\001", u->nick, u->user, u->host, |
---|
| 1147 | type, to, line + 4 ); |
---|
| 1148 | } |
---|
| 1149 | else |
---|
| 1150 | { |
---|
| 1151 | irc_write( irc, ":%s!%s@%s %s %s :%s%s", u->nick, u->user, u->host, |
---|
[25d1be7] | 1152 | type, to, prefix ? prefix : "", line ); |
---|
[b7d3cc34] | 1153 | } |
---|
| 1154 | line = s + 1; |
---|
| 1155 | } |
---|
| 1156 | s ++; |
---|
| 1157 | } |
---|
| 1158 | |
---|
| 1159 | return( 1 ); |
---|
| 1160 | } |
---|
| 1161 | |
---|
| 1162 | int irc_msgfrom( irc_t *irc, char *nick, char *msg ) |
---|
| 1163 | { |
---|
| 1164 | user_t *u = user_find( irc, nick ); |
---|
| 1165 | static char *prefix = NULL; |
---|
| 1166 | |
---|
| 1167 | if( !u ) return( 0 ); |
---|
| 1168 | if( prefix && *prefix ) g_free( prefix ); |
---|
| 1169 | |
---|
| 1170 | if( !u->is_private && nick_cmp( u->nick, irc->mynick ) != 0 ) |
---|
| 1171 | { |
---|
| 1172 | int len = strlen( irc->nick) + 3; |
---|
| 1173 | prefix = g_new (char, len ); |
---|
[5c9512f] | 1174 | g_snprintf( prefix, len, "%s%s", irc->nick, set_getstr( &irc->set, "to_char" ) ); |
---|
[b7d3cc34] | 1175 | prefix[len-1] = 0; |
---|
| 1176 | } |
---|
| 1177 | else |
---|
| 1178 | { |
---|
| 1179 | prefix = ""; |
---|
| 1180 | } |
---|
| 1181 | |
---|
| 1182 | return( irc_privmsg( irc, u, "PRIVMSG", u->is_private ? irc->nick : irc->channel, prefix, msg ) ); |
---|
| 1183 | } |
---|
| 1184 | |
---|
| 1185 | int irc_noticefrom( irc_t *irc, char *nick, char *msg ) |
---|
| 1186 | { |
---|
| 1187 | user_t *u = user_find( irc, nick ); |
---|
| 1188 | |
---|
| 1189 | if( u ) |
---|
| 1190 | return( irc_privmsg( irc, u, "NOTICE", irc->nick, "", msg ) ); |
---|
| 1191 | else |
---|
| 1192 | return( 0 ); |
---|
| 1193 | } |
---|
| 1194 | |
---|
| 1195 | /* Returns 0 if everything seems to be okay, a number >0 when there was a |
---|
| 1196 | timeout. The number returned is the number of seconds we received no |
---|
| 1197 | pongs from the user. When not connected yet, we don't ping but drop the |
---|
| 1198 | connection when the user fails to connect in IRC_LOGIN_TIMEOUT secs. */ |
---|
[ba9edaa] | 1199 | static gboolean irc_userping( gpointer _irc, gint fd, b_input_condition cond ) |
---|
[b7d3cc34] | 1200 | { |
---|
| 1201 | irc_t *irc = _irc; |
---|
| 1202 | int rv = 0; |
---|
| 1203 | |
---|
[3af70b0] | 1204 | if( !( irc->status & USTATUS_LOGGED_IN ) ) |
---|
[b7d3cc34] | 1205 | { |
---|
| 1206 | if( gettime() > ( irc->last_pong + IRC_LOGIN_TIMEOUT ) ) |
---|
| 1207 | rv = gettime() - irc->last_pong; |
---|
| 1208 | } |
---|
| 1209 | else |
---|
| 1210 | { |
---|
| 1211 | if( ( gettime() > ( irc->last_pong + global.conf->ping_interval ) ) && !irc->pinging ) |
---|
| 1212 | { |
---|
| 1213 | irc_write( irc, "PING :%s", IRC_PING_STRING ); |
---|
| 1214 | irc->pinging = 1; |
---|
| 1215 | } |
---|
| 1216 | else if( gettime() > ( irc->last_pong + global.conf->ping_timeout ) ) |
---|
| 1217 | { |
---|
| 1218 | rv = gettime() - irc->last_pong; |
---|
| 1219 | } |
---|
| 1220 | } |
---|
| 1221 | |
---|
| 1222 | if( rv > 0 ) |
---|
| 1223 | { |
---|
[f73b969] | 1224 | irc_abort( irc, 0, "Ping Timeout: %d seconds", rv ); |
---|
[b7d3cc34] | 1225 | return FALSE; |
---|
| 1226 | } |
---|
| 1227 | |
---|
| 1228 | return TRUE; |
---|
| 1229 | } |
---|
[0e7ab64] | 1230 | |
---|
| 1231 | struct groupchat *irc_chat_by_channel( irc_t *irc, char *channel ) |
---|
| 1232 | { |
---|
| 1233 | struct groupchat *c; |
---|
| 1234 | account_t *a; |
---|
| 1235 | |
---|
| 1236 | /* This finds the connection which has a conversation which belongs to this channel */ |
---|
| 1237 | for( a = irc->accounts; a; a = a->next ) |
---|
| 1238 | { |
---|
[bdda9e9] | 1239 | if( a->ic == NULL ) |
---|
| 1240 | continue; |
---|
| 1241 | |
---|
| 1242 | c = a->ic->groupchats; |
---|
| 1243 | while( c ) |
---|
| 1244 | { |
---|
| 1245 | if( c->channel && g_strcasecmp( c->channel, channel ) == 0 ) |
---|
| 1246 | return c; |
---|
| 1247 | |
---|
| 1248 | c = c->next; |
---|
| 1249 | } |
---|
[0e7ab64] | 1250 | } |
---|
| 1251 | |
---|
| 1252 | return NULL; |
---|
| 1253 | } |
---|