folder.c - rohrpost - A commandline mail client to change the world as we see it.
(HTM) git clone git://r-36.net/rohrpost
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) README
(DIR) LICENSE
---
folder.c (3486B)
---
1 /*
2 * Copy me if you can.
3 * by 20h
4 */
5
6 #include <unistd.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9
10 #include "ind.h"
11 #include "arg.h"
12 #include "cfg.h"
13 #include "llist.h"
14 #include "folder.h"
15 #include "imap.h"
16
17 void
18 folderusage(char *argv0)
19 {
20 die("usage: %s [-qh] [-c cfg] [-l [folder|searchterm]|-n folder|"
21 "-d folder|-s [folder]|-u folder|-r old new]\n",
22 argv0);
23 }
24
25 int
26 foldermain(int argc, char *argv[])
27 {
28 config_t *cfg;
29 imap_t *imap;
30 int status;
31 char *user, *pass, *netspec, *cfgn, *argv0;
32 llist_t *folders, *results;
33 llistelem_t *elem;
34
35 enum {
36 BEQUIET = 0x01,
37 DOLIST = 0x02,
38 DOCREATE = 0x04,
39 DODELETE = 0x08,
40 DOSUBSCRIBE = 0x10,
41 DOUNSUBSCRIBE = 0x20,
42 DORENAME = 0x40,
43
44 NOARGS = 0x80
45 };
46
47 status = 0;
48 cfgn = NULL;
49
50 ARGBEGIN(argv0) {
51 case 'c':
52 cfgn = EARGF(folderusage(argv0));
53 break;
54 case 'd':
55 status |= DODELETE;
56 break;
57 case 'l':
58 status |= DOLIST;
59 break;
60 case 'n':
61 status |= DOCREATE;
62 break;
63 case 'q':
64 status |= BEQUIET;
65 break;
66 case 'r':
67 status |= DORENAME;
68 break;
69 case 's':
70 status |= DOSUBSCRIBE;
71 break;
72 case 'u':
73 status |= DOUNSUBSCRIBE;
74 break;
75 default:
76 folderusage(argv0);
77 } ARGEND;
78
79 cfg = config_init(cfgn);
80 user = (config_checkget(cfg, "imapuser"))->data;
81 pass = (config_checkget(cfg, "imappass"))->data;
82 netspec = (config_checkget(cfg, "imapnet"))->data;
83 imap = imap_new(netspec, user, pass);
84 config_free(cfg);
85
86 if (imap_init(imap))
87 imap_die(imap, "imap_init");
88
89 if (status & DOLIST) {
90 folders = imap_listfolders(imap);
91 if (folders == NULL)
92 imap_die(imap, "imap_listfolders");
93 llist_sort(folders);
94
95 if (argc > 0) {
96 results = llist_find(folders, argv[0]);
97 if (results == NULL) {
98 if (!(status & BEQUIET))
99 printf("No matching folder found.\n");
100
101 llist_free(folders);
102 imap_close(imap);
103 imap_free(imap);
104 return 1;
105 }
106 } else {
107 results = folders;
108 }
109
110 forllist(results, elem) {
111 if (!(status & BEQUIET))
112 printf("%s\n", elem->key);
113 }
114 llist_free(folders);
115 if (argc > 0)
116 llist_free(results);
117 }
118 if (status & DOCREATE) {
119 if (argc < 1)
120 folderusage(argv0);
121
122 if (imap_createfolder(imap, argv[0]))
123 imap_die(imap, "imap_createfolder");
124 imap_subscribe(imap, argv[0]);
125 if (!(status & BEQUIET))
126 printf("Folder %s created.\n", argv[0]);
127 }
128 if (status & DODELETE) {
129 if (argc < 1)
130 folderusage(argv0);
131
132 imap_unsubscribe(imap, argv[0]);
133 if (imap_deletefolder(imap, argv[0]))
134 imap_die(imap, "imap_deletefolder");
135 if (!(status & BEQUIET))
136 printf("Folder %s deleted.\n", argv[0]);
137 }
138 if (status & DOSUBSCRIBE) {
139 if (argc < 1) {
140 folders = imap_subscribed(imap);
141 if (folders == NULL)
142 imap_die(imap, "imap_subscribed");
143
144 forllist(folders, elem)
145 printf("%s\n", elem->key);
146 llist_free(folders);
147 } else {
148 if (imap_subscribe(imap, argv[0]))
149 imap_die(imap, "imap_subscribe");
150 if (!(status & BEQUIET))
151 printf("Subscribed to folder %s.\n", argv[0]);
152 }
153 }
154 if (status & DOUNSUBSCRIBE) {
155 if (argc < 1)
156 folderusage(argv0);
157 if (imap_unsubscribe(imap, argv[0]))
158 imap_die(imap, "imap_unsubscribe");
159 if (!(status & BEQUIET))
160 printf("Unsubscribed from folder %s.\n", argv[0]);
161 }
162 if (status & DORENAME) {
163 if (argc < 2)
164 folderusage(argv0);
165 if (imap_renamefolder(imap, argv[0], argv[1]))
166 imap_die(imap, "imap_renamefolder");
167 if (!(status & BEQUIET))
168 printf("Folder %s renamed to %s.\n", argv[0], argv[1]);
169 }
170
171 imap_close(imap);
172 imap_free(imap);
173 return 0;
174 }
175