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