[f06894d] | 1 | /***************************************************************************\ |
---|
| 2 | * * |
---|
| 3 | * BitlBee - An IRC to IM gateway * |
---|
| 4 | * Jabber module - Main file * |
---|
| 5 | * * |
---|
| 6 | * Copyright 2006 Wilmer van der Gaast <wilmer@gaast.net> * |
---|
| 7 | * * |
---|
| 8 | * This program is free software; you can redistribute it and/or modify * |
---|
| 9 | * it under the terms of the GNU General Public License as published by * |
---|
| 10 | * the Free Software Foundation; either version 2 of the License, or * |
---|
| 11 | * (at your option) any later version. * |
---|
| 12 | * * |
---|
| 13 | * This program is distributed in the hope that it will be useful, * |
---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
---|
| 16 | * GNU General Public License for more details. * |
---|
| 17 | * * |
---|
| 18 | * You should have received a copy of the GNU General Public License along * |
---|
| 19 | * with this program; if not, write to the Free Software Foundation, Inc., * |
---|
| 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * |
---|
| 21 | * * |
---|
| 22 | \***************************************************************************/ |
---|
| 23 | |
---|
| 24 | #include <glib.h> |
---|
| 25 | #include <string.h> |
---|
| 26 | #include <unistd.h> |
---|
| 27 | #include <ctype.h> |
---|
| 28 | #include <stdio.h> |
---|
| 29 | |
---|
[21167d2] | 30 | #include "ssl_client.h" |
---|
[f06894d] | 31 | #include "xmltree.h" |
---|
| 32 | #include "bitlbee.h" |
---|
| 33 | #include "jabber.h" |
---|
| 34 | |
---|
| 35 | static void jabber_acc_init( account_t *acc ) |
---|
| 36 | { |
---|
| 37 | set_t *s; |
---|
| 38 | |
---|
| 39 | s = set_add( &acc->set, "port", "5222", set_eval_int, acc ); |
---|
| 40 | s->flags |= ACC_SET_OFFLINE_ONLY; |
---|
| 41 | |
---|
[ebe7b36] | 42 | s = set_add( &acc->set, "priority", "0", set_eval_priority, acc ); |
---|
[f06894d] | 43 | |
---|
[6baca2a] | 44 | s = set_add( &acc->set, "privacy_list", NULL, NULL, acc ); |
---|
| 45 | /* TODO: Add evaluator. */ |
---|
| 46 | |
---|
[ebe7b36] | 47 | s = set_add( &acc->set, "resource", "BitlBee", NULL, acc ); |
---|
| 48 | s->flags |= ACC_SET_OFFLINE_ONLY; |
---|
[f06894d] | 49 | |
---|
| 50 | s = set_add( &acc->set, "server", NULL, set_eval_account, acc ); |
---|
| 51 | s->flags |= ACC_SET_NOSAVE | ACC_SET_OFFLINE_ONLY; |
---|
| 52 | |
---|
| 53 | s = set_add( &acc->set, "ssl", "false", set_eval_bool, acc ); |
---|
| 54 | s->flags |= ACC_SET_OFFLINE_ONLY; |
---|
| 55 | |
---|
| 56 | s = set_add( &acc->set, "tls", "try", set_eval_tls, acc ); |
---|
| 57 | s->flags |= ACC_SET_OFFLINE_ONLY; |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | static void jabber_login( account_t *acc ) |
---|
| 61 | { |
---|
[21167d2] | 62 | struct gaim_connection *gc = new_gaim_conn( acc ); |
---|
| 63 | struct jabber_data *jd = g_new0( struct jabber_data, 1 ); |
---|
| 64 | |
---|
| 65 | jd->gc = gc; |
---|
| 66 | gc->proto_data = jd; |
---|
| 67 | |
---|
| 68 | jd->username = g_strdup( acc->user ); |
---|
| 69 | jd->server = strchr( jd->username, '@' ); |
---|
| 70 | |
---|
| 71 | if( jd->server == NULL ) |
---|
| 72 | { |
---|
| 73 | hide_login_progress( gc, "Incomplete account name (format it like <username@jabberserver.name>)" ); |
---|
| 74 | signoff( gc ); |
---|
| 75 | return; |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | /* So don't think of free()ing jd->server.. :-) */ |
---|
| 79 | *jd->server = 0; |
---|
| 80 | jd->server ++; |
---|
| 81 | |
---|
[fe7a554] | 82 | jd->node_cache = xt_new_node( "cache", NULL, NULL ); |
---|
| 83 | |
---|
[21167d2] | 84 | if( set_getbool( &acc->set, "ssl" ) ) |
---|
| 85 | { |
---|
[6baca2a] | 86 | jd->ssl = ssl_connect( acc->server ? acc->server : jd->server, set_getint( &acc->set, "port" ), jabber_connected_ssl, gc ); |
---|
[42127dc] | 87 | jd->fd = ssl_getfd( jd->ssl ); |
---|
[21167d2] | 88 | } |
---|
| 89 | else |
---|
| 90 | { |
---|
[6baca2a] | 91 | jd->fd = proxy_connect( acc->server ? acc->server : jd->server, set_getint( &acc->set, "port" ), jabber_connected_plain, gc ); |
---|
[21167d2] | 92 | } |
---|
[f06894d] | 93 | } |
---|
| 94 | |
---|
| 95 | static void jabber_close( struct gaim_connection *gc ) |
---|
| 96 | { |
---|
[21167d2] | 97 | struct jabber_data *jd = gc->proto_data; |
---|
| 98 | |
---|
[4a0614e] | 99 | jabber_end_stream( gc ); |
---|
| 100 | |
---|
[21167d2] | 101 | if( jd->r_inpa >= 0 ) |
---|
| 102 | b_event_remove( jd->r_inpa ); |
---|
| 103 | if( jd->w_inpa >= 0 ) |
---|
| 104 | b_event_remove( jd->w_inpa ); |
---|
| 105 | |
---|
| 106 | if( jd->ssl ) |
---|
| 107 | ssl_disconnect( jd->ssl ); |
---|
| 108 | if( jd->fd >= 0 ) |
---|
| 109 | closesocket( jd->fd ); |
---|
| 110 | |
---|
[fe7a554] | 111 | if( jd->tx_len ) |
---|
| 112 | g_free( jd->txq ); |
---|
| 113 | |
---|
[6baca2a] | 114 | xt_free_node( jd->privacy_list ); |
---|
| 115 | g_free( jd->privacy_active ); |
---|
| 116 | |
---|
[fe7a554] | 117 | xt_free_node( jd->node_cache ); |
---|
[70f6aab8] | 118 | xt_free( jd->xt ); |
---|
| 119 | |
---|
[5e202b0] | 120 | g_free( jd->away_message ); |
---|
[21167d2] | 121 | g_free( jd->username ); |
---|
| 122 | g_free( jd ); |
---|
[f06894d] | 123 | } |
---|
| 124 | |
---|
| 125 | static int jabber_send_im( struct gaim_connection *gc, char *who, char *message, int len, int away ) |
---|
| 126 | { |
---|
[a214954] | 127 | struct xt_node *node, *event; |
---|
[4a0614e] | 128 | int st; |
---|
| 129 | |
---|
[a214954] | 130 | /* |
---|
| 131 | event = xt_new_node( "active", NULL, NULL ); |
---|
| 132 | xt_add_attr( event, "xlmns", "http://jabber.org/protocol/chatstates" ); |
---|
| 133 | |
---|
| 134 | event = xt_new_node( "x", NULL, xt_new_node( "composing", NULL, NULL ) ); |
---|
| 135 | xt_add_attr( event, "xmlns", "jabber:x:event" ); |
---|
| 136 | */ |
---|
| 137 | |
---|
[4a0614e] | 138 | node = xt_new_node( "body", message, NULL ); |
---|
| 139 | node = jabber_make_packet( "message", "chat", who, node ); |
---|
| 140 | st = jabber_write_packet( gc, node ); |
---|
| 141 | xt_free_node( node ); |
---|
| 142 | |
---|
| 143 | return st; |
---|
[f06894d] | 144 | } |
---|
| 145 | |
---|
[dd788bb] | 146 | static GList *jabber_away_states( struct gaim_connection *gc ) |
---|
| 147 | { |
---|
[5e202b0] | 148 | static GList *l = NULL; |
---|
| 149 | int i; |
---|
[dd788bb] | 150 | |
---|
[5e202b0] | 151 | if( l == NULL ) |
---|
| 152 | for( i = 0; jabber_away_state_list[i].full_name; i ++ ) |
---|
| 153 | l = g_list_append( l, (void*) jabber_away_state_list[i].full_name ); |
---|
[dd788bb] | 154 | |
---|
[5e202b0] | 155 | return l; |
---|
[dd788bb] | 156 | } |
---|
| 157 | |
---|
[5e202b0] | 158 | static void jabber_set_away( struct gaim_connection *gc, char *state_txt, char *message ) |
---|
[dd788bb] | 159 | { |
---|
[5e202b0] | 160 | struct jabber_data *jd = gc->proto_data; |
---|
| 161 | struct jabber_away_state *state; |
---|
| 162 | |
---|
| 163 | /* Save all this info. We need it, for example, when changing the priority setting. */ |
---|
| 164 | state = (void *) jabber_away_state_by_name( state_txt ); |
---|
| 165 | jd->away_state = state ? state : (void *) jabber_away_state_list; /* Fall back to "Away" if necessary. */ |
---|
| 166 | g_free( jd->away_message ); |
---|
| 167 | jd->away_message = ( message && *message ) ? g_strdup( message ) : NULL; |
---|
| 168 | |
---|
[172a73f1] | 169 | presence_send_update( gc ); |
---|
[deff040] | 170 | } |
---|
| 171 | |
---|
[cfbb3a6] | 172 | static void jabber_add_buddy( struct gaim_connection *gc, char *who ) |
---|
| 173 | { |
---|
| 174 | if( jabber_add_to_roster( gc, who, NULL ) ) |
---|
| 175 | presence_send_request( gc, who, "subscribe" ); |
---|
| 176 | } |
---|
| 177 | |
---|
| 178 | static void jabber_remove_buddy( struct gaim_connection *gc, char *who, char *group ) |
---|
| 179 | { |
---|
| 180 | if( jabber_remove_from_roster( gc, who ) ) |
---|
| 181 | presence_send_request( gc, who, "unsubscribe" ); |
---|
| 182 | } |
---|
| 183 | |
---|
[deff040] | 184 | static void jabber_keepalive( struct gaim_connection *gc ) |
---|
| 185 | { |
---|
[a214954] | 186 | struct jabber_data *jd = gc->proto_data; |
---|
[eab2ac4] | 187 | struct xt_node *c, *tmp; |
---|
[a214954] | 188 | |
---|
[deff040] | 189 | /* Just any whitespace character is enough as a keepalive for XMPP sessions. */ |
---|
| 190 | jabber_write( gc, "\n", 1 ); |
---|
[a214954] | 191 | |
---|
| 192 | /* Let's abuse this keepalive for garbage collection of the node cache too. |
---|
| 193 | It runs every minute, so let's mark every node with a special flag the |
---|
| 194 | first time we see it, and clean it up the second time (clean up all |
---|
| 195 | packets with the flag set). |
---|
| 196 | |
---|
| 197 | node->flags is normally only used by xmltree itself for parsing/handling, |
---|
| 198 | so it should be safe to use the variable for gc. */ |
---|
| 199 | |
---|
| 200 | /* This horrible loop is explained in xmltree.c. Makes me wonder if maybe I |
---|
| 201 | didn't choose the perfect data structure... */ |
---|
[eab2ac4] | 202 | for( c = jd->node_cache->children; c; c = c->next ) |
---|
| 203 | if( !( c->flags & XT_SEEN ) ) |
---|
| 204 | break; |
---|
| 205 | |
---|
| 206 | /* Now c points at the first unflagged node (or at NULL). Clean up |
---|
| 207 | everything until that point. */ |
---|
| 208 | while( jd->node_cache->children != c ) |
---|
| 209 | { |
---|
| 210 | /* |
---|
| 211 | printf( "Cleaning up:\n" ); |
---|
| 212 | xt_print( jd->node_cache->children ); |
---|
| 213 | */ |
---|
| 214 | |
---|
| 215 | tmp = jd->node_cache->children->next; |
---|
| 216 | xt_free_node( jd->node_cache->children ); |
---|
| 217 | jd->node_cache->children = tmp; |
---|
| 218 | } |
---|
| 219 | |
---|
| 220 | /* Now flag the ones that were still unflagged. */ |
---|
| 221 | for( c = jd->node_cache->children; c; c = c->next ) |
---|
[a214954] | 222 | { |
---|
[eab2ac4] | 223 | /* |
---|
| 224 | printf( "Flagged:\n" ); |
---|
| 225 | xt_print( c ); |
---|
| 226 | */ |
---|
| 227 | |
---|
| 228 | c->flags |= XT_SEEN; |
---|
[a214954] | 229 | } |
---|
[dd788bb] | 230 | } |
---|
| 231 | |
---|
[8e5e2e9] | 232 | static void jabber_add_permit( struct gaim_connection *gc, char *who ) |
---|
| 233 | { |
---|
| 234 | presence_send_request( gc, who, "subscribed" ); |
---|
| 235 | } |
---|
| 236 | |
---|
| 237 | static void jabber_rem_permit( struct gaim_connection *gc, char *who ) |
---|
| 238 | { |
---|
| 239 | presence_send_request( gc, who, "unsubscribed" ); |
---|
| 240 | } |
---|
| 241 | |
---|
[ebe7b36] | 242 | /* XMPP doesn't have both a block- and and allow-list, so these two functions |
---|
| 243 | will be no-ops: */ |
---|
[8e5e2e9] | 244 | static void jabber_add_deny( struct gaim_connection *gc, char *who ) |
---|
| 245 | { |
---|
| 246 | } |
---|
| 247 | |
---|
| 248 | static void jabber_rem_deny( struct gaim_connection *gc, char *who ) |
---|
| 249 | { |
---|
| 250 | } |
---|
| 251 | |
---|
[f06894d] | 252 | void jabber_init() |
---|
| 253 | { |
---|
[deff040] | 254 | struct prpl *ret = g_new0( struct prpl, 1 ); |
---|
[f06894d] | 255 | |
---|
| 256 | ret->name = "jabber"; |
---|
| 257 | ret->login = jabber_login; |
---|
| 258 | ret->acc_init = jabber_acc_init; |
---|
| 259 | ret->close = jabber_close; |
---|
| 260 | ret->send_im = jabber_send_im; |
---|
[dd788bb] | 261 | ret->away_states = jabber_away_states; |
---|
[f06894d] | 262 | // ret->get_status_string = jabber_get_status_string; |
---|
[dd788bb] | 263 | ret->set_away = jabber_set_away; |
---|
[f06894d] | 264 | // ret->set_info = jabber_set_info; |
---|
| 265 | // ret->get_info = jabber_get_info; |
---|
[cfbb3a6] | 266 | ret->add_buddy = jabber_add_buddy; |
---|
| 267 | ret->remove_buddy = jabber_remove_buddy; |
---|
[f06894d] | 268 | // ret->chat_send = jabber_chat_send; |
---|
| 269 | // ret->chat_invite = jabber_chat_invite; |
---|
| 270 | // ret->chat_leave = jabber_chat_leave; |
---|
| 271 | // ret->chat_open = jabber_chat_open; |
---|
[deff040] | 272 | ret->keepalive = jabber_keepalive; |
---|
[8e5e2e9] | 273 | ret->add_permit = jabber_add_permit; |
---|
| 274 | ret->rem_permit = jabber_rem_permit; |
---|
| 275 | ret->add_deny = jabber_add_deny; |
---|
| 276 | ret->rem_deny = jabber_rem_deny; |
---|
[f06894d] | 277 | // ret->send_typing = jabber_send_typing; |
---|
| 278 | ret->handle_cmp = g_strcasecmp; |
---|
| 279 | |
---|
[deff040] | 280 | register_protocol( ret ); |
---|
[f06894d] | 281 | } |
---|