doc: Update maximum message size
[libisds.git] / client / getreceived.c
blob9e1e3995f66a20af8b0f955e3b5c9cf12f6314e0
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 *last_message_id = 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 /* Get list of received messages */
49 struct tm from_time_tm = {
50 .tm_year = 2000 - 1900,
51 .tm_mon = 1 - 1,
52 .tm_mday = 1,
53 .tm_hour = 1,
54 .tm_min = 2,
55 .tm_sec = 3,
56 .tm_isdst = -1
58 time_t from_time_t = mktime(&from_time_tm);
59 struct timeval from_time = {
60 .tv_sec = from_time_t,
61 .tv_usec = 4000
63 unsigned long int number = 0;
64 struct isds_list *messages = NULL, *item;
65 struct isds_message *last_message = NULL;
67 /* TODO: Try different criteria */
68 printf("Getting list of received messages\n");
69 err = isds_get_list_of_received_messages(ctx, &from_time, NULL, NULL,
70 MESSAGESTATE_ANY, 0, &number, &messages);
71 if (err)
72 printf("isds_get_list_of_received_messages() failed: %s: %s\n",
73 isds_strerror(err), isds_long_message(ctx));
74 else {
75 printf("isds_get_list_of_received_messages() succeeded: "
76 "number of messages = %lu:\n", number);
77 for(item = messages; item; item = item->next) {
78 printf("List item:\n");
79 print_message(item->data);
80 last_message = (struct isds_message *) (item->data);
85 if (last_message) {
86 /*Save last message for latter refference */
87 if (last_message->envelope && last_message->envelope->dmID) {
88 last_message_id = strdup(last_message->envelope->dmID);
92 isds_list_free(&messages);
96 /* Download last message */
97 if (last_message_id) {
98 struct isds_message *message = NULL;
100 printf("Getting last received message with ID: %s\n", last_message_id);
101 err = isds_get_received_message(ctx, last_message_id, &message);
102 if (err)
103 printf("isds_get_received_message() failed: %s: %s\n",
104 isds_strerror(err), isds_long_message(ctx));
105 else {
106 printf("isds_get_received_message() succeeded:\n");
107 print_message(message);
108 save_data("Saving message", message->raw, message->raw_length);
111 isds_message_free(&message);
112 free(last_message_id);
116 /* Download message with invalid ID*/
118 struct isds_message *message = NULL;
119 char *id = "123456789112345678921";
121 printf("Getting received message with invalid ID: %s\n", id);
122 err = isds_get_received_message(ctx, id, &message);
123 if (err)
124 printf("isds_get_received_message() failed as assumed: %s: %s\n",
125 isds_strerror(err), isds_long_message(ctx));
126 else {
127 printf("isds_get_received_message() succeeded. "
128 "This should not happen:\n");
129 print_message(message);
132 isds_message_free(&message);
136 /* Download nonexistent message */
138 struct isds_message *message = NULL;
139 char *id = "7777777";
141 printf("Getting nonexistent received message with ID: %s\n", id);
142 err = isds_get_received_message(ctx, id, &message);
143 if (err)
144 printf("isds_get_received_message() failed as assumed: %s: %s\n",
145 isds_strerror(err), isds_long_message(ctx));
146 else {
147 printf("isds_get_received_message() succeeded. "
148 "This should not happen:\n");
149 print_message(message);
152 isds_message_free(&message);
156 err = isds_logout(ctx);
157 if (err) {
158 printf("isds_logout() failed: %s\n", isds_strerror(err));
162 err = isds_ctx_free(&ctx);
163 if (err) {
164 printf("isds_ctx_free() failed: %s\n", isds_strerror(err));
168 err = isds_cleanup();
169 if (err) {
170 printf("isds_cleanup() failed: %s\n", isds_strerror(err));
173 exit (EXIT_SUCCESS);