[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 | |
---|
| 42 | s = set_add( &acc->set, "priority", "0", set_eval_resprio, acc ); |
---|
| 43 | |
---|
| 44 | s = set_add( &acc->set, "resource", "BitlBee", set_eval_resprio, acc ); |
---|
| 45 | |
---|
| 46 | s = set_add( &acc->set, "server", NULL, set_eval_account, acc ); |
---|
| 47 | s->flags |= ACC_SET_NOSAVE | ACC_SET_OFFLINE_ONLY; |
---|
| 48 | |
---|
| 49 | s = set_add( &acc->set, "ssl", "false", set_eval_bool, acc ); |
---|
| 50 | s->flags |= ACC_SET_OFFLINE_ONLY; |
---|
| 51 | |
---|
| 52 | s = set_add( &acc->set, "tls", "try", set_eval_tls, acc ); |
---|
| 53 | s->flags |= ACC_SET_OFFLINE_ONLY; |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | static void jabber_login( account_t *acc ) |
---|
| 57 | { |
---|
[21167d2] | 58 | struct gaim_connection *gc = new_gaim_conn( acc ); |
---|
| 59 | struct jabber_data *jd = g_new0( struct jabber_data, 1 ); |
---|
| 60 | |
---|
| 61 | jd->gc = gc; |
---|
| 62 | gc->proto_data = jd; |
---|
| 63 | |
---|
| 64 | jd->username = g_strdup( acc->user ); |
---|
| 65 | jd->server = strchr( jd->username, '@' ); |
---|
| 66 | |
---|
| 67 | if( jd->server == NULL ) |
---|
| 68 | { |
---|
| 69 | hide_login_progress( gc, "Incomplete account name (format it like <username@jabberserver.name>)" ); |
---|
| 70 | signoff( gc ); |
---|
| 71 | return; |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | /* So don't think of free()ing jd->server.. :-) */ |
---|
| 75 | *jd->server = 0; |
---|
| 76 | jd->server ++; |
---|
| 77 | |
---|
[fe7a554] | 78 | jd->node_cache = xt_new_node( "cache", NULL, NULL ); |
---|
| 79 | |
---|
[21167d2] | 80 | if( set_getbool( &acc->set, "ssl" ) ) |
---|
| 81 | { |
---|
[42127dc] | 82 | jd->ssl = ssl_connect( jd->server, set_getint( &acc->set, "port" ), jabber_connected_ssl, gc ); |
---|
| 83 | jd->fd = ssl_getfd( jd->ssl ); |
---|
[21167d2] | 84 | } |
---|
| 85 | else |
---|
| 86 | { |
---|
| 87 | jd->fd = proxy_connect( jd->server, set_getint( &acc->set, "port" ), jabber_connected_plain, gc ); |
---|
| 88 | } |
---|
[f06894d] | 89 | } |
---|
| 90 | |
---|
| 91 | static void jabber_close( struct gaim_connection *gc ) |
---|
| 92 | { |
---|
[21167d2] | 93 | struct jabber_data *jd = gc->proto_data; |
---|
| 94 | |
---|
[4a0614e] | 95 | jabber_end_stream( gc ); |
---|
| 96 | |
---|
[21167d2] | 97 | if( jd->r_inpa >= 0 ) |
---|
| 98 | b_event_remove( jd->r_inpa ); |
---|
| 99 | if( jd->w_inpa >= 0 ) |
---|
| 100 | b_event_remove( jd->w_inpa ); |
---|
| 101 | |
---|
| 102 | if( jd->ssl ) |
---|
| 103 | ssl_disconnect( jd->ssl ); |
---|
| 104 | if( jd->fd >= 0 ) |
---|
| 105 | closesocket( jd->fd ); |
---|
| 106 | |
---|
[fe7a554] | 107 | if( jd->tx_len ) |
---|
| 108 | g_free( jd->txq ); |
---|
| 109 | |
---|
| 110 | xt_free_node( jd->node_cache ); |
---|
[70f6aab8] | 111 | xt_free( jd->xt ); |
---|
| 112 | |
---|
[5e202b0] | 113 | g_free( jd->away_message ); |
---|
[21167d2] | 114 | g_free( jd->username ); |
---|
| 115 | g_free( jd ); |
---|
[f06894d] | 116 | } |
---|
| 117 | |
---|
| 118 | static int jabber_send_im( struct gaim_connection *gc, char *who, char *message, int len, int away ) |
---|
| 119 | { |
---|
[a214954] | 120 | struct xt_node *node, *event; |
---|
[4a0614e] | 121 | int st; |
---|
| 122 | |
---|
[a214954] | 123 | /* |
---|
| 124 | event = xt_new_node( "active", NULL, NULL ); |
---|
| 125 | xt_add_attr( event, "xlmns", "http://jabber.org/protocol/chatstates" ); |
---|
| 126 | |
---|
| 127 | event = xt_new_node( "x", NULL, xt_new_node( "composing", NULL, NULL ) ); |
---|
| 128 | xt_add_attr( event, "xmlns", "jabber:x:event" ); |
---|
| 129 | */ |
---|
| 130 | |
---|
[4a0614e] | 131 | node = xt_new_node( "body", message, NULL ); |
---|
| 132 | node = jabber_make_packet( "message", "chat", who, node ); |
---|
[a214954] | 133 | xt_add_child( node, event ); |
---|
[4a0614e] | 134 | st = jabber_write_packet( gc, node ); |
---|
| 135 | xt_free_node( node ); |
---|
| 136 | |
---|
| 137 | return st; |
---|
[f06894d] | 138 | } |
---|
| 139 | |
---|
[dd788bb] | 140 | static GList *jabber_away_states( struct gaim_connection *gc ) |
---|
| 141 | { |
---|
[5e202b0] | 142 | static GList *l = NULL; |
---|
| 143 | int i; |
---|
[dd788bb] | 144 | |
---|
[5e202b0] | 145 | if( l == NULL ) |
---|
| 146 | for( i = 0; jabber_away_state_list[i].full_name; i ++ ) |
---|
| 147 | l = g_list_append( l, (void*) jabber_away_state_list[i].full_name ); |
---|
[dd788bb] | 148 | |
---|
[5e202b0] | 149 | return l; |
---|
[dd788bb] | 150 | } |
---|
| 151 | |
---|
[5e202b0] | 152 | static void jabber_set_away( struct gaim_connection *gc, char *state_txt, char *message ) |
---|
[dd788bb] | 153 | { |
---|
[5e202b0] | 154 | struct jabber_data *jd = gc->proto_data; |
---|
| 155 | struct jabber_away_state *state; |
---|
| 156 | |
---|
| 157 | /* Save all this info. We need it, for example, when changing the priority setting. */ |
---|
| 158 | state = (void *) jabber_away_state_by_name( state_txt ); |
---|
| 159 | jd->away_state = state ? state : (void *) jabber_away_state_list; /* Fall back to "Away" if necessary. */ |
---|
| 160 | g_free( jd->away_message ); |
---|
| 161 | jd->away_message = ( message && *message ) ? g_strdup( message ) : NULL; |
---|
| 162 | |
---|
[172a73f1] | 163 | presence_send_update( gc ); |
---|
[deff040] | 164 | } |
---|
| 165 | |
---|
[cfbb3a6] | 166 | static void jabber_add_buddy( struct gaim_connection *gc, char *who ) |
---|
| 167 | { |
---|
| 168 | if( jabber_add_to_roster( gc, who, NULL ) ) |
---|
| 169 | presence_send_request( gc, who, "subscribe" ); |
---|
| 170 | } |
---|
| 171 | |
---|
| 172 | static void jabber_remove_buddy( struct gaim_connection *gc, char *who, char *group ) |
---|
| 173 | { |
---|
| 174 | if( jabber_remove_from_roster( gc, who ) ) |
---|
| 175 | presence_send_request( gc, who, "unsubscribe" ); |
---|
| 176 | } |
---|
| 177 | |
---|
[deff040] | 178 | static void jabber_keepalive( struct gaim_connection *gc ) |
---|
| 179 | { |
---|
[a214954] | 180 | struct jabber_data *jd = gc->proto_data; |
---|
[eab2ac4] | 181 | struct xt_node *c, *tmp; |
---|
[a214954] | 182 | |
---|
[deff040] | 183 | /* Just any whitespace character is enough as a keepalive for XMPP sessions. */ |
---|
| 184 | jabber_write( gc, "\n", 1 ); |
---|
[a214954] | 185 | |
---|
| 186 | /* Let's abuse this keepalive for garbage collection of the node cache too. |
---|
| 187 | It runs every minute, so let's mark every node with a special flag the |
---|
| 188 | first time we see it, and clean it up the second time (clean up all |
---|
| 189 | packets with the flag set). |
---|
| 190 | |
---|
| 191 | node->flags is normally only used by xmltree itself for parsing/handling, |
---|
| 192 | so it should be safe to use the variable for gc. */ |
---|
| 193 | |
---|
| 194 | /* This horrible loop is explained in xmltree.c. Makes me wonder if maybe I |
---|
| 195 | didn't choose the perfect data structure... */ |
---|
[eab2ac4] | 196 | for( c = jd->node_cache->children; c; c = c->next ) |
---|
| 197 | if( !( c->flags & XT_SEEN ) ) |
---|
| 198 | break; |
---|
| 199 | |
---|
| 200 | /* Now c points at the first unflagged node (or at NULL). Clean up |
---|
| 201 | everything until that point. */ |
---|
| 202 | while( jd->node_cache->children != c ) |
---|
| 203 | { |
---|
| 204 | /* |
---|
| 205 | printf( "Cleaning up:\n" ); |
---|
| 206 | xt_print( jd->node_cache->children ); |
---|
| 207 | */ |
---|
| 208 | |
---|
| 209 | tmp = jd->node_cache->children->next; |
---|
| 210 | xt_free_node( jd->node_cache->children ); |
---|
| 211 | jd->node_cache->children = tmp; |
---|
| 212 | } |
---|
| 213 | |
---|
| 214 | /* Now flag the ones that were still unflagged. */ |
---|
| 215 | for( c = jd->node_cache->children; c; c = c->next ) |
---|
[a214954] | 216 | { |
---|
[eab2ac4] | 217 | /* |
---|
| 218 | printf( "Flagged:\n" ); |
---|
| 219 | xt_print( c ); |
---|
| 220 | */ |
---|
| 221 | |
---|
| 222 | c->flags |= XT_SEEN; |
---|
[a214954] | 223 | } |
---|
[dd788bb] | 224 | } |
---|
| 225 | |
---|
[8e5e2e9] | 226 | static void jabber_add_permit( struct gaim_connection *gc, char *who ) |
---|
| 227 | { |
---|
| 228 | presence_send_request( gc, who, "subscribed" ); |
---|
| 229 | } |
---|
| 230 | |
---|
| 231 | static void jabber_rem_permit( struct gaim_connection *gc, char *who ) |
---|
| 232 | { |
---|
| 233 | presence_send_request( gc, who, "unsubscribed" ); |
---|
| 234 | } |
---|
| 235 | |
---|
| 236 | static void jabber_add_deny( struct gaim_connection *gc, char *who ) |
---|
| 237 | { |
---|
| 238 | } |
---|
| 239 | |
---|
| 240 | static void jabber_rem_deny( struct gaim_connection *gc, char *who ) |
---|
| 241 | { |
---|
| 242 | } |
---|
| 243 | |
---|
[f06894d] | 244 | void jabber_init() |
---|
| 245 | { |
---|
[deff040] | 246 | struct prpl *ret = g_new0( struct prpl, 1 ); |
---|
[f06894d] | 247 | |
---|
| 248 | ret->name = "jabber"; |
---|
| 249 | ret->login = jabber_login; |
---|
| 250 | ret->acc_init = jabber_acc_init; |
---|
| 251 | ret->close = jabber_close; |
---|
| 252 | ret->send_im = jabber_send_im; |
---|
[dd788bb] | 253 | ret->away_states = jabber_away_states; |
---|
[f06894d] | 254 | // ret->get_status_string = jabber_get_status_string; |
---|
[dd788bb] | 255 | ret->set_away = jabber_set_away; |
---|
[f06894d] | 256 | // ret->set_info = jabber_set_info; |
---|
| 257 | // ret->get_info = jabber_get_info; |
---|
[cfbb3a6] | 258 | ret->add_buddy = jabber_add_buddy; |
---|
| 259 | ret->remove_buddy = jabber_remove_buddy; |
---|
[f06894d] | 260 | // ret->chat_send = jabber_chat_send; |
---|
| 261 | // ret->chat_invite = jabber_chat_invite; |
---|
| 262 | // ret->chat_leave = jabber_chat_leave; |
---|
| 263 | // ret->chat_open = jabber_chat_open; |
---|
[deff040] | 264 | ret->keepalive = jabber_keepalive; |
---|
[8e5e2e9] | 265 | ret->add_permit = jabber_add_permit; |
---|
| 266 | ret->rem_permit = jabber_rem_permit; |
---|
| 267 | ret->add_deny = jabber_add_deny; |
---|
| 268 | ret->rem_deny = jabber_rem_deny; |
---|
[f06894d] | 269 | // ret->send_typing = jabber_send_typing; |
---|
| 270 | ret->handle_cmp = g_strcasecmp; |
---|
| 271 | |
---|
[deff040] | 272 | register_protocol( ret ); |
---|
[f06894d] | 273 | } |
---|