[5ebff60] | 1 | /********************************************************************\ |
---|
[b7d3cc34] | 2 | * BitlBee -- An IRC to other IM-networks gateway * |
---|
| 3 | * * |
---|
[0e788f5] | 4 | * Copyright 2002-2012 Wilmer van der Gaast and others * |
---|
[b7d3cc34] | 5 | \********************************************************************/ |
---|
| 6 | |
---|
[3ddb7477] | 7 | /* The IRC-based UI (for now the only one) */ |
---|
[b7d3cc34] | 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; |
---|
[6f10697] | 22 | if not, write to the Free Software Foundation, Inc., 51 Franklin St., |
---|
| 23 | Fifth Floor, Boston, MA 02110-1301 USA |
---|
[b7d3cc34] | 24 | */ |
---|
| 25 | |
---|
| 26 | #include "bitlbee.h" |
---|
[fb117aee] | 27 | #include "ipc.h" |
---|
[2ff2076] | 28 | #include "dcc.h" |
---|
[59e66ff] | 29 | #include "lib/ssl_client.h" |
---|
[b7d3cc34] | 30 | |
---|
[3ddb7477] | 31 | GSList *irc_connection_list; |
---|
[0c85c08] | 32 | GSList *irc_plugins; |
---|
[b7d3cc34] | 33 | |
---|
[5ebff60] | 34 | static gboolean irc_userping(gpointer _irc, gint fd, b_input_condition cond); |
---|
| 35 | static char *set_eval_charset(set_t *set, char *value); |
---|
| 36 | static char *set_eval_password(set_t *set, char *value); |
---|
| 37 | static char *set_eval_bw_compat(set_t *set, char *value); |
---|
| 38 | static char *set_eval_utf8_nicks(set_t *set, char *value); |
---|
[58adb7e] | 39 | |
---|
[5ebff60] | 40 | irc_t *irc_new(int fd) |
---|
[b7d3cc34] | 41 | { |
---|
[e4d6271] | 42 | irc_t *irc; |
---|
[e9b755e] | 43 | struct sockaddr_storage sock; |
---|
[5ebff60] | 44 | socklen_t socklen = sizeof(sock); |
---|
[3ddb7477] | 45 | char *host = NULL, *myhost = NULL; |
---|
| 46 | irc_user_t *iu; |
---|
[0c85c08] | 47 | GSList *l; |
---|
[7125cb3] | 48 | set_t *s; |
---|
[3ddb7477] | 49 | bee_t *b; |
---|
[5ebff60] | 50 | |
---|
| 51 | irc = g_new0(irc_t, 1); |
---|
| 52 | |
---|
[b7d3cc34] | 53 | irc->fd = fd; |
---|
[5ebff60] | 54 | sock_make_nonblocking(irc->fd); |
---|
| 55 | |
---|
| 56 | irc->r_watch_source_id = b_input_add(irc->fd, B_EV_IO_READ, bitlbee_io_current_client_read, irc); |
---|
| 57 | |
---|
[b7d3cc34] | 58 | irc->status = USTATUS_OFFLINE; |
---|
| 59 | irc->last_pong = gettime(); |
---|
[5ebff60] | 60 | |
---|
| 61 | irc->nick_user_hash = g_hash_table_new(g_str_hash, g_str_equal); |
---|
| 62 | irc->watches = g_hash_table_new(g_str_hash, g_str_equal); |
---|
| 63 | |
---|
| 64 | irc->iconv = (GIConv) - 1; |
---|
| 65 | irc->oconv = (GIConv) - 1; |
---|
| 66 | |
---|
| 67 | if (global.conf->hostname) { |
---|
| 68 | myhost = g_strdup(global.conf->hostname); |
---|
| 69 | } else if (getsockname(irc->fd, (struct sockaddr*) &sock, &socklen) == 0) { |
---|
| 70 | char buf[NI_MAXHOST + 1]; |
---|
| 71 | |
---|
| 72 | if (getnameinfo((struct sockaddr *) &sock, socklen, buf, |
---|
| 73 | NI_MAXHOST, NULL, 0, 0) == 0) { |
---|
| 74 | myhost = g_strdup(ipv6_unwrap(buf)); |
---|
[2231302] | 75 | } |
---|
[b7d3cc34] | 76 | } |
---|
[e9b755e] | 77 | |
---|
[5ebff60] | 78 | if (getpeername(irc->fd, (struct sockaddr*) &sock, &socklen) == 0) { |
---|
| 79 | char buf[NI_MAXHOST + 1]; |
---|
| 80 | |
---|
| 81 | if (getnameinfo((struct sockaddr *) &sock, socklen, buf, |
---|
| 82 | NI_MAXHOST, NULL, 0, 0) == 0) { |
---|
| 83 | host = g_strdup(ipv6_unwrap(buf)); |
---|
[2231302] | 84 | } |
---|
[b7d3cc34] | 85 | } |
---|
[5ebff60] | 86 | |
---|
| 87 | if (host == NULL) { |
---|
| 88 | host = g_strdup("localhost.localdomain"); |
---|
| 89 | } |
---|
| 90 | if (myhost == NULL) { |
---|
| 91 | myhost = g_strdup("localhost.localdomain"); |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | if (global.conf->ping_interval > 0 && global.conf->ping_timeout > 0) { |
---|
| 95 | irc->ping_source_id = b_timeout_add(global.conf->ping_interval * 1000, irc_userping, irc); |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | irc_connection_list = g_slist_append(irc_connection_list, irc); |
---|
| 99 | |
---|
[3ddb7477] | 100 | b = irc->b = bee_new(); |
---|
[d860a8d] | 101 | b->ui_data = irc; |
---|
| 102 | b->ui = &irc_ui_funcs; |
---|
[5ebff60] | 103 | |
---|
| 104 | s = set_add(&b->set, "allow_takeover", "true", set_eval_bool, irc); |
---|
| 105 | s = set_add(&b->set, "away_devoice", "true", set_eval_bw_compat, irc); |
---|
[a758ec1] | 106 | s->flags |= SET_HIDDEN; |
---|
[5ebff60] | 107 | s = set_add(&b->set, "away_reply_timeout", "3600", set_eval_int, irc); |
---|
| 108 | s = set_add(&b->set, "charset", "utf-8", set_eval_charset, irc); |
---|
| 109 | s = set_add(&b->set, "default_target", "root", NULL, irc); |
---|
| 110 | s = set_add(&b->set, "display_namechanges", "false", set_eval_bool, irc); |
---|
| 111 | s = set_add(&b->set, "display_timestamps", "true", set_eval_bool, irc); |
---|
| 112 | s = set_add(&b->set, "handle_unknown", "add_channel", NULL, irc); |
---|
| 113 | s = set_add(&b->set, "last_version", "0", NULL, irc); |
---|
[180ab31] | 114 | s->flags |= SET_HIDDEN; |
---|
[5ebff60] | 115 | s = set_add(&b->set, "lcnicks", "true", set_eval_bool, irc); |
---|
| 116 | s = set_add(&b->set, "nick_format", "%-@nick", NULL, irc); |
---|
| 117 | s = set_add(&b->set, "offline_user_quits", "true", set_eval_bool, irc); |
---|
| 118 | s = set_add(&b->set, "ops", "both", set_eval_irc_channel_ops, irc); |
---|
| 119 | s = set_add(&b->set, "paste_buffer", "false", set_eval_bool, irc); |
---|
| 120 | s->old_key = g_strdup("buddy_sendbuffer"); |
---|
| 121 | s = set_add(&b->set, "paste_buffer_delay", "200", set_eval_int, irc); |
---|
| 122 | s->old_key = g_strdup("buddy_sendbuffer_delay"); |
---|
| 123 | s = set_add(&b->set, "password", NULL, set_eval_password, irc); |
---|
[09d4922] | 124 | s->flags |= SET_NULL_OK | SET_PASSWORD; |
---|
[5ebff60] | 125 | s = set_add(&b->set, "private", "true", set_eval_bool, irc); |
---|
| 126 | s = set_add(&b->set, "query_order", "lifo", NULL, irc); |
---|
| 127 | s = set_add(&b->set, "root_nick", ROOT_NICK, set_eval_root_nick, irc); |
---|
[180ab31] | 128 | s->flags |= SET_HIDDEN; |
---|
[5ebff60] | 129 | s = set_add(&b->set, "show_offline", "false", set_eval_bw_compat, irc); |
---|
[a758ec1] | 130 | s->flags |= SET_HIDDEN; |
---|
[5ebff60] | 131 | s = set_add(&b->set, "simulate_netsplit", "true", set_eval_bool, irc); |
---|
| 132 | s = set_add(&b->set, "timezone", "local", set_eval_timezone, irc); |
---|
| 133 | s = set_add(&b->set, "to_char", ": ", set_eval_to_char, irc); |
---|
| 134 | s = set_add(&b->set, "typing_notice", "false", set_eval_bool, irc); |
---|
| 135 | s = set_add(&b->set, "utf8_nicks", "false", set_eval_utf8_nicks, irc); |
---|
| 136 | |
---|
| 137 | irc->root = iu = irc_user_new(irc, ROOT_NICK); |
---|
| 138 | iu->host = g_strdup(myhost); |
---|
| 139 | iu->fullname = g_strdup(ROOT_FN); |
---|
[280c56a] | 140 | iu->f = &irc_user_root_funcs; |
---|
[5ebff60] | 141 | |
---|
| 142 | iu = irc_user_new(irc, NS_NICK); |
---|
| 143 | iu->host = g_strdup(myhost); |
---|
| 144 | iu->fullname = g_strdup(ROOT_FN); |
---|
[280c56a] | 145 | iu->f = &irc_user_root_funcs; |
---|
[5ebff60] | 146 | |
---|
| 147 | irc->user = g_new0(irc_user_t, 1); |
---|
| 148 | irc->user->host = g_strdup(host); |
---|
| 149 | |
---|
| 150 | conf_loaddefaults(irc); |
---|
| 151 | |
---|
[f9756bd] | 152 | /* Evaluator sets the iconv/oconv structures. */ |
---|
[5ebff60] | 153 | set_eval_charset(set_find(&b->set, "charset"), set_getstr(&b->set, "charset")); |
---|
| 154 | |
---|
[b39859e] | 155 | irc_write(irc, ":%s NOTICE * :%s", irc->root->host, "BitlBee-IRCd initialized, please go on"); |
---|
[5ebff60] | 156 | if (isatty(irc->fd)) { |
---|
[b39859e] | 157 | irc_write(irc, ":%s NOTICE * :%s", irc->root->host, |
---|
[5ebff60] | 158 | "If you read this, you most likely accidentally " |
---|
| 159 | "started BitlBee in inetd mode on the command line. " |
---|
| 160 | "You probably want to run it in (Fork)Daemon mode. " |
---|
| 161 | "See doc/README for more information."); |
---|
| 162 | } |
---|
| 163 | |
---|
| 164 | g_free(myhost); |
---|
| 165 | g_free(host); |
---|
| 166 | |
---|
[3fc6c32] | 167 | /* libpurple doesn't like fork()s after initializing itself, so this |
---|
| 168 | is the right moment to initialize it. */ |
---|
| 169 | #ifdef WITH_PURPLE |
---|
[5674207] | 170 | nogaim_init(); |
---|
[3fc6c32] | 171 | #endif |
---|
[59e66ff] | 172 | |
---|
| 173 | /* SSL library initialization also should be done after the fork, to |
---|
| 174 | avoid shared CSPRNG state. This is required by NSS, which refuses to |
---|
| 175 | work if a fork is detected */ |
---|
| 176 | ssl_init(); |
---|
[5ebff60] | 177 | |
---|
| 178 | for (l = irc_plugins; l; l = l->next) { |
---|
[0c85c08] | 179 | irc_plugin_t *p = l->data; |
---|
[5ebff60] | 180 | if (p->irc_new) { |
---|
| 181 | p->irc_new(irc); |
---|
| 182 | } |
---|
[0c85c08] | 183 | } |
---|
[5ebff60] | 184 | |
---|
[3ddb7477] | 185 | return irc; |
---|
[b7d3cc34] | 186 | } |
---|
| 187 | |
---|
[f73b969] | 188 | /* immed=1 makes this function pretty much equal to irc_free(), except that |
---|
| 189 | this one will "log". In case the connection is already broken and we |
---|
| 190 | shouldn't try to write to it. */ |
---|
[5ebff60] | 191 | void irc_abort(irc_t *irc, int immed, char *format, ...) |
---|
[c1826c6] | 192 | { |
---|
[82ca986] | 193 | char *reason = NULL; |
---|
[5ebff60] | 194 | |
---|
| 195 | if (format != NULL) { |
---|
[f73b969] | 196 | va_list params; |
---|
[5ebff60] | 197 | |
---|
| 198 | va_start(params, format); |
---|
| 199 | reason = g_strdup_vprintf(format, params); |
---|
| 200 | va_end(params); |
---|
| 201 | } |
---|
| 202 | |
---|
| 203 | if (reason) { |
---|
| 204 | irc_write(irc, "ERROR :Closing link: %s", reason); |
---|
| 205 | } |
---|
| 206 | |
---|
| 207 | ipc_to_master_str("OPERMSG :Client exiting: %s@%s [%s]\r\n", |
---|
| 208 | irc->user->nick ? irc->user->nick : "(NONE)", |
---|
| 209 | irc->user->host, reason ? : ""); |
---|
| 210 | |
---|
| 211 | g_free(reason); |
---|
| 212 | |
---|
| 213 | irc_flush(irc); |
---|
| 214 | if (immed) { |
---|
| 215 | irc_free(irc); |
---|
| 216 | } else { |
---|
| 217 | b_event_remove(irc->ping_source_id); |
---|
| 218 | irc->ping_source_id = b_timeout_add(1, (b_event_handler) irc_free, irc); |
---|
[c1826c6] | 219 | } |
---|
| 220 | } |
---|
| 221 | |
---|
[5ebff60] | 222 | static gboolean irc_free_hashkey(gpointer key, gpointer value, gpointer data); |
---|
[b7d3cc34] | 223 | |
---|
[5ebff60] | 224 | void irc_free(irc_t * irc) |
---|
[b7d3cc34] | 225 | { |
---|
[0c85c08] | 226 | GSList *l; |
---|
[5ebff60] | 227 | |
---|
[82ca986] | 228 | irc->status |= USTATUS_SHUTDOWN; |
---|
[5ebff60] | 229 | |
---|
| 230 | log_message(LOGLVL_INFO, "Destroying connection with fd %d", irc->fd); |
---|
| 231 | |
---|
| 232 | if (irc->status & USTATUS_IDENTIFIED && set_getbool(&irc->b->set, "save_on_quit")) { |
---|
| 233 | if (storage_save(irc, NULL, TRUE) != STORAGE_OK) { |
---|
| 234 | log_message(LOGLVL_WARNING, "Error while saving settings for user %s", irc->user->nick); |
---|
| 235 | } |
---|
| 236 | } |
---|
| 237 | |
---|
| 238 | for (l = irc_plugins; l; l = l->next) { |
---|
[0c85c08] | 239 | irc_plugin_t *p = l->data; |
---|
[5ebff60] | 240 | if (p->irc_free) { |
---|
| 241 | p->irc_free(irc); |
---|
| 242 | } |
---|
| 243 | } |
---|
| 244 | |
---|
| 245 | irc_connection_list = g_slist_remove(irc_connection_list, irc); |
---|
| 246 | |
---|
| 247 | while (irc->queries != NULL) { |
---|
| 248 | query_del(irc, irc->queries); |
---|
| 249 | } |
---|
| 250 | |
---|
[d33679e] | 251 | /* This is a little bit messy: bee_free() frees all b->users which |
---|
| 252 | calls us back to free the corresponding irc->users. So do this |
---|
| 253 | before we clear the remaining ones ourselves. */ |
---|
[5ebff60] | 254 | bee_free(irc->b); |
---|
| 255 | |
---|
| 256 | while (irc->users) { |
---|
| 257 | irc_user_free(irc, (irc_user_t *) irc->users->data); |
---|
| 258 | } |
---|
| 259 | |
---|
| 260 | while (irc->channels) { |
---|
| 261 | irc_channel_free(irc->channels->data); |
---|
| 262 | } |
---|
| 263 | |
---|
| 264 | if (irc->ping_source_id > 0) { |
---|
| 265 | b_event_remove(irc->ping_source_id); |
---|
| 266 | } |
---|
| 267 | if (irc->r_watch_source_id > 0) { |
---|
| 268 | b_event_remove(irc->r_watch_source_id); |
---|
| 269 | } |
---|
| 270 | if (irc->w_watch_source_id > 0) { |
---|
| 271 | b_event_remove(irc->w_watch_source_id); |
---|
| 272 | } |
---|
| 273 | |
---|
| 274 | closesocket(irc->fd); |
---|
[fa75134] | 275 | irc->fd = -1; |
---|
[5ebff60] | 276 | |
---|
| 277 | g_hash_table_foreach_remove(irc->nick_user_hash, irc_free_hashkey, NULL); |
---|
| 278 | g_hash_table_destroy(irc->nick_user_hash); |
---|
| 279 | |
---|
| 280 | g_hash_table_foreach_remove(irc->watches, irc_free_hashkey, NULL); |
---|
| 281 | g_hash_table_destroy(irc->watches); |
---|
| 282 | |
---|
| 283 | if (irc->iconv != (GIConv) - 1) { |
---|
| 284 | g_iconv_close(irc->iconv); |
---|
| 285 | } |
---|
| 286 | if (irc->oconv != (GIConv) - 1) { |
---|
| 287 | g_iconv_close(irc->oconv); |
---|
| 288 | } |
---|
| 289 | |
---|
| 290 | g_free(irc->sendbuffer); |
---|
| 291 | g_free(irc->readbuffer); |
---|
| 292 | g_free(irc->password); |
---|
| 293 | |
---|
| 294 | g_free(irc); |
---|
| 295 | |
---|
| 296 | if (global.conf->runmode == RUNMODE_INETD || |
---|
[565a1ea] | 297 | global.conf->runmode == RUNMODE_FORKDAEMON || |
---|
[5ebff60] | 298 | (global.conf->runmode == RUNMODE_DAEMON && |
---|
| 299 | global.listen_socket == -1 && |
---|
| 300 | irc_connection_list == NULL)) { |
---|
[ba9edaa] | 301 | b_main_quit(); |
---|
[5ebff60] | 302 | } |
---|
[b7d3cc34] | 303 | } |
---|
| 304 | |
---|
[5ebff60] | 305 | static gboolean irc_free_hashkey(gpointer key, gpointer value, gpointer data) |
---|
[7cad7b4] | 306 | { |
---|
[5ebff60] | 307 | g_free(key); |
---|
| 308 | |
---|
| 309 | return(TRUE); |
---|
[7cad7b4] | 310 | } |
---|
| 311 | |
---|
[1f92a58] | 312 | /* USE WITH CAUTION! |
---|
| 313 | Sets pass without checking */ |
---|
[5ebff60] | 314 | void irc_setpass(irc_t *irc, const char *pass) |
---|
[1f92a58] | 315 | { |
---|
[5ebff60] | 316 | g_free(irc->password); |
---|
| 317 | |
---|
[1f92a58] | 318 | if (pass) { |
---|
[5ebff60] | 319 | irc->password = g_strdup(pass); |
---|
[1f92a58] | 320 | } else { |
---|
| 321 | irc->password = NULL; |
---|
| 322 | } |
---|
| 323 | } |
---|
| 324 | |
---|
[5ebff60] | 325 | static char *set_eval_password(set_t *set, char *value) |
---|
[0d9d53e] | 326 | { |
---|
| 327 | irc_t *irc = set->data; |
---|
[5ebff60] | 328 | |
---|
| 329 | if (irc->status & USTATUS_IDENTIFIED && value) { |
---|
| 330 | irc_setpass(irc, value); |
---|
[0d9d53e] | 331 | return NULL; |
---|
[5ebff60] | 332 | } else { |
---|
[0d9d53e] | 333 | return SET_INVALID; |
---|
| 334 | } |
---|
| 335 | } |
---|
| 336 | |
---|
[5ebff60] | 337 | static char **irc_splitlines(char *buffer); |
---|
[3ddb7477] | 338 | |
---|
[5ebff60] | 339 | void irc_process(irc_t *irc) |
---|
[b7d3cc34] | 340 | { |
---|
[f9756bd] | 341 | char **lines, *temp, **cmd; |
---|
[b7d3cc34] | 342 | int i; |
---|
| 343 | |
---|
[5ebff60] | 344 | if (irc->readbuffer != NULL) { |
---|
| 345 | lines = irc_splitlines(irc->readbuffer); |
---|
| 346 | |
---|
| 347 | for (i = 0; *lines[i] != '\0'; i++) { |
---|
[f9756bd] | 348 | char *conv = NULL; |
---|
[5ebff60] | 349 | |
---|
[18ff38f] | 350 | /* [WvG] If the last line isn't empty, it's an incomplete line and we |
---|
| 351 | should wait for the rest to come in before processing it. */ |
---|
[5ebff60] | 352 | if (lines[i + 1] == NULL) { |
---|
| 353 | temp = g_strdup(lines[i]); |
---|
| 354 | g_free(irc->readbuffer); |
---|
[b7d3cc34] | 355 | irc->readbuffer = temp; |
---|
[5ebff60] | 356 | i++; |
---|
[b7d3cc34] | 357 | break; |
---|
[e27661d] | 358 | } |
---|
[5ebff60] | 359 | |
---|
| 360 | if (irc->iconv != (GIConv) - 1) { |
---|
[f9756bd] | 361 | gsize bytes_read, bytes_written; |
---|
[5ebff60] | 362 | |
---|
| 363 | conv = g_convert_with_iconv(lines[i], -1, irc->iconv, |
---|
| 364 | &bytes_read, &bytes_written, NULL); |
---|
| 365 | |
---|
| 366 | if (conv == NULL || bytes_read != strlen(lines[i])) { |
---|
[fc0cf92] | 367 | /* GLib can do strange things if things are not in the expected charset, |
---|
| 368 | so let's be a little bit paranoid here: */ |
---|
[5ebff60] | 369 | if (irc->status & USTATUS_LOGGED_IN) { |
---|
| 370 | irc_rootmsg(irc, "Error: Charset mismatch detected. The charset " |
---|
| 371 | "setting is currently set to %s, so please make " |
---|
| 372 | "sure your IRC client will send and accept text in " |
---|
| 373 | "that charset, or tell BitlBee which charset to " |
---|
| 374 | "expect by changing the charset setting. See " |
---|
| 375 | "`help set charset' for more information. Your " |
---|
| 376 | "message was ignored.", |
---|
| 377 | set_getstr(&irc->b->set, "charset")); |
---|
| 378 | |
---|
| 379 | g_free(conv); |
---|
[f9756bd] | 380 | conv = NULL; |
---|
[5ebff60] | 381 | } else { |
---|
[b39859e] | 382 | irc_write(irc, ":%s NOTICE * :%s", irc->root->host, |
---|
[5ebff60] | 383 | "Warning: invalid characters received at login time."); |
---|
| 384 | |
---|
| 385 | conv = g_strdup(lines[i]); |
---|
| 386 | for (temp = conv; *temp; temp++) { |
---|
| 387 | if (*temp & 0x80) { |
---|
[fc0cf92] | 388 | *temp = '?'; |
---|
[5ebff60] | 389 | } |
---|
| 390 | } |
---|
[fc0cf92] | 391 | } |
---|
[94d52d64] | 392 | } |
---|
| 393 | lines[i] = conv; |
---|
[e27661d] | 394 | } |
---|
[5ebff60] | 395 | |
---|
| 396 | if (lines[i] && (cmd = irc_parse_line(lines[i]))) { |
---|
| 397 | irc_exec(irc, cmd); |
---|
| 398 | g_free(cmd); |
---|
[f9756bd] | 399 | } |
---|
[5ebff60] | 400 | |
---|
| 401 | g_free(conv); |
---|
| 402 | |
---|
[f73b969] | 403 | /* Shouldn't really happen, but just in case... */ |
---|
[5ebff60] | 404 | if (!g_slist_find(irc_connection_list, irc)) { |
---|
| 405 | g_free(lines); |
---|
[f73b969] | 406 | return; |
---|
[b7d3cc34] | 407 | } |
---|
| 408 | } |
---|
[5ebff60] | 409 | |
---|
| 410 | if (lines[i] != NULL) { |
---|
| 411 | g_free(irc->readbuffer); |
---|
[0431ea1] | 412 | irc->readbuffer = NULL; |
---|
[b7d3cc34] | 413 | } |
---|
[5ebff60] | 414 | |
---|
| 415 | g_free(lines); |
---|
[b7d3cc34] | 416 | } |
---|
| 417 | } |
---|
| 418 | |
---|
[3ddb7477] | 419 | /* Splits a long string into separate lines. The array is NULL-terminated |
---|
| 420 | and, unless the string contains an incomplete line at the end, ends with |
---|
| 421 | an empty string. Could use g_strsplit() but this one does it in-place. |
---|
| 422 | (So yes, it's destructive.) */ |
---|
[5ebff60] | 423 | static char **irc_splitlines(char *buffer) |
---|
[b7d3cc34] | 424 | { |
---|
[18ff38f] | 425 | int i, j, n = 3; |
---|
[b7d3cc34] | 426 | char **lines; |
---|
| 427 | |
---|
[18ff38f] | 428 | /* Allocate n+1 elements. */ |
---|
[5ebff60] | 429 | lines = g_new(char *, n + 1); |
---|
| 430 | |
---|
[de3e100] | 431 | lines[0] = buffer; |
---|
[5ebff60] | 432 | |
---|
[18ff38f] | 433 | /* Split the buffer in several strings, and accept any kind of line endings, |
---|
| 434 | * knowing that ERC on Windows may send something interesting like \r\r\n, |
---|
| 435 | * and surely there must be clients that think just \n is enough... */ |
---|
[5ebff60] | 436 | for (i = 0, j = 0; buffer[i] != '\0'; i++) { |
---|
| 437 | if (buffer[i] == '\r' || buffer[i] == '\n') { |
---|
| 438 | while (buffer[i] == '\r' || buffer[i] == '\n') { |
---|
[18ff38f] | 439 | buffer[i++] = '\0'; |
---|
[5ebff60] | 440 | } |
---|
| 441 | |
---|
[18ff38f] | 442 | lines[++j] = buffer + i; |
---|
[5ebff60] | 443 | |
---|
| 444 | if (j >= n) { |
---|
[18ff38f] | 445 | n *= 2; |
---|
[5ebff60] | 446 | lines = g_renew(char *, lines, n + 1); |
---|
[18ff38f] | 447 | } |
---|
| 448 | |
---|
[5ebff60] | 449 | if (buffer[i] == '\0') { |
---|
[18ff38f] | 450 | break; |
---|
[5ebff60] | 451 | } |
---|
[b7d3cc34] | 452 | } |
---|
| 453 | } |
---|
[5ebff60] | 454 | |
---|
| 455 | /* NULL terminate our list. */ |
---|
[18ff38f] | 456 | lines[++j] = NULL; |
---|
[5ebff60] | 457 | |
---|
[18ff38f] | 458 | return lines; |
---|
[b7d3cc34] | 459 | } |
---|
| 460 | |
---|
[e27661d] | 461 | /* Split an IRC-style line into little parts/arguments. */ |
---|
[5ebff60] | 462 | char **irc_parse_line(char *line) |
---|
[b7d3cc34] | 463 | { |
---|
| 464 | int i, j; |
---|
| 465 | char **cmd; |
---|
[5ebff60] | 466 | |
---|
[b7d3cc34] | 467 | /* Move the line pointer to the start of the command, skipping spaces and the optional prefix. */ |
---|
[5ebff60] | 468 | if (line[0] == ':') { |
---|
| 469 | for (i = 0; line[i] && line[i] != ' '; i++) { |
---|
| 470 | ; |
---|
| 471 | } |
---|
[de3e100] | 472 | line = line + i; |
---|
[b7d3cc34] | 473 | } |
---|
[5ebff60] | 474 | for (i = 0; line[i] == ' '; i++) { |
---|
| 475 | ; |
---|
| 476 | } |
---|
[de3e100] | 477 | line = line + i; |
---|
[5ebff60] | 478 | |
---|
[b7d3cc34] | 479 | /* If we're already at the end of the line, return. If not, we're going to need at least one element. */ |
---|
[5ebff60] | 480 | if (line[0] == '\0') { |
---|
[de3e100] | 481 | return NULL; |
---|
[5ebff60] | 482 | } |
---|
| 483 | |
---|
[de3e100] | 484 | /* Count the number of char **cmd elements we're going to need. */ |
---|
| 485 | j = 1; |
---|
[5ebff60] | 486 | for (i = 0; line[i] != '\0'; i++) { |
---|
| 487 | if (line[i] == ' ') { |
---|
| 488 | j++; |
---|
| 489 | |
---|
| 490 | if (line[i + 1] == ':') { |
---|
[de3e100] | 491 | break; |
---|
[5ebff60] | 492 | } |
---|
[de3e100] | 493 | } |
---|
[5ebff60] | 494 | } |
---|
[b7d3cc34] | 495 | |
---|
| 496 | /* Allocate the space we need. */ |
---|
[5ebff60] | 497 | cmd = g_new(char *, j + 1); |
---|
[de3e100] | 498 | cmd[j] = NULL; |
---|
[5ebff60] | 499 | |
---|
[b7d3cc34] | 500 | /* Do the actual line splitting, format is: |
---|
| 501 | * Input: "PRIVMSG #bitlbee :foo bar" |
---|
| 502 | * Output: cmd[0]=="PRIVMSG", cmd[1]=="#bitlbee", cmd[2]=="foo bar", cmd[3]==NULL |
---|
| 503 | */ |
---|
| 504 | |
---|
[de3e100] | 505 | cmd[0] = line; |
---|
[5ebff60] | 506 | for (i = 0, j = 0; line[i] != '\0'; i++) { |
---|
| 507 | if (line[i] == ' ') { |
---|
[de3e100] | 508 | line[i] = '\0'; |
---|
| 509 | cmd[++j] = line + i + 1; |
---|
[5ebff60] | 510 | |
---|
| 511 | if (line[i + 1] == ':') { |
---|
| 512 | cmd[j]++; |
---|
[b7d3cc34] | 513 | break; |
---|
| 514 | } |
---|
| 515 | } |
---|
| 516 | } |
---|
[5ebff60] | 517 | |
---|
[de3e100] | 518 | return cmd; |
---|
[b7d3cc34] | 519 | } |
---|
| 520 | |
---|
[e27661d] | 521 | /* Converts such an array back into a command string. Mainly used for the IPC code right now. */ |
---|
[5ebff60] | 522 | char *irc_build_line(char **cmd) |
---|
[74c119d] | 523 | { |
---|
| 524 | int i, len; |
---|
| 525 | char *s; |
---|
[5ebff60] | 526 | |
---|
| 527 | if (cmd[0] == NULL) { |
---|
[74c119d] | 528 | return NULL; |
---|
[5ebff60] | 529 | } |
---|
| 530 | |
---|
[74c119d] | 531 | len = 1; |
---|
[5ebff60] | 532 | for (i = 0; cmd[i]; i++) { |
---|
| 533 | len += strlen(cmd[i]) + 1; |
---|
| 534 | } |
---|
| 535 | |
---|
| 536 | if (strchr(cmd[i - 1], ' ') != NULL) { |
---|
| 537 | len++; |
---|
| 538 | } |
---|
| 539 | |
---|
| 540 | s = g_new0(char, len + 1); |
---|
| 541 | for (i = 0; cmd[i]; i++) { |
---|
| 542 | if (cmd[i + 1] == NULL && strchr(cmd[i], ' ') != NULL) { |
---|
| 543 | strcat(s, ":"); |
---|
| 544 | } |
---|
| 545 | |
---|
| 546 | strcat(s, cmd[i]); |
---|
| 547 | |
---|
| 548 | if (cmd[i + 1]) { |
---|
| 549 | strcat(s, " "); |
---|
| 550 | } |
---|
| 551 | } |
---|
| 552 | strcat(s, "\r\n"); |
---|
| 553 | |
---|
[74c119d] | 554 | return s; |
---|
[b7d3cc34] | 555 | } |
---|
| 556 | |
---|
[5ebff60] | 557 | void irc_write(irc_t *irc, char *format, ...) |
---|
[b7d3cc34] | 558 | { |
---|
| 559 | va_list params; |
---|
[3ddb7477] | 560 | |
---|
[5ebff60] | 561 | va_start(params, format); |
---|
| 562 | irc_vawrite(irc, format, params); |
---|
| 563 | va_end(params); |
---|
[3ddb7477] | 564 | |
---|
[b7d3cc34] | 565 | return; |
---|
| 566 | } |
---|
| 567 | |
---|
[5ebff60] | 568 | void irc_write_all(int now, char *format, ...) |
---|
[b7d3cc34] | 569 | { |
---|
| 570 | va_list params; |
---|
[5ebff60] | 571 | GSList *temp; |
---|
| 572 | |
---|
| 573 | va_start(params, format); |
---|
| 574 | |
---|
[3ddb7477] | 575 | temp = irc_connection_list; |
---|
[5ebff60] | 576 | while (temp != NULL) { |
---|
[3ddb7477] | 577 | irc_t *irc = temp->data; |
---|
[5ebff60] | 578 | |
---|
| 579 | if (now) { |
---|
| 580 | g_free(irc->sendbuffer); |
---|
| 581 | irc->sendbuffer = g_strdup("\r\n"); |
---|
[3ddb7477] | 582 | } |
---|
[5ebff60] | 583 | irc_vawrite(temp->data, format, params); |
---|
| 584 | if (now) { |
---|
| 585 | bitlbee_io_current_client_write(irc, irc->fd, B_EV_IO_WRITE); |
---|
[3ddb7477] | 586 | } |
---|
| 587 | temp = temp->next; |
---|
| 588 | } |
---|
[5ebff60] | 589 | |
---|
| 590 | va_end(params); |
---|
[b7d3cc34] | 591 | return; |
---|
[5ebff60] | 592 | } |
---|
[b7d3cc34] | 593 | |
---|
[5ebff60] | 594 | void irc_vawrite(irc_t *irc, char *format, va_list params) |
---|
[b7d3cc34] | 595 | { |
---|
| 596 | int size; |
---|
[5ebff60] | 597 | char line[IRC_MAX_LINE + 1]; |
---|
| 598 | |
---|
[0356ae3] | 599 | /* Don't try to write anything new anymore when shutting down. */ |
---|
[5ebff60] | 600 | if (irc->status & USTATUS_SHUTDOWN) { |
---|
[b7d3cc34] | 601 | return; |
---|
[5ebff60] | 602 | } |
---|
| 603 | |
---|
| 604 | memset(line, 0, sizeof(line)); |
---|
| 605 | g_vsnprintf(line, IRC_MAX_LINE - 2, format, params); |
---|
| 606 | strip_newlines(line); |
---|
| 607 | |
---|
| 608 | if (irc->oconv != (GIConv) - 1) { |
---|
[f9756bd] | 609 | gsize bytes_read, bytes_written; |
---|
| 610 | char *conv; |
---|
[5ebff60] | 611 | |
---|
| 612 | conv = g_convert_with_iconv(line, -1, irc->oconv, |
---|
| 613 | &bytes_read, &bytes_written, NULL); |
---|
| 614 | |
---|
| 615 | if (bytes_read == strlen(line)) { |
---|
| 616 | strncpy(line, conv, IRC_MAX_LINE - 2); |
---|
| 617 | } |
---|
| 618 | |
---|
| 619 | g_free(conv); |
---|
| 620 | } |
---|
| 621 | g_strlcat(line, "\r\n", IRC_MAX_LINE + 1); |
---|
| 622 | |
---|
| 623 | if (irc->sendbuffer != NULL) { |
---|
| 624 | size = strlen(irc->sendbuffer) + strlen(line); |
---|
| 625 | irc->sendbuffer = g_renew(char, irc->sendbuffer, size + 1); |
---|
| 626 | strcpy((irc->sendbuffer + strlen(irc->sendbuffer)), line); |
---|
| 627 | } else { |
---|
[a0d04d6] | 628 | irc->sendbuffer = g_strdup(line); |
---|
[b7d3cc34] | 629 | } |
---|
[5ebff60] | 630 | |
---|
| 631 | if (irc->w_watch_source_id == 0) { |
---|
[0356ae3] | 632 | /* If the buffer is empty we can probably write, so call the write event handler |
---|
| 633 | immediately. If it returns TRUE, it should be called again, so add the event to |
---|
| 634 | the queue. If it's FALSE, we emptied the buffer and saved ourselves some work |
---|
| 635 | in the event queue. */ |
---|
[bbb6ffb] | 636 | /* Really can't be done as long as the code doesn't do error checking very well: |
---|
[e046390] | 637 | if( bitlbee_io_current_client_write( irc, irc->fd, B_EV_IO_WRITE ) ) */ |
---|
[5ebff60] | 638 | |
---|
[bbb6ffb] | 639 | /* So just always do it via the event handler. */ |
---|
[5ebff60] | 640 | irc->w_watch_source_id = b_input_add(irc->fd, B_EV_IO_WRITE, bitlbee_io_current_client_write, irc); |
---|
[0356ae3] | 641 | } |
---|
[5ebff60] | 642 | |
---|
[b7d3cc34] | 643 | return; |
---|
| 644 | } |
---|
| 645 | |
---|
[f1c2b21] | 646 | /* Flush sendbuffer if you can. If it fails, fail silently and let some |
---|
| 647 | I/O event handler clean up. */ |
---|
[5ebff60] | 648 | void irc_flush(irc_t *irc) |
---|
[f1c2b21] | 649 | { |
---|
| 650 | ssize_t n; |
---|
| 651 | size_t len; |
---|
[5ebff60] | 652 | |
---|
| 653 | if (irc->sendbuffer == NULL) { |
---|
[f1c2b21] | 654 | return; |
---|
[5ebff60] | 655 | } |
---|
| 656 | |
---|
| 657 | len = strlen(irc->sendbuffer); |
---|
| 658 | if ((n = send(irc->fd, irc->sendbuffer, len, 0)) == len) { |
---|
| 659 | g_free(irc->sendbuffer); |
---|
[f1c2b21] | 660 | irc->sendbuffer = NULL; |
---|
[5ebff60] | 661 | |
---|
| 662 | b_event_remove(irc->w_watch_source_id); |
---|
[f1c2b21] | 663 | irc->w_watch_source_id = 0; |
---|
[5ebff60] | 664 | } else if (n > 0) { |
---|
| 665 | char *s = g_strdup(irc->sendbuffer + n); |
---|
| 666 | g_free(irc->sendbuffer); |
---|
[f1c2b21] | 667 | irc->sendbuffer = s; |
---|
| 668 | } |
---|
| 669 | /* Otherwise something went wrong and we don't currently care |
---|
| 670 | what the error was. We may or may not succeed later, we |
---|
| 671 | were just trying to flush the buffer immediately. */ |
---|
| 672 | } |
---|
| 673 | |
---|
| 674 | /* Meant for takeover functionality. Transfer an IRC connection to a different |
---|
| 675 | socket. */ |
---|
[5ebff60] | 676 | void irc_switch_fd(irc_t *irc, int fd) |
---|
[f1c2b21] | 677 | { |
---|
[5ebff60] | 678 | irc_write(irc, "ERROR :Transferring session to a new connection"); |
---|
| 679 | irc_flush(irc); /* Write it now or forget about it forever. */ |
---|
| 680 | |
---|
| 681 | if (irc->sendbuffer) { |
---|
| 682 | b_event_remove(irc->w_watch_source_id); |
---|
[af9f2ca] | 683 | irc->w_watch_source_id = 0; |
---|
[5ebff60] | 684 | g_free(irc->sendbuffer); |
---|
[af9f2ca] | 685 | irc->sendbuffer = NULL; |
---|
[f1c2b21] | 686 | } |
---|
[5ebff60] | 687 | |
---|
| 688 | b_event_remove(irc->r_watch_source_id); |
---|
| 689 | closesocket(irc->fd); |
---|
[f1c2b21] | 690 | irc->fd = fd; |
---|
[5ebff60] | 691 | irc->r_watch_source_id = b_input_add(irc->fd, B_EV_IO_READ, bitlbee_io_current_client_read, irc); |
---|
[f1c2b21] | 692 | } |
---|
| 693 | |
---|
[5ebff60] | 694 | void irc_sync(irc_t *irc) |
---|
[f1c2b21] | 695 | { |
---|
| 696 | GSList *l; |
---|
[5ebff60] | 697 | |
---|
| 698 | irc_write(irc, ":%s!%s@%s MODE %s :+%s", irc->user->nick, |
---|
| 699 | irc->user->user, irc->user->host, irc->user->nick, |
---|
| 700 | irc->umode); |
---|
| 701 | |
---|
| 702 | for (l = irc->channels; l; l = l->next) { |
---|
[f1c2b21] | 703 | irc_channel_t *ic = l->data; |
---|
[5ebff60] | 704 | if (ic->flags & IRC_CHANNEL_JOINED) { |
---|
| 705 | irc_send_join(ic, irc->user); |
---|
| 706 | } |
---|
[f1c2b21] | 707 | } |
---|
[5ebff60] | 708 | |
---|
[e1aaea4] | 709 | /* We may be waiting for a PONG from the previous client connection. */ |
---|
| 710 | irc->pinging = FALSE; |
---|
[f1c2b21] | 711 | } |
---|
| 712 | |
---|
[5ebff60] | 713 | void irc_desync(irc_t *irc) |
---|
[f1c2b21] | 714 | { |
---|
| 715 | GSList *l; |
---|
[5ebff60] | 716 | |
---|
| 717 | for (l = irc->channels; l; l = l->next) { |
---|
| 718 | irc_channel_del_user(l->data, irc->user, IRC_CDU_KICK, |
---|
| 719 | "Switching to old session"); |
---|
| 720 | } |
---|
| 721 | |
---|
| 722 | irc_write(irc, ":%s!%s@%s MODE %s :-%s", irc->user->nick, |
---|
| 723 | irc->user->user, irc->user->host, irc->user->nick, |
---|
| 724 | irc->umode); |
---|
[f1c2b21] | 725 | } |
---|
| 726 | |
---|
[5ebff60] | 727 | int irc_check_login(irc_t *irc) |
---|
[b7d3cc34] | 728 | { |
---|
[5ebff60] | 729 | if (irc->user->user && irc->user->nick) { |
---|
| 730 | if (global.conf->authmode == AUTHMODE_CLOSED && !(irc->status & USTATUS_AUTHORIZED)) { |
---|
| 731 | irc_send_num(irc, 464, ":This server is password-protected."); |
---|
[edf9657] | 732 | return 0; |
---|
[5ebff60] | 733 | } else { |
---|
[4be8239] | 734 | irc_channel_t *ic; |
---|
| 735 | irc_user_t *iu = irc->user; |
---|
[5ebff60] | 736 | |
---|
| 737 | irc->user = irc_user_new(irc, iu->nick); |
---|
[4be8239] | 738 | irc->user->user = iu->user; |
---|
[b95932e] | 739 | irc->user->host = iu->host; |
---|
[4be8239] | 740 | irc->user->fullname = iu->fullname; |
---|
[280c56a] | 741 | irc->user->f = &irc_user_self_funcs; |
---|
[5ebff60] | 742 | g_free(iu->nick); |
---|
| 743 | g_free(iu); |
---|
| 744 | |
---|
| 745 | if (global.conf->runmode == RUNMODE_FORKDAEMON || global.conf->runmode == RUNMODE_DAEMON) { |
---|
| 746 | ipc_to_master_str("CLIENT %s %s :%s\r\n", irc->user->host, irc->user->nick, |
---|
| 747 | irc->user->fullname); |
---|
| 748 | } |
---|
| 749 | |
---|
[4be8239] | 750 | irc->status |= USTATUS_LOGGED_IN; |
---|
[5ebff60] | 751 | |
---|
| 752 | irc_send_login(irc); |
---|
| 753 | |
---|
[b919363] | 754 | irc->umode[0] = '\0'; |
---|
[5ebff60] | 755 | irc_umode_set(irc, "+" UMODE, TRUE); |
---|
| 756 | |
---|
| 757 | ic = irc->default_channel = irc_channel_new(irc, ROOT_CHAN); |
---|
| 758 | irc_channel_set_topic(ic, CONTROL_TOPIC, irc->root); |
---|
| 759 | set_setstr(&ic->set, "auto_join", "true"); |
---|
| 760 | irc_channel_auto_joins(irc, NULL); |
---|
| 761 | |
---|
[f7ca587] | 762 | irc->root->last_channel = irc->default_channel; |
---|
[5ebff60] | 763 | |
---|
| 764 | irc_rootmsg(irc, |
---|
| 765 | "Welcome to the BitlBee gateway!\n\n" |
---|
[7320610] | 766 | "Running %s %s\n\n" |
---|
[5ebff60] | 767 | "If you've never used BitlBee before, please do read the help " |
---|
| 768 | "information using the \x02help\x02 command. Lots of FAQs are " |
---|
| 769 | "answered there.\n" |
---|
| 770 | "If you already have an account on this server, just use the " |
---|
[7320610] | 771 | "\x02identify\x02 command to identify yourself.", |
---|
| 772 | PACKAGE, BITLBEE_VERSION); |
---|
[5ebff60] | 773 | |
---|
[70f69ecc] | 774 | /* This is for bug #209 (use PASS to identify to NickServ). */ |
---|
[5ebff60] | 775 | if (irc->password != NULL) { |
---|
| 776 | char *send_cmd[] = { "identify", g_strdup(irc->password), NULL }; |
---|
| 777 | |
---|
| 778 | irc_setpass(irc, NULL); |
---|
| 779 | root_command(irc, send_cmd); |
---|
| 780 | g_free(send_cmd[1]); |
---|
[70f69ecc] | 781 | } |
---|
[5ebff60] | 782 | |
---|
[edf9657] | 783 | return 1; |
---|
[b7d3cc34] | 784 | } |
---|
[5ebff60] | 785 | } else { |
---|
[edf9657] | 786 | /* More information needed. */ |
---|
| 787 | return 0; |
---|
| 788 | } |
---|
[b7d3cc34] | 789 | } |
---|
| 790 | |
---|
[65016a6] | 791 | /* TODO: This is a mess, but this function is a bit too complicated to be |
---|
| 792 | converted to something more generic. */ |
---|
[5ebff60] | 793 | void irc_umode_set(irc_t *irc, const char *s, gboolean allow_priv) |
---|
[b919363] | 794 | { |
---|
| 795 | /* allow_priv: Set to 0 if s contains user input, 1 if you want |
---|
| 796 | to set a "privileged" mode (+o, +R, etc). */ |
---|
| 797 | char m[128], st = 1; |
---|
| 798 | const char *t; |
---|
| 799 | int i; |
---|
[00fd005] | 800 | char changes[512], st2 = 2; |
---|
[b919363] | 801 | char badflag = 0; |
---|
[5ebff60] | 802 | |
---|
| 803 | memset(m, 0, sizeof(m)); |
---|
| 804 | |
---|
[00fd005] | 805 | /* Keep track of which modes are enabled in this array. */ |
---|
[5ebff60] | 806 | for (t = irc->umode; *t; t++) { |
---|
| 807 | if (*t < sizeof(m)) { |
---|
| 808 | m[(int) *t] = 1; |
---|
| 809 | } |
---|
| 810 | } |
---|
| 811 | |
---|
[00fd005] | 812 | i = 0; |
---|
[5ebff60] | 813 | for (t = s; *t && i < sizeof(changes) - 3; t++) { |
---|
| 814 | if (*t == '+' || *t == '-') { |
---|
[b919363] | 815 | st = *t == '+'; |
---|
[5ebff60] | 816 | } else if ((st == 0 && (!strchr(UMODES_KEEP, *t) || allow_priv)) || |
---|
| 817 | (st == 1 && strchr(UMODES, *t)) || |
---|
| 818 | (st == 1 && allow_priv && strchr(UMODES_PRIV, *t))) { |
---|
| 819 | if (m[(int) *t] != st) { |
---|
[00fd005] | 820 | /* If we're actually making a change, remember this |
---|
| 821 | for the response. */ |
---|
[5ebff60] | 822 | if (st != st2) { |
---|
[00fd005] | 823 | st2 = st, changes[i++] = st ? '+' : '-'; |
---|
[5ebff60] | 824 | } |
---|
[00fd005] | 825 | changes[i++] = *t; |
---|
[b919363] | 826 | } |
---|
[5ebff60] | 827 | m[(int) *t] = st; |
---|
| 828 | } else { |
---|
[b919363] | 829 | badflag = 1; |
---|
[5ebff60] | 830 | } |
---|
[b919363] | 831 | } |
---|
[00fd005] | 832 | changes[i] = '\0'; |
---|
[5ebff60] | 833 | |
---|
[00fd005] | 834 | /* Convert the m array back into an umode string. */ |
---|
[5ebff60] | 835 | memset(irc->umode, 0, sizeof(irc->umode)); |
---|
| 836 | for (i = 'A'; i <= 'z' && strlen(irc->umode) < (sizeof(irc->umode) - 1); i++) { |
---|
| 837 | if (m[i]) { |
---|
[b919363] | 838 | irc->umode[strlen(irc->umode)] = i; |
---|
[5ebff60] | 839 | } |
---|
| 840 | } |
---|
| 841 | |
---|
| 842 | if (badflag) { |
---|
| 843 | irc_send_num(irc, 501, ":Unknown MODE flag"); |
---|
| 844 | } |
---|
| 845 | if (*changes) { |
---|
| 846 | irc_write(irc, ":%s!%s@%s MODE %s :%s", irc->user->nick, |
---|
| 847 | irc->user->user, irc->user->host, irc->user->nick, |
---|
| 848 | changes); |
---|
| 849 | } |
---|
[b919363] | 850 | } |
---|
| 851 | |
---|
[3923003] | 852 | /* Returns 0 if everything seems to be okay, a number >0 when there was a |
---|
| 853 | timeout. The number returned is the number of seconds we received no |
---|
| 854 | pongs from the user. When not connected yet, we don't ping but drop the |
---|
| 855 | connection when the user fails to connect in IRC_LOGIN_TIMEOUT secs. */ |
---|
[5ebff60] | 856 | static gboolean irc_userping(gpointer _irc, gint fd, b_input_condition cond) |
---|
[3923003] | 857 | { |
---|
[b958cb5] | 858 | double now = gettime(); |
---|
[3923003] | 859 | irc_t *irc = _irc; |
---|
[b958cb5] | 860 | int fail = 0; |
---|
[5ebff60] | 861 | |
---|
| 862 | if (!(irc->status & USTATUS_LOGGED_IN)) { |
---|
| 863 | if (now > (irc->last_pong + IRC_LOGIN_TIMEOUT)) { |
---|
[b958cb5] | 864 | fail = now - irc->last_pong; |
---|
[3923003] | 865 | } |
---|
[5ebff60] | 866 | } else { |
---|
| 867 | if (now > (irc->last_pong + global.conf->ping_timeout)) { |
---|
| 868 | fail = now - irc->last_pong; |
---|
| 869 | } else { |
---|
| 870 | irc_write(irc, "PING :%s", IRC_PING_STRING); |
---|
[3923003] | 871 | } |
---|
| 872 | } |
---|
[5ebff60] | 873 | |
---|
| 874 | if (fail > 0) { |
---|
| 875 | irc_abort(irc, 0, "Ping Timeout: %d seconds", fail); |
---|
[3923003] | 876 | return FALSE; |
---|
| 877 | } |
---|
[5ebff60] | 878 | |
---|
[3923003] | 879 | return TRUE; |
---|
| 880 | } |
---|
| 881 | |
---|
[5ebff60] | 882 | static char *set_eval_charset(set_t *set, char *value) |
---|
[b7d3cc34] | 883 | { |
---|
[5ebff60] | 884 | irc_t *irc = (irc_t *) set->data; |
---|
[21c87a7] | 885 | char *test; |
---|
| 886 | gsize test_bytes = 0; |
---|
[3ddb7477] | 887 | GIConv ic, oc; |
---|
[b7d3cc34] | 888 | |
---|
[5ebff60] | 889 | if (g_strcasecmp(value, "none") == 0) { |
---|
| 890 | value = g_strdup("utf-8"); |
---|
| 891 | } |
---|
[b7d3cc34] | 892 | |
---|
[5ebff60] | 893 | if ((oc = g_iconv_open(value, "utf-8")) == (GIConv) - 1) { |
---|
[3ddb7477] | 894 | return NULL; |
---|
[b7d3cc34] | 895 | } |
---|
[5ebff60] | 896 | |
---|
[21c87a7] | 897 | /* Do a test iconv to see if the user picked an IRC-compatible |
---|
| 898 | charset (for example utf-16 goes *horribly* wrong). */ |
---|
[5ebff60] | 899 | if ((test = g_convert_with_iconv(" ", 1, oc, NULL, &test_bytes, NULL)) == NULL || |
---|
| 900 | test_bytes > 1) { |
---|
| 901 | g_free(test); |
---|
| 902 | g_iconv_close(oc); |
---|
| 903 | irc_rootmsg(irc, "Unsupported character set: The IRC protocol " |
---|
| 904 | "only supports 8-bit character sets."); |
---|
[21c87a7] | 905 | return NULL; |
---|
[0e7ab64] | 906 | } |
---|
[5ebff60] | 907 | g_free(test); |
---|
| 908 | |
---|
| 909 | if ((ic = g_iconv_open("utf-8", value)) == (GIConv) - 1) { |
---|
| 910 | g_iconv_close(oc); |
---|
[3ddb7477] | 911 | return NULL; |
---|
[b7d3cc34] | 912 | } |
---|
[5ebff60] | 913 | |
---|
| 914 | if (irc->iconv != (GIConv) - 1) { |
---|
| 915 | g_iconv_close(irc->iconv); |
---|
| 916 | } |
---|
| 917 | if (irc->oconv != (GIConv) - 1) { |
---|
| 918 | g_iconv_close(irc->oconv); |
---|
| 919 | } |
---|
| 920 | |
---|
[3ddb7477] | 921 | irc->iconv = ic; |
---|
| 922 | irc->oconv = oc; |
---|
[0e7ab64] | 923 | |
---|
[3ddb7477] | 924 | return value; |
---|
[0e7ab64] | 925 | } |
---|
[0e8b3e8] | 926 | |
---|
[6d8cc05] | 927 | /* Mostly meant for upgrades. If one of these is set to the non-default, |
---|
| 928 | set show_users of all channels to something with the same effect. */ |
---|
[5ebff60] | 929 | static char *set_eval_bw_compat(set_t *set, char *value) |
---|
[0e8b3e8] | 930 | { |
---|
| 931 | irc_t *irc = set->data; |
---|
[6d8cc05] | 932 | char *val; |
---|
| 933 | GSList *l; |
---|
[5ebff60] | 934 | |
---|
| 935 | irc_rootmsg(irc, "Setting `%s' is obsolete, use the `show_users' " |
---|
| 936 | "channel setting instead.", set->key); |
---|
| 937 | |
---|
| 938 | if (strcmp(set->key, "away_devoice") == 0 && !bool2int(value)) { |
---|
[7b8238d] | 939 | val = "online,special%,away"; |
---|
[5ebff60] | 940 | } else if (strcmp(set->key, "show_offline") == 0 && bool2int(value)) { |
---|
[7b8238d] | 941 | val = "online@,special%,away+,offline"; |
---|
[5ebff60] | 942 | } else { |
---|
[7b8238d] | 943 | val = "online+,special%,away"; |
---|
[5ebff60] | 944 | } |
---|
| 945 | |
---|
| 946 | for (l = irc->channels; l; l = l->next) { |
---|
[6d8cc05] | 947 | irc_channel_t *ic = l->data; |
---|
| 948 | /* No need to check channel type, if the setting doesn't exist it |
---|
| 949 | will just be ignored. */ |
---|
[5ebff60] | 950 | set_setstr(&ic->set, "show_users", val); |
---|
[6d8cc05] | 951 | } |
---|
[5ebff60] | 952 | |
---|
[6d8cc05] | 953 | return SET_INVALID; |
---|
[0e8b3e8] | 954 | } |
---|
[0c85c08] | 955 | |
---|
[5ebff60] | 956 | static char *set_eval_utf8_nicks(set_t *set, char *value) |
---|
[c608891] | 957 | { |
---|
| 958 | irc_t *irc = set->data; |
---|
[5ebff60] | 959 | gboolean val = bool2int(value); |
---|
| 960 | |
---|
[c608891] | 961 | /* Do *NOT* unset this flag in the middle of a session. There will |
---|
| 962 | be UTF-8 nicks around already so if we suddenly disable support |
---|
| 963 | for them, various functions might behave strangely. */ |
---|
[5ebff60] | 964 | if (val) { |
---|
[c608891] | 965 | irc->status |= IRC_UTF8_NICKS; |
---|
[5ebff60] | 966 | } else if (irc->status & IRC_UTF8_NICKS) { |
---|
| 967 | irc_rootmsg(irc, "You need to reconnect to BitlBee for this " |
---|
| 968 | "change to take effect."); |
---|
| 969 | } |
---|
| 970 | |
---|
| 971 | return set_eval_bool(set, value); |
---|
[c608891] | 972 | } |
---|
| 973 | |
---|
[5ebff60] | 974 | void register_irc_plugin(const struct irc_plugin *p) |
---|
[0c85c08] | 975 | { |
---|
[5ebff60] | 976 | irc_plugins = g_slist_prepend(irc_plugins, (gpointer) p); |
---|
[0c85c08] | 977 | } |
---|