[f06894d] | 1 | /***************************************************************************\ |
---|
| 2 | * * |
---|
| 3 | * BitlBee - An IRC to IM gateway * |
---|
[21167d2] | 4 | * Jabber module - Handling of presence (tags), etc * |
---|
[f06894d] | 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 "jabber.h" |
---|
| 25 | |
---|
| 26 | xt_status jabber_pkt_presence( struct xt_node *node, gpointer data ) |
---|
| 27 | { |
---|
[4a0614e] | 28 | struct gaim_connection *gc = data; |
---|
[f06894d] | 29 | char *from = xt_find_attr( node, "from" ); |
---|
[4a0614e] | 30 | char *type = xt_find_attr( node, "type" ); /* NULL should mean the person is online. */ |
---|
[6a1128d] | 31 | struct xt_node *c; |
---|
[a21a8ac] | 32 | struct jabber_buddy *bud; |
---|
[788a1af] | 33 | char *s; |
---|
[f06894d] | 34 | |
---|
[4a0614e] | 35 | if( !from ) |
---|
| 36 | return XT_HANDLED; |
---|
| 37 | |
---|
| 38 | if( type == NULL ) |
---|
[6a1128d] | 39 | { |
---|
[0d3f30f] | 40 | if( !( bud = jabber_buddy_by_jid( gc, from, GET_BUDDY_EXACT | GET_BUDDY_CREAT ) ) ) |
---|
[a21a8ac] | 41 | { |
---|
[21782b3] | 42 | if( set_getbool( &gc->irc->set, "debug" ) ) |
---|
| 43 | serv_got_crap( gc, "WARNING: Could not handle presence information from JID: %s", from ); |
---|
[8eb10c9] | 44 | return XT_HANDLED; |
---|
[a21a8ac] | 45 | } |
---|
[6a1128d] | 46 | |
---|
| 47 | g_free( bud->away_message ); |
---|
| 48 | if( ( c = xt_find_node( node->children, "status" ) ) && c->text_len > 0 ) |
---|
| 49 | bud->away_message = g_strdup( c->text ); |
---|
| 50 | else |
---|
| 51 | bud->away_message = NULL; |
---|
| 52 | |
---|
| 53 | if( ( c = xt_find_node( node->children, "show" ) ) && c->text_len > 0 ) |
---|
| 54 | bud->away_state = (void*) jabber_away_state_by_code( c->text ); |
---|
| 55 | else |
---|
[a21a8ac] | 56 | { |
---|
[6a1128d] | 57 | bud->away_state = NULL; |
---|
[a21a8ac] | 58 | /* Let's only set last_act if there's *no* away state, |
---|
| 59 | since it could be some auto-away thingy. */ |
---|
| 60 | bud->last_act = time( NULL ); |
---|
| 61 | } |
---|
[6a1128d] | 62 | |
---|
| 63 | if( ( c = xt_find_node( node->children, "priority" ) ) && c->text_len > 0 ) |
---|
| 64 | bud->priority = atoi( c->text ); |
---|
| 65 | else |
---|
| 66 | bud->priority = 0; |
---|
| 67 | |
---|
[0d3f30f] | 68 | serv_got_update( gc, bud->bare_jid, 1, 0, 0, 0, |
---|
[a21a8ac] | 69 | bud->away_state ? UC_UNAVAILABLE : 0, 0 ); |
---|
[6a1128d] | 70 | } |
---|
[4a0614e] | 71 | else if( strcmp( type, "unavailable" ) == 0 ) |
---|
[6a1128d] | 72 | { |
---|
[0d3f30f] | 73 | if( jabber_buddy_by_jid( gc, from, GET_BUDDY_EXACT ) == NULL ) |
---|
[788a1af] | 74 | { |
---|
[21782b3] | 75 | if( set_getbool( &gc->irc->set, "debug" ) ) |
---|
| 76 | serv_got_crap( gc, "WARNING: Received presence information from unknown JID: %s", from ); |
---|
[788a1af] | 77 | return XT_HANDLED; |
---|
| 78 | } |
---|
| 79 | |
---|
[a21a8ac] | 80 | jabber_buddy_remove( gc, from ); |
---|
[6a1128d] | 81 | |
---|
[0d3f30f] | 82 | if( ( s = strchr( from, '/' ) ) ) |
---|
| 83 | { |
---|
| 84 | *s = 0; |
---|
[6a1128d] | 85 | |
---|
[0d3f30f] | 86 | /* Only count this as offline if there's no other resource |
---|
| 87 | available anymore. */ |
---|
| 88 | if( jabber_buddy_by_jid( gc, from, 0 ) == NULL ) |
---|
| 89 | serv_got_update( gc, from, 0, 0, 0, 0, 0, 0 ); |
---|
| 90 | |
---|
| 91 | *s = '/'; |
---|
| 92 | } |
---|
| 93 | else |
---|
| 94 | { |
---|
| 95 | serv_got_update( gc, from, 0, 0, 0, 0, 0, 0 ); |
---|
| 96 | } |
---|
[6a1128d] | 97 | } |
---|
[8e5e2e9] | 98 | else if( strcmp( type, "subscribe" ) == 0 ) |
---|
[6a1128d] | 99 | { |
---|
[8e5e2e9] | 100 | jabber_buddy_ask( gc, from ); |
---|
[6a1128d] | 101 | } |
---|
[8e5e2e9] | 102 | else if( strcmp( type, "subscribed" ) == 0 ) |
---|
[6a1128d] | 103 | { |
---|
[0d3f30f] | 104 | /* Not sure about this one, actually... */ |
---|
[8e5e2e9] | 105 | serv_got_crap( gc, "%s just accepted your authorization request", from ); |
---|
[6a1128d] | 106 | } |
---|
[8e5e2e9] | 107 | else if( strcmp( type, "unsubscribe" ) == 0 || strcmp( type, "unsubscribed" ) == 0 ) |
---|
| 108 | { |
---|
| 109 | /* Do nothing here. Plenty of control freaks or over-curious |
---|
| 110 | souls get excited when they can see who still has them in |
---|
| 111 | their buddy list and who finally removed them. Somehow I |
---|
| 112 | got the impression that those are the people who get |
---|
| 113 | removed from many buddy lists for "some" reason... |
---|
| 114 | |
---|
| 115 | If you're one of those people, this is your chance to write |
---|
| 116 | your first line of code in C... */ |
---|
| 117 | } |
---|
[6266fca] | 118 | else if( strcmp( type, "error" ) == 0 ) |
---|
| 119 | { |
---|
| 120 | /* What to do with it? */ |
---|
| 121 | } |
---|
[4a0614e] | 122 | else |
---|
| 123 | { |
---|
| 124 | printf( "Received PRES from %s:\n", from ); |
---|
| 125 | xt_print( node ); |
---|
| 126 | } |
---|
| 127 | |
---|
[f06894d] | 128 | return XT_HANDLED; |
---|
| 129 | } |
---|
| 130 | |
---|
[172a73f1] | 131 | /* Whenever presence information is updated, call this function to inform the |
---|
| 132 | server. */ |
---|
| 133 | int presence_send_update( struct gaim_connection *gc ) |
---|
[deff040] | 134 | { |
---|
[172a73f1] | 135 | struct jabber_data *jd = gc->proto_data; |
---|
[deff040] | 136 | struct xt_node *node; |
---|
[172a73f1] | 137 | char *show = jd->away_state->code; |
---|
| 138 | char *status = jd->away_message; |
---|
[deff040] | 139 | int st; |
---|
| 140 | |
---|
[172a73f1] | 141 | node = jabber_make_packet( "presence", NULL, NULL, NULL ); |
---|
[36e9f62] | 142 | xt_add_child( node, xt_new_node( "priority", set_getstr( &gc->acc->set, "priority" ), NULL ) ); |
---|
[5e202b0] | 143 | if( show && *show ) |
---|
[deff040] | 144 | xt_add_child( node, xt_new_node( "show", show, NULL ) ); |
---|
| 145 | if( status ) |
---|
| 146 | xt_add_child( node, xt_new_node( "status", status, NULL ) ); |
---|
| 147 | |
---|
| 148 | st = jabber_write_packet( gc, node ); |
---|
| 149 | |
---|
| 150 | xt_free_node( node ); |
---|
| 151 | return st; |
---|
| 152 | } |
---|
[cfbb3a6] | 153 | |
---|
| 154 | /* Send a subscribe/unsubscribe request to a buddy. */ |
---|
| 155 | int presence_send_request( struct gaim_connection *gc, char *handle, char *request ) |
---|
| 156 | { |
---|
| 157 | struct xt_node *node; |
---|
| 158 | int st; |
---|
| 159 | |
---|
| 160 | node = jabber_make_packet( "presence", NULL, NULL, NULL ); |
---|
| 161 | xt_add_attr( node, "to", handle ); |
---|
| 162 | xt_add_attr( node, "type", request ); |
---|
| 163 | |
---|
| 164 | st = jabber_write_packet( gc, node ); |
---|
| 165 | |
---|
| 166 | xt_free_node( node ); |
---|
| 167 | return st; |
---|
| 168 | } |
---|