Rocco Rutte:
[apps/madmutt.git] / imap / mx_imap.c
1 /*
2  * This file is part of mutt-ng, see http://www.muttng.org/.
3  * It's licensed under the GNU General Public License,
4  * please see the file GPL in the top level source directory.
5  */
6 #ifdef HAVE_CONFIG_H
7 #include "config.h"
8 #endif
9
10 #include <sys/stat.h>
11
12 #include "mutt.h"
13 #include "imap_private.h"
14
15 #include "mx.h"
16 #include "mx_imap.h"
17
18 #include "lib/mem.h"
19 #include "lib/str.h"
20
21 #include "url.h"
22
23 int imap_is_magic (const char* path, struct stat* st) {
24   url_scheme_t s;
25   if (!path || !*path)
26     return (-1);
27   s = url_check_scheme (NONULL (path));
28   return ((s == U_IMAP || s == U_IMAPS) ? M_IMAP : -1);
29 }
30
31 static int acl_check_imap (CONTEXT* ctx, int bit) {
32   return (!mutt_bit_isset (((IMAP_DATA*) ctx->data)->capabilities, ACL) ||
33           mutt_bit_isset (((IMAP_DATA*) ctx->data)->rights, bit));
34 }
35
36 static int imap_open_new_message (MESSAGE * msg, CONTEXT * dest, HEADER * hdr)
37 {
38   char tmp[_POSIX_PATH_MAX];
39
40   mutt_mktemp (tmp);
41   if ((msg->fp = safe_fopen (tmp, "w")) == NULL) {
42     mutt_perror (tmp);
43     return (-1);
44   }
45   msg->path = safe_strdup (tmp);
46   return 0;
47 }
48
49 /* this ugly kludge is required since the last int to
50  * imap_check_mailbox() doesn't mean 'lock' but 'force'... */
51 static int _imap_check_mailbox (CONTEXT* ctx, int* index_hint, int lock) {
52   return (imap_check_mailbox (ctx, index_hint, 0));
53 }
54
55 static int imap_commit_message (MESSAGE* msg, CONTEXT* ctx) {
56   int r = 0;
57
58   if ((r = safe_fclose (&msg->fp)) == 0)
59     r = imap_append_message (ctx, msg);
60   return (r);
61 }
62
63 mx_t* imap_reg_mx (void) {
64   mx_t* fmt = safe_calloc (1, sizeof (mx_t));
65
66   /* make up mx_t record... */
67   fmt->type = M_IMAP;
68   fmt->mx_is_magic = imap_is_magic;
69   fmt->mx_access = imap_access;
70   fmt->mx_open_mailbox = imap_open_mailbox;
71   fmt->mx_open_new_message = imap_open_new_message;
72   fmt->mx_acl_check = acl_check_imap;
73   fmt->mx_fastclose_mailbox = imap_close_mailbox;
74   fmt->mx_sync_mailbox = imap_sync_mailbox;
75   fmt->mx_check_mailbox = _imap_check_mailbox;
76   fmt->mx_commit_message = imap_commit_message;
77   return (fmt);
78 }