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