-
Notifications
You must be signed in to change notification settings - Fork 0
/
opensim_inventory_glue.cpp
783 lines (674 loc) · 24.6 KB
/
opensim_inventory_glue.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
/* Copyright (c) 2009-2010 Aidan Thornton, all rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY AIDAN THORNTON ''AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL AIDAN THORNTON BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "cajeput_core.h"
#include "cajeput_user.h"
#include <libsoup/soup.h>
#include "caj_types.h"
#include <uuid/uuid.h>
#include <stdio.h>
#include <stdlib.h>
#include <glib.h>
#include <libsoup/soup.h>
#include "opensim_grid_glue.h"
#include "opensim_xml_glue.h"
#include <cassert>
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <libxml/xmlwriter.h>
#include <stddef.h>
// FIXME - this code has bucket-loads of code duplication!
static int check_node(xmlNodePtr node, const char* name) {
if(node == NULL) {
printf("ERROR: missing %s node\n", name);
return 0;
} else if(strcmp((char*)node->name, name) != 0) {
printf("ERROR: unexpected node: wanted %s, got %s\n",
name, (char*)node->name);
return 0;
}
return 1;
}
struct os_inv_folder {
char *name;
uuid_t folder_id, owner_id, parent_id;
int asset_type;
int version; // FIXME - use long/int32_t?
};
static xml_serialisation_desc deserialise_inv_folder[] = {
{ "Name", XML_STYPE_STRING, offsetof(os_inv_folder, name) },
{ "ID", XML_STYPE_UUID, offsetof(os_inv_folder, folder_id) },
{ "Owner", XML_STYPE_UUID,offsetof(os_inv_folder, owner_id) },
{ "ParentID", XML_STYPE_UUID,offsetof(os_inv_folder, parent_id) },
{ "Type", XML_STYPE_INT,offsetof(os_inv_folder, asset_type) },
{ "Version", XML_STYPE_INT,offsetof(os_inv_folder, version) },
{ NULL, }
};
// FIXME - deal with whitespace!
static void parse_inv_folders(xmlDocPtr doc, xmlNodePtr node,
struct inventory_contents* inv) {
for( ; node != NULL; node = node->next) {
if(!check_node(node,"InventoryFolderBase")) continue;
os_inv_folder folder;
if(!osglue_deserialise_xml(doc, node->children, deserialise_inv_folder,
&folder)) continue;
// DEBUG
char buf[40];
uuid_unparse(folder.folder_id, buf);
printf("Inventory folder: %s [%s]\n", folder.name, buf);
// FIXME - need to check parent ID matches expected one
caj_inv_add_folder(inv, folder.folder_id, folder.owner_id,
folder.name, folder.asset_type);
xmlFree(folder.name);
}
}
struct os_inv_item {
char *name;
uuid_t item_id, owner_id;
int inv_type;
uuid_t folder_id;
char *creator_id;
uuid_t creator_as_uuid;
char *description;
int next_perms, current_perms, base_perms; // FIXME - should be uint32_t
int everyone_perms, group_perms; // ditto!
int asset_type;
uuid_t asset_id, group_id;
int group_owned;
int sale_price; // FIXME - should be int32_t
int sale_type;
int flags; // FIXME - should be uint32_t
int creation_date; // FIXME - should be int32_t (or larger?)
};
static xml_serialisation_desc deserialise_inv_item[] = {
{ "Name", XML_STYPE_STRING, offsetof(os_inv_item, name) },
{ "ID", XML_STYPE_UUID, offsetof(os_inv_item, item_id) },
{ "Owner", XML_STYPE_UUID,offsetof(os_inv_item, owner_id) },
{ "InvType", XML_STYPE_INT,offsetof(os_inv_item, inv_type) },
{ "Folder", XML_STYPE_UUID,offsetof(os_inv_item, folder_id) },
{ "CreatorId", XML_STYPE_STRING, offsetof(os_inv_item, creator_id) },
{ "CreatorIdAsUuid", XML_STYPE_UUID, offsetof(os_inv_item, creator_as_uuid) },
{ "Description", XML_STYPE_STRING, offsetof(os_inv_item, description) },
{ "NextPermissions", XML_STYPE_INT,offsetof(os_inv_item, next_perms) },
{ "CurrentPermissions", XML_STYPE_INT,offsetof(os_inv_item, current_perms) },
{ "BasePermissions", XML_STYPE_INT,offsetof(os_inv_item, base_perms) },
{ "EveryOnePermissions", XML_STYPE_INT,offsetof(os_inv_item, everyone_perms) },
{ "GroupPermissions", XML_STYPE_INT,offsetof(os_inv_item, group_perms) },
{ "AssetType", XML_STYPE_INT,offsetof(os_inv_item, asset_type) },
{ "AssetID", XML_STYPE_UUID,offsetof(os_inv_item, asset_id) },
{ "GroupID", XML_STYPE_UUID,offsetof(os_inv_item, group_id) },
{ "GroupOwned", XML_STYPE_BOOL,offsetof(os_inv_item, group_owned) },
{ "SalePrice", XML_STYPE_INT,offsetof(os_inv_item, sale_price) },
{ "SaleType", XML_STYPE_INT,offsetof(os_inv_item, sale_type) },
{ "Flags", XML_STYPE_INT,offsetof(os_inv_item, flags) },
{ "CreationDate", XML_STYPE_INT,offsetof(os_inv_item, creation_date) },
{ NULL, }
};
static void fill_inv_item_from_opensim(inventory_item *citem,
os_inv_item &item) {
uuid_copy(citem->item_id, item.item_id);
uuid_copy(citem->owner_id, item.owner_id);
citem->inv_type = item.inv_type;
uuid_copy(citem->folder_id, item.folder_id);
uuid_copy(citem->creator_as_uuid, item.creator_as_uuid);
citem->perms.next = item.next_perms;
citem->perms.current = item.current_perms;
citem->perms.base = item.base_perms;
citem->perms.everyone = item.everyone_perms;
citem->perms.group = item.group_perms;
citem->asset_type = item.asset_type;
uuid_copy(citem->asset_id, item.asset_id);
uuid_copy(citem->group_id, item.group_id);
citem->group_owned = item.group_owned;
citem->sale_price = item.sale_price;
citem->sale_type = item.sale_type;
citem->flags = item.flags;
citem->creation_date = item.creation_date;
xmlFree(item.name); xmlFree(item.description);
xmlFree(item.creator_id);
}
static void inv_item_to_opensim(const inventory_item *citem,
os_inv_item &item) {
item.name = citem->name;
item.description = citem->description;
item.creator_id = citem->creator_id;
uuid_copy(item.item_id, citem->item_id);
uuid_copy(item.owner_id, citem->owner_id);
item.inv_type = citem->inv_type;
uuid_copy(item.folder_id, citem->folder_id);
uuid_copy(item.creator_as_uuid, citem->creator_as_uuid);
item.next_perms = citem->perms.next;
item.current_perms = citem->perms.current;
item.base_perms = citem->perms.base;
item.group_perms = citem->perms.group;
item.everyone_perms = citem->perms.everyone;
item.asset_type = citem->asset_type;
uuid_copy(item.asset_id, citem->asset_id);
uuid_copy(item.group_id, citem->group_id);
item.group_owned = citem->group_owned;
item.sale_price = citem->sale_price;
item.sale_type = citem->sale_type;
item.flags = citem->flags;
item.creation_date = citem->creation_date;
}
static void conv_inv_item_from_opensim(inventory_item *citem,
os_inv_item &item) {
citem->name = strdup(item.name);
citem->description = strdup(item.description);
citem->creator_id = strdup(item.creator_id);
fill_inv_item_from_opensim(citem, item);
}
static void free_inv_item_from_opensim(inventory_item *citem) {
free(citem->name); free(citem->description); free(citem->creator_id);
}
static void parse_inv_items(xmlDocPtr doc, xmlNodePtr node,
struct inventory_contents* inv) {
for( ; node != NULL; node = node->next) {
if(!check_node(node,"InventoryItemBase")) continue;
os_inv_item item;
if(!osglue_deserialise_xml(doc, node->children, deserialise_inv_item,
&item)) continue;
// DEBUG
char buf[40];
uuid_unparse(item.item_id, buf);
printf("Inventory item: %s [%s]\n", item.name, buf);
inventory_item *citem = caj_add_inventory_item(inv, item.name,
item.description,
item.creator_id);
fill_inv_item_from_opensim(citem, item);
if(citem != NULL)
printf(" DEBUG: item flags 0x%x\n",(unsigned)citem->flags);
}
}
struct inv_items_req {
user_grid_glue* user_glue;
uuid_t folder_id;
void(*cb)(struct inventory_contents* inv,
void *cb_priv);
void *cb_priv;
};
// FIXME - deal with whitespace!
static void got_inventory_items_resp(SoupSession *session, SoupMessage *msg, gpointer user_data) {
//USER_PRIV_DEF(user_data);
//GRID_PRIV_DEF(sim);
inv_items_req *req = (inv_items_req*)user_data;
user_grid_glue* user_glue = req->user_glue;
xmlDocPtr doc;
xmlNodePtr node;
struct inventory_contents* inv;
user_grid_glue_deref(user_glue);
if(msg->status_code != 200) {
printf("ERROR: Inventory request failed: got %i %s\n",(int)msg->status_code,msg->reason_phrase);
goto fail;
}
printf("DEBUG: inventory folder content resp {{%s}}\n",
msg->response_body->data);
doc = xmlReadMemory(msg->response_body->data,
msg->response_body->length,
"inventory.xml", NULL, 0);
if(doc == NULL) {
printf("ERROR: inventory XML parse failed\n");
goto fail;
}
node = xmlDocGetRootElement(doc);
if(strcmp((char*)node->name, "InventoryCollection") != 0) {
printf("ERROR: unexpected root node %s\n",(char*)node->name);
goto free_fail;
}
node = node->children;
if(!check_node(node,"Folders")) goto free_fail;
inv = caj_inv_new_contents_desc(req->folder_id);
parse_inv_folders(doc, node->children, inv);
node = node->next;
if(!check_node(node,"Items")) goto free_inv_fail;
parse_inv_items(doc, node->children, inv);
// there's a UserID node next, but it's just the null UUID anyway.
req->cb(inv, req->cb_priv);
caj_inv_free_contents_desc(inv);
xmlFreeDoc(doc);
delete req;
return;
free_inv_fail:
caj_inv_free_contents_desc(inv);
free_fail:
xmlFreeDoc(doc);
fail:
req->cb(NULL, req->cb_priv);
delete req;
printf("ERROR: inventory response parse failure\n");
return;
}
// fetch contents of inventory folder
void fetch_inventory_folder(simgroup_ctx *sgrp, user_ctx *user,
void *user_priv, const uuid_t folder_id,
void(*cb)(struct inventory_contents* inv,
void* priv),
void *cb_priv) {
uuid_t u; char tmp[40]; char uri[256];
GRID_PRIV_DEF_SGRP(sgrp);
USER_PRIV_DEF(user_priv);
xmlTextWriterPtr writer;
xmlBufferPtr buf;
SoupMessage *msg;
inv_items_req *req;
assert(grid->inventory_server != NULL);
buf = xmlBufferCreate();
if(buf == NULL) goto fail;
writer = xmlNewTextWriterMemory(buf, 0);
if(writer == NULL) goto free_fail_1;
if(xmlTextWriterStartDocument(writer,NULL,"UTF-8",NULL) < 0)
goto free_fail;
if(xmlTextWriterStartElement(writer, BAD_CAST "RestSessionObjectOfGuid") < 0)
goto free_fail;
user_get_session_id(user, u);
uuid_unparse(u, tmp);
if(xmlTextWriterWriteFormatElement(writer,BAD_CAST "SessionID",
"%s",tmp) < 0) goto free_fail;
user_get_uuid(user, u);
uuid_unparse(u, tmp);
if(xmlTextWriterWriteFormatElement(writer,BAD_CAST "AvatarID",
"%s",tmp) < 0) goto free_fail;
uuid_unparse(folder_id, tmp);
if(xmlTextWriterWriteFormatElement(writer,BAD_CAST "Body",
"%s",tmp) < 0) goto free_fail;
if(xmlTextWriterEndElement(writer) < 0)
goto free_fail;
if(xmlTextWriterEndDocument(writer) < 0) {
printf("DEBUG: couldn't end XML document\n"); goto fail;
}
// FIXME - don't use fixed-length buffer, and handle missing trailing /
snprintf(uri, 256, "%sGetFolderContent/", grid->inventory_server);
printf("DEBUG: sending inventory request to %s\n", uri);
msg = soup_message_new ("POST", uri);
// FIXME - avoid unnecessary strlen
soup_message_set_request (msg, "application/xml",
SOUP_MEMORY_COPY, (char*)buf->content,
strlen ((char*)buf->content));
req = new inv_items_req();
req->user_glue = user_glue; req->cb = cb;
req->cb_priv = cb_priv;
uuid_copy(req->folder_id, folder_id);
user_grid_glue_ref(user_glue);
caj_queue_soup_message(sgrp, SOUP_MESSAGE(msg),
got_inventory_items_resp, req);
xmlFreeTextWriter(writer);
xmlBufferFree(buf);
return;
free_fail:
xmlFreeTextWriter(writer);
free_fail_1:
xmlBufferFree(buf);
fail:
printf("DEBUG: ran into issues sending inventory request\n");
cb(NULL, cb_priv);
// FIXME - handle this
}
struct inv_item_req {
user_grid_glue* user_glue;
uuid_t item_id;
void(*cb)(struct inventory_item* item,
void *cb_priv);
void *cb_priv;
};
// FIXME - deal with whitespace!
static void got_inventory_item_resp(SoupSession *session, SoupMessage *msg, gpointer user_data) {
//USER_PRIV_DEF(user_data);
//GRID_PRIV_DEF(sim);
inv_item_req *req = (inv_item_req*)user_data;
user_grid_glue* user_glue = req->user_glue;
xmlDocPtr doc;
xmlNodePtr node;
struct inventory_item invit;
os_inv_item item;
user_grid_glue_deref(user_glue);
if(msg->status_code != 200) {
printf("Inventory request failed: got %i %s\n",(int)msg->status_code,msg->reason_phrase);
goto fail;
}
printf("DEBUG: inventory item query resp {{%s}}\n",
msg->response_body->data);
doc = xmlReadMemory(msg->response_body->data,
msg->response_body->length,
"inventory.xml", NULL, 0);
if(doc == NULL) {
printf("ERROR: inventory XML parse failed\n");
goto fail;
}
node = xmlDocGetRootElement(doc);
if(strcmp((char*)node->name, "InventoryItemBase") != 0) {
printf("ERROR: unexpected root node %s\n",(char*)node->name);
goto free_fail;
}
if(!osglue_deserialise_xml(doc, node->children, deserialise_inv_item,
&item)) {
printf("ERROR: couldn't deserialise XML inventory item\n");
goto free_fail;
}
conv_inv_item_from_opensim(&invit, item);
req->cb(&invit, req->cb_priv);
free_inv_item_from_opensim(&invit);
xmlFreeDoc(doc);
delete req;
return;
free_fail:
xmlFreeDoc(doc);
fail:
req->cb(NULL, req->cb_priv);
delete req;
printf("ERROR: inventory item response parse failure\n");
return;
}
void fetch_inventory_item(simgroup_ctx *sgrp, user_ctx *user,
void *user_priv, const uuid_t item_id,
void(*cb)(struct inventory_item* item,
void* priv),
void *cb_priv) {
uuid_t u, user_id; char tmp[40]; char uri[256];
GRID_PRIV_DEF_SGRP(sgrp);
USER_PRIV_DEF(user_priv);
xmlTextWriterPtr writer;
xmlBufferPtr buf;
SoupMessage *msg;
inv_item_req *req;
struct os_inv_item invitem; // don't ask. Please.
assert(grid->inventory_server != NULL);
buf = xmlBufferCreate();
if(buf == NULL) goto fail;
writer = xmlNewTextWriterMemory(buf, 0);
if(writer == NULL) goto free_fail_1;
if(xmlTextWriterStartDocument(writer,NULL,"UTF-8",NULL) < 0)
goto free_fail;
if(xmlTextWriterStartElement(writer, BAD_CAST "RestSessionObjectOfInventoryItemBase") < 0)
goto free_fail;
user_get_session_id(user, u);
uuid_unparse(u, tmp);
if(xmlTextWriterWriteFormatElement(writer,BAD_CAST "SessionID",
"%s",tmp) < 0) goto free_fail;
user_get_uuid(user, user_id);
uuid_unparse(user_id, tmp);
if(xmlTextWriterWriteFormatElement(writer,BAD_CAST "AvatarID",
"%s",tmp) < 0) goto free_fail;
// okay, this is just painful... we have to serialise an entire complex
// object in this cruddy .Net XML serialisation format... and the only bit
// they actually use or need is a single UUID. Bletch *vomit*.
if(xmlTextWriterStartElement(writer,BAD_CAST "Body") < 0)
goto free_fail;
memset(&invitem, 0, sizeof(invitem));
invitem.name = invitem.description = const_cast<char*>("");
invitem.creator_id = const_cast<char*>("");
uuid_copy(invitem.owner_id, user_id);
uuid_copy(invitem.item_id, item_id);
osglue_serialise_xml(writer, deserialise_inv_item, &invitem);
if(xmlTextWriterEndElement(writer) < 0)
goto free_fail;
// now we should be done serialising the XML crud.
if(xmlTextWriterEndElement(writer) < 0)
goto free_fail;
if(xmlTextWriterEndDocument(writer) < 0) {
printf("DEBUG: couldn't end XML document\n"); goto fail;
}
// FIXME - don't use fixed-length buffer, and handle missing trailing /
snprintf(uri, 256, "%sQueryItem/", grid->inventory_server);
printf("DEBUG: sending inventory request to %s\n", uri);
msg = soup_message_new ("POST", uri);
// FIXME - avoid unnecessary strlen
soup_message_set_request (msg, "text/xml",
SOUP_MEMORY_COPY, (char*)buf->content,
strlen ((char*)buf->content));
req = new inv_item_req();
req->user_glue = user_glue; req->cb = cb;
req->cb_priv = cb_priv;
uuid_copy(req->item_id, item_id);
user_grid_glue_ref(user_glue);
caj_queue_soup_message(sgrp, SOUP_MESSAGE(msg),
got_inventory_item_resp, req);
xmlFreeTextWriter(writer);
xmlBufferFree(buf);
return;
free_fail:
xmlFreeTextWriter(writer);
free_fail_1:
xmlBufferFree(buf);
fail:
printf("DEBUG: ran into issues sending inventory QueryItem request\n");
cb(NULL, cb_priv);
// FIXME - handle this
}
struct add_inv_item_req {
user_grid_glue* user_glue;
uuid_t item_id;
void(*cb)(void *cb_priv, int success, uuid_t item_id);
void *cb_priv;
};
// FIXME - most of this should be factored out into boilerplate code
static void got_add_inv_item_resp(SoupSession *session, SoupMessage *msg, gpointer user_data) {
//USER_PRIV_DEF(user_data);
//GRID_PRIV_DEF(sim);
uuid_t u; char* s;
add_inv_item_req *req = (add_inv_item_req*)user_data;
user_grid_glue* user_glue = req->user_glue;
xmlDocPtr doc;
xmlNodePtr node;
user_grid_glue_deref(user_glue);
if(msg->status_code != 200) {
printf("Inventory request failed: got %i %s\n",(int)msg->status_code,msg->reason_phrase);
goto fail;
}
printf("DEBUG: inventory item add resp {{%s}}\n",
msg->response_body->data);
doc = xmlReadMemory(msg->response_body->data,
msg->response_body->length,
"inventory_resp.xml", NULL, 0);
if(doc == NULL) {
printf("ERROR: inventory XML response parse failed\n");
goto fail;
}
node = xmlDocGetRootElement(doc);
if(strcmp((char*)node->name, "boolean") != 0) {
printf("ERROR: unexpected root node %s\n",(char*)node->name);
goto free_fail;
}
s = (char*)xmlNodeListGetString(doc, node->children, 1);
req->cb(req->cb_priv, s != NULL && strcmp(s, "true") == 0,
req->item_id);
xmlFree(s);
xmlFreeDoc(doc);
delete req;
return;
free_fail:
xmlFreeDoc(doc);
fail:
uuid_clear(u); req->cb(req->cb_priv, FALSE, u);
delete req;
printf("ERROR: add inventory item response parse failure\n");
return;
}
void add_inventory_item(simgroup_ctx *sgrp, user_ctx *user,
void *user_priv, inventory_item *inv,
void(*cb)(void* priv, int success, uuid_t item_id),
void *cb_priv) {
uuid_t u, user_id; char tmp[40]; char uri[256];
GRID_PRIV_DEF_SGRP(sgrp);
USER_PRIV_DEF(user_priv);
xmlTextWriterPtr writer;
xmlBufferPtr buf;
SoupMessage *msg;
add_inv_item_req *req;
struct os_inv_item invitem;
assert(grid->inventory_server != NULL);
buf = xmlBufferCreate();
if(buf == NULL) goto fail;
writer = xmlNewTextWriterMemory(buf, 0);
if(writer == NULL) goto free_fail_1;
if(xmlTextWriterStartDocument(writer,NULL,"UTF-8",NULL) < 0)
goto free_fail;
if(xmlTextWriterStartElement(writer, BAD_CAST "RestSessionObjectOfInventoryItemBase") < 0)
goto free_fail;
user_get_session_id(user, u);
uuid_unparse(u, tmp);
if(xmlTextWriterWriteFormatElement(writer,BAD_CAST "SessionID",
"%s",tmp) < 0) goto free_fail;
user_get_uuid(user, user_id);
uuid_unparse(user_id, tmp);
if(xmlTextWriterWriteFormatElement(writer,BAD_CAST "AvatarID",
"%s",tmp) < 0) goto free_fail;
if(xmlTextWriterStartElement(writer,BAD_CAST "Body") < 0)
goto free_fail;
inv_item_to_opensim(inv, invitem);
osglue_serialise_xml(writer, deserialise_inv_item, &invitem);
if(xmlTextWriterEndElement(writer) < 0)
goto free_fail;
if(xmlTextWriterEndElement(writer) < 0)
goto free_fail;
if(xmlTextWriterEndDocument(writer) < 0) {
printf("DEBUG: couldn't end XML document\n"); goto fail;
}
// FIXME - don't use fixed-length buffer, and handle missing trailing /
// (note: not AddNewItem, as that's not meant for sim use!)
snprintf(uri, 256, "%sNewItem/", grid->inventory_server);
printf("DEBUG: sending inventory add request to %s\n", uri);
msg = soup_message_new ("POST", uri);
// FIXME - avoid unnecessary strlen
soup_message_set_request (msg, "text/xml",
SOUP_MEMORY_COPY, (char*)buf->content,
strlen ((char*)buf->content));
req = new add_inv_item_req();
req->user_glue = user_glue; req->cb = cb;
req->cb_priv = cb_priv;
uuid_copy(req->item_id, inv->item_id);
user_grid_glue_ref(user_glue);
caj_queue_soup_message(sgrp, SOUP_MESSAGE(msg),
got_add_inv_item_resp, req);
xmlFreeTextWriter(writer);
xmlBufferFree(buf);
return;
free_fail:
xmlFreeTextWriter(writer);
free_fail_1:
xmlBufferFree(buf);
fail:
printf("DEBUG: ran into issues sending inventory NewItem request\n");
uuid_clear(u); cb(cb_priv, FALSE, u);
}
// FIXME - deal with whitespace!
static void got_system_folders_resp(SoupSession *session, SoupMessage *msg, gpointer user_data) {
//USER_PRIV_DEF(user_data);
//GRID_PRIV_DEF(sim);
inv_items_req *req = (inv_items_req*)user_data;
user_grid_glue* user_glue = req->user_glue;
user_ctx* ctx = user_glue->ctx;
xmlDocPtr doc;
xmlNodePtr node;
struct inventory_contents* inv;
user_grid_glue_deref(user_glue);
if(msg->status_code != 200) {
printf("System folders request failed: got %i %s\n",(int)msg->status_code,msg->reason_phrase);
goto fail;
}
printf("DEBUG: system folders resp {{%s}}\n",
msg->response_body->data);
doc = xmlReadMemory(msg->response_body->data,
msg->response_body->length,
"system_folders.xml", NULL, 0);
if(doc == NULL) {
printf("ERROR: system folders XML parse failed\n");
goto fail;
}
node = xmlDocGetRootElement(doc);
if(strcmp((char*)node->name, "ArrayOfInventoryFolderBase") != 0) {
printf("ERROR: unexpected root node %s\n",(char*)node->name);
goto free_fail;
}
// FIXME - proper error handling!
if(ctx != NULL) {
inv = caj_inv_new_contents_desc(req->folder_id);
parse_inv_folders(doc, node->children, inv);
user_set_system_folders(ctx, inv);
}
xmlFreeDoc(doc); delete req;
return;
free_fail:
xmlFreeDoc(doc);
fail:
// FIXME - handle this!
delete req;
printf("ERROR: request for system folders failed\n");
if(ctx != NULL) user_set_system_folders(ctx, NULL);
return;
}
// fetch contents of inventory folder
void fetch_system_folders(simgroup_ctx *sgrp, user_ctx *user,
void *user_priv) {
uuid_t u; char tmp[40]; char uri[256];
GRID_PRIV_DEF_SGRP(sgrp);
USER_PRIV_DEF(user_priv);
xmlTextWriterPtr writer;
xmlBufferPtr buf;
SoupMessage *msg;
inv_items_req *req;
assert(grid->inventory_server != NULL);
buf = xmlBufferCreate();
if(buf == NULL) goto fail;
writer = xmlNewTextWriterMemory(buf, 0);
if(writer == NULL) goto free_fail_1;
if(xmlTextWriterStartDocument(writer,NULL,"UTF-8",NULL) < 0)
goto free_fail;
if(xmlTextWriterStartElement(writer, BAD_CAST "RestSessionObjectOfGuid") < 0)
goto free_fail;
user_get_session_id(user, u);
uuid_unparse(u, tmp);
if(xmlTextWriterWriteFormatElement(writer,BAD_CAST "SessionID",
"%s",tmp) < 0) goto free_fail;
user_get_uuid(user, u);
uuid_unparse(u, tmp);
if(xmlTextWriterWriteFormatElement(writer,BAD_CAST "AvatarID",
"%s",tmp) < 0) goto free_fail;
if(xmlTextWriterWriteFormatElement(writer,BAD_CAST "Body",
"%s",tmp) < 0) goto free_fail;
if(xmlTextWriterEndElement(writer) < 0)
goto free_fail;
if(xmlTextWriterEndDocument(writer) < 0) {
printf("DEBUG: couldn't end XML document\n"); goto fail;
}
// FIXME - don't use fixed-length buffer, and handle missing trailing /
snprintf(uri, 256, "%sSystemFolders/", grid->inventory_server);
printf("DEBUG: sending inventory request to %s\n", uri);
msg = soup_message_new ("POST", uri);
// FIXME - avoid unnecessary strlen
soup_message_set_request (msg, "application/xml",
SOUP_MEMORY_COPY, (char*)buf->content,
strlen ((char*)buf->content));
req = new inv_items_req();
req->user_glue = user_glue;
user_grid_glue_ref(user_glue);
caj_queue_soup_message(sgrp, SOUP_MESSAGE(msg),
got_system_folders_resp, req);
xmlFreeTextWriter(writer);
xmlBufferFree(buf);
return;
free_fail:
xmlFreeTextWriter(writer);
free_fail_1:
xmlBufferFree(buf);
fail:
printf("DEBUG: ran into issues sending inventory request\n");
// FIXME - handle this
}