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