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