source: protocols/jabber/jabber_util.c @ 861c199

Last change on this file since 861c199 was 861c199, checked in by Wilmer van der Gaast <wilmer@…>, at 2006-10-08T18:41:11Z

Moved handling of all IQ packets to event handlers. Cleaned up a LOT of
mess in iq.c!

  • Property mode set to 100644
File size: 6.3 KB
Line 
1/***************************************************************************\
2*                                                                           *
3*  BitlBee - An IRC to IM gateway                                           *
4*  Jabber module - Misc. stuff                                              *
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
26static int next_id = 1;
27
28char *set_eval_priority( set_t *set, char *value )
29{
30        account_t *acc = set->data;
31        char *ret;
32       
33        ret = set_eval_int( set, value );
34       
35        /* Only run this stuff if the account is online ATM,
36           and if the setting seems to be acceptable. */
37        if( acc->gc && ret )
38        {
39                /* Although set_eval functions usually are very nice and
40                   convenient, they have one disadvantage: If I would just
41                   call p_s_u() now to send the new prio setting, it would
42                   send the old setting because the set->value gets changed
43                   when the eval returns a non-NULL value.
44                   
45                   So now I can choose between implementing post-set
46                   functions next to evals, or just do this little hack: */
47               
48                g_free( set->value );
49                set->value = g_strdup( ret );
50               
51                /* (Yes, sorry, I prefer the hack. :-P) */
52               
53                presence_send_update( acc->gc );
54        }
55       
56        return ret;
57}
58
59char *set_eval_tls( set_t *set, char *value )
60{
61        if( g_strcasecmp( value, "try" ) == 0 )
62                return value;
63        else
64                return set_eval_bool( set, value );
65}
66
67struct xt_node *jabber_make_packet( char *name, char *type, char *to, struct xt_node *children )
68{
69        struct xt_node *node;
70       
71        node = xt_new_node( name, NULL, children );
72       
73        if( type )
74                xt_add_attr( node, "type", type );
75        if( to )
76                xt_add_attr( node, "to", to );
77       
78        return node;
79}
80
81/* Cache a node/packet for later use. Mainly useful for IQ packets if you need
82   them when you receive the response. Use this BEFORE sending the packet so
83   it'll get an id= tag, and do NOT free() the packet after writing it! */
84void jabber_cache_add( struct gaim_connection *gc, struct xt_node *node, jabber_cache_event func )
85{
86        struct jabber_data *jd = gc->proto_data;
87        char *id = g_strdup_printf( "BeeX%04x", next_id++ );
88        struct jabber_cache_entry *entry = g_new0( struct jabber_cache_entry, 1 );
89       
90        xt_add_attr( node, "id", id );
91        g_free( id );
92       
93        entry->node = node;
94        entry->func = func;
95        g_hash_table_insert( jd->node_cache, xt_find_attr( node, "id" ), entry );
96}
97
98void jabber_cache_entry_free( gpointer data )
99{
100        struct jabber_cache_entry *entry = data;
101       
102        xt_free_node( entry->node );
103        g_free( entry );
104}
105
106gboolean jabber_cache_clean_entry( gpointer key, gpointer entry, gpointer nullpointer );
107
108/* This one should be called from time to time (from keepalive, in this case)
109   to make sure things don't stay in the node cache forever. By marking nodes
110   during the first run and deleting marked nodes during a next run, every
111   node should be available in the cache for at least a minute (assuming the
112   function is indeed called every minute). */
113void jabber_cache_clean( struct gaim_connection *gc )
114{
115        struct jabber_data *jd = gc->proto_data;
116       
117        g_hash_table_foreach_remove( jd->node_cache, jabber_cache_clean_entry, NULL );
118}
119
120gboolean jabber_cache_clean_entry( gpointer key, gpointer entry_, gpointer nullpointer )
121{
122        struct jabber_cache_entry *entry = entry_;
123        struct xt_node *node = entry->node;
124       
125        if( node->flags & XT_SEEN )
126                return TRUE;
127        else
128        {
129                node->flags |= XT_SEEN;
130                return FALSE;
131        }
132}
133
134const struct jabber_away_state jabber_away_state_list[] =
135{
136        { "away",  "Away" },
137        { "chat",  "Free for Chat" },
138        { "dnd",   "Do not Disturb" },
139        { "xa",    "Extended Away" },
140        { "",      "Online" },
141        { "",      NULL }
142};
143
144const struct jabber_away_state *jabber_away_state_by_code( char *code )
145{
146        int i;
147       
148        for( i = 0; jabber_away_state_list[i].full_name; i ++ )
149                if( g_strcasecmp( jabber_away_state_list[i].code, code ) == 0 )
150                        return jabber_away_state_list + i;
151       
152        return NULL;
153}
154
155const struct jabber_away_state *jabber_away_state_by_name( char *name )
156{
157        int i;
158       
159        for( i = 0; jabber_away_state_list[i].full_name; i ++ )
160                if( g_strcasecmp( jabber_away_state_list[i].full_name, name ) == 0 )
161                        return jabber_away_state_list + i;
162       
163        return NULL;
164}
165
166struct jabber_buddy_ask_data
167{
168        struct gaim_connection *gc;
169        char *handle;
170        char *realname;
171};
172
173static void jabber_buddy_ask_yes( gpointer w, struct jabber_buddy_ask_data *bla )
174{
175        presence_send_request( bla->gc, bla->handle, "subscribed" );
176       
177        if( find_buddy( bla->gc, bla->handle ) == NULL )
178                show_got_added( bla->gc, bla->handle, NULL );
179       
180        g_free( bla->handle );
181        g_free( bla );
182}
183
184static void jabber_buddy_ask_no( gpointer w, struct jabber_buddy_ask_data *bla )
185{
186        presence_send_request( bla->gc, bla->handle, "subscribed" );
187       
188        g_free( bla->handle );
189        g_free( bla );
190}
191
192void jabber_buddy_ask( struct gaim_connection *gc, char *handle )
193{
194        struct jabber_buddy_ask_data *bla = g_new0( struct jabber_buddy_ask_data, 1 );
195        char *buf;
196       
197        bla->gc = gc;
198        bla->handle = g_strdup( handle );
199       
200        buf = g_strdup_printf( "The user %s wants to add you to his/her buddy list.", handle );
201        do_ask_dialog( gc, buf, bla, jabber_buddy_ask_yes, jabber_buddy_ask_no );
202        g_free( buf );
203}
Note: See TracBrowser for help on using the repository browser.