source: protocols/jabber/presence.c @ 20e830b

Last change on this file since 20e830b was 76c85b4c, checked in by Wilmer van der Gaast <wilmer@…>, at 2009-11-19T13:11:38Z

resource_select now defaults to activity instead of priority. Also, adding
a activity_timeout setting. Now, messages to someone who hasn't spoken for
a while will be sent to his/her bare JID, usually resulting in a broadcast.
This should fix issues with messages sometimes arriving on someone's
Crackberry/Android/etc instead of some place s/he's paying attention to.
Last, the activity timer is only reset on incoming messages.

  • Property mode set to 100644
File size: 8.5 KB
Line 
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
26xt_status jabber_pkt_presence( struct xt_node *node, gpointer data )
27{
28        struct im_connection *ic = 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, *cap;
32        struct jabber_buddy *bud, *send_presence = NULL;
33        int is_chat = 0;
34        char *s;
35       
36        if( !from )
37                return XT_HANDLED;
38       
39        if( ( s = strchr( from, '/' ) ) )
40        {
41                *s = 0;
42                if( jabber_chat_by_jid( ic, from ) )
43                        is_chat = 1;
44                *s = '/';
45        }
46       
47        if( type == NULL )
48        {
49                if( !( bud = jabber_buddy_by_jid( ic, from, GET_BUDDY_EXACT | GET_BUDDY_CREAT ) ) )
50                {
51                        /*
52                        imcb_log( ic, "Warning: Could not handle presence information from JID: %s", from );
53                        */
54                        return XT_HANDLED;
55                }
56               
57                g_free( bud->away_message );
58                if( ( c = xt_find_node( node->children, "status" ) ) && c->text_len > 0 )
59                        bud->away_message = g_strdup( c->text );
60                else
61                        bud->away_message = NULL;
62               
63                if( ( c = xt_find_node( node->children, "show" ) ) && c->text_len > 0 )
64                {
65                        bud->away_state = (void*) jabber_away_state_by_code( c->text );
66                }
67                else
68                {
69                        bud->away_state = NULL;
70                }
71               
72                if( ( c = xt_find_node( node->children, "priority" ) ) && c->text_len > 0 )
73                        bud->priority = atoi( c->text );
74                else
75                        bud->priority = 0;
76               
77                if( bud && ( cap = xt_find_node( node->children, "c" ) ) &&
78                    ( s = xt_find_attr( cap, "xmlns" ) ) && strcmp( s, XMLNS_CAPS ) == 0 )
79                {
80                        /* This <presence> stanza includes an XEP-0115
81                           capabilities part. Not too interesting, but we can
82                           see if it has an ext= attribute. */
83                        s = xt_find_attr( cap, "ext" );
84                        if( s && ( strstr( s, "cstates" ) || strstr( s, "chatstate" ) ) )
85                                bud->flags |= JBFLAG_DOES_XEP85;
86                       
87                        /* This field can contain more information like xhtml
88                           support, but we don't support that ourselves.
89                           Officially the ext= tag was deprecated, but enough
90                           clients do send it.
91                           
92                           (I'm aware that this is not the right way to use
93                           this field.) See for an explanation of ext=:
94                           http://www.xmpp.org/extensions/attic/xep-0115-1.3.html*/
95                }
96               
97                if( is_chat )
98                        jabber_chat_pkt_presence( ic, bud, node );
99                else
100                        send_presence = jabber_buddy_by_jid( ic, bud->bare_jid, 0 );
101        }
102        else if( strcmp( type, "unavailable" ) == 0 )
103        {
104                if( ( bud = jabber_buddy_by_jid( ic, from, 0 ) ) == NULL )
105                {
106                        /*
107                        imcb_log( ic, "Warning: Received presence information from unknown JID: %s", from );
108                        */
109                        return XT_HANDLED;
110                }
111               
112                /* Handle this before we delete the JID. */
113                if( is_chat )
114                {
115                        jabber_chat_pkt_presence( ic, bud, node );
116                }
117               
118                if( strchr( from, '/' ) == NULL )
119                        /* Sometimes servers send a type="unavailable" from a
120                           bare JID, which should mean that suddenly all
121                           resources for this JID disappeared. */
122                        jabber_buddy_remove_bare( ic, from );
123                else
124                        jabber_buddy_remove( ic, from );
125               
126                if( is_chat )
127                {
128                        /* Nothing else to do for now? */
129                }
130                else if( ( s = strchr( from, '/' ) ) )
131                {
132                        *s = 0;
133               
134                        /* If another resource is still available, send its presence
135                           information. */
136                        if( ( send_presence = jabber_buddy_by_jid( ic, from, 0 ) ) == NULL )
137                        {
138                                /* Otherwise, count him/her as offline now. */
139                                imcb_buddy_status( ic, from, 0, NULL, NULL );
140                        }
141                       
142                        *s = '/';
143                }
144                else
145                {
146                        imcb_buddy_status( ic, from, 0, NULL, NULL );
147                }
148        }
149        else if( strcmp( type, "subscribe" ) == 0 )
150        {
151                jabber_buddy_ask( ic, from );
152        }
153        else if( strcmp( type, "subscribed" ) == 0 )
154        {
155                /* Not sure about this one, actually... */
156                imcb_log( ic, "%s just accepted your authorization request", from );
157        }
158        else if( strcmp( type, "unsubscribe" ) == 0 || strcmp( type, "unsubscribed" ) == 0 )
159        {
160                /* Do nothing here. Plenty of control freaks or over-curious
161                   souls get excited when they can see who still has them in
162                   their buddy list and who finally removed them. Somehow I
163                   got the impression that those are the people who get
164                   removed from many buddy lists for "some" reason...
165                   
166                   If you're one of those people, this is your chance to write
167                   your first line of code in C... */
168        }
169        else if( strcmp( type, "error" ) == 0 )
170        {
171                return jabber_cache_handle_packet( ic, node );
172               
173                /*
174                struct jabber_error *err;
175                if( ( c = xt_find_node( node->children, "error" ) ) )
176                {
177                        err = jabber_error_parse( c, XMLNS_STANZA_ERROR );
178                        imcb_error( ic, "Stanza (%s) error: %s%s%s", node->name,
179                                    err->code, err->text ? ": " : "",
180                                    err->text ? err->text : "" );
181                        jabber_error_free( err );
182                } */
183        }
184
185        if( send_presence )
186        {
187                int is_away = 0;
188
189                if( send_presence->away_state && !( *send_presence->away_state->code == 0 ||
190                    strcmp( send_presence->away_state->code, "chat" ) == 0 ) )
191                        is_away = OPT_AWAY;
192
193                imcb_buddy_status( ic, send_presence->bare_jid, OPT_LOGGED_IN | is_away,
194                                   ( is_away && send_presence->away_state ) ?
195                                   send_presence->away_state->full_name : NULL,
196                                   send_presence->away_message );
197        }
198       
199        return XT_HANDLED;
200}
201
202/* Whenever presence information is updated, call this function to inform the
203   server. */
204int presence_send_update( struct im_connection *ic )
205{
206        struct jabber_data *jd = ic->proto_data;
207        struct xt_node *node, *cap;
208        char *show = jd->away_state->code;
209        char *status = jd->away_message;
210        struct groupchat *c;
211        int st;
212       
213        node = jabber_make_packet( "presence", NULL, NULL, NULL );
214        xt_add_child( node, xt_new_node( "priority", set_getstr( &ic->acc->set, "priority" ), NULL ) );
215        if( show && *show )
216                xt_add_child( node, xt_new_node( "show", show, NULL ) );
217        if( status )
218                xt_add_child( node, xt_new_node( "status", status, NULL ) );
219       
220        /* This makes the packet slightly bigger, but clients interested in
221           capabilities can now cache the discovery info. This reduces the
222           usual post-login iq-flood. See XEP-0115. At least libpurple and
223           Trillian seem to do this right. */
224        cap = xt_new_node( "c", NULL, NULL );
225        xt_add_attr( cap, "xmlns", XMLNS_CAPS );
226        xt_add_attr( cap, "node", "http://bitlbee.org/xmpp/caps" );
227        xt_add_attr( cap, "ver", BITLBEE_VERSION ); /* The XEP wants this hashed, but nobody's doing that. */
228        xt_add_child( node, cap );
229       
230        st = jabber_write_packet( ic, node );
231       
232        /* Have to send this update to all groupchats too, the server won't
233           do this automatically. */
234        for( c = ic->groupchats; c && st; c = c->next )
235        {
236                struct jabber_chat *jc = c->data;
237               
238                xt_add_attr( node, "to", jc->my_full_jid );
239                st = jabber_write_packet( ic, node );
240        }
241       
242        xt_free_node( node );
243        return st;
244}
245
246/* Send a subscribe/unsubscribe request to a buddy. */
247int presence_send_request( struct im_connection *ic, char *handle, char *request )
248{
249        struct xt_node *node;
250        int st;
251       
252        node = jabber_make_packet( "presence", NULL, NULL, NULL );
253        xt_add_attr( node, "to", handle );
254        xt_add_attr( node, "type", request );
255       
256        st = jabber_write_packet( ic, node );
257       
258        xt_free_node( node );
259        return st;
260}
Note: See TracBrowser for help on using the repository browser.