source: protocols/jabber/jabber_util.c @ 038d17f

Last change on this file since 038d17f was 038d17f, checked in by Wilmer van der Gaast <wilmer@…>, at 2006-10-08T16:11:16Z

Implemented a better node cache using a GLib hash, and preparing to add
event handlers that can be set when sending a packet to handle the reply
to this specific packet. This should allow me to make the iq handler a
lot cleaner.

  • Property mode set to 100644
File size: 6.2 KB
RevLine 
[f06894d]1/***************************************************************************\
2*                                                                           *
3*  BitlBee - An IRC to IM gateway                                           *
[21167d2]4*  Jabber module - Misc. stuff                                              *
[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
[21167d2]26static int next_id = 1;
27
[ebe7b36]28char *set_eval_priority( set_t *set, char *value )
[f06894d]29{
30        account_t *acc = set->data;
[172a73f1]31        char *ret;
[f06894d]32       
[ebe7b36]33        ret = set_eval_int( set, value );
[172a73f1]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 )
[f06894d]38        {
[ebe7b36]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 );
[f06894d]54        }
55       
[172a73f1]56        return ret;
[f06894d]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}
[21167d2]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       
[fe7a554]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! */
[038d17f]84void jabber_cache_add( struct gaim_connection *gc, struct xt_node *node )
[fe7a554]85{
86        struct jabber_data *jd = gc->proto_data;
87        char *id = g_strdup_printf( "BeeX%04x", next_id++ );
[038d17f]88        struct jabber_cache_entry *entry = g_new0( struct jabber_cache_entry, 1 );
[fe7a554]89       
90        xt_add_attr( node, "id", id );
[21167d2]91        g_free( id );
[038d17f]92       
93        entry->node = node;
94        g_hash_table_insert( jd->node_cache, xt_find_attr( node, "id" ), entry );
[fe7a554]95}
96
[038d17f]97struct xt_node *jabber_cache_get( struct gaim_connection *gc, char *id )
[fe7a554]98{
99        struct jabber_data *jd = gc->proto_data;
[038d17f]100        struct jabber_cache_entry *entry = g_hash_table_lookup( jd->node_cache, id );
[fe7a554]101       
[038d17f]102        return entry ? entry->node : NULL;
103}
104
105void jabber_cache_entry_free( gpointer data )
106{
107        struct jabber_cache_entry *entry = data;
[21167d2]108       
[038d17f]109        xt_free_node( entry->node );
110        g_free( entry );
111}
112
113gboolean jabber_cache_clean_entry( gpointer key, gpointer entry, gpointer nullpointer );
114
115void jabber_cache_clean( struct gaim_connection *gc )
116{
117        struct jabber_data *jd = gc->proto_data;
118       
119        g_hash_table_foreach_remove( jd->node_cache, jabber_cache_clean_entry, NULL );
120}
121
122gboolean jabber_cache_clean_entry( gpointer key, gpointer entry_, gpointer nullpointer )
123{
124        struct jabber_cache_entry *entry = entry_;
125        struct xt_node *node = entry->node;
126       
127        if( node->flags & XT_SEEN )
128                return TRUE;
129        else
130        {
131                node->flags |= XT_SEEN;
132                return FALSE;
133        }
[21167d2]134}
[5e202b0]135
136const struct jabber_away_state jabber_away_state_list[] =
137{
138        { "away",  "Away" },
139        { "chat",  "Free for Chat" },
140        { "dnd",   "Do not Disturb" },
141        { "xa",    "Extended Away" },
142        { "",      "Online" },
143        { "",      NULL }
144};
145
146const struct jabber_away_state *jabber_away_state_by_code( char *code )
147{
148        int i;
149       
150        for( i = 0; jabber_away_state_list[i].full_name; i ++ )
151                if( g_strcasecmp( jabber_away_state_list[i].code, code ) == 0 )
152                        return jabber_away_state_list + i;
153       
154        return NULL;
155}
156
157const struct jabber_away_state *jabber_away_state_by_name( char *name )
158{
159        int i;
160       
161        for( i = 0; jabber_away_state_list[i].full_name; i ++ )
162                if( g_strcasecmp( jabber_away_state_list[i].full_name, name ) == 0 )
163                        return jabber_away_state_list + i;
164       
165        return NULL;
166}
[8e5e2e9]167
168struct jabber_buddy_ask_data
169{
170        struct gaim_connection *gc;
171        char *handle;
172        char *realname;
173};
174
175static void jabber_buddy_ask_yes( gpointer w, struct jabber_buddy_ask_data *bla )
176{
177        presence_send_request( bla->gc, bla->handle, "subscribed" );
178       
179        if( find_buddy( bla->gc, bla->handle ) == NULL )
180                show_got_added( bla->gc, bla->handle, NULL );
181       
182        g_free( bla->handle );
183        g_free( bla );
184}
185
186static void jabber_buddy_ask_no( gpointer w, struct jabber_buddy_ask_data *bla )
187{
188        presence_send_request( bla->gc, bla->handle, "subscribed" );
189       
190        g_free( bla->handle );
191        g_free( bla );
192}
193
194void jabber_buddy_ask( struct gaim_connection *gc, char *handle )
195{
196        struct jabber_buddy_ask_data *bla = g_new0( struct jabber_buddy_ask_data, 1 );
197        char *buf;
198       
199        bla->gc = gc;
200        bla->handle = g_strdup( handle );
201       
202        buf = g_strdup_printf( "The user %s wants to add you to his/her buddy list.", handle );
203        do_ask_dialog( gc, buf, bla, jabber_buddy_ask_yes, jabber_buddy_ask_no );
[6266fca]204        g_free( buf );
[8e5e2e9]205}
Note: See TracBrowser for help on using the repository browser.