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