Increase version to 0.10.2
[libisds.git] / client / isdsclient.c
blob364a66e5496d9b80afadd5578014331dd5daeaa4
1 #include "../config.h"
2 #define _XOPEN_SOURCE XOPEN_SOURCE_LEVEL_FOR_STRDUP
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <locale.h>
6 #include <time.h>
7 #include <string.h>
8 #include <isds.h>
9 #include "common.h"
12 int main(void) {
13 struct isds_ctx *ctx = NULL;
14 isds_error err;
15 struct isds_DbOwnerInfo *db_owner_info = NULL;
16 char *recipient = NULL;
18 setlocale(LC_ALL, "");
20 err = isds_init();
21 if (err) {
22 printf("isds_init() failed: %s\n", isds_strerror(err));
23 exit(EXIT_FAILURE);
26 isds_set_logging(ILF_ALL & ~ILF_HTTP, ILL_ALL);
28 ctx = isds_ctx_create();
29 if (!ctx) {
30 printf("isds_ctx_create() failed");
33 err = isds_set_timeout(ctx, 10000);
34 if (err) {
35 printf("isds_set_timeout() failed: %s\n", isds_strerror(err));
38 err = isds_login(ctx, url, username(), password(), NULL, NULL);
39 if (err) {
40 printf("isds_login() failed: %s: %s\n", isds_strerror(err),
41 isds_long_message(ctx));
42 } else {
43 printf("Logged in :)\n");
46 err = isds_ping(ctx);
47 if (err) {
48 printf("isds_ping() failed: %s\n", isds_strerror(err));
49 } else {
50 printf("isds_ping() succeeded\n");
53 printf("Sending bogus request\n");
54 err = isds_bogus_request(ctx);
55 if (err) {
56 printf("isds_bogus_request() failed: %s: %s\n", isds_strerror(err),
57 isds_long_message(ctx));
58 } else {
59 printf("isds_bogus_request() succeeded\n");
63 printf("Getting info about my box:\n");
64 err = isds_GetOwnerInfoFromLogin(ctx, &db_owner_info);
65 if (err) {
66 printf("isds_GetOwnerInfoFromLogin() failed: %s: %s\n",
67 isds_strerror(err), isds_long_message(ctx));
68 } else {
69 printf("isds_GetOwnerInfoFromLogin() succeeded\n");
71 print_DbOwnerInfo(db_owner_info);
76 /* Current server implementation (2009-11-17) does not allow to find
77 * myself. Previous version allowed it. */
78 struct isds_list *boxes = NULL, *item;
80 printf("Searching for my own box:\n");
81 err = isds_FindDataBox(ctx, db_owner_info, &boxes);
82 if (err == IE_SUCCESS || err == IE_2BIG) {
83 if (err == IE_2BIG)
84 printf("isds_FindDataBox() results truncated\n");
85 printf("isds_FindDataBox() succeeded:\n");
87 for(item = boxes; item; item = item->next) {
88 printf("List item:\n");
89 print_DbOwnerInfo(item->data);
91 } else {
92 printf("isds_FindDataBox() failed: %s: %s\n",
93 isds_strerror(err), isds_long_message(ctx));
96 isds_list_free(&boxes);
100 struct isds_list *boxes = NULL, *item;
101 struct isds_DbOwnerInfo *criteria = calloc(1, sizeof(*criteria));
102 if (!criteria) {
103 printf("Not enough memory for struct isds_DbOwnerInfo criteria\n");
104 exit(-1);
106 criteria->firmName = strdup("Obec");
107 if (!criteria->firmName) {
108 printf("Not enough memory for criteria->firmName\n");
109 exit(-1);
111 criteria->dbType = malloc(sizeof(*(criteria->dbType)));
112 if (!criteria->dbType) {
113 printf("Not enough memory for criteria->dbType\n");
114 exit(-1);
116 *(criteria->dbType) = DBTYPE_OVM;
118 printf("Searching box with firm name `%s':\n", criteria->firmName);
119 err = isds_FindDataBox(ctx, criteria, &boxes);
120 if (err == IE_SUCCESS || err == IE_2BIG) {
121 printf("isds_FindDataBox() succeeded:\n");
123 int n;
124 for(item = boxes, n = 1; item; item = item->next, n++) {
125 if (err != IE_2BIG) {
126 printf("List item #%d:\n", n);
127 print_DbOwnerInfo(item->data);
130 if (err == IE_2BIG)
131 printf("isds_FindDataBox() results truncated to %d boxes\n",
132 --n);
133 } else {
134 printf("isds_FindDataBox() failed: %s: %s\n",
135 isds_strerror(err), isds_long_message(ctx));
138 isds_list_free(&boxes);
139 isds_DbOwnerInfo_free(&criteria);
143 struct isds_list *boxes = NULL, *item;
144 struct isds_DbOwnerInfo criteria;
145 isds_DbType criteria_db_type = DBTYPE_OVM;
146 memset(&criteria, 0, sizeof(criteria));
147 criteria.dbType = &criteria_db_type;
148 criteria.dbID = "vqbab52";
150 printf("Searching for exact box by ID `%s' and type:\n", criteria.dbID);
151 err = isds_FindDataBox(ctx, &criteria, &boxes);
152 if (err == IE_SUCCESS || err == IE_2BIG) {
153 printf("isds_FindDataBox() succeeded:\n");
155 int n;
156 for(item = boxes, n = 1; item; item = item->next, n++) {
157 if (err != IE_2BIG) {
158 printf("List item #%d:\n", n);
159 print_DbOwnerInfo(item->data);
161 if (n == 1) recipient = strdup(
162 ((struct isds_DbOwnerInfo *)(item->data))->dbID);
164 if (err == IE_2BIG)
165 printf("isds_FindDataBox() results truncated to %d boxes\n",
166 --n);
167 } else {
168 printf("isds_FindDataBox() failed: %s: %s\n",
169 isds_strerror(err), isds_long_message(ctx));
172 isds_list_free(&boxes);
177 struct isds_list *boxes = NULL, *item;
178 struct isds_DbOwnerInfo criteria;
179 isds_DbType criteria_db_type = DBTYPE_OVM;
180 memset(&criteria, 0, sizeof(criteria));
181 criteria.firmName = "Místní";
182 criteria.dbType = &criteria_db_type;
184 printf("Searching box with firm name `%s':\n", criteria.firmName);
185 err = isds_FindDataBox(ctx, &criteria, &boxes);
186 if (err == IE_SUCCESS || err == IE_2BIG) {
187 printf("isds_FindDataBox() succeeded:\n");
189 int n;
190 for(item = boxes, n = 1; item; item = item->next, n++) {
191 if (err != IE_2BIG) {
192 printf("List item #%d:\n", n);
193 print_DbOwnerInfo(item->data);
195 if (n == 1) recipient = strdup(
196 ((struct isds_DbOwnerInfo *)(item->data))->dbID);
198 if (err == IE_2BIG)
199 printf("isds_FindDataBox() results truncated to %d boxes\n",
200 --n);
201 } else {
202 printf("isds_FindDataBox() failed: %s: %s\n",
203 isds_strerror(err), isds_long_message(ctx));
206 isds_list_free(&boxes);
210 if (db_owner_info) {
211 long int box_status = 0;
212 printf("Getting status of my box with ID `%s'\n", db_owner_info->dbID);
213 err = isds_CheckDataBox(ctx, db_owner_info->dbID, &box_status);
214 if (err)
215 printf("isds_CheckDataBox() failed: %s: %s\n",
216 isds_strerror(err), isds_long_message(ctx));
217 else {
218 printf("isds_CheckDataBox() succeeded: status = ");
219 print_DbState(box_status);
223 isds_DbOwnerInfo_free(&db_owner_info);
226 char *box_id = "7777777";
227 long int box_status = 0;
228 printf("Getting status of non existing box with ID `%s'\n", box_id);
229 err = isds_CheckDataBox(ctx, box_id, &box_status);
230 if (err)
231 printf("isds_CheckDataBox() failed: %s: %s\n",
232 isds_strerror(err), isds_long_message(ctx));
233 else {
234 printf("isds_CheckDataBox() succeeded: status = ");
235 print_DbState(box_status);
240 /* Send message */
242 struct isds_message message;
243 memset(&message, 0, sizeof(message));
245 struct isds_envelope envelope;
246 memset(&envelope, 0, sizeof(envelope));
247 message.envelope = &envelope;
248 envelope.dbIDRecipient = recipient;
249 long int dmSenderOrgUnitNum = 42;
250 envelope.dmSenderOrgUnitNum = &dmSenderOrgUnitNum;
251 envelope.dmAnnotation = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
252 /* "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
253 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
254 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
255 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
256 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
257 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
258 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
259 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
260 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
261 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
262 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
263 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
264 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
265 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
266 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
267 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
268 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
269 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
270 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"*/
271 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
273 struct isds_document minor_document;
274 memset(&minor_document, 0, sizeof(minor_document));
275 minor_document.data = "hello world?";
276 minor_document.data_length = strlen(minor_document.data);
277 minor_document.dmMimeType = "text/plain";
278 /* XXX: This should fail
279 minor_document.dmFileMetaType = FILEMETATYPE_MAIN; */
280 minor_document.dmFileMetaType = FILEMETATYPE_ENCLOSURE;
281 /* Server implementation demands dmFileDescr to be valid file name */
282 /*minor_document.dmFileDescr = "Standard text.txt";*/
283 minor_document.dmFileDescr = "minor_standard_text.txt";
284 minor_document.dmFileGuid = "2";
285 minor_document.dmUpFileGuid = "1";
287 struct isds_document main_document;
288 memset(&main_document, 0, sizeof(main_document));
289 main_document.data = "Hello World!";
290 main_document.data_length = strlen(main_document.data);
291 /* Server implementation sais text is not text file
292 * See <http://www.abclinuxu.cz/forum/show/284940> */
293 main_document.dmMimeType = "text/plain";
294 /* XXX: This should fail */
295 /*main_document.dmFileMetaType = FILEMETATYPE_ENCLOSURE;*/
296 main_document.dmFileMetaType = FILEMETATYPE_MAIN;
297 /* Server implementation demands dmFileDescr to be valid file name */
298 /*main_document.dmFileDescr = "Standard text.txt";*/
299 main_document.dmFileDescr = "standard_text.txt";
300 main_document.dmFileGuid = "1";
302 struct isds_list documents_main_item = {
303 .data = &main_document,
304 .next = NULL,
305 .destructor = NULL
307 struct isds_list documents_minor_item = {
308 .data = &minor_document,
309 .next = &documents_main_item,
310 .destructor = NULL
312 message.documents = &documents_minor_item;
315 printf("Sending message to box ID `%s'\n",
316 message.envelope->dbIDRecipient);
317 err = isds_send_message(ctx, &message);
318 if (err)
319 printf("isds_send_message() failed: %s: %s\n",
320 isds_strerror(err), isds_long_message(ctx));
321 else {
322 printf("isds_send_message() succeeded: assigned message ID = %s\n",
323 message.envelope->dmID);
326 free(message.envelope->dmID);
331 err = isds_logout(ctx);
332 if (err) {
333 printf("isds_logout() failed: %s\n", isds_strerror(err));
336 printf("Ping after logout should fail\n");
337 err = isds_ping(ctx);
338 if (err) {
339 printf("isds_ping() failed: %s\n", isds_strerror(err));
340 } else {
341 printf("isds_ping() succeeded\n");
344 printf("Sending bogus request after logout\n");
345 err = isds_bogus_request(ctx);
346 if (err) {
347 printf("isds_bogus_request() failed: %s: %s\n", isds_strerror(err),
348 isds_long_message(ctx));
349 } else {
350 printf("isds_bogus_request() succeeded\n");
353 err = isds_ctx_free(&ctx);
354 if (err) {
355 printf("isds_ctx_free() failed: %s\n", isds_strerror(err));
359 err = isds_cleanup();
360 if (err) {
361 printf("isds_cleanup() failed: %s\n", isds_strerror(err));
364 exit (EXIT_SUCCESS);