Generalize isds_FindDataBox()
[libisds.git] / client / sendmultiple.c
blob5a3d0a0da64537614e6d404b05d356fddfcc1718
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 char *recipient1 = NULL, *recipient2 = NULL;
17 setlocale(LC_ALL, "");
19 err = isds_init();
20 if (err) {
21 printf("isds_init() failed: %s\n", isds_strerror(err));
22 exit(EXIT_FAILURE);
25 isds_set_logging(ILF_ALL & ~ILF_HTTP, ILL_ALL);
27 ctx = isds_ctx_create();
28 if (!ctx) {
29 printf("isds_ctx_create() failed");
32 err = isds_set_timeout(ctx, 10000);
33 if (err) {
34 printf("isds_set_timeout() failed: %s\n", isds_strerror(err));
37 err = isds_login(ctx, url, username(), password(), NULL, NULL);
38 if (err) {
39 printf("isds_login() failed: %s: %s\n", isds_strerror(err),
40 isds_long_message(ctx));
41 } else {
42 printf("Logged in :)\n");
47 /* Find some recipients */
48 struct isds_list *boxes = NULL, *item;
49 struct isds_DbOwnerInfo criteria;
50 isds_DbType criteria_db_type = DBTYPE_OVM;
51 memset(&criteria, 0, sizeof(criteria));
52 criteria.firmName = "Místní";
53 criteria.dbType = &criteria_db_type;
55 printf("Searching box with firm name `%s':\n", criteria.firmName);
56 err = isds_FindDataBox(ctx, &criteria, &boxes);
57 if (err == IE_SUCCESS || err == IE_2BIG) {
58 printf("isds_FindDataBox() succeeded:\n");
60 int n;
61 for(item = boxes, n = 1; item; item = item->next, n++) {
62 if (err != IE_2BIG) {
63 printf("List item #%d:\n", n);
64 print_DbOwnerInfo(item->data);
66 if (n == 1) recipient1 = strdup(
67 ((struct isds_DbOwnerInfo *)(item->data))->dbID);
68 if (n == 2) recipient2 = strdup(
69 ((struct isds_DbOwnerInfo *)(item->data))->dbID);
71 if (err == IE_2BIG)
72 printf("isds_FindDataBox() results truncated to %d boxes\n",
73 --n);
74 } else {
75 printf("isds_FindDataBox() failed: %s: %s\n",
76 isds_strerror(err), isds_long_message(ctx));
79 isds_list_free(&boxes);
84 /* Send one message to more recipients */
85 struct isds_message message;
86 memset(&message, 0, sizeof(message));
88 struct isds_envelope envelope;
89 memset(&envelope, 0, sizeof(envelope));
90 message.envelope = &envelope;
91 long int dmSenderOrgUnitNum = 42;
92 envelope.dmSenderOrgUnitNum = &dmSenderOrgUnitNum;
93 envelope.dmAnnotation = "Multiple recipients";
95 struct isds_document minor_document;
96 memset(&minor_document, 0, sizeof(minor_document));
97 minor_document.data = "hello world?";
98 minor_document.data_length = strlen(minor_document.data);
99 minor_document.dmMimeType = "text/plain";
100 minor_document.dmFileMetaType = FILEMETATYPE_ENCLOSURE;
101 minor_document.dmFileDescr = "minor_standard_text.txt";
102 minor_document.dmFileGuid = "2";
103 minor_document.dmUpFileGuid = "1";
105 struct isds_document main_document;
106 memset(&main_document, 0, sizeof(main_document));
107 main_document.data = "Hello World!";
108 main_document.data_length = strlen(main_document.data);
109 main_document.dmMimeType = "text/plain";
110 main_document.dmFileMetaType = FILEMETATYPE_MAIN;
111 main_document.dmFileDescr = "standard_text.txt";
112 main_document.dmFileGuid = "1";
114 struct isds_list documents_main_item = {
115 .data = &main_document,
116 .next = NULL,
117 .destructor = NULL
119 struct isds_list documents_minor_item = {
120 .data = &minor_document,
121 .next = &documents_main_item,
122 .destructor = NULL
124 message.documents = &documents_minor_item;
126 /* Build recipients list */
127 struct isds_list *copies = NULL;
128 copies = calloc(1, sizeof(*copies));
129 copies->destructor = (void (*)(void**)) isds_message_copy_free;
130 copies->data = calloc(1, sizeof(struct isds_message_copy));
131 ((struct isds_message_copy*) copies->data)->dbIDRecipient = recipient1;
133 copies->next = calloc(1, sizeof(*copies->next));
134 copies->next->destructor = (void (*)(void**)) isds_message_copy_free;
135 copies->next->data = calloc(1, sizeof(struct isds_message_copy));
136 ((struct isds_message_copy*)copies->next->data)->dbIDRecipient =
137 recipient2;
140 printf("Sending message to box ID `%s' and box ID `%s'\n",
141 recipient1, recipient2);
142 err = isds_send_message_to_multiple_recipients(ctx, &message, copies);
144 if (err == IE_SUCCESS || err == IE_PARTIAL_SUCCESS) {
145 if (err == IE_SUCCESS){
146 printf("isds_send_message_to_multiple_recipients() succeeded "
147 "cempletely:\n");
149 if (err == IE_PARTIAL_SUCCESS){
150 printf("isds_send_message_to_multiple_recipients() succeeded "
151 "partialy:\n");
153 print_copies(copies);
154 } else
155 printf("isds_send_message_to_multiple_recipients() failed: "
156 "%s: %s\n", isds_strerror(err), isds_long_message(ctx));
158 isds_list_free(&copies);
163 err = isds_logout(ctx);
164 if (err) {
165 printf("isds_logout() failed: %s\n", isds_strerror(err));
169 err = isds_ctx_free(&ctx);
170 if (err) {
171 printf("isds_ctx_free() failed: %s\n", isds_strerror(err));
175 err = isds_cleanup();
176 if (err) {
177 printf("isds_cleanup() failed: %s\n", isds_strerror(err));
180 exit (EXIT_SUCCESS);