Export isds_PersonName_free(), isds_BirthInfo_free(), isds_Address_free()
[libisds.git] / client / markasread.c
blob2ee8cbbeca81095b53ecfe840dd0397eb32f8015
1 #define _XOPEN_SOURCE 500
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <locale.h>
5 #include <time.h>
6 #include <string.h>
7 #include <isds.h>
8 #include "common.h"
11 int main(int argc, char **argv) {
12 struct isds_ctx *ctx = NULL;
13 isds_error err;
14 char *last_message_id = NULL;
16 setlocale(LC_ALL, "");
18 err = isds_init();
19 if (err) {
20 printf("isds_init() failed: %s\n", isds_strerror(err));
21 exit(EXIT_FAILURE);
24 isds_set_logging(ILF_ALL & ~ILF_HTTP, ILL_ALL);
26 ctx = isds_ctx_create();
27 if (!ctx) {
28 printf("isds_ctx_create() failed");
31 err = isds_set_timeout(ctx, 10000);
32 if (err) {
33 printf("isds_set_timeout() failed: %s\n", isds_strerror(err));
36 err = isds_login(ctx, url, username(), password(), NULL, NULL);
37 if (err) {
38 printf("isds_login() failed: %s: %s\n", isds_strerror(err),
39 isds_long_message(ctx));
40 } else {
41 printf("Logged in :)\n");
46 /* Get list of received unread messages */
48 struct tm from_time_tm = {
49 .tm_year = 2000 - 1900,
50 .tm_mon = 1 - 1,
51 .tm_mday = 1,
52 .tm_hour = 1,
53 .tm_min = 2,
54 .tm_sec = 3,
55 .tm_isdst = -1
57 time_t from_time_t = mktime(&from_time_tm);
58 struct timeval from_time = {
59 .tv_sec = from_time_t,
60 .tv_usec = 4000
62 unsigned long int number = 0;
63 struct isds_list *messages = NULL, *item;
64 struct isds_message *last_message = NULL;
66 /* TODO: Try different criteria */
67 printf("Getting list of unread received messages\n");
68 err = isds_get_list_of_received_messages(ctx, &from_time, NULL, NULL,
69 MESSAGESTATE_ANY & ~MESSAGESTATE_READ, 0, &number, &messages);
70 if (err)
71 printf("isds_get_list_of_received_messages() failed: %s: %s\n",
72 isds_strerror(err), isds_long_message(ctx));
73 else {
74 printf("isds_get_list_of_received_messages() succeeded: "
75 "number of messages = %lu\n", number);
76 for(item = messages; item; item = item->next) {
77 last_message = (struct isds_message *) (item->data);
82 if (last_message) {
83 /*Save last message for latter refference */
84 if (last_message->envelope && last_message->envelope->dmID) {
85 last_message_id = strdup(last_message->envelope->dmID);
89 isds_list_free(&messages);
93 /* Mark last message as read */
94 if (last_message_id) {
95 struct isds_message *message = NULL;
97 printf("Marking as read last received message with ID: %s\n",
98 last_message_id);
99 err = isds_mark_message_read(ctx, last_message_id);
100 if (err)
101 printf("isds_mark_message_read() failed: %s: %s\n",
102 isds_strerror(err), isds_long_message(ctx));
103 else {
104 printf("isds_mark_message_read() succeeded:\n");
105 print_message(message);
108 isds_message_free(&message);
109 free(last_message_id);
113 /* Mark as read message with invalid ID*/
115 struct isds_message *message = NULL;
116 char *id = "123456789112345678921";
118 printf("Marking message as read with invalid ID: %s\n", id);
119 err = isds_mark_message_read(ctx, id);
120 if (err)
121 printf("isds_mark_message_read() failed as assumed: %s: %s\n",
122 isds_strerror(err), isds_long_message(ctx));
123 else {
124 printf("isds_mark_message_read() succeeded. "
125 "This should not happen:\n");
126 print_message(message);
129 isds_message_free(&message);
133 /* Mark nonexistent message as read */
135 struct isds_message *message = NULL;
136 char *id = "7777777";
138 printf("Marking as read nonexistent message with ID: %s\n", id);
139 err = isds_mark_message_read(ctx, id);
140 if (err)
141 printf("isds_mark_message_read() failed as assumed: %s: %s\n",
142 isds_strerror(err), isds_long_message(ctx));
143 else {
144 printf("isds_mark_message_read() succeeded. "
145 "This should not happen:\n");
146 print_message(message);
149 isds_message_free(&message);
153 err = isds_logout(ctx);
154 if (err) {
155 printf("isds_logout() failed: %s\n", isds_strerror(err));
159 err = isds_ctx_free(&ctx);
160 if (err) {
161 printf("isds_ctx_free() failed: %s\n", isds_strerror(err));
165 err = isds_cleanup();
166 if (err) {
167 printf("isds_cleanup() failed: %s\n", isds_strerror(err));
170 exit (EXIT_SUCCESS);