source: protocols/jabber/presence.c @ a4effbf

Last change on this file since a4effbf was 8eb10c9, checked in by Wilmer van der Gaast <wilmer@…>, at 2006-10-10T12:10:20Z

Oops... When I say ignore, I really mean ignore!

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