[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; |
---|
[e7f8838] | 32 | struct jabber_buddy *bud, *send_presence = NULL; |
---|
| 33 | int is_chat = 0; |
---|
[788a1af] | 34 | char *s; |
---|
[f06894d] | 35 | |
---|
[4a0614e] | 36 | if( !from ) |
---|
| 37 | return XT_HANDLED; |
---|
| 38 | |
---|
[e35d1a1] | 39 | if( ( s = strchr( from, '/' ) ) ) |
---|
| 40 | { |
---|
| 41 | *s = 0; |
---|
[5bd21df] | 42 | if( jabber_chat_by_jid( ic, from ) ) |
---|
[e35d1a1] | 43 | is_chat = 1; |
---|
| 44 | *s = '/'; |
---|
| 45 | } |
---|
| 46 | |
---|
[4a0614e] | 47 | if( type == NULL ) |
---|
[6a1128d] | 48 | { |
---|
[0da65d5] | 49 | if( !( bud = jabber_buddy_by_jid( ic, from, GET_BUDDY_EXACT | GET_BUDDY_CREAT ) ) ) |
---|
[a21a8ac] | 50 | { |
---|
[0da65d5] | 51 | if( set_getbool( &ic->irc->set, "debug" ) ) |
---|
[43462708] | 52 | imcb_log( ic, "Warning: Could not handle presence information from JID: %s", from ); |
---|
[8eb10c9] | 53 | return XT_HANDLED; |
---|
[a21a8ac] | 54 | } |
---|
[6a1128d] | 55 | |
---|
| 56 | g_free( bud->away_message ); |
---|
| 57 | if( ( c = xt_find_node( node->children, "status" ) ) && c->text_len > 0 ) |
---|
| 58 | bud->away_message = g_strdup( c->text ); |
---|
| 59 | else |
---|
| 60 | bud->away_message = NULL; |
---|
| 61 | |
---|
| 62 | if( ( c = xt_find_node( node->children, "show" ) ) && c->text_len > 0 ) |
---|
[6bbb939] | 63 | { |
---|
[6a1128d] | 64 | bud->away_state = (void*) jabber_away_state_by_code( c->text ); |
---|
[6bbb939] | 65 | } |
---|
[6a1128d] | 66 | else |
---|
[a21a8ac] | 67 | { |
---|
[6a1128d] | 68 | bud->away_state = NULL; |
---|
[a21a8ac] | 69 | /* Let's only set last_act if there's *no* away state, |
---|
| 70 | since it could be some auto-away thingy. */ |
---|
| 71 | bud->last_act = time( NULL ); |
---|
| 72 | } |
---|
[6a1128d] | 73 | |
---|
| 74 | if( ( c = xt_find_node( node->children, "priority" ) ) && c->text_len > 0 ) |
---|
| 75 | bud->priority = atoi( c->text ); |
---|
| 76 | else |
---|
| 77 | bud->priority = 0; |
---|
| 78 | |
---|
[e35d1a1] | 79 | if( is_chat ) |
---|
| 80 | jabber_chat_pkt_presence( ic, bud, node ); |
---|
[e7f8838] | 81 | else |
---|
| 82 | send_presence = jabber_buddy_by_jid( ic, bud->bare_jid, 0 ); |
---|
[6a1128d] | 83 | } |
---|
[4a0614e] | 84 | else if( strcmp( type, "unavailable" ) == 0 ) |
---|
[6a1128d] | 85 | { |
---|
[3a80471] | 86 | if( ( bud = jabber_buddy_by_jid( ic, from, 0 ) ) == NULL ) |
---|
[788a1af] | 87 | { |
---|
[0da65d5] | 88 | if( set_getbool( &ic->irc->set, "debug" ) ) |
---|
[43462708] | 89 | imcb_log( ic, "Warning: Received presence information from unknown JID: %s", from ); |
---|
[788a1af] | 90 | return XT_HANDLED; |
---|
| 91 | } |
---|
| 92 | |
---|
[e35d1a1] | 93 | /* Handle this before we delete the JID. */ |
---|
| 94 | if( is_chat ) |
---|
| 95 | { |
---|
| 96 | jabber_chat_pkt_presence( ic, bud, node ); |
---|
| 97 | } |
---|
| 98 | |
---|
[3a80471] | 99 | if( strchr( from, '/' ) == NULL ) |
---|
| 100 | /* Sometimes servers send a type="unavailable" from a |
---|
| 101 | bare JID, which should mean that suddenly all |
---|
| 102 | resources for this JID disappeared. */ |
---|
| 103 | jabber_buddy_remove_bare( ic, from ); |
---|
| 104 | else |
---|
| 105 | jabber_buddy_remove( ic, from ); |
---|
[6a1128d] | 106 | |
---|
[e35d1a1] | 107 | if( is_chat ) |
---|
| 108 | { |
---|
| 109 | /* Nothing else to do for now? */ |
---|
| 110 | } |
---|
| 111 | else if( ( s = strchr( from, '/' ) ) ) |
---|
[0d3f30f] | 112 | { |
---|
| 113 | *s = 0; |
---|
[6a1128d] | 114 | |
---|
[2758cfe] | 115 | /* If another resource is still available, send its presence |
---|
| 116 | information. */ |
---|
[e7f8838] | 117 | if( ( send_presence = jabber_buddy_by_jid( ic, from, 0 ) ) == NULL ) |
---|
[2758cfe] | 118 | { |
---|
| 119 | /* Otherwise, count him/her as offline now. */ |
---|
[6bbb939] | 120 | imcb_buddy_status( ic, from, 0, NULL, NULL ); |
---|
[2758cfe] | 121 | } |
---|
[0d3f30f] | 122 | |
---|
| 123 | *s = '/'; |
---|
| 124 | } |
---|
| 125 | else |
---|
| 126 | { |
---|
[6bbb939] | 127 | imcb_buddy_status( ic, from, 0, NULL, NULL ); |
---|
[0d3f30f] | 128 | } |
---|
[6a1128d] | 129 | } |
---|
[8e5e2e9] | 130 | else if( strcmp( type, "subscribe" ) == 0 ) |
---|
[6a1128d] | 131 | { |
---|
[0da65d5] | 132 | jabber_buddy_ask( ic, from ); |
---|
[6a1128d] | 133 | } |
---|
[8e5e2e9] | 134 | else if( strcmp( type, "subscribed" ) == 0 ) |
---|
[6a1128d] | 135 | { |
---|
[0d3f30f] | 136 | /* Not sure about this one, actually... */ |
---|
[84b045d] | 137 | imcb_log( ic, "%s just accepted your authorization request", from ); |
---|
[6a1128d] | 138 | } |
---|
[8e5e2e9] | 139 | else if( strcmp( type, "unsubscribe" ) == 0 || strcmp( type, "unsubscribed" ) == 0 ) |
---|
| 140 | { |
---|
| 141 | /* Do nothing here. Plenty of control freaks or over-curious |
---|
| 142 | souls get excited when they can see who still has them in |
---|
| 143 | their buddy list and who finally removed them. Somehow I |
---|
| 144 | got the impression that those are the people who get |
---|
| 145 | removed from many buddy lists for "some" reason... |
---|
| 146 | |
---|
| 147 | If you're one of those people, this is your chance to write |
---|
| 148 | your first line of code in C... */ |
---|
| 149 | } |
---|
[6266fca] | 150 | else if( strcmp( type, "error" ) == 0 ) |
---|
| 151 | { |
---|
[5bd21df] | 152 | return jabber_cache_handle_packet( ic, node ); |
---|
[1baaef8] | 153 | |
---|
[5bd21df] | 154 | /* |
---|
| 155 | struct jabber_error *err; |
---|
[1baaef8] | 156 | if( ( c = xt_find_node( node->children, "error" ) ) ) |
---|
| 157 | { |
---|
| 158 | err = jabber_error_parse( c, XMLNS_STANZA_ERROR ); |
---|
| 159 | imcb_error( ic, "Stanza (%s) error: %s%s%s", node->name, |
---|
| 160 | err->code, err->text ? ": " : "", |
---|
| 161 | err->text ? err->text : "" ); |
---|
| 162 | jabber_error_free( err ); |
---|
[5bd21df] | 163 | } */ |
---|
[6266fca] | 164 | } |
---|
[e7f8838] | 165 | |
---|
| 166 | if( send_presence ) |
---|
| 167 | { |
---|
[dded27d] | 168 | int is_away = 0; |
---|
[e7f8838] | 169 | |
---|
| 170 | if( send_presence->away_state && !( *send_presence->away_state->code == 0 || |
---|
| 171 | strcmp( send_presence->away_state->code, "chat" ) == 0 ) ) |
---|
| 172 | is_away = OPT_AWAY; |
---|
| 173 | |
---|
| 174 | imcb_buddy_status( ic, send_presence->bare_jid, OPT_LOGGED_IN | is_away, |
---|
| 175 | ( is_away && send_presence->away_state ) ? |
---|
| 176 | send_presence->away_state->full_name : NULL, |
---|
| 177 | send_presence->away_message ); |
---|
| 178 | } |
---|
[4a0614e] | 179 | |
---|
[f06894d] | 180 | return XT_HANDLED; |
---|
| 181 | } |
---|
| 182 | |
---|
[172a73f1] | 183 | /* Whenever presence information is updated, call this function to inform the |
---|
| 184 | server. */ |
---|
[0da65d5] | 185 | int presence_send_update( struct im_connection *ic ) |
---|
[deff040] | 186 | { |
---|
[0da65d5] | 187 | struct jabber_data *jd = ic->proto_data; |
---|
[deff040] | 188 | struct xt_node *node; |
---|
[172a73f1] | 189 | char *show = jd->away_state->code; |
---|
| 190 | char *status = jd->away_message; |
---|
[2d317bb] | 191 | struct groupchat *c; |
---|
[deff040] | 192 | int st; |
---|
| 193 | |
---|
[172a73f1] | 194 | node = jabber_make_packet( "presence", NULL, NULL, NULL ); |
---|
[0da65d5] | 195 | xt_add_child( node, xt_new_node( "priority", set_getstr( &ic->acc->set, "priority" ), NULL ) ); |
---|
[5e202b0] | 196 | if( show && *show ) |
---|
[deff040] | 197 | xt_add_child( node, xt_new_node( "show", show, NULL ) ); |
---|
| 198 | if( status ) |
---|
| 199 | xt_add_child( node, xt_new_node( "status", status, NULL ) ); |
---|
| 200 | |
---|
[0da65d5] | 201 | st = jabber_write_packet( ic, node ); |
---|
[deff040] | 202 | |
---|
[2d317bb] | 203 | /* Have to send this update to all groupchats too, the server won't |
---|
| 204 | do this automatically. */ |
---|
| 205 | for( c = ic->groupchats; c && st; c = c->next ) |
---|
| 206 | { |
---|
| 207 | struct jabber_chat *jc = c->data; |
---|
| 208 | |
---|
[9c9b37c] | 209 | xt_add_attr( node, "to", jc->my_full_jid ); |
---|
[2d317bb] | 210 | st = jabber_write_packet( ic, node ); |
---|
| 211 | } |
---|
| 212 | |
---|
[deff040] | 213 | xt_free_node( node ); |
---|
| 214 | return st; |
---|
| 215 | } |
---|
[cfbb3a6] | 216 | |
---|
| 217 | /* Send a subscribe/unsubscribe request to a buddy. */ |
---|
[0da65d5] | 218 | int presence_send_request( struct im_connection *ic, char *handle, char *request ) |
---|
[cfbb3a6] | 219 | { |
---|
| 220 | struct xt_node *node; |
---|
| 221 | int st; |
---|
| 222 | |
---|
| 223 | node = jabber_make_packet( "presence", NULL, NULL, NULL ); |
---|
| 224 | xt_add_attr( node, "to", handle ); |
---|
| 225 | xt_add_attr( node, "type", request ); |
---|
| 226 | |
---|
[0da65d5] | 227 | st = jabber_write_packet( ic, node ); |
---|
[cfbb3a6] | 228 | |
---|
| 229 | xt_free_node( node ); |
---|
| 230 | return st; |
---|
| 231 | } |
---|