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 | |
---|
26 | static int next_id = 1; |
---|
27 | |
---|
28 | char *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 | |
---|
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 | } |
---|
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 | |
---|
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! */ |
---|
84 | void jabber_cache_packet( struct gaim_connection *gc, struct xt_node *node ) |
---|
85 | { |
---|
86 | struct jabber_data *jd = gc->proto_data; |
---|
87 | char *id = g_strdup_printf( "BeeX%04x", next_id++ ); |
---|
88 | |
---|
89 | /* FIXME: Maybe start using g_error() here if nodes still have a parent, for example? */ |
---|
90 | |
---|
91 | xt_add_attr( node, "id", id ); |
---|
92 | xt_add_child( jd->node_cache, node ); |
---|
93 | g_free( id ); |
---|
94 | } |
---|
95 | |
---|
96 | /* Emptying this cache is a BIG TODO! */ |
---|
97 | struct xt_node *jabber_packet_from_cache( struct gaim_connection *gc, char *id ) |
---|
98 | { |
---|
99 | struct jabber_data *jd = gc->proto_data; |
---|
100 | struct xt_node *node; |
---|
101 | char *s; |
---|
102 | |
---|
103 | for( node = jd->node_cache->children; node; node = node->next ) |
---|
104 | if( ( s = xt_find_attr( node, "id" ) ) && strcmp( id, s ) == 0 ) |
---|
105 | break; |
---|
106 | |
---|
107 | return node; |
---|
108 | } |
---|
109 | |
---|
110 | const struct jabber_away_state jabber_away_state_list[] = |
---|
111 | { |
---|
112 | { "away", "Away" }, |
---|
113 | { "chat", "Free for Chat" }, |
---|
114 | { "dnd", "Do not Disturb" }, |
---|
115 | { "xa", "Extended Away" }, |
---|
116 | { "", "Online" }, |
---|
117 | { "", NULL } |
---|
118 | }; |
---|
119 | |
---|
120 | const struct jabber_away_state *jabber_away_state_by_code( char *code ) |
---|
121 | { |
---|
122 | int i; |
---|
123 | |
---|
124 | for( i = 0; jabber_away_state_list[i].full_name; i ++ ) |
---|
125 | if( g_strcasecmp( jabber_away_state_list[i].code, code ) == 0 ) |
---|
126 | return jabber_away_state_list + i; |
---|
127 | |
---|
128 | return NULL; |
---|
129 | } |
---|
130 | |
---|
131 | const struct jabber_away_state *jabber_away_state_by_name( char *name ) |
---|
132 | { |
---|
133 | int i; |
---|
134 | |
---|
135 | for( i = 0; jabber_away_state_list[i].full_name; i ++ ) |
---|
136 | if( g_strcasecmp( jabber_away_state_list[i].full_name, name ) == 0 ) |
---|
137 | return jabber_away_state_list + i; |
---|
138 | |
---|
139 | return NULL; |
---|
140 | } |
---|
141 | |
---|
142 | struct jabber_buddy_ask_data |
---|
143 | { |
---|
144 | struct gaim_connection *gc; |
---|
145 | char *handle; |
---|
146 | char *realname; |
---|
147 | }; |
---|
148 | |
---|
149 | static void jabber_buddy_ask_yes( gpointer w, struct jabber_buddy_ask_data *bla ) |
---|
150 | { |
---|
151 | presence_send_request( bla->gc, bla->handle, "subscribed" ); |
---|
152 | |
---|
153 | if( find_buddy( bla->gc, bla->handle ) == NULL ) |
---|
154 | show_got_added( bla->gc, bla->handle, NULL ); |
---|
155 | |
---|
156 | g_free( bla->handle ); |
---|
157 | g_free( bla ); |
---|
158 | } |
---|
159 | |
---|
160 | static void jabber_buddy_ask_no( gpointer w, struct jabber_buddy_ask_data *bla ) |
---|
161 | { |
---|
162 | presence_send_request( bla->gc, bla->handle, "subscribed" ); |
---|
163 | |
---|
164 | g_free( bla->handle ); |
---|
165 | g_free( bla ); |
---|
166 | } |
---|
167 | |
---|
168 | void jabber_buddy_ask( struct gaim_connection *gc, char *handle ) |
---|
169 | { |
---|
170 | struct jabber_buddy_ask_data *bla = g_new0( struct jabber_buddy_ask_data, 1 ); |
---|
171 | char *buf; |
---|
172 | |
---|
173 | bla->gc = gc; |
---|
174 | bla->handle = g_strdup( handle ); |
---|
175 | |
---|
176 | buf = g_strdup_printf( "The user %s wants to add you to his/her buddy list.", handle ); |
---|
177 | do_ask_dialog( gc, buf, bla, jabber_buddy_ask_yes, jabber_buddy_ask_no ); |
---|
178 | g_free( buf ); |
---|
179 | } |
---|