1 |
#ifndef DATA_H |
2 |
#define DATA_H |
3 |
|
4 |
#include <sys/types.h> |
5 |
#include <regex.h> |
6 |
|
7 |
/* String lengths of account settings. */ |
8 |
#define SERVER_LEN (635 + 1) /* Section 2.5 RFC 1123. */ |
9 |
#define USERNAME_LEN 128 |
10 |
#define PASSWORD_LEN 128 |
11 |
|
12 |
/* String lengths of master password and encrypted passwords. */ |
13 |
#define PASSPHRASE_LEN 256 |
14 |
#define ENCRYPTED_PASSWORD_LEN 128 |
15 |
|
16 |
/* Attributes of a password. */ |
17 |
#define PASSWORD_NONE 0 |
18 |
#define PASSWORD_PLAIN 1 |
19 |
#define PASSWORD_ENCRYPTED 2 |
20 |
|
21 |
/* Secure Socket Layer protocols. */ |
22 |
#define SSL_DISABLED 0 |
23 |
#define SSL_SSL_V2 1 |
24 |
#define SSL_SSL_V3 2 |
25 |
#define SSL_TLS_V1 3 |
26 |
|
27 |
/* String length of keys (aliases). */ |
28 |
#define KEY_LEN 32 |
29 |
|
30 |
/* String length of action's arguments. */ |
31 |
#define ARGS_LEN 256 |
32 |
|
33 |
/* String length of mailbox's name. */ |
34 |
#define MBOX_NAME_LEN 128 |
35 |
|
36 |
/* String length of mask's body. */ |
37 |
#define MASK_BODY_LEN 256 |
38 |
|
39 |
/* Maximum filters assigned to a mailbox. */ |
40 |
#define MBOX_FILTERS_MAX 64 |
41 |
|
42 |
/* Maximum mailboxes contained in a mailbox-group. */ |
43 |
#define MBOXGRP_MBOXES_MAX 64 |
44 |
|
45 |
/* |
46 |
* Mode for masks that comprise a filter. So if a filter is of AND type then |
47 |
* as default masks are ANDed. |
48 |
*/ |
49 |
#define FILTER_MODE_OR 1 |
50 |
#define FILTER_MODE_AND 2 |
51 |
|
52 |
/* Action to do in case of filter matching. */ |
53 |
#define FILTER_ACTION_DELETE 1 |
54 |
#define FILTER_ACTION_COPY 2 |
55 |
#define FILTER_ACTION_MOVE 3 |
56 |
#define FILTER_ACTION_RCOPY 4 |
57 |
#define FILTER_ACTION_RMOVE 5 |
58 |
#define FILTER_ACTION_FLAG_REPLACE 6 |
59 |
#define FILTER_ACTION_FLAG_ADD 7 |
60 |
#define FILTER_ACTION_FLAG_REMOVE 8 |
61 |
#define FILTER_ACTION_LIST 9 |
62 |
|
63 |
/* Message flags to replace/add/remove. */ |
64 |
#define MESSAGE_FLAG_NONE 0x00 |
65 |
#define MESSAGE_FLAG_SEEN 0x01 |
66 |
#define MESSAGE_FLAG_ANSWERED 0x02 |
67 |
#define MESSAGE_FLAG_FLAGGED 0x04 |
68 |
#define MESSAGE_FLAG_DELETED 0x08 |
69 |
#define MESSAGE_FLAG_DRAFT 0x10 |
70 |
|
71 |
/* Type of mask. This overrides the default filter mode (AND/OR). */ |
72 |
#define MASK_TYPE_OR 1 |
73 |
#define MASK_TYPE_AND 2 |
74 |
|
75 |
/* Type of mask match according to the four different regular expressions. */ |
76 |
#define MASK_MATCH_1 1 |
77 |
#define MASK_MATCH_2 2 |
78 |
#define MASK_MATCH_3 3 |
79 |
#define MASK_MATCH_4 4 |
80 |
|
81 |
|
82 |
/* |
83 |
* Find last node of linked list and append at the end of the list the |
84 |
* supplied node. |
85 |
*/ |
86 |
#define APPEND_LINKED_LIST(f, n, p, a) \ |
87 |
a = &f; \ |
88 |
p = f; \ |
89 |
while (p) { \ |
90 |
a = &(p->next); \ |
91 |
p = p->next; \ |
92 |
} \ |
93 |
*a = n; |
94 |
|
95 |
/* |
96 |
* Find, based on the key of the supplied node, the apropriate position for it |
97 |
* in the tree, and then insert it there. |
98 |
*/ |
99 |
#define INSERT_TREE(r, n, p, i, c) \ |
100 |
i = &r; \ |
101 |
p = r; \ |
102 |
while (p) { \ |
103 |
c = strncmp(n->key, p->key, KEY_LEN); \ |
104 |
if (c < 0) { \ |
105 |
i = &(p->left); \ |
106 |
p = p->left; \ |
107 |
} else if (c > 0) { \ |
108 |
i = &(p->right); \ |
109 |
p = p->right; \ |
110 |
} else \ |
111 |
return; \ |
112 |
} \ |
113 |
*i = n; |
114 |
|
115 |
/* |
116 |
* Find, based on the supplied key, the position of a node, and return a |
117 |
* pointer directing to that node. |
118 |
*/ |
119 |
#define FIND_TREE(r, k, p, c) \ |
120 |
p = r; \ |
121 |
while (p) { \ |
122 |
c = strncmp(k, p->key, KEY_LEN); \ |
123 |
if (c < 0) \ |
124 |
p = p->left; \ |
125 |
else if (c > 0) \ |
126 |
p = p->right; \ |
127 |
else \ |
128 |
return p; \ |
129 |
} \ |
130 |
return NULL; |
131 |
|
132 |
#define create_node(A) xmalloc(A) |
133 |
|
134 |
|
135 |
/* Account. */ |
136 |
typedef struct account { |
137 |
char key[KEY_LEN]; /* Alias of account. */ |
138 |
char server[SERVER_LEN];/* Hostname of mail server. */ |
139 |
unsigned short int port;/* Port to connect. */ |
140 |
|
141 |
char username[USERNAME_LEN]; /* Username. */ |
142 |
char *password; /* Password. */ |
143 |
int passwdattr; /* Password attributes. */ |
144 |
unsigned int ssl; /* Secure Socket Layer support. */ |
145 |
struct mbox *mboxes; /* Mailboxes. */ |
146 |
struct account *next; /* Next node of linked list. */ |
147 |
} account_t; |
148 |
|
149 |
/* Mailbox. */ |
150 |
typedef struct mbox { |
151 |
char name[MBOX_NAME_LEN]; /* Name of mailbox. */ |
152 |
struct filter *filters[MBOX_FILTERS_MAX]; /* Filters to be |
153 |
* applied. */ |
154 |
struct mbox *next; /* Next node of linked list. */ |
155 |
} mbox_t; |
156 |
|
157 |
/* Group of mailboxes. */ |
158 |
typedef struct mboxgrp { |
159 |
char key[KEY_LEN]; /* Alias of mailbox group. */ |
160 |
struct mbox *mboxes[MBOXGRP_MBOXES_MAX]; /* Mailboxes of group. */ |
161 |
struct mboxgrp *left, *right; /* Left/right nodes of tree. */ |
162 |
} mboxgrp_t; |
163 |
|
164 |
/* Filter. */ |
165 |
typedef struct filter { |
166 |
char key[KEY_LEN]; /* Alias of filter. */ |
167 |
unsigned int mode; /* AND/OR mode. */ |
168 |
|
169 |
struct { /* What to do on filter match. */ |
170 |
unsigned int type; /* Action. */ |
171 |
account_t *raccount; /* Remote account to rcopy/rmove |
172 |
* message. */ |
173 |
char destmbox[MBOX_NAME_LEN]; /* Destination mailbox. */ |
174 |
unsigned int msgflags; /* Message flags to |
175 |
* replace/add/remove. */ |
176 |
char args[ARGS_LEN]; /* Action's arguments. */ |
177 |
} action; |
178 |
|
179 |
struct mask *masks; /* Masks comprising the filter. */ |
180 |
unsigned int masknum; /* Total number of masks. */ |
181 |
unsigned int masklen; /* Total length of body of masks. */ |
182 |
|
183 |
struct filter *left, *right; /* Left/right node of tree. */ |
184 |
} filter_t; |
185 |
|
186 |
/* Mask. */ |
187 |
typedef struct mask { |
188 |
char body[MASK_BODY_LEN]; /* Body of mask. */ |
189 |
unsigned int type; /* AND/OR type. */ |
190 |
struct mask *next; /* Next node of linked list. */ |
191 |
} mask_t; |
192 |
|
193 |
|
194 |
/* data.c */ |
195 |
void init_account(account_t * node); |
196 |
void append_account(account_t * node); |
197 |
int set_account(char *line, regmatch_t * match); |
198 |
|
199 |
#ifdef ENCRYPTED_PASSWORDS |
200 |
char *find_password(char *user, char *serv); |
201 |
|
202 |
#endif |
203 |
|
204 |
void init_mboxgrp(mboxgrp_t * node); |
205 |
void insert_mboxgrp(mboxgrp_t * node); |
206 |
int set_mboxgrp(char *line, regmatch_t * match); |
207 |
void process_mboxgrp(mboxgrp_t * node, char *mboxs); |
208 |
mboxgrp_t *find_mboxgrp(char *key); |
209 |
|
210 |
void init_mbox(mbox_t * node); |
211 |
void append_mbox(mbox_t * node); |
212 |
mbox_t *set_mbox(char *name); |
213 |
|
214 |
void init_filter(filter_t * node); |
215 |
void insert_filter(filter_t * node); |
216 |
int set_filter(char *line, regmatch_t * match); |
217 |
filter_t *find_filter(char *key); |
218 |
int set_action(char *line, regmatch_t * match); |
219 |
|
220 |
void init_mask(mask_t * node); |
221 |
void append_mask(mask_t * node); |
222 |
int set_mask(char *line, regmatch_t * match, int mmt); |
223 |
void convert_date(mask_t * node); |
224 |
|
225 |
int set_job(char *line, regmatch_t * match); |
226 |
void link_mbox_filter(filter_t * cf, mboxgrp_t * cg); |
227 |
|
228 |
void destroy_data(void); |
229 |
void destroy_mboxgrps(mboxgrp_t * node); |
230 |
void destroy_mboxs(mbox_t * node); |
231 |
void destroy_accounts(account_t * node); |
232 |
void destroy_filters(filter_t * node); |
233 |
void destroy_masks(mask_t * node); |
234 |
|
235 |
void string_upper(char *str, size_t size); |
236 |
int string_decode(char *str); |
237 |
|
238 |
char *apply_namespace(char *mbox, char *prefix, char delim); |
239 |
|
240 |
#endif /* DATA_H */ |