Sun, 12 Aug 2007 02:11:05 +0000
Use -1 as length with g_convert() functions instead of strlen()
| 7923 | 1 | /* |
| 15884 | 2 | * purple - Jabber Protocol Plugin |
| 7923 | 3 | * |
| 4 | * Copyright (C) 2003, Nathan Walp <faceprint@faceprint.com> | |
| 5 | * | |
| 6 | * This program is free software; you can redistribute it and/or modify | |
| 7 | * it under the terms of the GNU General Public License as published by | |
| 8 | * the Free Software Foundation; either version 2 of the License, or | |
| 9 | * (at your option) any later version. | |
| 10 | * | |
| 11 | * This program is distributed in the hope that it will be useful, | |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 14 | * GNU General Public License for more details. | |
| 15 | * | |
| 16 | * You should have received a copy of the GNU General Public License | |
| 17 | * along with this program; if not, write to the Free Software | |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
| 19 | * | |
| 20 | */ | |
| 21 | #include "internal.h" | |
| 22 | #include "request.h" | |
| 23 | #include "server.h" | |
| 24 | ||
| 25 | #include "xdata.h" | |
| 26 | ||
| 27 | typedef enum { | |
| 28 | JABBER_X_DATA_IGNORE = 0, | |
| 29 | JABBER_X_DATA_TEXT_SINGLE, | |
| 30 | JABBER_X_DATA_TEXT_MULTI, | |
| 31 | JABBER_X_DATA_LIST_SINGLE, | |
| 32 | JABBER_X_DATA_LIST_MULTI, | |
| 8295 | 33 | JABBER_X_DATA_BOOLEAN, |
| 34 | JABBER_X_DATA_JID_SINGLE | |
| 7923 | 35 | } jabber_x_data_field_type; |
| 36 | ||
| 37 | struct jabber_x_data_data { | |
| 38 | GHashTable *fields; | |
| 39 | GSList *values; | |
| 40 | jabber_x_data_cb cb; | |
| 41 | gpointer user_data; | |
| 42 | JabberStream *js; | |
| 43 | }; | |
| 44 | ||
| 15884 | 45 | static void jabber_x_data_ok_cb(struct jabber_x_data_data *data, PurpleRequestFields *fields) { |
| 7923 | 46 | xmlnode *result = xmlnode_new("x"); |
| 47 | jabber_x_data_cb cb = data->cb; | |
| 48 | gpointer user_data = data->user_data; | |
| 49 | JabberStream *js = data->js; | |
| 50 | GList *groups, *flds; | |
| 51 | ||
| 13808 | 52 | xmlnode_set_namespace(result, "jabber:x:data"); |
| 7923 | 53 | xmlnode_set_attrib(result, "type", "submit"); |
| 54 | ||
| 15884 | 55 | for(groups = purple_request_fields_get_groups(fields); groups; groups = groups->next) { |
| 56 | for(flds = purple_request_field_group_get_fields(groups->data); flds; flds = flds->next) { | |
| 7923 | 57 | xmlnode *fieldnode, *valuenode; |
| 15884 | 58 | PurpleRequestField *field = flds->data; |
| 59 | const char *id = purple_request_field_get_id(field); | |
| 7923 | 60 | jabber_x_data_field_type type = GPOINTER_TO_INT(g_hash_table_lookup(data->fields, id)); |
| 61 | ||
| 62 | switch(type) { | |
| 63 | case JABBER_X_DATA_TEXT_SINGLE: | |
| 8295 | 64 | case JABBER_X_DATA_JID_SINGLE: |
| 7923 | 65 | { |
| 15884 | 66 | const char *value = purple_request_field_string_get_value(field); |
|
16398
a895aca0a7ce
Ok I couldn't resist. For iChat and Adium buddies who are advertising
Mark Doliner <markdoliner@pidgin.im>
parents:
16136
diff
changeset
|
67 | if (value == NULL) |
|
a895aca0a7ce
Ok I couldn't resist. For iChat and Adium buddies who are advertising
Mark Doliner <markdoliner@pidgin.im>
parents:
16136
diff
changeset
|
68 | break; |
| 7923 | 69 | fieldnode = xmlnode_new_child(result, "field"); |
| 70 | xmlnode_set_attrib(fieldnode, "var", id); | |
| 71 | valuenode = xmlnode_new_child(fieldnode, "value"); | |
| 72 | if(value) | |
| 73 | xmlnode_insert_data(valuenode, value, -1); | |
| 74 | break; | |
| 75 | } | |
| 76 | case JABBER_X_DATA_TEXT_MULTI: | |
| 77 | { | |
| 78 | char **pieces, **p; | |
| 15884 | 79 | const char *value = purple_request_field_string_get_value(field); |
|
16398
a895aca0a7ce
Ok I couldn't resist. For iChat and Adium buddies who are advertising
Mark Doliner <markdoliner@pidgin.im>
parents:
16136
diff
changeset
|
80 | if (value == NULL) |
|
a895aca0a7ce
Ok I couldn't resist. For iChat and Adium buddies who are advertising
Mark Doliner <markdoliner@pidgin.im>
parents:
16136
diff
changeset
|
81 | break; |
| 7923 | 82 | fieldnode = xmlnode_new_child(result, "field"); |
| 83 | xmlnode_set_attrib(fieldnode, "var", id); | |
| 84 | ||
| 85 | pieces = g_strsplit(value, "\n", -1); | |
| 86 | for(p = pieces; *p != NULL; p++) { | |
| 87 | valuenode = xmlnode_new_child(fieldnode, "value"); | |
| 88 | xmlnode_insert_data(valuenode, *p, -1); | |
| 89 | } | |
| 90 | g_strfreev(pieces); | |
| 91 | } | |
| 92 | break; | |
| 93 | case JABBER_X_DATA_LIST_SINGLE: | |
| 94 | case JABBER_X_DATA_LIST_MULTI: | |
| 95 | { | |
|
18190
bcf28ef7e8ff
Re-fix the DBus list handling code by killing const GList* / const GSList*
Richard Laager <rlaager@pidgin.im>
parents:
16490
diff
changeset
|
96 | GList *selected = purple_request_field_list_get_selected(field); |
| 7923 | 97 | char *value; |
| 98 | fieldnode = xmlnode_new_child(result, "field"); | |
| 99 | xmlnode_set_attrib(fieldnode, "var", id); | |
| 100 | ||
| 101 | while(selected) { | |
| 15884 | 102 | value = purple_request_field_list_get_data(field, selected->data); |
| 7923 | 103 | valuenode = xmlnode_new_child(fieldnode, "value"); |
| 104 | if(value) | |
| 105 | xmlnode_insert_data(valuenode, value, -1); | |
| 106 | selected = selected->next; | |
| 107 | } | |
| 108 | } | |
| 109 | break; | |
| 110 | case JABBER_X_DATA_BOOLEAN: | |
| 111 | fieldnode = xmlnode_new_child(result, "field"); | |
| 112 | xmlnode_set_attrib(fieldnode, "var", id); | |
| 113 | valuenode = xmlnode_new_child(fieldnode, "value"); | |
| 15884 | 114 | if(purple_request_field_bool_get_value(field)) |
| 7923 | 115 | xmlnode_insert_data(valuenode, "1", -1); |
| 116 | else | |
| 117 | xmlnode_insert_data(valuenode, "0", -1); | |
| 118 | break; | |
| 119 | case JABBER_X_DATA_IGNORE: | |
| 120 | break; | |
| 121 | } | |
| 122 | } | |
| 123 | } | |
| 124 | ||
| 125 | g_hash_table_destroy(data->fields); | |
| 126 | while(data->values) { | |
| 127 | g_free(data->values->data); | |
| 128 | data->values = g_slist_delete_link(data->values, data->values); | |
| 129 | } | |
| 130 | g_free(data); | |
| 131 | ||
| 132 | cb(js, result, user_data); | |
| 133 | } | |
| 134 | ||
| 15884 | 135 | static void jabber_x_data_cancel_cb(struct jabber_x_data_data *data, PurpleRequestFields *fields) { |
| 7923 | 136 | xmlnode *result = xmlnode_new("x"); |
| 137 | jabber_x_data_cb cb = data->cb; | |
| 138 | gpointer user_data = data->user_data; | |
| 139 | JabberStream *js = data->js; | |
| 140 | g_hash_table_destroy(data->fields); | |
| 141 | while(data->values) { | |
| 142 | g_free(data->values->data); | |
| 143 | data->values = g_slist_delete_link(data->values, data->values); | |
| 144 | } | |
| 145 | g_free(data); | |
| 146 | ||
| 13808 | 147 | xmlnode_set_namespace(result, "jabber:x:data"); |
| 7923 | 148 | xmlnode_set_attrib(result, "type", "cancel"); |
| 149 | ||
| 150 | cb(js, result, user_data); | |
| 151 | } | |
| 152 | ||
| 8396 | 153 | void *jabber_x_data_request(JabberStream *js, xmlnode *packet, jabber_x_data_cb cb, gpointer user_data) |
| 7923 | 154 | { |
| 8396 | 155 | void *handle; |
| 7923 | 156 | xmlnode *fn, *x; |
| 15884 | 157 | PurpleRequestFields *fields; |
| 158 | PurpleRequestFieldGroup *group; | |
| 159 | PurpleRequestField *field; | |
| 7923 | 160 | |
| 161 | char *title = NULL; | |
| 162 | char *instructions = NULL; | |
| 163 | ||
| 164 | struct jabber_x_data_data *data = g_new0(struct jabber_x_data_data, 1); | |
| 165 | ||
| 166 | data->fields = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL); | |
| 167 | data->user_data = user_data; | |
| 168 | data->cb = cb; | |
| 169 | data->js = js; | |
| 170 | ||
| 15884 | 171 | fields = purple_request_fields_new(); |
| 172 | group = purple_request_field_group_new(NULL); | |
| 173 | purple_request_fields_add_group(fields, group); | |
| 7923 | 174 | |
| 8135 | 175 | for(fn = xmlnode_get_child(packet, "field"); fn; fn = xmlnode_get_next_twin(fn)) { |
| 176 | xmlnode *valuenode; | |
| 177 | const char *type = xmlnode_get_attrib(fn, "type"); | |
| 178 | const char *label = xmlnode_get_attrib(fn, "label"); | |
| 179 | const char *var = xmlnode_get_attrib(fn, "var"); | |
| 180 | char *value = NULL; | |
| 181 | ||
| 182 | if(!type) | |
| 183 | continue; | |
| 184 | ||
| 185 | if(!var && strcmp(type, "fixed")) | |
| 186 | continue; | |
| 187 | if(!label) | |
| 188 | label = var; | |
| 189 | ||
| 190 | if((valuenode = xmlnode_get_child(fn, "value"))) | |
| 191 | value = xmlnode_get_data(valuenode); | |
| 192 | ||
| 193 | ||
| 194 | /* XXX: handle <required/> */ | |
| 195 | ||
| 196 | if(!strcmp(type, "text-private")) { | |
| 197 | if((valuenode = xmlnode_get_child(fn, "value"))) | |
| 198 | value = xmlnode_get_data(valuenode); | |
| 199 | ||
| 15884 | 200 | field = purple_request_field_string_new(var, label, |
| 8135 | 201 | value ? value : "", FALSE); |
| 15884 | 202 | purple_request_field_string_set_masked(field, TRUE); |
| 203 | purple_request_field_group_add_field(group, field); | |
| 8135 | 204 | |
| 205 | g_hash_table_replace(data->fields, g_strdup(var), GINT_TO_POINTER(JABBER_X_DATA_TEXT_SINGLE)); | |
| 206 | ||
|
16136
49ddb8f06d96
I noticed some places where we were doing the whole
Mark Doliner <markdoliner@pidgin.im>
parents:
15884
diff
changeset
|
207 | g_free(value); |
| 8135 | 208 | } else if(!strcmp(type, "text-multi") || !strcmp(type, "jid-multi")) { |
| 209 | GString *str = g_string_new(""); | |
| 210 | ||
| 211 | for(valuenode = xmlnode_get_child(fn, "value"); valuenode; | |
| 212 | valuenode = xmlnode_get_next_twin(valuenode)) { | |
| 213 | ||
| 214 | if(!(value = xmlnode_get_data(valuenode))) | |
| 215 | continue; | |
| 216 | ||
| 217 | g_string_append_printf(str, "%s\n", value); | |
| 218 | g_free(value); | |
| 219 | } | |
| 220 | ||
| 15884 | 221 | field = purple_request_field_string_new(var, label, |
| 8135 | 222 | str->str, TRUE); |
| 15884 | 223 | purple_request_field_group_add_field(group, field); |
| 7923 | 224 | |
| 8135 | 225 | g_hash_table_replace(data->fields, g_strdup(var), GINT_TO_POINTER(JABBER_X_DATA_TEXT_MULTI)); |
| 226 | ||
| 227 | g_string_free(str, TRUE); | |
| 228 | } else if(!strcmp(type, "list-single") || !strcmp(type, "list-multi")) { | |
| 229 | xmlnode *optnode; | |
| 230 | GList *selected = NULL; | |
| 231 | ||
| 15884 | 232 | field = purple_request_field_list_new(var, label); |
| 8135 | 233 | |
| 234 | if(!strcmp(type, "list-multi")) { | |
| 15884 | 235 | purple_request_field_list_set_multi_select(field, TRUE); |
| 8135 | 236 | g_hash_table_replace(data->fields, g_strdup(var), |
| 237 | GINT_TO_POINTER(JABBER_X_DATA_LIST_MULTI)); | |
| 238 | } else { | |
| 239 | g_hash_table_replace(data->fields, g_strdup(var), | |
| 240 | GINT_TO_POINTER(JABBER_X_DATA_LIST_SINGLE)); | |
| 241 | } | |
| 242 | ||
| 243 | for(valuenode = xmlnode_get_child(fn, "value"); valuenode; | |
| 244 | valuenode = xmlnode_get_next_twin(valuenode)) { | |
| 245 | selected = g_list_prepend(selected, xmlnode_get_data(valuenode)); | |
| 246 | } | |
| 7923 | 247 | |
| 8135 | 248 | for(optnode = xmlnode_get_child(fn, "option"); optnode; |
| 249 | optnode = xmlnode_get_next_twin(optnode)) { | |
| 250 | const char *lbl; | |
| 251 | ||
| 252 | if(!(valuenode = xmlnode_get_child(optnode, "value"))) | |
| 253 | continue; | |
| 254 | ||
| 255 | if(!(value = xmlnode_get_data(valuenode))) | |
| 256 | continue; | |
| 257 | ||
| 258 | if(!(lbl = xmlnode_get_attrib(optnode, "label"))) | |
|
19051
b932fc2e28cd
For XMPP multi-selection lists in generic data forms, the intent here
Mark Doliner <markdoliner@pidgin.im>
parents:
18190
diff
changeset
|
259 | lbl = value; |
| 8135 | 260 | |
| 261 | data->values = g_slist_prepend(data->values, value); | |
| 262 | ||
| 15884 | 263 | purple_request_field_list_add(field, lbl, value); |
| 8135 | 264 | if(g_list_find_custom(selected, value, (GCompareFunc)strcmp)) |
| 15884 | 265 | purple_request_field_list_add_selected(field, lbl); |
| 8135 | 266 | } |
| 15884 | 267 | purple_request_field_group_add_field(group, field); |
| 8135 | 268 | |
| 269 | while(selected) { | |
| 270 | g_free(selected->data); | |
| 271 | selected = g_list_delete_link(selected, selected); | |
| 272 | } | |
| 273 | ||
| 274 | } else if(!strcmp(type, "boolean")) { | |
| 275 | gboolean def = FALSE; | |
| 7923 | 276 | |
| 277 | if((valuenode = xmlnode_get_child(fn, "value"))) | |
| 278 | value = xmlnode_get_data(valuenode); | |
| 279 | ||
|
8390
13e1ff9b2850
[gaim-migrate @ 9119]
Mark Doliner <markdoliner@pidgin.im>
parents:
8385
diff
changeset
|
280 | if(value && (!g_ascii_strcasecmp(value, "yes") || |
|
13e1ff9b2850
[gaim-migrate @ 9119]
Mark Doliner <markdoliner@pidgin.im>
parents:
8385
diff
changeset
|
281 | !g_ascii_strcasecmp(value, "true") || !g_ascii_strcasecmp(value, "1"))) |
| 8135 | 282 | def = TRUE; |
| 7923 | 283 | |
| 15884 | 284 | field = purple_request_field_bool_new(var, label, def); |
| 285 | purple_request_field_group_add_field(group, field); | |
| 7923 | 286 | |
| 8135 | 287 | g_hash_table_replace(data->fields, g_strdup(var), GINT_TO_POINTER(JABBER_X_DATA_BOOLEAN)); |
| 7923 | 288 | |
|
16136
49ddb8f06d96
I noticed some places where we were doing the whole
Mark Doliner <markdoliner@pidgin.im>
parents:
15884
diff
changeset
|
289 | g_free(value); |
| 8135 | 290 | } else if(!strcmp(type, "fixed") && value) { |
| 291 | if((valuenode = xmlnode_get_child(fn, "value"))) | |
| 292 | value = xmlnode_get_data(valuenode); | |
| 7923 | 293 | |
| 15884 | 294 | field = purple_request_field_label_new("", value); |
| 295 | purple_request_field_group_add_field(group, field); | |
| 7923 | 296 | |
|
16136
49ddb8f06d96
I noticed some places where we were doing the whole
Mark Doliner <markdoliner@pidgin.im>
parents:
15884
diff
changeset
|
297 | g_free(value); |
| 8135 | 298 | } else if(!strcmp(type, "hidden")) { |
| 299 | if((valuenode = xmlnode_get_child(fn, "value"))) | |
| 300 | value = xmlnode_get_data(valuenode); | |
| 7923 | 301 | |
| 15884 | 302 | field = purple_request_field_string_new(var, "", value ? value : "", |
| 8135 | 303 | FALSE); |
| 15884 | 304 | purple_request_field_set_visible(field, FALSE); |
| 305 | purple_request_field_group_add_field(group, field); | |
| 7923 | 306 | |
| 8135 | 307 | g_hash_table_replace(data->fields, g_strdup(var), GINT_TO_POINTER(JABBER_X_DATA_TEXT_SINGLE)); |
| 7923 | 308 | |
|
16136
49ddb8f06d96
I noticed some places where we were doing the whole
Mark Doliner <markdoliner@pidgin.im>
parents:
15884
diff
changeset
|
309 | g_free(value); |
| 8135 | 310 | } else { /* text-single, jid-single, and the default */ |
| 311 | if((valuenode = xmlnode_get_child(fn, "value"))) | |
| 312 | value = xmlnode_get_data(valuenode); | |
| 7923 | 313 | |
| 15884 | 314 | field = purple_request_field_string_new(var, label, |
| 8135 | 315 | value ? value : "", FALSE); |
| 15884 | 316 | purple_request_field_group_add_field(group, field); |
| 7923 | 317 | |
| 8295 | 318 | if(!strcmp(type, "jid-single")) { |
| 15884 | 319 | purple_request_field_set_type_hint(field, "screenname"); |
| 8295 | 320 | g_hash_table_replace(data->fields, g_strdup(var), GINT_TO_POINTER(JABBER_X_DATA_JID_SINGLE)); |
| 321 | } else { | |
| 322 | g_hash_table_replace(data->fields, g_strdup(var), GINT_TO_POINTER(JABBER_X_DATA_TEXT_SINGLE)); | |
| 323 | } | |
| 7923 | 324 | |
|
16136
49ddb8f06d96
I noticed some places where we were doing the whole
Mark Doliner <markdoliner@pidgin.im>
parents:
15884
diff
changeset
|
325 | g_free(value); |
| 7923 | 326 | } |
| 327 | } | |
| 328 | ||
| 329 | if((x = xmlnode_get_child(packet, "title"))) | |
| 330 | title = xmlnode_get_data(x); | |
| 331 | ||
| 332 | if((x = xmlnode_get_child(packet, "instructions"))) | |
| 333 | instructions = xmlnode_get_data(x); | |
| 334 | ||
| 15884 | 335 | handle = purple_request_fields(js->gc, title, title, instructions, fields, |
| 8396 | 336 | _("OK"), G_CALLBACK(jabber_x_data_ok_cb), |
|
16490
68c22924d66b
Added account, who, and conversation parameters to the request API calls, and updated all code to match. I can't compile the Perl module, so I'd appreciate it if someone who knows it would verify that this doesn't break Perl.
Evan Schoenberg <evands@pidgin.im>
parents:
16398
diff
changeset
|
337 | _("Cancel"), G_CALLBACK(jabber_x_data_cancel_cb), |
|
68c22924d66b
Added account, who, and conversation parameters to the request API calls, and updated all code to match. I can't compile the Perl module, so I'd appreciate it if someone who knows it would verify that this doesn't break Perl.
Evan Schoenberg <evands@pidgin.im>
parents:
16398
diff
changeset
|
338 | purple_connection_get_account(js->gc), /* XXX Do we have a who here? */ NULL, NULL, |
|
68c22924d66b
Added account, who, and conversation parameters to the request API calls, and updated all code to match. I can't compile the Perl module, so I'd appreciate it if someone who knows it would verify that this doesn't break Perl.
Evan Schoenberg <evands@pidgin.im>
parents:
16398
diff
changeset
|
339 | data); |
| 7923 | 340 | |
|
16136
49ddb8f06d96
I noticed some places where we were doing the whole
Mark Doliner <markdoliner@pidgin.im>
parents:
15884
diff
changeset
|
341 | g_free(title); |
|
49ddb8f06d96
I noticed some places where we were doing the whole
Mark Doliner <markdoliner@pidgin.im>
parents:
15884
diff
changeset
|
342 | g_free(instructions); |
| 7923 | 343 | |
| 8396 | 344 | return handle; |
| 7923 | 345 | } |
| 346 | ||
| 347 |