source: protocols/jabber/presence.c @ df1fb67

Last change on this file since df1fb67 was 17fa798, checked in by Wilmer van der Gaast <wilmer@…>, at 2007-04-21T05:02:05Z

Jabber module should always send the status information of the primary
resource now, instead of just whatever came in last.

  • Property mode set to 100644
File size: 5.9 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;
32        struct jabber_buddy *bud;
33        char *s;
34       
35        if( !from )
36                return XT_HANDLED;
37       
38        if( type == NULL )
39        {
40                int is_away = 0;
41               
42                if( !( bud = jabber_buddy_by_jid( ic, from, GET_BUDDY_EXACT | GET_BUDDY_CREAT ) ) )
43                {
44                        if( set_getbool( &ic->irc->set, "debug" ) )
45                                imcb_log( ic, "WARNING: Could not handle presence information from JID: %s", from );
46                        return XT_HANDLED;
47                }
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 )
56                {
57                        bud->away_state = (void*) jabber_away_state_by_code( c->text );
58                        if( strcmp( c->text, "chat" ) != 0 )
59                                is_away = OPT_AWAY;
60                }
61                else
62                {
63                        bud->away_state = NULL;
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                }
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               
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 );
78        }
79        else if( strcmp( type, "unavailable" ) == 0 )
80        {
81                if( jabber_buddy_by_jid( ic, from, GET_BUDDY_EXACT ) == NULL )
82                {
83                        if( set_getbool( &ic->irc->set, "debug" ) )
84                                imcb_log( ic, "WARNING: Received presence information from unknown JID: %s", from );
85                        return XT_HANDLED;
86                }
87               
88                jabber_buddy_remove( ic, from );
89               
90                if( ( s = strchr( from, '/' ) ) )
91                {
92                        *s = 0;
93               
94                        /* Only count this as offline if there's no other resource
95                           available anymore. */
96                        if( jabber_buddy_by_jid( ic, from, 0 ) == NULL )
97                                imcb_buddy_status( ic, from, 0, NULL, NULL );
98                       
99                        *s = '/';
100                }
101                else
102                {
103                        imcb_buddy_status( ic, from, 0, NULL, NULL );
104                }
105        }
106        else if( strcmp( type, "subscribe" ) == 0 )
107        {
108                jabber_buddy_ask( ic, from );
109        }
110        else if( strcmp( type, "subscribed" ) == 0 )
111        {
112                /* Not sure about this one, actually... */
113                imcb_log( ic, "%s just accepted your authorization request", from );
114        }
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        }
126        else if( strcmp( type, "error" ) == 0 )
127        {
128                /* What to do with it? */
129        }
130        else
131        {
132                printf( "Received PRES from %s:\n", from );
133                xt_print( node );
134        }
135       
136        return XT_HANDLED;
137}
138
139/* Whenever presence information is updated, call this function to inform the
140   server. */
141int presence_send_update( struct im_connection *ic )
142{
143        struct jabber_data *jd = ic->proto_data;
144        struct xt_node *node;
145        char *show = jd->away_state->code;
146        char *status = jd->away_message;
147        int st;
148       
149        node = jabber_make_packet( "presence", NULL, NULL, NULL );
150        xt_add_child( node, xt_new_node( "priority", set_getstr( &ic->acc->set, "priority" ), NULL ) );
151        if( show && *show )
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       
156        st = jabber_write_packet( ic, node );
157       
158        xt_free_node( node );
159        return st;
160}
161
162/* Send a subscribe/unsubscribe request to a buddy. */
163int presence_send_request( struct im_connection *ic, char *handle, char *request )
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       
172        st = jabber_write_packet( ic, node );
173       
174        xt_free_node( node );
175        return st;
176}
Note: See TracBrowser for help on using the repository browser.