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