isync.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /* $Id$
  2. *
  3. * isync - IMAP4 to maildir mailbox synchronizer
  4. * Copyright (C) 2000-2 Michael R. Elkins <me@mutt.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #if HAVE_LIBDB
  21. #define DB_DBM_HSEARCH 1
  22. #include <db.h>
  23. #else
  24. #include <ndbm.h>
  25. #endif
  26. #include <sys/types.h>
  27. #include <stdarg.h>
  28. #if HAVE_LIBSSL
  29. #include <openssl/ssl.h>
  30. #endif
  31. #include "debug.h"
  32. typedef struct
  33. {
  34. int rdfd; /* read filedes */
  35. int wrfd; /* write filedes */
  36. #if HAVE_LIBSSL
  37. SSL *ssl;
  38. unsigned int use_ssl:1;
  39. #endif
  40. } Socket_t;
  41. typedef struct
  42. {
  43. Socket_t *sock;
  44. char buf[1024];
  45. int bytes;
  46. int offset;
  47. }
  48. buffer_t;
  49. typedef struct config config_t;
  50. typedef struct mailbox mailbox_t;
  51. typedef struct message message_t;
  52. struct config
  53. {
  54. char *maildir;
  55. char *path; /* path relative to .maildir, or absolute path */
  56. char *host;
  57. int port;
  58. char *user;
  59. char *pass;
  60. char *box;
  61. char *alias;
  62. char *copy_deleted_to;
  63. char *tunnel;
  64. unsigned int max_messages;
  65. off_t max_size;
  66. config_t *next;
  67. #if HAVE_LIBSSL
  68. char *cert_file;
  69. unsigned int use_imaps:1;
  70. unsigned int require_ssl:1;
  71. unsigned int use_sslv2:1;
  72. unsigned int use_sslv3:1;
  73. unsigned int use_tlsv1:1;
  74. unsigned int require_cram:1;
  75. #endif
  76. unsigned int use_namespace:1;
  77. unsigned int expunge:1;
  78. unsigned int delete:1;
  79. unsigned int wanted:1;
  80. };
  81. /* struct representing local mailbox file */
  82. struct mailbox
  83. {
  84. DBM *db;
  85. char *path;
  86. message_t *msgs;
  87. int lockfd;
  88. unsigned int deleted; /* # of deleted messages */
  89. unsigned int uidvalidity;
  90. unsigned int maxuid; /* largest uid we know about */
  91. unsigned int uidseen : 1; /* flag indicating whether or not we saw a
  92. valid value for UIDVALIDITY */
  93. };
  94. /* message dispositions */
  95. #define D_SEEN (1<<0)
  96. #define D_ANSWERED (1<<1)
  97. #define D_DELETED (1<<2)
  98. #define D_FLAGGED (1<<3)
  99. #define D_RECENT (1<<4)
  100. #define D_DRAFT (1<<5)
  101. #define D_MAX 6
  102. struct message
  103. {
  104. char *file;
  105. unsigned int uid;
  106. unsigned int flags;
  107. size_t size;
  108. message_t *next;
  109. unsigned int processed:1; /* message has already been evaluated */
  110. unsigned int new:1; /* message is in the new/ subdir */
  111. unsigned int dead:1; /* message doesn't exist on the server */
  112. unsigned int wanted:1; /* when using MaxMessages, keep this message */
  113. };
  114. /* struct used for parsing IMAP lists */
  115. typedef struct _list list_t;
  116. #define NIL (void*)0x1
  117. #define LIST (void*)0x2
  118. struct _list
  119. {
  120. char *val;
  121. list_t *next;
  122. list_t *child;
  123. };
  124. /* imap connection info */
  125. typedef struct
  126. {
  127. Socket_t *sock;
  128. unsigned int count; /* # of msgs */
  129. unsigned int recent; /* # of recent messages */
  130. buffer_t *buf; /* input buffer for reading server output */
  131. message_t *msgs; /* list of messages on the server */
  132. config_t *box; /* mailbox to open */
  133. char *prefix; /* namespace prefix */
  134. unsigned int deleted; /* # of deleted messages */
  135. unsigned int uidvalidity;
  136. unsigned int maxuid;
  137. unsigned int minuid;
  138. /* NAMESPACE info */
  139. list_t *ns_personal;
  140. list_t *ns_other;
  141. list_t *ns_shared;
  142. unsigned int have_namespace:1;
  143. #if HAVE_LIBSSL
  144. unsigned int have_cram:1;
  145. unsigned int have_starttls:1;
  146. unsigned int cram:1;
  147. #endif
  148. }
  149. imap_t;
  150. /* flags for sync_mailbox */
  151. #define SYNC_DELETE (1<<0) /* delete local that don't exist on server */
  152. #define SYNC_EXPUNGE (1<<1) /* don't fetch deleted messages */
  153. #define SYNC_QUIET (1<<2) /* only display critical errors */
  154. /* flags for maildir_open */
  155. #define OPEN_FAST (1<<0) /* fast open - don't parse */
  156. #define OPEN_CREATE (1<<1) /* create mailbox if nonexistent */
  157. extern config_t global;
  158. extern config_t *boxes;
  159. extern unsigned int Tag;
  160. extern char Hostname[256];
  161. extern int Verbose;
  162. #if HAVE_LIBSSL
  163. extern SSL_CTX *SSLContext;
  164. char *cram (const char *, const char *, const char *);
  165. #endif
  166. char *next_arg (char **);
  167. int sync_mailbox (mailbox_t *, imap_t *, int, unsigned int, unsigned int);
  168. void load_config (const char *);
  169. char * expand_strdup (const char *s);
  170. config_t *find_box (const char *);
  171. void free_config (void);
  172. void imap_close (imap_t *);
  173. int imap_copy_message (imap_t * imap, unsigned int uid, const char *mailbox);
  174. int imap_fetch_message (imap_t *, unsigned int, int);
  175. int imap_set_flags (imap_t *, unsigned int, unsigned int);
  176. int imap_expunge (imap_t *);
  177. imap_t *imap_open (config_t *, unsigned int, imap_t *, int);
  178. int imap_append_message (imap_t *, int, message_t *);
  179. mailbox_t *maildir_open (const char *, int flags);
  180. int maildir_expunge (mailbox_t *, int);
  181. int maildir_set_uidvalidity (mailbox_t *, unsigned int uidvalidity);
  182. void maildir_close (mailbox_t *);
  183. int maildir_update_maxuid (mailbox_t * mbox);
  184. message_t * find_msg (message_t * list, unsigned int uid);
  185. void free_message (message_t *);
  186. /* parse an IMAP list construct */
  187. list_t * parse_list (char *s, char **end);
  188. int is_atom (list_t *list);
  189. int is_list (list_t *list);
  190. int is_nil (list_t *list);
  191. void free_list (list_t *list);
  192. #define strfcpy(a,b,c) {strncpy(a,b,c);(a)[c-1]=0;}