isync.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*
  2. * mbsync - mailbox synchronizer
  3. * Copyright (C) 2000-2002 Michael R. Elkins <me@mutt.org>
  4. * Copyright (C) 2002-2004 Oswald Buddenhagen <ossi@users.sf.net>
  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 Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  19. *
  20. * As a special exception, mbsync may be linked with the OpenSSL library,
  21. * despite that library's more restrictive license.
  22. */
  23. #define _GNU_SOURCE
  24. #include <config.h>
  25. #include <sys/types.h>
  26. #include <stdarg.h>
  27. #include <stdio.h>
  28. #define as(ar) (sizeof(ar)/sizeof(ar[0]))
  29. #define __stringify(x) #x
  30. #define stringify(x) __stringify(x)
  31. #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)
  32. # define ATTR_UNUSED __attribute__((unused))
  33. # define ATTR_NORETURN __attribute__((noreturn))
  34. # define ATTR_PRINTFLIKE(fmt,var) __attribute__((format(printf,fmt,var)))
  35. #else
  36. # define ATTR_UNUSED
  37. # define ATTR_NORETURN
  38. # define ATTR_PRINTFLIKE(fmt,var)
  39. #endif
  40. #define EXE "mbsync"
  41. typedef struct {
  42. const char *file;
  43. FILE *fp;
  44. char *buf;
  45. int bufl;
  46. int line;
  47. char *cmd, *val, *rest;
  48. } conffile_t;
  49. #define OP_NEW (1<<0)
  50. #define OP_RENEW (1<<1)
  51. #define OP_DELETE (1<<2)
  52. #define OP_FLAGS (1<<3)
  53. #define OP_MASK_TYPE (OP_NEW|OP_RENEW|OP_DELETE|OP_FLAGS) /* asserted in the target ops */
  54. #define OP_EXPUNGE (1<<4)
  55. #define OP_CREATE (1<<5)
  56. #define XOP_PUSH (1<<6)
  57. #define XOP_PULL (1<<7)
  58. #define XOP_MASK_DIR (XOP_PUSH|XOP_PULL)
  59. #define XOP_HAVE_TYPE (1<<8)
  60. #define XOP_HAVE_EXPUNGE (1<<9)
  61. #define XOP_HAVE_CREATE (1<<10)
  62. typedef struct driver driver_t;
  63. typedef struct store_conf {
  64. struct store_conf *next;
  65. char *name;
  66. driver_t *driver;
  67. const char *path; /* should this be here? its interpretation is driver-specific */
  68. const char *map_inbox;
  69. const char *trash;
  70. unsigned max_size; /* off_t is overkill */
  71. unsigned trash_remote_new:1, trash_only_new:1;
  72. } store_conf_t;
  73. typedef struct string_list {
  74. struct string_list *next;
  75. char string[1];
  76. } string_list_t;
  77. #define M 0 /* master */
  78. #define S 1 /* slave */
  79. typedef struct channel_conf {
  80. struct channel_conf *next;
  81. const char *name;
  82. store_conf_t *stores[2];
  83. const char *boxes[2];
  84. char *sync_state;
  85. string_list_t *patterns;
  86. int ops[2];
  87. unsigned max_messages; /* for slave only */
  88. } channel_conf_t;
  89. typedef struct group_conf {
  90. struct group_conf *next;
  91. const char *name;
  92. string_list_t *channels;
  93. } group_conf_t;
  94. /* For message->flags */
  95. /* Keep the mailbox driver flag definitions in sync! */
  96. /* The order is according to alphabetical maildir flag sort */
  97. #define F_DRAFT (1<<0) /* Draft */
  98. #define F_FLAGGED (1<<1) /* Flagged */
  99. #define F_ANSWERED (1<<2) /* Replied */
  100. #define F_SEEN (1<<3) /* Seen */
  101. #define F_DELETED (1<<4) /* Trashed */
  102. #define NUM_FLAGS 5
  103. /* For message->status */
  104. #define M_RECENT (1<<0) /* unsyncable flag; maildir_* depend on this being 1<<0 */
  105. #define M_DEAD (1<<1) /* expunged */
  106. #define M_FLAGS (1<<2) /* flags fetched */
  107. typedef struct message {
  108. struct message *next;
  109. struct sync_rec *srec;
  110. /* string_list_t *keywords; */
  111. size_t size; /* zero implies "not fetched" */
  112. int uid;
  113. unsigned char flags, status;
  114. } message_t;
  115. /* For opts, both in store and driver_t->select() */
  116. #define OPEN_OLD (1<<0)
  117. #define OPEN_NEW (1<<1)
  118. #define OPEN_FLAGS (1<<2)
  119. #define OPEN_SIZE (1<<3)
  120. #define OPEN_CREATE (1<<4)
  121. #define OPEN_EXPUNGE (1<<5)
  122. #define OPEN_SETFLAGS (1<<6)
  123. #define OPEN_APPEND (1<<7)
  124. #define OPEN_FIND (1<<8)
  125. typedef struct store {
  126. store_conf_t *conf; /* foreign */
  127. /* currently open mailbox */
  128. const char *name; /* foreign! maybe preset? */
  129. char *path; /* own */
  130. message_t *msgs; /* own */
  131. int uidvalidity;
  132. unsigned opts; /* maybe preset? */
  133. /* note that the following do _not_ reflect stats from msgs, but mailbox totals */
  134. int count; /* # of messages */
  135. int recent; /* # of recent messages - don't trust this beyond the initial read */
  136. } store_t;
  137. typedef struct {
  138. char *data;
  139. int len;
  140. unsigned char flags;
  141. } msg_data_t;
  142. #define DRV_OK 0
  143. #define DRV_MSG_BAD -1
  144. #define DRV_BOX_BAD -2
  145. #define DRV_STORE_BAD -3
  146. #define DRV_CRLF 1
  147. #define TUIDL 12
  148. struct driver {
  149. int flags;
  150. int (*parse_store)( conffile_t *cfg, store_conf_t **storep, int *err );
  151. store_t *(*open_store)( store_conf_t *conf, store_t *oldctx );
  152. void (*close_store)( store_t *ctx );
  153. int (*list)( store_t *ctx, string_list_t **boxes );
  154. void (*prepare_paths)( store_t *ctx );
  155. void (*prepare_opts)( store_t *ctx, int opts );
  156. int (*select)( store_t *ctx, int minuid, int maxuid, int *excs, int nexcs );
  157. int (*fetch_msg)( store_t *ctx, message_t *msg, msg_data_t *data );
  158. int (*store_msg)( store_t *ctx, msg_data_t *data, int *uid ); /* if uid is null, store to trash */
  159. int (*find_msg)( store_t *ctx, const char *tuid, int *uid );
  160. int (*set_flags)( store_t *ctx, message_t *msg, int uid, int add, int del ); /* msg can be null, therefore uid as a fallback */
  161. int (*trash_msg)( store_t *ctx, message_t *msg ); /* This may expunge the original message immediately, but it needn't to */
  162. int (*check)( store_t *ctx ); /* IMAP-style: flush */
  163. int (*close)( store_t *ctx ); /* IMAP-style: expunge inclusive */
  164. };
  165. /* main.c */
  166. extern int Pid;
  167. extern char Hostname[256];
  168. extern const char *Home;
  169. /* util.c */
  170. #define DEBUG 1
  171. #define VERBOSE 2
  172. #define QUIET 4
  173. #define VERYQUIET 8
  174. #define KEEPJOURNAL 16
  175. extern int DFlags, Ontty;
  176. void debug( const char *, ... );
  177. void debugn( const char *, ... );
  178. void info( const char *, ... );
  179. void infon( const char *, ... );
  180. void infoc( char );
  181. void warn( const char *, ... );
  182. void error( const char *, ... );
  183. char *next_arg( char ** );
  184. void add_string_list( string_list_t **list, const char *str );
  185. void free_string_list( string_list_t *list );
  186. void free_generic_messages( message_t * );
  187. void *nfmalloc( size_t sz );
  188. void *nfcalloc( size_t sz );
  189. void *nfrealloc( void *mem, size_t sz );
  190. char *nfstrdup( const char *str );
  191. int nfvasprintf( char **str, const char *fmt, va_list va );
  192. int nfasprintf( char **str, const char *fmt, ... );
  193. int nfsnprintf( char *buf, int blen, const char *fmt, ... );
  194. void ATTR_NORETURN oob( void );
  195. char *expand_strdup( const char *s );
  196. void sort_ints( int *arr, int len );
  197. void arc4_init( void );
  198. unsigned char arc4_getbyte( void );
  199. /* sync.c */
  200. #define SYNC_OK 0
  201. #define SYNC_FAIL 1
  202. #define SYNC_BAD(ms) (2+(ms))
  203. #define SYNC_NOGOOD 4 /* internal */
  204. int sync_boxes( store_t *ctx[], const char *names[], channel_conf_t * );
  205. /* config.c */
  206. extern channel_conf_t *channels;
  207. extern group_conf_t *groups;
  208. extern int global_ops[2];
  209. extern char *global_sync_state;
  210. int parse_bool( conffile_t *cfile );
  211. int parse_int( conffile_t *cfile );
  212. int parse_size( conffile_t *cfile );
  213. int getcline( conffile_t *cfile );
  214. int merge_ops( int cops, int ops[] );
  215. int load_config( const char *filename, int pseudo );
  216. void parse_generic_store( store_conf_t *store, conffile_t *cfg, int *err );
  217. /* drv_*.c */
  218. extern driver_t maildir_driver, imap_driver;