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 |
|
76 |
/* |
77 |
* Find last node of linked list and append at the end of the list the |
78 |
* supplied node. |
79 |
*/ |
80 |
#define APPEND_LINKED_LIST(f, n, p, a) \ |
81 |
a = &f; \ |
82 |
p = f; \ |
83 |
while (p) { \ |
84 |
a = &(p->next); \ |
85 |
p = p->next; \ |
86 |
} \ |
87 |
*a = n; |
88 |
|
89 |
/* |
90 |
* Find, based on the key of the supplied node, the apropriate position for it |
91 |
* in the tree, and then insert it there. |
92 |
*/ |
93 |
#define INSERT_TREE(r, n, p, i, c) \ |
94 |
i = &r; \ |
95 |
p = r; \ |
96 |
while (p) { \ |
97 |
c = strncmp(n->key, p->key, KEY_LEN); \ |
98 |
if (c < 0) { \ |
99 |
i = &(p->left); \ |
100 |
p = p->left; \ |
101 |
} else if (c > 0) { \ |
102 |
i = &(p->right); \ |
103 |
p = p->right; \ |
104 |
} else \ |
105 |
return; \ |
106 |
} \ |
107 |
*i = n; |
108 |
|
109 |
/* |
110 |
* Find, based on the supplied key, the position of a node, and return a |
111 |
* pointer directing to that node. |
112 |
*/ |
113 |
#define FIND_TREE(r, k, p, c) \ |
114 |
p = r; \ |
115 |
while (p) { \ |
116 |
c = strncmp(k, p->key, KEY_LEN); \ |
117 |
if (c < 0) \ |
118 |
p = p->left; \ |
119 |
else if (c > 0) \ |
120 |
p = p->right; \ |
121 |
else \ |
122 |
return p; \ |
123 |
} \ |
124 |
return NULL; |
125 |
|
126 |
#define create_node(A) xmalloc(A) |
127 |
|
128 |
|
129 |
/* Account. */ |
130 |
typedef struct account { |
131 |
char key[KEY_LEN]; /* Alias of account. */ |
132 |
char server[SERVER_LEN]; /* Hostname of mail server. */ |
133 |
unsigned short int port; /* Port to connect. */ |
134 |
|
135 |
char username[USERNAME_LEN];/* Username. */ |
136 |
char *password; /* Password. */ |
137 |
int passwdattr; /* Password attributes. */ |
138 |
unsigned int ssl; /* Secure Socket Layer support. */ |
139 |
struct mbox *mboxes; /* Mailboxes. */ |
140 |
struct account *next; /* Next node of linked list. */ |
141 |
} account_t; |
142 |
|
143 |
/* Mailbox. */ |
144 |
typedef struct mbox { |
145 |
char name[MBOX_NAME_LEN]; /* Name of mailbox. */ |
146 |
struct filter *filters[MBOX_FILTERS_MAX]; /* Filters to be applied. */ |
147 |
struct mbox *next; /* Next node of linked list. */ |
148 |
} mbox_t; |
149 |
|
150 |
/* Group of mailboxes. */ |
151 |
typedef struct mboxgrp { |
152 |
char key[KEY_LEN]; /* Alias of mailbox group. */ |
153 |
struct mbox *mboxes[MBOXGRP_MBOXES_MAX]; /* Mailboxes of group. */ |
154 |
struct mboxgrp *left, *right; /* Left/right nodes of tree. */ |
155 |
} mboxgrp_t; |
156 |
|
157 |
/* Filter. */ |
158 |
typedef struct filter { |
159 |
char key[KEY_LEN]; /* Alias of filter. */ |
160 |
unsigned int mode; /* AND/OR mode. */ |
161 |
|
162 |
struct { /* What to do on filter match. */ |
163 |
unsigned int type; /* Action. */ |
164 |
account_t *raccount; /* Remote account to rcopy/rmove message. */ |
165 |
char destmbox[MBOX_NAME_LEN]; /* Destination mailbox. */ |
166 |
unsigned int msgflags; /* Message flags to replace/add/remove. */ |
167 |
char args[ARGS_LEN]; /* Action's arguments. */ |
168 |
} action; |
169 |
|
170 |
struct mask *masks; /* Masks comprising the filter. */ |
171 |
unsigned int masknum; /* Total number of masks. */ |
172 |
unsigned int ormasknum; /* Total number of OR masks. */ |
173 |
unsigned int masklen; /* Total length of body of masks. */ |
174 |
|
175 |
struct filter *left, *right;/* Left/right node of tree. */ |
176 |
} filter_t; |
177 |
|
178 |
/* Mask. */ |
179 |
typedef struct mask { |
180 |
char body[MASK_BODY_LEN]; /* Body of mask. */ |
181 |
unsigned int type; /* AND/OR type. */ |
182 |
struct mask *next; /* Next node of linked list. */ |
183 |
} mask_t; |
184 |
|
185 |
|
186 |
/* data.c */ |
187 |
void init_account(account_t * node); |
188 |
void append_account(account_t * node); |
189 |
int set_account(char *line, regmatch_t * match); |
190 |
#ifdef ENCRYPTED_PASSWORDS |
191 |
char *find_password(char *user, char *serv); |
192 |
#endif |
193 |
|
194 |
void init_mboxgrp(mboxgrp_t * node); |
195 |
void ins_mboxgrp(mboxgrp_t * node); |
196 |
int set_mboxgrp(char *line, regmatch_t * match); |
197 |
void process_mboxgrp(mboxgrp_t * node, char *mboxs); |
198 |
mboxgrp_t *find_mboxgrp(char *key); |
199 |
|
200 |
void init_mbox(mbox_t * node); |
201 |
void append_mbox(mbox_t * node); |
202 |
mbox_t *set_mbox(char *name); |
203 |
|
204 |
void init_filter(filter_t * node); |
205 |
void ins_filter(filter_t * node); |
206 |
int set_filter(char *line, regmatch_t * match); |
207 |
filter_t *find_filter(char *key); |
208 |
int set_action(char *line, regmatch_t * match); |
209 |
|
210 |
void init_mask(mask_t * node); |
211 |
void append_mask(mask_t * node); |
212 |
int set_mask(char *line, regmatch_t * match); |
213 |
void convert_date(mask_t * node); |
214 |
|
215 |
int set_job(char *line, regmatch_t * match); |
216 |
void link_mbox_filter(filter_t * cf, mboxgrp_t * cg); |
217 |
|
218 |
void destroy_data(void); |
219 |
void destroy_mboxgrp(mboxgrp_t * node); |
220 |
|
221 |
void string_upper(char *str, size_t size); |
222 |
int string_decode(char *str); |
223 |
|
224 |
char *apply_namespace(char *mbox, char *prefix, char delim); |
225 |
|
226 |
#endif /* DATA_H */ |