imaptest.c (5682B)
1 /* 2 * Copy me if you can. 3 * by 20h 4 * 5 * % make imaptest 6 * % ./imaptest user pass 'tcps!host!993' 7 */ 8 9 #include <unistd.h> 10 #include <stdlib.h> 11 #include <stdio.h> 12 #include <string.h> 13 14 #include "ind.h" 15 #include "llist.h" 16 #include "imap.h" 17 #include "flag.h" 18 #include "mime.h" 19 20 int 21 main(int argc, char *argv[]) 22 { 23 imap_t *imap; 24 llist_t *folders, *idsl, *flagl, *flag, *bodies; 25 llistelem_t *elem, *status; 26 mime_t *mime; 27 char *ids; 28 int i; 29 30 if (argc < 4) { 31 fprintf(stderr, "usage: %s user pass netspec\n", argv[0]); 32 return 1; 33 } 34 35 imap = imap_new(argv[3], argv[1], argv[2]); 36 37 if (imap_init(imap)) 38 imap_die(imap, "imap_init"); 39 40 folders = imap_listfolders(imap); 41 if (folders == NULL) 42 imap_die(imap, "imap_listfolders"); 43 printf("folders:\n"); 44 forllist(folders, elem) { 45 printf("%s", elem->key); 46 if (elem->next != NULL) 47 printf(", "); 48 } 49 printf("\n\n"); 50 llist_efree(folders); 51 52 folders = imap_subscribed(imap); 53 if (folders == NULL) 54 imap_die(imap, "imap_subscribed"); 55 printf("subscribed:\n"); 56 forllist(folders, elem) { 57 printf("%s", elem->key); 58 if (elem->next != NULL) 59 printf(", "); 60 } 61 printf("\n\n"); 62 llist_efree(folders); 63 64 printf("Creating 'testingthis' folder.\n"); 65 if (imap_createfolder(imap, "testingthis")) 66 imap_die(imap, "imap_createfolder"); 67 printf("Subscribing to 'testingthis'.\n"); 68 if (imap_subscribe(imap, "testingthis")) 69 imap_die(imap, "imap_subscribe"); 70 printf("Unsubscribing from 'testingthis'.\n"); 71 if (imap_unsubscribe(imap, "testingthis")) 72 imap_die(imap, "imap_subscribe"); 73 printf("Renaming it to 'testingthis2'.\n"); 74 if (imap_renamefolder(imap, "testingthis", "testingthis2")) 75 imap_die(imap, "imap_renamefolder"); 76 printf("Deleting 'testingthis2'.\n"); 77 if (imap_deletefolder(imap, "testingthis2")) 78 imap_die(imap, "imap_deletefolder"); 79 80 printf("\nSelecting folder 'INBOX'\n"); 81 if (imap_select(imap, "INBOX")) 82 imap_die(imap, "imap_select"); 83 printf("Searching for all seen messages.\n"); 84 folders = imap_search(imap, "SEEN"); 85 if (folders == NULL) 86 imap_die(imap, "imap_search"); 87 printf("result:\n"); 88 forllist(folders, elem) { 89 printf("%s", elem->key); 90 if (elem->next != NULL) 91 printf(", "); 92 } 93 printf("\n\n"); 94 95 printf("Adding test ids 6789, 6790, 6791 and 5020.\n"); 96 llist_add(folders, "6789", NULL, 0); 97 llist_add(folders, "6790", NULL, 0); 98 llist_add(folders, "6791", NULL, 0); 99 llist_add(folders, "5020", NULL, 0); 100 ids = imap_ids2str(folders); 101 llist_efree(folders); 102 printf("Sequence in short id form: %s\n", ids); 103 free(ids); 104 105 folders = imap_statuses(imap); 106 if (folders == NULL) 107 imap_die(imap, "imap_statuses"); 108 109 printf("\nstatus:\n"); 110 forllist(folders, status) { 111 if (status->key != NULL || status->data == NULL) 112 continue; 113 elem = llist_get((llist_t *)status->data, "mb"); 114 printf("%s:\n", (char *)elem->data); 115 llist_delelem((llist_t *)status->data, elem); 116 117 printf("\t"); 118 forllist((llist_t *)status->data, elem) { 119 printf("%s = %s", elem->key, (char *)elem->data); 120 if (elem->next != NULL) 121 printf(", "); 122 } 123 printf("\n"); 124 } 125 llist_efree(folders); 126 127 idsl = imap_str2ids("INBOX", "15:34 64 c c+2"); 128 flagl = flag_sanitize("seen flagged"); 129 printf("\nSetting 'seen' and 'flagged' on 15:34 64 c c+2." 130 " If no 'cur' is set, 'c' will be 0.\n"); 131 if (imap_setflags(imap, idsl, flagl)) 132 imap_die(imap, "imap_setflags"); 133 134 folders = imap_getflags(imap, idsl); 135 if (folders == NULL) 136 imap_die(imap, "imap_getflags"); 137 printf("Flags:\n"); 138 forllist(folders, elem) { 139 flag = (llist_t *)elem->data; 140 ids = llist_joinstr((llist_t *)flag->last->data, " "); 141 printf("%10s\t%20s\n", (char *)flag->first->data, ids); 142 free(ids); 143 } 144 llist_efree(folders); 145 146 printf("Unsetting 'seen' and 'flagged' on 15:34 64 c c+2.\n"); 147 if (imap_delflags(imap, idsl, flagl)) 148 imap_die(imap, "imap_delflags"); 149 llist_efree(flagl); 150 151 printf("Setting them as 'seen' again.\n"); 152 flagl = flag_sanitize("seen"); 153 if (imap_setflags(imap, idsl, flagl)) 154 imap_die(imap, "imap_setflags"); 155 llist_efree(flagl); 156 llist_efree(idsl); 157 158 printf("\nGetting (BODY) of message 3.\n"); 159 idsl = imap_str2ids("INBOX", "3"); 160 bodies = imap_fetchbody(imap, idsl); 161 llist_efree(idsl); 162 if (bodies == NULL) 163 imap_die(imap, "imap_fetchpart"); 164 llist_eprint(bodies); 165 llist_efree(bodies); 166 167 printf("\nGetting part 1 of message 3.\n"); 168 idsl = imap_str2ids("INBOX", "3"); 169 bodies = imap_fetchpart(imap, idsl, "1"); 170 llist_efree(idsl); 171 if (bodies == NULL) 172 imap_die(imap, "imap_fetchpart"); 173 llist_eprint(bodies); 174 llist_efree(bodies); 175 176 printf("\nGetting headers of message 3.\n"); 177 idsl = imap_str2ids("INBOX", "3"); 178 bodies = imap_fetchheaders(imap, idsl); 179 llist_efree(idsl); 180 if (bodies == NULL) 181 imap_die(imap, "imap_fetchpart"); 182 llist_eprint(bodies); 183 llist_efree(bodies); 184 185 printf("\nGetting raw message 3.\n"); 186 idsl = imap_str2ids("INBOX", "3"); 187 bodies = imap_fetchraw(imap, idsl); 188 llist_efree(idsl); 189 if (bodies == NULL) 190 imap_die(imap, "imap_fetchpart"); 191 llist_eprint(bodies); 192 elem = llist_get((llist_t *)bodies->first->data, "literal"); 193 if (elem != NULL) { 194 mime = mime_parsestr((char *)elem->data, elem->datalen); 195 if (mime != NULL) { 196 mime_print(mime); 197 mime_free(mime); 198 } 199 } 200 llist_efree(bodies); 201 202 printf("Getting body structure of message 3.\n"); 203 idsl = imap_str2ids("INBOX", "3"); 204 bodies = imap_fetchbody(imap, idsl); 205 llist_efree(idsl); 206 if (bodies == NULL) 207 imap_die(imap, "imap_fetchbody"); 208 llist_eprint(bodies); 209 llist_efree(bodies); 210 211 printf("\nNoop.\n"); 212 if (imap_noop(imap)) 213 imap_die(imap, "imap_noop"); 214 printf("Logout\n"); 215 if (imap_logout(imap)) 216 imap_die(imap, "imap_logout"); 217 printf("All tests done.\n"); 218 219 imap_close(imap); 220 imap_free(imap); 221 222 return 0; 223 } 224