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