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

Last change on this file since 6bbb939 was 6bbb939, checked in by Wilmer van der Gaast <wilmer@…>, at 2007-04-16T04:01:13Z

Split serv_got_update() into imcb_buddy_(status|times). (Well, the second
one isn't implemented yet, but I'll do that later.) At last I got rid of
the hack called get_status_string(). And now Yahoo seems to mess up away
messages...

  • Property mode set to 100644
File size: 5.8 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                /* FIXME: What to send if there are other resources??? */
75                imcb_buddy_status( ic, bud->bare_jid, OPT_LOGGED_IN | is_away,
76                                   bud->away_state->full_name, bud->away_message );
77        }
78        else if( strcmp( type, "unavailable" ) == 0 )
79        {
80                if( jabber_buddy_by_jid( ic, from, GET_BUDDY_EXACT ) == NULL )
81                {
82                        if( set_getbool( &ic->irc->set, "debug" ) )
83                                imcb_log( ic, "WARNING: Received presence information from unknown JID: %s", from );
84                        return XT_HANDLED;
85                }
86               
87                jabber_buddy_remove( ic, from );
88               
89                if( ( s = strchr( from, '/' ) ) )
90                {
91                        *s = 0;
92               
93                        /* Only count this as offline if there's no other resource
94                           available anymore. */
95                        if( jabber_buddy_by_jid( ic, from, 0 ) == NULL )
96                                imcb_buddy_status( ic, from, 0, NULL, NULL );
97                       
98                        *s = '/';
99                }
100                else
101                {
102                        imcb_buddy_status( ic, from, 0, NULL, NULL );
103                }
104        }
105        else if( strcmp( type, "subscribe" ) == 0 )
106        {
107                jabber_buddy_ask( ic, from );
108        }
109        else if( strcmp( type, "subscribed" ) == 0 )
110        {
111                /* Not sure about this one, actually... */
112                imcb_log( ic, "%s just accepted your authorization request", from );
113        }
114        else if( strcmp( type, "unsubscribe" ) == 0 || strcmp( type, "unsubscribed" ) == 0 )
115        {
116                /* Do nothing here. Plenty of control freaks or over-curious
117                   souls get excited when they can see who still has them in
118                   their buddy list and who finally removed them. Somehow I
119                   got the impression that those are the people who get
120                   removed from many buddy lists for "some" reason...
121                   
122                   If you're one of those people, this is your chance to write
123                   your first line of code in C... */
124        }
125        else if( strcmp( type, "error" ) == 0 )
126        {
127                /* What to do with it? */
128        }
129        else
130        {
131                printf( "Received PRES from %s:\n", from );
132                xt_print( node );
133        }
134       
135        return XT_HANDLED;
136}
137
138/* Whenever presence information is updated, call this function to inform the
139   server. */
140int presence_send_update( struct im_connection *ic )
141{
142        struct jabber_data *jd = ic->proto_data;
143        struct xt_node *node;
144        char *show = jd->away_state->code;
145        char *status = jd->away_message;
146        int st;
147       
148        node = jabber_make_packet( "presence", NULL, NULL, NULL );
149        xt_add_child( node, xt_new_node( "priority", set_getstr( &ic->acc->set, "priority" ), NULL ) );
150        if( show && *show )
151                xt_add_child( node, xt_new_node( "show", show, NULL ) );
152        if( status )
153                xt_add_child( node, xt_new_node( "status", status, NULL ) );
154       
155        st = jabber_write_packet( ic, node );
156       
157        xt_free_node( node );
158        return st;
159}
160
161/* Send a subscribe/unsubscribe request to a buddy. */
162int presence_send_request( struct im_connection *ic, char *handle, char *request )
163{
164        struct xt_node *node;
165        int st;
166       
167        node = jabber_make_packet( "presence", NULL, NULL, NULL );
168        xt_add_attr( node, "to", handle );
169        xt_add_attr( node, "type", request );
170       
171        st = jabber_write_packet( ic, node );
172       
173        xt_free_node( node );
174        return st;
175}
Note: See TracBrowser for help on using the repository browser.