1 |
#ifndef DATA_H |
2 |
#define DATA_H |
3 |
|
4 |
#include <regex.h> |
5 |
|
6 |
/* String lengths of account settings. */ |
7 |
#define SERVER_LEN (635 + 1) |
8 |
#define USERNAME_LEN 64 |
9 |
#define PASSWORD_LEN 64 |
10 |
|
11 |
/* String length of keys (aliases). */ |
12 |
#define KEY_LEN 32 |
13 |
|
14 |
/* String length of action's arguments. */ |
15 |
#define ARGS_LEN 256 |
16 |
|
17 |
/* String length of mailbox's name. */ |
18 |
#define MBOX_NAME_LEN 128 |
19 |
|
20 |
/* String length of mask's body. */ |
21 |
#define MASK_BODY_LEN 256 |
22 |
|
23 |
/* Maximum filters assigned to a mailbox. */ |
24 |
#define MBOX_FILTERS_MAX 32 |
25 |
|
26 |
/* Maximum mailboxes contained in a mailbox-group. */ |
27 |
#define MBOXGRP_MBOXES_MAX 64 |
28 |
|
29 |
/* |
30 |
* Mode for masks that comprise a filter. So if a filter is of AND type then |
31 |
* as default masks are ANDed. |
32 |
*/ |
33 |
#define FILTER_MODE_OR 1 |
34 |
#define FILTER_MODE_AND 2 |
35 |
|
36 |
/* Action to do in case of filter matching. */ |
37 |
#define FILTER_ACTION_DELETE 1 |
38 |
#define FILTER_ACTION_COPY 2 |
39 |
#define FILTER_ACTION_MOVE 3 |
40 |
#define FILTER_ACTION_LIST 4 |
41 |
|
42 |
/* Type of mask. This overrides the default filter mode (AND/OR) of filter. */ |
43 |
#define MASK_TYPE_OR 1 |
44 |
#define MASK_TYPE_AND 2 |
45 |
|
46 |
|
47 |
/* |
48 |
* Find last node of linked list and append at the end of the list the |
49 |
* supplied node. |
50 |
*/ |
51 |
#define APPEND_LINKED_LIST(f, n, p, a) \ |
52 |
a = &f; \ |
53 |
p = f; \ |
54 |
while (p) { \ |
55 |
a = &(p->next); \ |
56 |
p = p->next; \ |
57 |
} \ |
58 |
*a = n; |
59 |
|
60 |
/* |
61 |
* Find, based on the key of the supplied node, the apropriate position for it |
62 |
* in the tree, and the insert it there. |
63 |
*/ |
64 |
#define INSERT_TREE(r, n, p, i, c) \ |
65 |
i = &r; \ |
66 |
p = r; \ |
67 |
while (p) { \ |
68 |
c = strncmp(n->key, p->key, KEY_LEN); \ |
69 |
if (c < 0) { \ |
70 |
i = &(p->left); \ |
71 |
p = p->left; \ |
72 |
} else if (c > 0) { \ |
73 |
i = &(p->right); \ |
74 |
p = p->right; \ |
75 |
} else \ |
76 |
return; \ |
77 |
} \ |
78 |
*i = n; |
79 |
|
80 |
/* |
81 |
* Find, based on the supplied key, the position of a node, and return a |
82 |
* pointer directing to that node. |
83 |
*/ |
84 |
#define FIND_TREE(r, k, p, c) \ |
85 |
p = r; \ |
86 |
while (p) { \ |
87 |
c = strncmp(k, p->key, KEY_LEN); \ |
88 |
if (c < 0) \ |
89 |
p = p->left; \ |
90 |
else if (c > 0) \ |
91 |
p = p->right; \ |
92 |
else \ |
93 |
return p; \ |
94 |
} \ |
95 |
return NULL; |
96 |
|
97 |
|
98 |
/* Account. */ |
99 |
typedef struct account_t { |
100 |
char server[SERVER_LEN]; /* Hostname of mail server. */ |
101 |
unsigned short int port; /* Port to connect. */ |
102 |
|
103 |
char username[USERNAME_LEN]; /* Username. */ |
104 |
char password[PASSWORD_LEN]; /* Password. */ |
105 |
|
106 |
struct mbox_t *mboxes; /* Mailboxes. */ |
107 |
struct account_t *next; /* Next node of linked list. */ |
108 |
} account_t; |
109 |
|
110 |
/* Mailbox. */ |
111 |
typedef struct mbox_t { |
112 |
char name[MBOX_NAME_LEN]; /* Name of mailbox. */ |
113 |
struct filter_t *filters[MBOX_FILTERS_MAX]; /* Filters to be applied. */ |
114 |
struct mbox_t *next; /* Next node of linked list. */ |
115 |
} mbox_t; |
116 |
|
117 |
/* Group of mailboxes. */ |
118 |
typedef struct mboxgrp_t { |
119 |
char key[KEY_LEN]; /* Alias of mailbox group. */ |
120 |
struct mbox_t *mboxes[MBOXGRP_MBOXES_MAX]; /* Mailboxes of group. */ |
121 |
struct mboxgrp_t *left, *right; /* Left/right nodes of tree. */ |
122 |
} mboxgrp_t; |
123 |
|
124 |
/* Filter. */ |
125 |
typedef struct filter_t { |
126 |
char key[KEY_LEN]; /* Alias of filter. */ |
127 |
unsigned int mode; /* AND/OR mode. */ |
128 |
|
129 |
struct { /* What to do on filter match. */ |
130 |
unsigned int type; /* Action. */ |
131 |
char destmbox[MBOX_NAME_LEN]; /* Destination mailbox. */ |
132 |
char args[ARGS_LEN]; /* Action's arguments. */ |
133 |
} action; |
134 |
|
135 |
struct mask_t *masks; /* Masks comprising the filter. */ |
136 |
unsigned int masknum; /* Total number of masks. */ |
137 |
unsigned int masklen; /* Total length of body of masks. */ |
138 |
|
139 |
struct filter_t *left, *right; /* Left/right node of tree. */ |
140 |
} filter_t; |
141 |
|
142 |
/* Mask. */ |
143 |
typedef struct mask_t { |
144 |
char body[MASK_BODY_LEN]; /* Body of mask. */ |
145 |
unsigned int type; /* AND/OR type. */ |
146 |
struct mask_t *next; /* Next node of linked list. */ |
147 |
} mask_t; |
148 |
|
149 |
|
150 |
/* data.c */ |
151 |
void *create_node(size_t size); |
152 |
|
153 |
void init_account(account_t * node); |
154 |
void append_account(account_t * node); |
155 |
int set_account(char *line, regmatch_t * match); |
156 |
|
157 |
void init_mboxgrp(mboxgrp_t * node); |
158 |
void ins_mboxgrp(mboxgrp_t * node); |
159 |
int set_mboxgrp(char *line, regmatch_t * match); |
160 |
void process_mboxgrp(mboxgrp_t * node, char *mboxs); |
161 |
mboxgrp_t *find_mboxgrp(char *key); |
162 |
|
163 |
void init_mbox(mbox_t * node); |
164 |
void append_mbox(mbox_t * node); |
165 |
mbox_t *set_mbox(char *name); |
166 |
|
167 |
void init_filter(filter_t * node); |
168 |
void ins_filter(filter_t * node); |
169 |
int set_filter(char *line, regmatch_t * match); |
170 |
filter_t *find_filter(char *key); |
171 |
int set_action(char *line, regmatch_t * match); |
172 |
|
173 |
void init_mask(mask_t * node); |
174 |
void append_mask(mask_t * node); |
175 |
int set_mask(char *line, regmatch_t * match); |
176 |
|
177 |
int set_job(char *line, regmatch_t * match); |
178 |
void link_mbox_filter(filter_t * cf, mboxgrp_t * cg); |
179 |
|
180 |
void destroy_data(void); |
181 |
void destroy_mboxgrp(mboxgrp_t * node); |
182 |
|
183 |
void string_upper(char *str, size_t size); |
184 |
|
185 |
#endif /* DATA_H */ |