Andreas Krennmair:
[apps/madmutt.git] / imap / imap_private.h
1 /*
2  * Copyright (C) 1996-9 Brandon Long <blong@fiction.net>
3  * Copyright (C) 1999-2001 Brendan Cully <brendan@kublai.com>
4  * 
5  *     This program is free software; you can redistribute it and/or modify
6  *     it under the terms of the GNU General Public License as published by
7  *     the Free Software Foundation; either version 2 of the License, or
8  *     (at your option) any later version.
9  * 
10  *     This program is distributed in the hope that it will be useful,
11  *     but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *     GNU General Public License for more details.
14  * 
15  *     You should have received a copy of the GNU General Public License
16  *     along with this program; if not, write to the Free Software
17  *     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
18  */
19
20 #ifndef _IMAP_PRIVATE_H
21 #define _IMAP_PRIVATE_H 1
22
23 #include <inttypes.h>
24
25 #include "imap.h"
26 #include "mutt_socket.h"
27
28 /* -- symbols -- */
29 #define IMAP_PORT 143
30 #define IMAP_SSL_PORT 993
31
32 /* logging levels */
33 #define IMAP_LOG_CMD  2
34 #define IMAP_LOG_LTRL 4
35 #define IMAP_LOG_PASS 5
36
37 /* IMAP command responses. Used in IMAP_COMMAND.state too */
38 /* <tag> OK ... */
39 #define IMAP_CMD_OK       (0)
40 /* <tag> BAD ... */
41 #define IMAP_CMD_BAD      (-1)
42 /* <tag> NO ... */
43 #define IMAP_CMD_NO       (-2)
44 /* * ... */
45 #define IMAP_CMD_CONTINUE (1)
46 /* + */
47 #define IMAP_CMD_RESPOND  (2)
48
49 /* number of entries in the hash table */
50 #define IMAP_CACHE_LEN 10
51
52 #define SEQLEN 5
53
54 #define IMAP_REOPEN_ALLOW     (1<<0)
55 #define IMAP_EXPUNGE_EXPECTED (1<<1)
56 #define IMAP_EXPUNGE_PENDING  (1<<2)
57 #define IMAP_NEWMAIL_PENDING  (1<<3)
58 #define IMAP_FLAGS_PENDING    (1<<4)
59
60 /* imap_exec flags (see imap_exec) */
61 #define IMAP_CMD_FAIL_OK (1<<0)
62 #define IMAP_CMD_PASS    (1<<1)
63
64 enum {
65   IMAP_FATAL = 1,
66   IMAP_BYE,
67   IMAP_REOPENED
68 };
69
70 enum {
71   /* States */
72   IMAP_DISCONNECTED = 0,
73   IMAP_CONNECTED,
74   IMAP_AUTHENTICATED,
75   IMAP_SELECTED
76 };
77
78 enum {
79   /* Namespace types */
80   IMAP_NS_PERSONAL = 0,
81   IMAP_NS_OTHER,
82   IMAP_NS_SHARED
83 };
84
85 /* ACL Rights */
86 enum {
87   IMAP_ACL_LOOKUP = 0,
88   IMAP_ACL_READ,
89   IMAP_ACL_SEEN,
90   IMAP_ACL_WRITE,
91   IMAP_ACL_INSERT,
92   IMAP_ACL_POST,
93   IMAP_ACL_CREATE,
94   IMAP_ACL_DELETE,
95   IMAP_ACL_ADMIN,
96
97   RIGHTSMAX
98 };
99
100 /* Capabilities we are interested in */
101 enum {
102   IMAP4 = 0,
103   IMAP4REV1,
104   STATUS,
105   ACL,                          /* RFC 2086: IMAP4 ACL extension */
106   NAMESPACE,                    /* RFC 2342: IMAP4 Namespace */
107   ACRAM_MD5,                    /* RFC 2195: CRAM-MD5 authentication */
108   AGSSAPI,                      /* RFC 1731: GSSAPI authentication */
109   AUTH_ANON,                    /* AUTH=ANONYMOUS */
110   STARTTLS,                     /* RFC 2595: STARTTLS */
111   LOGINDISABLED,                /*           LOGINDISABLED */
112
113   CAPMAX
114 };
115
116 /* imap_conn_find flags */
117 #define M_IMAP_CONN_NONEW    (1<<0)
118 #define M_IMAP_CONN_NOSELECT (1<<1)
119
120 /* -- data structures -- */
121 typedef struct {
122   unsigned int uid;
123   char *path;
124 } IMAP_CACHE;
125
126 typedef struct {
127   int type;
128   int listable;
129   char *prefix;
130   char delim;
131   int home_namespace;
132   /* We get these when we check if namespace exists - cache them */
133   int noselect;
134   int noinferiors;
135 } IMAP_NAMESPACE_INFO;
136
137 /* IMAP command structure */
138 typedef struct {
139   char seq[SEQLEN + 1];
140   char *buf;
141   unsigned int blen;
142   int state;
143 } IMAP_COMMAND;
144
145 typedef struct {
146   /* This data is specific to a CONNECTION to an IMAP server */
147   CONNECTION *conn;
148   unsigned char state;
149   unsigned char status;
150   /* let me explain capstr: SASL needs the capability string (not bits).
151    * we have 3 options:
152    *   1. rerun CAPABILITY inside SASL function.
153    *   2. build appropriate CAPABILITY string by reverse-engineering from bits.
154    *   3. keep a copy until after authentication.
155    * I've chosen (3) for now. (2) might not be too bad, but it involves
156    * tracking all possible capabilities. bah. (1) I don't like because
157    * it's just no fun to get the same information twice */
158   char *capstr;
159   unsigned char capabilities[(CAPMAX + 7) / 8];
160   unsigned int seqno;
161   time_t lastread;              /* last time we read a command for the server */
162   /* who knows, one day we may run multiple commands in parallel */
163   IMAP_COMMAND cmd;
164
165   /* The following data is all specific to the currently SELECTED mbox */
166   char delim;
167   CONTEXT *ctx;
168   char *mailbox;
169   unsigned short check_status;
170   unsigned char reopen;
171   unsigned char rights[(RIGHTSMAX + 7) / 8];
172   unsigned int newMailCount;
173   IMAP_CACHE cache[IMAP_CACHE_LEN];
174   int noclose:1;
175 #ifdef USE_HCACHE
176   uint64_t uid_validity;
177 #endif
178
179   /* all folder flags - system flags AND keywords */
180   LIST *flags;
181 } IMAP_DATA;
182
183 /* I wish that were called IMAP_CONTEXT :( */
184
185 /* -- macros -- */
186 #define CTX_DATA ((IMAP_DATA *) ctx->data)
187
188 /* -- private IMAP functions -- */
189 /* imap.c */
190 int imap_create_mailbox (IMAP_DATA * idata, char *mailbox);
191 int imap_rename_mailbox (IMAP_DATA * idata, IMAP_MBOX * mx,
192                          const char *newname);
193 int imap_make_msg_set (IMAP_DATA * idata, BUFFER * buf, int flag,
194                        int changed);
195 int imap_open_connection (IMAP_DATA * idata);
196 IMAP_DATA *imap_conn_find (const ACCOUNT * account, int flags);
197 int imap_parse_list_response (IMAP_DATA * idata, char **name, int *noselect,
198                               int *noinferiors, char *delim);
199 int imap_read_literal (FILE * fp, IMAP_DATA * idata, long bytes);
200 void imap_expunge_mailbox (IMAP_DATA * idata);
201 int imap_reconnect (CONTEXT * ctx);
202 void imap_logout (IMAP_DATA * idata);
203
204 /* auth.c */
205 int imap_authenticate (IMAP_DATA * idata);
206
207 /* command.c */
208 int imap_cmd_start (IMAP_DATA * idata, const char *cmd);
209 int imap_cmd_step (IMAP_DATA * idata);
210 void imap_cmd_finish (IMAP_DATA * idata);
211 int imap_code (const char *s);
212 int imap_exec (IMAP_DATA * idata, const char *cmd, int flags);
213
214 /* message.c */
215 void imap_add_keywords (char *s, HEADER * keywords, LIST * mailbox_flags,
216                         size_t slen);
217 void imap_free_header_data (void **data);
218 int imap_read_headers (IMAP_DATA * idata, int msgbegin, int msgend);
219 char *imap_set_flags (IMAP_DATA * idata, HEADER * h, char *s);
220
221 /* util.c */
222 int imap_continue (const char *msg, const char *resp);
223 void imap_error (const char *where, const char *msg);
224 IMAP_DATA *imap_new_idata (void);
225 void imap_free_idata (IMAP_DATA ** idata);
226 char *imap_fix_path (IMAP_DATA * idata, char *mailbox, char *path,
227                      size_t plen);
228 int imap_get_literal_count (const char *buf, long *bytes);
229 char *imap_get_qualifier (char *buf);
230 char *imap_next_word (char *s);
231 time_t imap_parse_date (char *s);
232 void imap_qualify_path (char *dest, size_t len, IMAP_MBOX * mx, char *path);
233 void imap_quote_string (char *dest, size_t slen, const char *src);
234 void imap_unquote_string (char *s);
235 void imap_munge_mbox_name (char *dest, size_t dlen, const char *src);
236 void imap_unmunge_mbox_name (char *s);
237 int imap_wordcasecmp (const char *a, const char *b);
238
239 /* utf7.c */
240 void imap_utf7_encode (char **s);
241 void imap_utf7_decode (char **s);
242
243 #endif