1 |
#include <stdio.h> |
2 |
|
3 |
#include "config.h" |
4 |
#include "imapfilter.h" |
5 |
|
6 |
|
7 |
extern conn_t connpri, connaux; |
8 |
extern unsigned int options; |
9 |
|
10 |
|
11 |
#ifdef DEBUG |
12 |
/* |
13 |
* Test/ping server. |
14 |
*/ |
15 |
int |
16 |
test(conn_t * conn) |
17 |
{ |
18 |
return server_response(conn, imap_noop(conn)); |
19 |
} |
20 |
#endif |
21 |
|
22 |
|
23 |
/* |
24 |
* Check server's capabilities. |
25 |
*/ |
26 |
int |
27 |
check_capabilities(conn_t * conn) |
28 |
{ |
29 |
conn->caps = CAPABILITY_NONE; |
30 |
|
31 |
return capability_response(conn, imap_capability(conn)); |
32 |
} |
33 |
|
34 |
|
35 |
/* |
36 |
* Get namespace of mail server's mailboxes. |
37 |
*/ |
38 |
int |
39 |
check_namespace(conn_t * conn) |
40 |
{ |
41 |
conn->nsp.prefix[0] = conn->nsp.delim = '\0'; |
42 |
|
43 |
if (!(options & OPTION_NAMESPACE) || |
44 |
!(conn->caps & CAPABILITY_NAMESPACE)) |
45 |
return 0; |
46 |
else |
47 |
return namespace_response(conn, imap_namespace(conn)); |
48 |
} |
49 |
|
50 |
|
51 |
#ifdef SSL_TLS |
52 |
/* |
53 |
* Begin TLS negotiation (STARTTLS). |
54 |
*/ |
55 |
int |
56 |
negotiate_tls(conn_t * conn) |
57 |
{ |
58 |
int r; |
59 |
|
60 |
r = server_response(conn, imap_starttls(conn)); |
61 |
init_secure_connection(conn, SSL_TLS_V1); |
62 |
|
63 |
return r; |
64 |
} |
65 |
#endif |
66 |
|
67 |
|
68 |
/* |
69 |
* Login to server. |
70 |
*/ |
71 |
int |
72 |
login(conn_t * conn, char *user, char *pass) |
73 |
{ |
74 |
return server_response(conn, imap_login(conn, user, pass)); |
75 |
} |
76 |
|
77 |
|
78 |
|
79 |
/* |
80 |
* Check if a mailbox exists. |
81 |
*/ |
82 |
int |
83 |
check_mailbox(conn_t * conn, char *mbox) |
84 |
{ |
85 |
return server_response(conn, imap_status(conn, mbox, "MESSAGES")); |
86 |
} |
87 |
|
88 |
|
89 |
/* |
90 |
* Open mailbox in read-write mode. |
91 |
*/ |
92 |
int |
93 |
select_mailbox(conn_t * conn, char *mbox) |
94 |
{ |
95 |
int r; |
96 |
|
97 |
if (mailbox_status(conn, mbox) == -2) |
98 |
return -2; /* No messages exist. No filters need to be |
99 |
* applied. */ |
100 |
|
101 |
r = select_response(conn, imap_select(conn, |
102 |
apply_namespace(mbox, conn->nsp.prefix, conn->nsp.delim))); |
103 |
|
104 |
log_info(LOG_MAILBOX, mbox); |
105 |
|
106 |
return r; |
107 |
} |
108 |
|
109 |
|
110 |
/* |
111 |
* Get mailbox's status. |
112 |
*/ |
113 |
int |
114 |
mailbox_status(conn_t * conn, char *mbox) |
115 |
{ |
116 |
return status_response(conn, imap_status(conn, |
117 |
apply_namespace(mbox, conn->nsp.prefix, conn->nsp.delim), |
118 |
"MESSAGES RECENT UNSEEN"), mbox); |
119 |
} |
120 |
|
121 |
|
122 |
/* |
123 |
* Close examined/selected mailbox. |
124 |
*/ |
125 |
int |
126 |
close_mailbox(conn_t * conn) |
127 |
{ |
128 |
return server_response(conn, imap_close(conn)); |
129 |
} |
130 |
|
131 |
|
132 |
/* |
133 |
* Logout from server. |
134 |
*/ |
135 |
int |
136 |
logout(conn_t * conn) |
137 |
{ |
138 |
return logout_response(conn, imap_logout(conn)); |
139 |
} |