source: protocols/jabber/presence.c @ 6a1128d

Last change on this file since 6a1128d was 6a1128d, checked in by Wilmer van der Gaast <wilmer@…>, at 2006-10-09T18:19:05Z

The module now keeps track of all resources available for a buddy. This
means the buddy won't show up offline when one resource goes down (while
there are still others available). It also remembers away state
information for every separate resource. Later this system will be used
to keep track of client capability information (Typing notices, yay...)
and who knows what else.

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