[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] | 26 | static int next_id = 1; |
---|
| 27 | |
---|
[ebe7b36] | 28 | char *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 | |
---|
| 59 | char *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 | |
---|
| 67 | struct 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! */ |
---|
[861c199] | 84 | void jabber_cache_add( struct gaim_connection *gc, struct xt_node *node, jabber_cache_event func ) |
---|
[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; |
---|
[861c199] | 94 | entry->func = func; |
---|
[038d17f] | 95 | g_hash_table_insert( jd->node_cache, xt_find_attr( node, "id" ), entry ); |
---|
[fe7a554] | 96 | } |
---|
| 97 | |
---|
[038d17f] | 98 | void jabber_cache_entry_free( gpointer data ) |
---|
| 99 | { |
---|
| 100 | struct jabber_cache_entry *entry = data; |
---|
[21167d2] | 101 | |
---|
[038d17f] | 102 | xt_free_node( entry->node ); |
---|
| 103 | g_free( entry ); |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | gboolean jabber_cache_clean_entry( gpointer key, gpointer entry, gpointer nullpointer ); |
---|
| 107 | |
---|
[861c199] | 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). */ |
---|
[038d17f] | 113 | void 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 | |
---|
| 120 | gboolean 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 | } |
---|
[21167d2] | 132 | } |
---|
[5e202b0] | 133 | |
---|
| 134 | const 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 | |
---|
| 144 | const 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 | |
---|
| 155 | const 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 | } |
---|
[8e5e2e9] | 165 | |
---|
| 166 | struct jabber_buddy_ask_data |
---|
| 167 | { |
---|
| 168 | struct gaim_connection *gc; |
---|
| 169 | char *handle; |
---|
| 170 | char *realname; |
---|
| 171 | }; |
---|
| 172 | |
---|
| 173 | static 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 | |
---|
| 184 | static 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 | |
---|
| 192 | void 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 ); |
---|
[6266fca] | 202 | g_free( buf ); |
---|
[8e5e2e9] | 203 | } |
---|