isync.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. * As a special exception, isync may be linked with the OpenSSL library,
  21. * despite that library's more restrictive license.
  22. */
  23. #if HAVE_LIBDB
  24. #define DB_DBM_HSEARCH 1
  25. #include <db.h>
  26. #else
  27. #include <ndbm.h>
  28. #endif
  29. #include <sys/types.h>
  30. #include <stdarg.h>
  31. #if HAVE_LIBSSL
  32. #include <openssl/ssl.h>
  33. #endif
  34. #include "debug.h"
  35. typedef struct
  36. {
  37. int fd;
  38. #if HAVE_LIBSSL
  39. SSL *ssl;
  40. unsigned int use_ssl:1;
  41. #endif
  42. } Socket_t;
  43. typedef struct
  44. {
  45. Socket_t *sock;
  46. char buf[1024];
  47. int bytes;
  48. int offset;
  49. }
  50. buffer_t;
  51. typedef struct config config_t;
  52. typedef struct mailbox mailbox_t;
  53. typedef struct message message_t;
  54. struct config
  55. {
  56. char *maildir;
  57. char *path; /* path relative to .maildir, or absolute path */
  58. char *host;
  59. int port;
  60. char *user;
  61. char *pass;
  62. char *folder;
  63. char *box;
  64. char *inbox;
  65. char *alias;
  66. char *copy_deleted_to;
  67. char *tunnel;
  68. unsigned int max_messages;
  69. off_t max_size;
  70. config_t *next;
  71. #if HAVE_LIBSSL
  72. char *cert_file;
  73. unsigned int use_imaps:1;
  74. unsigned int require_ssl:1;
  75. unsigned int use_sslv2:1;
  76. unsigned int use_sslv3:1;
  77. unsigned int use_tlsv1:1;
  78. unsigned int require_cram:1;
  79. #endif
  80. unsigned int use_namespace:1;
  81. unsigned int expunge:1;
  82. unsigned int delete:1;
  83. unsigned int wanted:1;
  84. };
  85. /* struct representing local mailbox file */
  86. struct mailbox
  87. {
  88. DBM *db;
  89. char *path;
  90. message_t *msgs;
  91. int lockfd;
  92. unsigned int deleted; /* # of deleted messages */
  93. unsigned int uidvalidity;
  94. unsigned int maxuid; /* largest uid we know about */
  95. unsigned int uidseen : 1; /* flag indicating whether or not we saw a
  96. valid value for UIDVALIDITY */
  97. };
  98. /* message dispositions */
  99. #define D_SEEN (1<<0)
  100. #define D_ANSWERED (1<<1)
  101. #define D_DELETED (1<<2)
  102. #define D_FLAGGED (1<<3)
  103. #define D_RECENT (1<<4)
  104. #define D_DRAFT (1<<5)
  105. #define D_MAX 6
  106. struct message
  107. {
  108. char *file;
  109. unsigned int uid;
  110. unsigned int flags;
  111. size_t size;
  112. message_t *next;
  113. unsigned int processed:1; /* message has already been evaluated */
  114. unsigned int new:1; /* message is in the new/ subdir */
  115. unsigned int dead:1; /* message doesn't exist on the server */
  116. unsigned int wanted:1; /* when using MaxMessages, keep this message */
  117. };
  118. /* struct used for parsing IMAP lists */
  119. typedef struct _list list_t;
  120. #define NIL (void*)0x1
  121. #define LIST (void*)0x2
  122. struct _list
  123. {
  124. char *val;
  125. list_t *next;
  126. list_t *child;
  127. };
  128. /* imap connection info */
  129. typedef struct
  130. {
  131. Socket_t *sock;
  132. unsigned int count; /* # of msgs */
  133. unsigned int recent; /* # of recent messages */
  134. buffer_t *buf; /* input buffer for reading server output */
  135. message_t *msgs; /* list of messages on the server */
  136. config_t *box; /* mailbox to open */
  137. char *prefix; /* namespace prefix */
  138. unsigned int deleted; /* # of deleted messages */
  139. unsigned int uidvalidity;
  140. unsigned int maxuid;
  141. unsigned int minuid;
  142. /* NAMESPACE info */
  143. list_t *ns_personal;
  144. list_t *ns_other;
  145. list_t *ns_shared;
  146. unsigned int have_uidplus:1;
  147. unsigned int have_namespace:1;
  148. #if HAVE_LIBSSL
  149. unsigned int have_cram:1;
  150. unsigned int have_starttls:1;
  151. unsigned int cram:1;
  152. #endif
  153. }
  154. imap_t;
  155. /* flags for sync_mailbox */
  156. #define SYNC_DELETE (1<<0) /* delete local that don't exist on server */
  157. #define SYNC_EXPUNGE (1<<1) /* don't fetch deleted messages */
  158. /* flags for maildir_open */
  159. #define OPEN_FAST (1<<0) /* fast open - don't parse */
  160. #define OPEN_CREATE (1<<1) /* create mailbox if nonexistent */
  161. extern config_t global;
  162. extern config_t *boxes;
  163. extern unsigned int Tag;
  164. extern char Hostname[256];
  165. extern int Verbose, Quiet;
  166. extern void info (const char *, ...);
  167. extern void infoc (char);
  168. #if HAVE_LIBSSL
  169. extern SSL_CTX *SSLContext;
  170. char *cram (const char *, const char *, const char *);
  171. #endif
  172. char *next_arg (char **);
  173. int sync_mailbox (mailbox_t *, imap_t *, int, unsigned int, unsigned int);
  174. void load_config (const char *, int *);
  175. char * expand_strdup (const char *s);
  176. config_t *find_box (const char *);
  177. void free_config (void);
  178. void imap_close (imap_t *);
  179. int imap_copy_message (imap_t * imap, unsigned int uid, const char *mailbox);
  180. int imap_fetch_message (imap_t *, unsigned int, int);
  181. int imap_set_flags (imap_t *, unsigned int, unsigned int);
  182. int imap_expunge (imap_t *);
  183. imap_t *imap_connect (config_t *);
  184. imap_t *imap_open (config_t *, unsigned int, imap_t *, int);
  185. int imap_append_message (imap_t *, int, message_t *);
  186. int imap_list (imap_t *);
  187. mailbox_t *maildir_open (const char *, int flags);
  188. int maildir_expunge (mailbox_t *, int);
  189. int maildir_set_uidvalidity (mailbox_t *, unsigned int uidvalidity);
  190. void maildir_close (mailbox_t *);
  191. int maildir_update_maxuid (mailbox_t * mbox);
  192. message_t * find_msg (message_t * list, unsigned int uid);
  193. void free_message (message_t *);
  194. /* parse an IMAP list construct */
  195. list_t * parse_list (char *s, char **end);
  196. int is_atom (list_t *list);
  197. int is_list (list_t *list);
  198. int is_nil (list_t *list);
  199. void free_list (list_t *list);
  200. #define strfcpy(a,b,c) {strncpy(a,b,c);(a)[c-1]=0;}