1 | /***************************************************************************\ |
---|
2 | * * |
---|
3 | * BitlBee - An IRC to IM gateway * |
---|
4 | * Jabber module - Handling of presence (tags), etc * |
---|
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 | { |
---|
28 | struct gaim_connection *gc = data; |
---|
29 | char *from = xt_find_attr( node, "from" ); |
---|
30 | char *type = xt_find_attr( node, "type" ); /* NULL should mean the person is online. */ |
---|
31 | struct xt_node *c; |
---|
32 | struct jabber_buddy *bud; |
---|
33 | char *s; |
---|
34 | |
---|
35 | if( !from ) |
---|
36 | return XT_HANDLED; |
---|
37 | |
---|
38 | if( type == NULL ) |
---|
39 | { |
---|
40 | if( !( bud = jabber_buddy_by_jid( gc, from, GET_BUDDY_EXACT | GET_BUDDY_CREAT ) ) ) |
---|
41 | { |
---|
42 | if( set_getbool( &gc->irc->set, "debug" ) ) |
---|
43 | serv_got_crap( gc, "WARNING: Could not handle presence information from JID: %s", from ); |
---|
44 | return XT_HANDLED; |
---|
45 | } |
---|
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 |
---|
56 | { |
---|
57 | bud->away_state = NULL; |
---|
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 | } |
---|
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 | |
---|
68 | serv_got_update( gc, bud->bare_jid, 1, 0, 0, 0, |
---|
69 | bud->away_state ? UC_UNAVAILABLE : 0, 0 ); |
---|
70 | } |
---|
71 | else if( strcmp( type, "unavailable" ) == 0 ) |
---|
72 | { |
---|
73 | if( jabber_buddy_by_jid( gc, from, GET_BUDDY_EXACT ) == NULL ) |
---|
74 | { |
---|
75 | if( set_getbool( &gc->irc->set, "debug" ) ) |
---|
76 | serv_got_crap( gc, "WARNING: Received presence information from unknown JID: %s", from ); |
---|
77 | return XT_HANDLED; |
---|
78 | } |
---|
79 | |
---|
80 | jabber_buddy_remove( gc, from ); |
---|
81 | |
---|
82 | if( ( s = strchr( from, '/' ) ) ) |
---|
83 | { |
---|
84 | *s = 0; |
---|
85 | |
---|
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 | } |
---|
97 | } |
---|
98 | else if( strcmp( type, "subscribe" ) == 0 ) |
---|
99 | { |
---|
100 | jabber_buddy_ask( gc, from ); |
---|
101 | } |
---|
102 | else if( strcmp( type, "subscribed" ) == 0 ) |
---|
103 | { |
---|
104 | /* Not sure about this one, actually... */ |
---|
105 | serv_got_crap( gc, "%s just accepted your authorization request", from ); |
---|
106 | } |
---|
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 | } |
---|
118 | else if( strcmp( type, "error" ) == 0 ) |
---|
119 | { |
---|
120 | /* What to do with it? */ |
---|
121 | } |
---|
122 | else |
---|
123 | { |
---|
124 | printf( "Received PRES from %s:\n", from ); |
---|
125 | xt_print( node ); |
---|
126 | } |
---|
127 | |
---|
128 | return XT_HANDLED; |
---|
129 | } |
---|
130 | |
---|
131 | /* Whenever presence information is updated, call this function to inform the |
---|
132 | server. */ |
---|
133 | int presence_send_update( struct gaim_connection *gc ) |
---|
134 | { |
---|
135 | struct jabber_data *jd = gc->proto_data; |
---|
136 | struct xt_node *node; |
---|
137 | char *show = jd->away_state->code; |
---|
138 | char *status = jd->away_message; |
---|
139 | int st; |
---|
140 | |
---|
141 | node = jabber_make_packet( "presence", NULL, NULL, NULL ); |
---|
142 | xt_add_child( node, xt_new_node( "priority", set_getstr( &gc->acc->set, "priority" ), NULL ) ); |
---|
143 | if( show && *show ) |
---|
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 | } |
---|
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 | } |
---|