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