source: protocols/jabber/presence.c @ 9bcbe48

Last change on this file since 9bcbe48 was 0d3f30f, checked in by Wilmer van der Gaast <wilmer@…>, at 2006-11-12T23:06:08Z

Improved handling of JIDs: Bare JIDs are allowed (*sigh*) and case
insensitivity. Probably not complete yet...

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