source: protocols/jabber/presence.c @ a21a8ac

Last change on this file since a21a8ac was a21a8ac, checked in by Wilmer van der Gaast <wilmer@…>, at 2006-10-10T12:05:42Z

Added resource selection (based on priority or time of last message) to
budd_by_jid(), added a full_jid property to easily address that resource
without having to rebuild the full JID every time and implemented typing
notification shite.

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