| 1 | /* |
|---|
| 2 | * Copyright (C) 1996-9 Brandon Long <blong@fiction.net> |
|---|
| 3 | * Copyright (C) 1999-2008 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|---|
| 18 | */ |
|---|
| 19 | |
|---|
| 20 | #ifndef _IMAP_PRIVATE_H |
|---|
| 21 | #define _IMAP_PRIVATE_H 1 |
|---|
| 22 | |
|---|
| 23 | #include "imap.h" |
|---|
| 24 | #include "mutt_curses.h" |
|---|
| 25 | #include "mutt_socket.h" |
|---|
| 26 | #include "bcache.h" |
|---|
| 27 | #ifdef USE_HCACHE |
|---|
| 28 | #include "hcache.h" |
|---|
| 29 | #endif |
|---|
| 30 | |
|---|
| 31 | /* -- symbols -- */ |
|---|
| 32 | #define IMAP_PORT 143 |
|---|
| 33 | #define IMAP_SSL_PORT 993 |
|---|
| 34 | |
|---|
| 35 | /* logging levels */ |
|---|
| 36 | #define IMAP_LOG_CMD 2 |
|---|
| 37 | #define IMAP_LOG_LTRL 4 |
|---|
| 38 | #define IMAP_LOG_PASS 5 |
|---|
| 39 | |
|---|
| 40 | /* IMAP command responses. Used in IMAP_COMMAND.state too */ |
|---|
| 41 | /* <tag> OK ... */ |
|---|
| 42 | #define IMAP_CMD_OK (0) |
|---|
| 43 | /* <tag> BAD ... */ |
|---|
| 44 | #define IMAP_CMD_BAD (-1) |
|---|
| 45 | /* <tag> NO ... */ |
|---|
| 46 | #define IMAP_CMD_NO (-2) |
|---|
| 47 | /* * ... */ |
|---|
| 48 | #define IMAP_CMD_CONTINUE (1) |
|---|
| 49 | /* + */ |
|---|
| 50 | #define IMAP_CMD_RESPOND (2) |
|---|
| 51 | /* IMAP_COMMAND.state additions */ |
|---|
| 52 | #define IMAP_CMD_NEW (3) |
|---|
| 53 | |
|---|
| 54 | /* number of entries in the hash table */ |
|---|
| 55 | #define IMAP_CACHE_LEN 10 |
|---|
| 56 | |
|---|
| 57 | #define SEQLEN 5 |
|---|
| 58 | /* maximum length of command lines before they must be split (for |
|---|
| 59 | * lazy servers) */ |
|---|
| 60 | #define IMAP_MAX_CMDLEN 1024 |
|---|
| 61 | |
|---|
| 62 | #define IMAP_REOPEN_ALLOW (1<<0) |
|---|
| 63 | #define IMAP_EXPUNGE_EXPECTED (1<<1) |
|---|
| 64 | #define IMAP_EXPUNGE_PENDING (1<<2) |
|---|
| 65 | #define IMAP_NEWMAIL_PENDING (1<<3) |
|---|
| 66 | #define IMAP_FLAGS_PENDING (1<<4) |
|---|
| 67 | |
|---|
| 68 | /* imap_exec flags (see imap_exec) */ |
|---|
| 69 | #define IMAP_CMD_FAIL_OK (1<<0) |
|---|
| 70 | #define IMAP_CMD_PASS (1<<1) |
|---|
| 71 | #define IMAP_CMD_QUEUE (1<<2) |
|---|
| 72 | |
|---|
| 73 | enum |
|---|
| 74 | { |
|---|
| 75 | IMAP_FATAL = 1, |
|---|
| 76 | IMAP_BYE |
|---|
| 77 | }; |
|---|
| 78 | |
|---|
| 79 | enum |
|---|
| 80 | { |
|---|
| 81 | /* States */ |
|---|
| 82 | IMAP_DISCONNECTED = 0, |
|---|
| 83 | IMAP_CONNECTED, |
|---|
| 84 | IMAP_AUTHENTICATED, |
|---|
| 85 | IMAP_SELECTED, |
|---|
| 86 | |
|---|
| 87 | /* and pseudo-states */ |
|---|
| 88 | IMAP_IDLE |
|---|
| 89 | }; |
|---|
| 90 | |
|---|
| 91 | enum |
|---|
| 92 | { |
|---|
| 93 | /* Namespace types */ |
|---|
| 94 | IMAP_NS_PERSONAL = 0, |
|---|
| 95 | IMAP_NS_OTHER, |
|---|
| 96 | IMAP_NS_SHARED |
|---|
| 97 | }; |
|---|
| 98 | |
|---|
| 99 | /* Capabilities we are interested in */ |
|---|
| 100 | enum |
|---|
| 101 | { |
|---|
| 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 | IDLE, /* RFC 2177: IDLE */ |
|---|
| 113 | SASL_IR, /* SASL initial response draft */ |
|---|
| 114 | |
|---|
| 115 | CAPMAX |
|---|
| 116 | }; |
|---|
| 117 | |
|---|
| 118 | /* imap_conn_find flags */ |
|---|
| 119 | #define M_IMAP_CONN_NONEW (1<<0) |
|---|
| 120 | #define M_IMAP_CONN_NOSELECT (1<<1) |
|---|
| 121 | |
|---|
| 122 | /* -- data structures -- */ |
|---|
| 123 | typedef struct |
|---|
| 124 | { |
|---|
| 125 | unsigned int uid; |
|---|
| 126 | char* path; |
|---|
| 127 | } IMAP_CACHE; |
|---|
| 128 | |
|---|
| 129 | typedef struct |
|---|
| 130 | { |
|---|
| 131 | char* name; |
|---|
| 132 | |
|---|
| 133 | unsigned int messages; |
|---|
| 134 | unsigned int recent; |
|---|
| 135 | unsigned int uidnext; |
|---|
| 136 | unsigned int uidvalidity; |
|---|
| 137 | unsigned int unseen; |
|---|
| 138 | } IMAP_STATUS; |
|---|
| 139 | |
|---|
| 140 | typedef struct |
|---|
| 141 | { |
|---|
| 142 | char* name; |
|---|
| 143 | |
|---|
| 144 | char delim; |
|---|
| 145 | /* if we end up storing a lot of these we could turn this into a bitfield */ |
|---|
| 146 | unsigned char noselect; |
|---|
| 147 | unsigned char noinferiors; |
|---|
| 148 | } IMAP_LIST; |
|---|
| 149 | |
|---|
| 150 | /* IMAP command structure */ |
|---|
| 151 | typedef struct |
|---|
| 152 | { |
|---|
| 153 | char seq[SEQLEN+1]; |
|---|
| 154 | int state; |
|---|
| 155 | } IMAP_COMMAND; |
|---|
| 156 | |
|---|
| 157 | typedef enum |
|---|
| 158 | { |
|---|
| 159 | IMAP_CT_NONE = 0, |
|---|
| 160 | IMAP_CT_LIST, |
|---|
| 161 | IMAP_CT_STATUS |
|---|
| 162 | } IMAP_COMMAND_TYPE; |
|---|
| 163 | |
|---|
| 164 | typedef struct |
|---|
| 165 | { |
|---|
| 166 | /* This data is specific to a CONNECTION to an IMAP server */ |
|---|
| 167 | CONNECTION *conn; |
|---|
| 168 | unsigned char state; |
|---|
| 169 | unsigned char status; |
|---|
| 170 | /* let me explain capstr: SASL needs the capability string (not bits). |
|---|
| 171 | * we have 3 options: |
|---|
| 172 | * 1. rerun CAPABILITY inside SASL function. |
|---|
| 173 | * 2. build appropriate CAPABILITY string by reverse-engineering from bits. |
|---|
| 174 | * 3. keep a copy until after authentication. |
|---|
| 175 | * I've chosen (3) for now. (2) might not be too bad, but it involves |
|---|
| 176 | * tracking all possible capabilities. bah. (1) I don't like because |
|---|
| 177 | * it's just no fun to get the same information twice */ |
|---|
| 178 | char* capstr; |
|---|
| 179 | unsigned char capabilities[(CAPMAX + 7)/8]; |
|---|
| 180 | unsigned int seqno; |
|---|
| 181 | time_t lastread; /* last time we read a command for the server */ |
|---|
| 182 | char* buf; |
|---|
| 183 | unsigned int blen; |
|---|
| 184 | |
|---|
| 185 | /* if set, the response parser will store results for complicated commands |
|---|
| 186 | * here. */ |
|---|
| 187 | IMAP_COMMAND_TYPE cmdtype; |
|---|
| 188 | void* cmddata; |
|---|
| 189 | |
|---|
| 190 | /* command queue */ |
|---|
| 191 | IMAP_COMMAND* cmds; |
|---|
| 192 | int cmdslots; |
|---|
| 193 | int nextcmd; |
|---|
| 194 | int lastcmd; |
|---|
| 195 | BUFFER* cmdbuf; |
|---|
| 196 | |
|---|
| 197 | /* cache IMAP_STATUS of visited mailboxes */ |
|---|
| 198 | LIST* mboxcache; |
|---|
| 199 | |
|---|
| 200 | /* The following data is all specific to the currently SELECTED mbox */ |
|---|
| 201 | char delim; |
|---|
| 202 | CONTEXT *ctx; |
|---|
| 203 | char *mailbox; |
|---|
| 204 | unsigned short check_status; |
|---|
| 205 | unsigned char reopen; |
|---|
| 206 | unsigned int newMailCount; |
|---|
| 207 | IMAP_CACHE cache[IMAP_CACHE_LEN]; |
|---|
| 208 | unsigned int uid_validity; |
|---|
| 209 | unsigned int uidnext; |
|---|
| 210 | body_cache_t *bcache; |
|---|
| 211 | |
|---|
| 212 | /* all folder flags - system flags AND keywords */ |
|---|
| 213 | LIST *flags; |
|---|
| 214 | #ifdef USE_HCACHE |
|---|
| 215 | header_cache_t *hcache; |
|---|
| 216 | #endif |
|---|
| 217 | } IMAP_DATA; |
|---|
| 218 | /* I wish that were called IMAP_CONTEXT :( */ |
|---|
| 219 | |
|---|
| 220 | /* -- macros -- */ |
|---|
| 221 | #define CTX_DATA ((IMAP_DATA *) ctx->data) |
|---|
| 222 | |
|---|
| 223 | /* -- private IMAP functions -- */ |
|---|
| 224 | /* imap.c */ |
|---|
| 225 | int imap_create_mailbox (IMAP_DATA* idata, char* mailbox); |
|---|
| 226 | int imap_rename_mailbox (IMAP_DATA* idata, IMAP_MBOX* mx, const char* newname); |
|---|
| 227 | IMAP_STATUS* imap_mboxcache_get (IMAP_DATA* idata, const char* mbox, |
|---|
| 228 | int create); |
|---|
| 229 | void imap_mboxcache_free (IMAP_DATA* idata); |
|---|
| 230 | int imap_exec_msgset (IMAP_DATA* idata, const char* pre, const char* post, |
|---|
| 231 | int flag, int changed, int invert); |
|---|
| 232 | int imap_open_connection (IMAP_DATA* idata); |
|---|
| 233 | void imap_close_connection (IMAP_DATA* idata); |
|---|
| 234 | IMAP_DATA* imap_conn_find (const ACCOUNT* account, int flags); |
|---|
| 235 | int imap_read_literal (FILE* fp, IMAP_DATA* idata, long bytes, progress_t*); |
|---|
| 236 | void imap_expunge_mailbox (IMAP_DATA* idata); |
|---|
| 237 | void imap_logout (IMAP_DATA** idata); |
|---|
| 238 | int imap_sync_message (IMAP_DATA *idata, HEADER *hdr, BUFFER *cmd, |
|---|
| 239 | int *err_continue); |
|---|
| 240 | int imap_has_flag (LIST* flag_list, const char* flag); |
|---|
| 241 | |
|---|
| 242 | /* auth.c */ |
|---|
| 243 | int imap_authenticate (IMAP_DATA* idata); |
|---|
| 244 | |
|---|
| 245 | /* command.c */ |
|---|
| 246 | int imap_cmd_start (IMAP_DATA* idata, const char* cmd); |
|---|
| 247 | int imap_cmd_step (IMAP_DATA* idata); |
|---|
| 248 | void imap_cmd_finish (IMAP_DATA* idata); |
|---|
| 249 | int imap_code (const char* s); |
|---|
| 250 | int imap_exec (IMAP_DATA* idata, const char* cmd, int flags); |
|---|
| 251 | int imap_cmd_idle (IMAP_DATA* idata); |
|---|
| 252 | |
|---|
| 253 | /* message.c */ |
|---|
| 254 | void imap_add_keywords (char* s, HEADER* keywords, LIST* mailbox_flags, size_t slen); |
|---|
| 255 | void imap_free_header_data (void** data); |
|---|
| 256 | int imap_read_headers (IMAP_DATA* idata, int msgbegin, int msgend); |
|---|
| 257 | char* imap_set_flags (IMAP_DATA* idata, HEADER* h, char* s); |
|---|
| 258 | int imap_cache_del (IMAP_DATA* idata, HEADER* h); |
|---|
| 259 | int imap_cache_clean (IMAP_DATA* idata); |
|---|
| 260 | |
|---|
| 261 | /* util.c */ |
|---|
| 262 | #ifdef USE_HCACHE |
|---|
| 263 | header_cache_t* imap_hcache_open (IMAP_DATA* idata, const char* path); |
|---|
| 264 | void imap_hcache_close (IMAP_DATA* idata); |
|---|
| 265 | HEADER* imap_hcache_get (IMAP_DATA* idata, unsigned int uid); |
|---|
| 266 | int imap_hcache_put (IMAP_DATA* idata, HEADER* h); |
|---|
| 267 | int imap_hcache_del (IMAP_DATA* idata, unsigned int uid); |
|---|
| 268 | #endif |
|---|
| 269 | |
|---|
| 270 | int imap_continue (const char* msg, const char* resp); |
|---|
| 271 | void imap_error (const char* where, const char* msg); |
|---|
| 272 | IMAP_DATA* imap_new_idata (void); |
|---|
| 273 | void imap_free_idata (IMAP_DATA** idata); |
|---|
| 274 | char* imap_fix_path (IMAP_DATA* idata, char* mailbox, char* path, |
|---|
| 275 | size_t plen); |
|---|
| 276 | void imap_cachepath(IMAP_DATA* idata, const char* mailbox, char* dest, |
|---|
| 277 | size_t dlen); |
|---|
| 278 | int imap_get_literal_count (const char* buf, long* bytes); |
|---|
| 279 | char* imap_get_qualifier (char* buf); |
|---|
| 280 | int imap_mxcmp (const char* mx1, const char* mx2); |
|---|
| 281 | char* imap_next_word (char* s); |
|---|
| 282 | time_t imap_parse_date (char* s); |
|---|
| 283 | void imap_qualify_path (char *dest, size_t len, IMAP_MBOX *mx, char* path); |
|---|
| 284 | void imap_quote_string (char* dest, size_t slen, const char* src); |
|---|
| 285 | void imap_unquote_string (char* s); |
|---|
| 286 | void imap_munge_mbox_name (char *dest, size_t dlen, const char *src); |
|---|
| 287 | void imap_unmunge_mbox_name (char *s); |
|---|
| 288 | int imap_wordcasecmp(const char *a, const char *b); |
|---|
| 289 | |
|---|
| 290 | /* utf7.c */ |
|---|
| 291 | void imap_utf7_encode (char **s); |
|---|
| 292 | void imap_utf7_decode (char **s); |
|---|
| 293 | |
|---|
| 294 | #if USE_HCACHE |
|---|
| 295 | /* typedef size_t (*hcache_keylen_t)(const char* fn); */ |
|---|
| 296 | #define imap_hcache_keylen mutt_strlen |
|---|
| 297 | #endif /* USE_HCACHE */ |
|---|
| 298 | |
|---|
| 299 | #endif |
|---|