0ee9d6330432aa930693608c613e4c2d52c56b72
[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 <lib-lib/lib-lib.h>
13
14 #include "mutt.h"
15 #include "imap_private.h"
16
17 #include "mx.h"
18 #include "mx_imap.h"
19
20 int imap_is_magic (const char* path, struct stat* st __attribute__ ((unused))) {
21   url_scheme_t s;
22   if (!path || !*path)
23     return (-1);
24   s = url_check_scheme (NONULL (path));
25   return ((s == U_IMAP || s == U_IMAPS) ? M_IMAP : -1);
26 }
27
28 static int acl_check_imap (CONTEXT* ctx, int bit) {
29   return (!mutt_bit_isset (((IMAP_DATA*) ctx->data)->capabilities, ACL) ||
30           mutt_bit_isset (((IMAP_DATA*) ctx->data)->rights, bit));
31 }
32
33 static int imap_open_new_message (MESSAGE * msg,
34                                   CONTEXT * dest __attribute__ ((unused)),
35                                   HEADER * hdr __attribute__ ((unused)))
36 {
37   char tmp[_POSIX_PATH_MAX];
38
39   mutt_mktemp (tmp);
40   if ((msg->fp = safe_fopen (tmp, "w")) == NULL) {
41     mutt_perror (tmp);
42     return (-1);
43   }
44   msg->path = m_strdup(tmp);
45   return 0;
46 }
47
48 /* this ugly kludge is required since the last int to
49  * imap_check_mailbox() doesn't mean 'lock' but 'force'... */
50 static int _imap_check_mailbox (CONTEXT* ctx,
51                                 int* index_hint,
52                                 int lock __attribute__ ((unused))) {
53   return (imap_check_mailbox (ctx, index_hint, 0));
54 }
55
56 static int imap_commit_message (MESSAGE* msg, CONTEXT* ctx) {
57   int r = 0;
58
59   if ((r = safe_fclose (&msg->fp)) == 0)
60     r = imap_append_message (ctx, msg);
61   return (r);
62 }
63
64 mx_t* imap_reg_mx (void) {
65   mx_t* fmt = p_new(mx_t, 1);
66
67   /* make up mx_t record... */
68   fmt->type = M_IMAP;
69   fmt->mx_is_magic = imap_is_magic;
70   fmt->mx_access = imap_access;
71   fmt->mx_open_mailbox = imap_open_mailbox;
72   fmt->mx_open_new_message = imap_open_new_message;
73   fmt->mx_acl_check = acl_check_imap;
74   fmt->mx_fastclose_mailbox = imap_close_mailbox;
75   fmt->mx_sync_mailbox = imap_sync_mailbox;
76   fmt->mx_check_mailbox = _imap_check_mailbox;
77   fmt->mx_commit_message = imap_commit_message;
78   return (fmt);
79 }