commit 03b3b566f1b108066294cc73dff8bf1a06b3ae91
Author: Oswald Buddenhagen <[email protected]>
Date:   Sun Dec 8 20:46:40 2013 +0100

    reshuffle sources a bit
    
    split header and move some code to more logical places.

 .gitignore                       |    4 +-
 configure.ac                     |    2 +-
 src/Makefile.am                  |    4 +-
 src/common.h                     |  131 ++++++++
 src/compat/isync.h               |    2 +-
 src/config.c                     |   38 +--
 debian/copyright => src/config.h |   43 ++-
 src/driver.c                     |   68 ++++
 src/driver.h                     |  235 ++++++++++++++
 src/drv_imap.c                   |    4 +-
 src/drv_maildir.c                |    2 +-
 src/isync.h                      |  504 ------------------------------
 src/main.c                       |    5 +-
 src/mdconvert.c                  |    2 +-
 src/socket.c                     |   18 +-
 src/socket.h                     |  118 +++++++
 src/sync.c                       |    6 +-
 src/sync.h                       |   81 +++++
 src/util.c                       |   14 +-
 19 files changed, 692 insertions(+), 589 deletions(-)

diff --git a/.gitignore b/.gitignore
index 08b1f24..95ef6c4 100644
--- a/.gitignore
+++ b/.gitignore
@@ -5,10 +5,10 @@ Makefile
 Makefile.in
 autom4te.cache
 aclocal.m4
+autodefs.h
+autodefs.h.in
 build-stamp
 compile
-config.h
-config.h.in
 config.cache
 config.guess
 config.log
diff --git a/configure.ac b/configure.ac
index 0c49a0c..ed3e124 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1,5 +1,5 @@
 AC_INIT([isync], [1.1.0])
-AC_CONFIG_HEADERS([config.h])
+AC_CONFIG_HEADERS([autodefs.h])
 AM_INIT_AUTOMAKE
 
 AM_MAINTAINER_MODE
diff --git a/src/Makefile.am b/src/Makefile.am
index 3cccc7f..9cfd0af 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -5,9 +5,9 @@ SUBDIRS = $(compat_dir)
 
 bin_PROGRAMS = mbsync mdconvert
 
-mbsync_SOURCES = main.c sync.c config.c util.c socket.c drv_imap.c 
drv_maildir.c
+mbsync_SOURCES = main.c sync.c config.c util.c socket.c driver.c drv_imap.c 
drv_maildir.c
 mbsync_LDADD = -ldb $(SSL_LIBS) $(SOCK_LIBS)
-noinst_HEADERS = isync.h
+noinst_HEADERS = common.h config.h driver.h sync.h socket.h
 
 mdconvert_SOURCES = mdconvert.c
 mdconvert_LDADD = -ldb
diff --git a/src/common.h b/src/common.h
new file mode 100644
index 0000000..d1ec8b7
--- /dev/null
+++ b/src/common.h
@@ -0,0 +1,131 @@
+/*
+ * mbsync - mailbox synchronizer
+ * Copyright (C) 2000-2002 Michael R. Elkins <[email protected]>
+ * Copyright (C) 2002-2006,2010-2012 Oswald Buddenhagen <[email protected]>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * As a special exception, mbsync may be linked with the OpenSSL library,
+ * despite that library's more restrictive license.
+ */
+
+#ifndef COMMON_H
+#define COMMON_H
+
+#include <autodefs.h>
+
+#include <sys/types.h>
+#include <stdarg.h>
+#include <stdio.h>
+
+#define as(ar) (sizeof(ar)/sizeof(ar[0]))
+
+#define __stringify(x) #x
+#define stringify(x) __stringify(x)
+
+#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)
+# define ATTR_UNUSED __attribute__((unused))
+# define ATTR_NORETURN __attribute__((noreturn))
+# define ATTR_PRINTFLIKE(fmt,var) __attribute__((format(printf,fmt,var)))
+#else
+# define ATTR_UNUSED
+# define ATTR_NORETURN
+# define ATTR_PRINTFLIKE(fmt,var)
+#endif
+
+#ifdef __GNUC__
+# define INLINE __inline__
+#else
+# define INLINE
+#endif
+
+#define EXE "mbsync"
+
+/* main.c */
+
+#define DEBUG        1
+#define VERBOSE      2
+#define XVERBOSE     4
+#define QUIET        8
+#define VERYQUIET    16
+#define KEEPJOURNAL  32
+#define ZERODELAY    64
+#define CRASHDEBUG   128
+
+extern int DFlags;
+extern int UseFSync;
+
+extern int Pid;
+extern char Hostname[256];
+extern const char *Home;
+
+/* util.c */
+
+void ATTR_PRINTFLIKE(1, 2) debug( const char *, ... );
+void ATTR_PRINTFLIKE(1, 2) debugn( const char *, ... );
+void ATTR_PRINTFLIKE(1, 2) info( const char *, ... );
+void ATTR_PRINTFLIKE(1, 2) infon( const char *, ... );
+void ATTR_PRINTFLIKE(1, 2) warn( const char *, ... );
+void ATTR_PRINTFLIKE(1, 2) error( const char *, ... );
+void ATTR_PRINTFLIKE(1, 2) sys_error( const char *, ... );
+void flushn( void );
+
+typedef struct string_list {
+       struct string_list *next;
+       char string[1];
+} string_list_t;
+
+void add_string_list_n( string_list_t **list, const char *str, int len );
+void add_string_list( string_list_t **list, const char *str );
+void free_string_list( string_list_t *list );
+
+#ifndef HAVE_MEMRCHR
+void *memrchr( const void *s, int c, size_t n );
+#endif
+
+void *nfmalloc( size_t sz );
+void *nfcalloc( size_t sz );
+void *nfrealloc( void *mem, size_t sz );
+char *nfstrdup( const char *str );
+int nfvasprintf( char **str, const char *fmt, va_list va );
+int ATTR_PRINTFLIKE(2, 3) nfasprintf( char **str, const char *fmt, ... );
+int ATTR_PRINTFLIKE(3, 4) nfsnprintf( char *buf, int blen, const char *fmt, 
... );
+void ATTR_NORETURN oob( void );
+
+char *expand_strdup( const char *s );
+
+int map_name( const char *arg, char **result, int reserve, const char *in, 
const char *out );
+
+void sort_ints( int *arr, int len );
+
+void arc4_init( void );
+unsigned char arc4_getbyte( void );
+
+int bucketsForSize( int size );
+
+#ifdef HAVE_SYS_POLL_H
+# include <sys/poll.h>
+#else
+# define POLLIN 1
+# define POLLOUT 4
+# define POLLERR 8
+#endif
+
+void add_fd( int fd, void (*cb)( int events, void *aux ), void *aux );
+void conf_fd( int fd, int and_events, int or_events );
+void fake_fd( int fd, int events );
+void del_fd( int fd );
+void main_loop( void );
+
+#endif
diff --git a/src/compat/isync.h b/src/compat/isync.h
index a1aee3f..1181aae 100644
--- a/src/compat/isync.h
+++ b/src/compat/isync.h
@@ -17,7 +17,7 @@
  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-#include <config.h>
+#include <autodefs.h>
 
 #include <sys/types.h>
 #include <stdarg.h>
diff --git a/src/config.c b/src/config.c
index 0181f7e..7743615 100644
--- a/src/config.c
+++ b/src/config.c
@@ -20,7 +20,9 @@
  * despite that library's more restrictive license.
  */
 
-#include "isync.h"
+#include "config.h"
+
+#include "sync.h"
 
 #include <assert.h>
 #include <unistd.h>
@@ -32,13 +34,7 @@
 #include <stdlib.h>
 #include <stdio.h>
 
-driver_t *drivers[N_DRIVERS] = { &maildir_driver, &imap_driver };
-
-channel_conf_t global_conf;
 store_conf_t *stores;
-channel_conf_t *channels;
-group_conf_t *groups;
-int UseFSync = 1;
 
 #define ARG_OPTIONAL 0
 #define ARG_REQUIRED 1
@@ -491,31 +487,3 @@ load_config( const char *where, int pseudo )
                unlink( where );
        return cfile.err;
 }
-
-void
-parse_generic_store( store_conf_t *store, conffile_t *cfg )
-{
-       if (!strcasecmp( "Trash", cfg->cmd ))
-               store->trash = nfstrdup( cfg->val );
-       else if (!strcasecmp( "TrashRemoteNew", cfg->cmd ))
-               store->trash_remote_new = parse_bool( cfg );
-       else if (!strcasecmp( "TrashNewOnly", cfg->cmd ))
-               store->trash_only_new = parse_bool( cfg );
-       else if (!strcasecmp( "MaxSize", cfg->cmd ))
-               store->max_size = parse_size( cfg );
-       else if (!strcasecmp( "MapInbox", cfg->cmd ))
-               store->map_inbox = nfstrdup( cfg->val );
-       else if (!strcasecmp( "Flatten", cfg->cmd )) {
-               const char *p;
-               for (p = cfg->val; *p; p++)
-                       if (*p == '/') {
-                               error( "%s:%d: flattened hierarchy delimiter 
cannot contain the canonical delimiter '/'\n", cfg->file, cfg->line );
-                               cfg->err = 1;
-                               return;
-                       }
-               store->flat_delim = nfstrdup( cfg->val );
-       } else {
-               error( "%s:%d: unknown keyword '%s'\n", cfg->file, cfg->line, 
cfg->cmd );
-               cfg->err = 1;
-       }
-}
diff --git a/debian/copyright b/src/config.h
similarity index 55%
copy from debian/copyright
copy to src/config.h
index c1bb05f..b9b4bfe 100644
--- a/debian/copyright
+++ b/src/config.h
@@ -1,16 +1,7 @@
-This package was debianized by Tommi Virtanen <[email protected]> and is now
-maintained by Nicolas Boullis <[email protected]>.
-
-It was downloaded from http://isync.sourceforge.net/
-
-Upstream Author: Michael R. Elkins <[email protected]>,
-                 Oswald Buddenhagen <[email protected]>
-
-Copyright:
-
- * isync - IMAP4 to maildir mailbox synchronizer
+/*
+ * mbsync - mailbox synchronizer
  * Copyright (C) 2000-2002 Michael R. Elkins <[email protected]>
- * Copyright (C) 2002-2006 Oswald Buddenhagen <[email protected]>
+ * Copyright (C) 2002-2006,2010-2012 Oswald Buddenhagen <[email protected]>
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
@@ -25,8 +16,30 @@ Copyright:
  *  You should have received a copy of the GNU General Public License
  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
  *
- * As a special exception, isync may be linked with the OpenSSL library,
+ * As a special exception, mbsync may be linked with the OpenSSL library,
  * despite that library's more restrictive license.
+ */
+
+#ifndef CONFIG_H
+#define CONFIG_H
+
+#include "common.h"
+
+typedef struct conffile {
+       const char *file;
+       FILE *fp;
+       char *buf;
+       int bufl;
+       int line;
+       int err;
+       char *cmd, *val, *rest;
+} conffile_t;
+
+int parse_bool( conffile_t *cfile );
+int parse_int( conffile_t *cfile );
+int parse_size( conffile_t *cfile );
+int getcline( conffile_t *cfile );
+int merge_ops( int cops, int ops[] );
+int load_config( const char *filename, int pseudo );
 
-On Debian systems, the complete text of the GNU General Public
-License can be found in /usr/share/common-licenses/GPL
+#endif
diff --git a/src/driver.c b/src/driver.c
new file mode 100644
index 0000000..86c172b
--- /dev/null
+++ b/src/driver.c
@@ -0,0 +1,68 @@
+/*
+ * mbsync - mailbox synchronizer
+ * Copyright (C) 2000-2002 Michael R. Elkins <[email protected]>
+ * Copyright (C) 2002-2006,2010-2012 Oswald Buddenhagen <[email protected]>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * As a special exception, mbsync may be linked with the OpenSSL library,
+ * despite that library's more restrictive license.
+ */
+
+#include "driver.h"
+
+#include <stdlib.h>
+#include <string.h>
+
+driver_t *drivers[N_DRIVERS] = { &maildir_driver, &imap_driver };
+
+void
+free_generic_messages( message_t *msgs )
+{
+       message_t *tmsg;
+
+       for (; msgs; msgs = tmsg) {
+               tmsg = msgs->next;
+               free( msgs );
+       }
+}
+
+void
+parse_generic_store( store_conf_t *store, conffile_t *cfg )
+{
+       if (!strcasecmp( "Trash", cfg->cmd )) {
+               store->trash = nfstrdup( cfg->val );
+       } else if (!strcasecmp( "TrashRemoteNew", cfg->cmd )) {
+               store->trash_remote_new = parse_bool( cfg );
+       } else if (!strcasecmp( "TrashNewOnly", cfg->cmd )) {
+               store->trash_only_new = parse_bool( cfg );
+       } else if (!strcasecmp( "MaxSize", cfg->cmd )) {
+               store->max_size = parse_size( cfg );
+       } else if (!strcasecmp( "MapInbox", cfg->cmd )) {
+               store->map_inbox = nfstrdup( cfg->val );
+       } else if (!strcasecmp( "Flatten", cfg->cmd )) {
+               const char *p;
+               for (p = cfg->val; *p; p++) {
+                       if (*p == '/') {
+                               error( "%s:%d: flattened hierarchy delimiter 
cannot contain the canonical delimiter '/'\n", cfg->file, cfg->line );
+                               cfg->err = 1;
+                               return;
+                       }
+               }
+               store->flat_delim = nfstrdup( cfg->val );
+       } else {
+               error( "%s:%d: unknown keyword '%s'\n", cfg->file, cfg->line, 
cfg->cmd );
+               cfg->err = 1;
+       }
+}
diff --git a/src/driver.h b/src/driver.h
new file mode 100644
index 0000000..9fd1428
--- /dev/null
+++ b/src/driver.h
@@ -0,0 +1,235 @@
+/*
+ * mbsync - mailbox synchronizer
+ * Copyright (C) 2000-2002 Michael R. Elkins <[email protected]>
+ * Copyright (C) 2002-2006,2010-2012 Oswald Buddenhagen <[email protected]>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * As a special exception, mbsync may be linked with the OpenSSL library,
+ * despite that library's more restrictive license.
+ */
+
+#ifndef DRIVER_H
+#define DRIVER_H
+
+#include "config.h"
+
+typedef struct driver driver_t;
+
+typedef struct store_conf {
+       struct store_conf *next;
+       char *name;
+       driver_t *driver;
+       const char *path; /* should this be here? its interpretation is 
driver-specific */
+       const char *flat_delim;
+       const char *map_inbox;
+       const char *trash;
+       unsigned max_size; /* off_t is overkill */
+       char trash_remote_new, trash_only_new;
+} store_conf_t;
+
+/* For message->flags */
+/* Keep the mailbox driver flag definitions in sync! */
+/* The order is according to alphabetical maildir flag sort */
+#define F_DRAFT             (1<<0) /* Draft */
+#define F_FLAGGED    (1<<1) /* Flagged */
+#define F_ANSWERED   (1<<2) /* Replied */
+#define F_SEEN       (1<<3) /* Seen */
+#define F_DELETED    (1<<4) /* Trashed */
+#define NUM_FLAGS 5
+
+/* For message->status */
+#define M_RECENT       (1<<0) /* unsyncable flag; maildir_* depend on this 
being 1<<0 */
+#define M_DEAD         (1<<1) /* expunged */
+#define M_FLAGS        (1<<2) /* flags fetched */
+
+#define TUIDL 12
+
+typedef struct message {
+       struct message *next;
+       struct sync_rec *srec;
+       /* string_list_t *keywords; */
+       size_t size; /* zero implies "not fetched" */
+       int uid;
+       unsigned char flags, status;
+       char tuid[TUIDL];
+} message_t;
+
+/* For opts, both in store and driver_t->select() */
+#define OPEN_OLD        (1<<0)
+#define OPEN_NEW        (1<<1)
+#define OPEN_FLAGS      (1<<2)
+#define OPEN_SIZE       (1<<3)
+#define OPEN_EXPUNGE    (1<<5)
+#define OPEN_SETFLAGS   (1<<6)
+#define OPEN_APPEND     (1<<7)
+#define OPEN_FIND       (1<<8)
+
+typedef struct store {
+       struct store *next;
+       store_conf_t *conf; /* foreign */
+       string_list_t *boxes; /* _list results - own */
+       char listed; /* was _list already run? */
+
+       void (*bad_callback)( void *aux );
+       void *bad_callback_aux;
+
+       /* currently open mailbox */
+       const char *orig_name; /* foreign! maybe preset? */
+       char *name; /* foreign! maybe preset? */
+       char *path; /* own */
+       message_t *msgs; /* own */
+       int uidvalidity;
+       int uidnext; /* from SELECT responses */
+       unsigned opts; /* maybe preset? */
+       /* note that the following do _not_ reflect stats from msgs, but 
mailbox totals */
+       int count; /* # of messages */
+       int recent; /* # of recent messages - don't trust this beyond the 
initial read */
+} store_t;
+
+/* When the callback is invoked (at most once per store), the store is fubar;
+ * call the driver's cancel_store() to dispose of it. */
+static INLINE void
+set_bad_callback( store_t *ctx, void (*cb)( void *aux ), void *aux )
+{
+       ctx->bad_callback = cb;
+       ctx->bad_callback_aux = aux;
+}
+
+typedef struct {
+       char *data;
+       int len;
+       time_t date;
+       unsigned char flags;
+} msg_data_t;
+
+#define DRV_OK          0
+/* Message went missing, or mailbox is full, etc. */
+#define DRV_MSG_BAD     1
+/* Something is wrong with the current mailbox - probably it is somehow 
inaccessible. */
+#define DRV_BOX_BAD     2
+/* The command has been cancel()ed or cancel_store()d. */
+#define DRV_CANCELED    3
+
+/* All memory belongs to the driver's user, unless stated otherwise. */
+
+/*
+   This flag says that the driver CAN store messages with CRLFs,
+   not that it must. The lack of it OTOH implies that it CANNOT,
+   and as CRLF is the canonical format, we convert.
+*/
+#define DRV_CRLF        1
+/*
+   This flag says that the driver will act upon (DFlags & VERBOSE).
+*/
+#define DRV_VERBOSE     2
+
+#define LIST_PATH       1
+#define LIST_INBOX      2
+
+struct driver {
+       int flags;
+
+       /* Parse configuration. */
+       int (*parse_store)( conffile_t *cfg, store_conf_t **storep );
+
+       /* Close remaining server connections. All stores must be disowned 
first. */
+       void (*cleanup)( void );
+
+       /* Open a store with the given configuration. This may recycle existing
+        * server connections. Upon failure, a null store is passed to the 
callback. */
+       void (*open_store)( store_conf_t *conf, const char *label,
+                           void (*cb)( store_t *ctx, void *aux ), void *aux );
+
+       /* Mark the store as available for recycling. Server connection may be 
kept alive. */
+       void (*disown_store)( store_t *ctx );
+
+       /* Discard the store after a bad_callback. The server connections will 
be closed.
+        * Pending commands will have their callbacks synchronously invoked 
with DRV_CANCELED. */
+       void (*cancel_store)( store_t *ctx );
+
+       /* List the mailboxes in this store. Flags are ORed LIST_* values. */
+       void (*list)( store_t *ctx, int flags,
+                     void (*cb)( int sts, void *aux ), void *aux );
+
+       /* Invoked before select(), this informs the driver which operations 
(OP_*)
+        * will be performed on the mailbox. The driver may extend the set by 
implicitly
+        * needed or available operations. */
+       void (*prepare_opts)( store_t *ctx, int opts );
+
+       /* Open the mailbox ctx->name. Optionally create missing boxes.
+        * As a side effect, this should resolve ctx->path if applicable. */
+       void (*select)( store_t *ctx, int create,
+                      void (*cb)( int sts, void *aux ), void *aux );
+
+       /* Load the message attributes needed to perform the requested 
operations.
+        * Consider only messages with UIDs between minuid and maxuid 
(inclusive)
+        * and those named in the excs array (smaller than minuid).
+        * The driver takes ownership of the excs array. Messages below newuid 
do not need
+        * to have the TUID populated even if OPEN_FIND is set. */
+       void (*load)( store_t *ctx, int minuid, int maxuid, int newuid, int 
*excs, int nexcs,
+                     void (*cb)( int sts, void *aux ), void *aux );
+
+       /* Fetch the contents and flags of the given message from the current 
mailbox. */
+       void (*fetch_msg)( store_t *ctx, message_t *msg, msg_data_t *data,
+                          void (*cb)( int sts, void *aux ), void *aux );
+
+       /* Store the given message to either the current mailbox or the trash 
folder.
+        * If the new copy's UID can be immediately determined, return it, 
otherwise -2. */
+       void (*store_msg)( store_t *ctx, msg_data_t *data, int to_trash,
+                          void (*cb)( int sts, int uid, void *aux ), void *aux 
);
+
+       /* Index the messages which have newly appeared in the mailbox, 
including their
+        * temporary UID headers. This is needed if store_msg() does not 
guarantee returning
+        * a UID; otherwise the driver needs to implement only the OPEN_FIND 
flag. */
+       void (*find_new_msgs)( store_t *ctx,
+                              void (*cb)( int sts, void *aux ), void *aux );
+
+       /* Add/remove the named flags to/from the given message. The message 
may be either
+        * a pre-fetched one (in which case the in-memory representation is 
updated),
+        * or it may be identifed by UID only. The operation may be delayed 
until commit()
+        * is called. */
+       void (*set_flags)( store_t *ctx, message_t *msg, int uid, int add, int 
del, /* msg can be null, therefore uid as a fallback */
+                          void (*cb)( int sts, void *aux ), void *aux );
+
+       /* Move the given message from the current mailbox to the trash folder.
+        * This may expunge the original message immediately, but it needn't 
to. */
+       void (*trash_msg)( store_t *ctx, message_t *msg, /* This may expunge 
the original message immediately, but it needn't to */
+                          void (*cb)( int sts, void *aux ), void *aux );
+
+       /* Expunge deleted messages from the current mailbox and close it.
+        * There is no need to explicitly close a mailbox if no expunge is 
needed. */
+       void (*close)( store_t *ctx, /* IMAP-style: expunge inclusive */
+                      void (*cb)( int sts, void *aux ), void *aux );
+
+       /* Cancel queued commands which are not in flight yet; they will have 
their
+        * callbacks invoked with DRV_CANCELED. Afterwards, wait for the 
completion of
+        * the in-flight commands. If the store is canceled before this command 
completes,
+        * the callback will *not* be invoked. */
+       void (*cancel)( store_t *ctx,
+                       void (*cb)( void *aux ), void *aux );
+
+       /* Commit any pending set_flags() commands. */
+       void (*commit)( store_t *ctx );
+};
+
+void free_generic_messages( message_t * );
+
+void parse_generic_store( store_conf_t *store, conffile_t *cfg );
+
+#define N_DRIVERS 2
+extern driver_t *drivers[N_DRIVERS];
+extern driver_t maildir_driver, imap_driver;
+
+#endif
diff --git a/src/drv_imap.c b/src/drv_imap.c
index 9f1c0ee..502be43 100644
--- a/src/drv_imap.c
+++ b/src/drv_imap.c
@@ -21,7 +21,9 @@
  * despite that library's more restrictive license.
  */
 
-#include "isync.h"
+#include "driver.h"
+
+#include "socket.h"
 
 #include <assert.h>
 #include <unistd.h>
diff --git a/src/drv_maildir.c b/src/drv_maildir.c
index f76fb30..b62848e 100644
--- a/src/drv_maildir.c
+++ b/src/drv_maildir.c
@@ -21,7 +21,7 @@
  * despite that library's more restrictive license.
  */
 
-#include "isync.h"
+#include "driver.h"
 
 #include <assert.h>
 #include <limits.h>
diff --git a/src/isync.h b/src/isync.h
deleted file mode 100644
index 35e815f..0000000
--- a/src/isync.h
+++ /dev/null
@@ -1,504 +0,0 @@
-/*
- * mbsync - mailbox synchronizer
- * Copyright (C) 2000-2002 Michael R. Elkins <[email protected]>
- * Copyright (C) 2002-2006,2010-2012 Oswald Buddenhagen <[email protected]>
- *
- *  This program is free software; you can redistribute it and/or modify
- *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 2 of the License, or
- *  (at your option) any later version.
- *
- *  This program is distributed in the hope that it will be useful,
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *  GNU General Public License for more details.
- *
- *  You should have received a copy of the GNU General Public License
- *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
- *
- * As a special exception, mbsync may be linked with the OpenSSL library,
- * despite that library's more restrictive license.
- */
-
-#include <config.h>
-
-#include <sys/types.h>
-#include <stdarg.h>
-#include <stdio.h>
-
-#define as(ar) (sizeof(ar)/sizeof(ar[0]))
-
-#define __stringify(x) #x
-#define stringify(x) __stringify(x)
-
-#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)
-# define ATTR_UNUSED __attribute__((unused))
-# define ATTR_NORETURN __attribute__((noreturn))
-# define ATTR_PRINTFLIKE(fmt,var) __attribute__((format(printf,fmt,var)))
-#else
-# define ATTR_UNUSED
-# define ATTR_NORETURN
-# define ATTR_PRINTFLIKE(fmt,var)
-#endif
-
-#ifdef __GNUC__
-# define INLINE __inline__
-#else
-# define INLINE
-#endif
-
-#define EXE "mbsync"
-
-typedef struct ssl_st SSL;
-typedef struct ssl_ctx_st SSL_CTX;
-typedef struct x509_store_st X509_STORE;
-
-typedef struct server_conf {
-       char *tunnel;
-       char *host;
-       int port;
-#ifdef HAVE_LIBSSL
-       char *cert_file;
-       char use_imaps;
-       char use_sslv2, use_sslv3, use_tlsv1, use_tlsv11, use_tlsv12;
-
-       /* these are actually variables and are leaked at the end */
-       char ssl_ctx_valid;
-       unsigned num_trusted;
-       SSL_CTX *SSLContext;
-#endif
-} server_conf_t;
-
-typedef struct buff_chunk {
-       struct buff_chunk *next;
-       char *data;
-       int len;
-       char buf[1];
-} buff_chunk_t;
-
-typedef struct {
-       /* connection */
-       int fd;
-       int state;
-       const server_conf_t *conf; /* needed during connect */
-#ifdef HAVE_IPV6
-       struct addrinfo *addrs, *curr_addr; /* needed during connect */
-#else
-       char **curr_addr; /* needed during connect */
-#endif
-       char *name;
-#ifdef HAVE_LIBSSL
-       SSL *ssl;
-       int force_trusted;
-#endif
-
-       void (*bad_callback)( void *aux ); /* async fail while sending or 
listening */
-       void (*read_callback)( void *aux ); /* data available for reading */
-       int (*write_callback)( void *aux ); /* all *queued* data was sent */
-       union {
-               void (*connect)( int ok, void *aux );
-               void (*starttls)( int ok, void *aux );
-       } callbacks;
-       void *callback_aux;
-
-       /* writing */
-       buff_chunk_t *write_buf, **write_buf_append; /* buffer head & tail */
-       int write_offset; /* offset into buffer head */
-
-       /* reading */
-       int offset; /* start of filled bytes in buffer */
-       int bytes; /* number of filled bytes in buffer */
-       int scanoff; /* offset to continue scanning for newline at, relative to 
'offset' */
-       char buf[100000];
-} conn_t;
-
-typedef struct {
-       const char *file;
-       FILE *fp;
-       char *buf;
-       int bufl;
-       int line;
-       int err;
-       char *cmd, *val, *rest;
-} conffile_t;
-
-#define OP_NEW             (1<<0)
-#define OP_RENEW           (1<<1)
-#define OP_DELETE          (1<<2)
-#define OP_FLAGS           (1<<3)
-#define  OP_MASK_TYPE      (OP_NEW|OP_RENEW|OP_DELETE|OP_FLAGS) /* asserted in 
the target ops */
-#define OP_EXPUNGE         (1<<4)
-#define OP_CREATE          (1<<5)
-#define XOP_PUSH           (1<<6)
-#define XOP_PULL           (1<<7)
-#define  XOP_MASK_DIR      (XOP_PUSH|XOP_PULL)
-#define XOP_HAVE_TYPE      (1<<8)
-#define XOP_HAVE_EXPUNGE   (1<<9)
-#define XOP_HAVE_CREATE    (1<<10)
-
-typedef struct driver driver_t;
-
-typedef struct store_conf {
-       struct store_conf *next;
-       char *name;
-       driver_t *driver;
-       const char *path; /* should this be here? its interpretation is 
driver-specific */
-       const char *flat_delim;
-       const char *map_inbox;
-       const char *trash;
-       unsigned max_size; /* off_t is overkill */
-       char trash_remote_new, trash_only_new;
-} store_conf_t;
-
-typedef struct string_list {
-       struct string_list *next;
-       char string[1];
-} string_list_t;
-
-#define M 0 /* master */
-#define S 1 /* slave */
-
-typedef struct channel_conf {
-       struct channel_conf *next;
-       const char *name;
-       store_conf_t *stores[2];
-       const char *boxes[2];
-       char *sync_state;
-       string_list_t *patterns;
-       int ops[2];
-       unsigned max_messages; /* for slave only */
-       signed char expire_unread;
-       char use_internal_date;
-} channel_conf_t;
-
-typedef struct group_conf {
-       struct group_conf *next;
-       const char *name;
-       string_list_t *channels;
-} group_conf_t;
-
-/* For message->flags */
-/* Keep the mailbox driver flag definitions in sync! */
-/* The order is according to alphabetical maildir flag sort */
-#define F_DRAFT             (1<<0) /* Draft */
-#define F_FLAGGED    (1<<1) /* Flagged */
-#define F_ANSWERED   (1<<2) /* Replied */
-#define F_SEEN       (1<<3) /* Seen */
-#define F_DELETED    (1<<4) /* Trashed */
-#define NUM_FLAGS 5
-
-/* For message->status */
-#define M_RECENT       (1<<0) /* unsyncable flag; maildir_* depend on this 
being 1<<0 */
-#define M_DEAD         (1<<1) /* expunged */
-#define M_FLAGS        (1<<2) /* flags fetched */
-
-#define TUIDL 12
-
-typedef struct message {
-       struct message *next;
-       struct sync_rec *srec;
-       /* string_list_t *keywords; */
-       size_t size; /* zero implies "not fetched" */
-       int uid;
-       unsigned char flags, status;
-       char tuid[TUIDL];
-} message_t;
-
-/* For opts, both in store and driver_t->select() */
-#define OPEN_OLD        (1<<0)
-#define OPEN_NEW        (1<<1)
-#define OPEN_FLAGS      (1<<2)
-#define OPEN_SIZE       (1<<3)
-#define OPEN_EXPUNGE    (1<<5)
-#define OPEN_SETFLAGS   (1<<6)
-#define OPEN_APPEND     (1<<7)
-#define OPEN_FIND       (1<<8)
-
-typedef struct store {
-       struct store *next;
-       store_conf_t *conf; /* foreign */
-       string_list_t *boxes; /* _list results - own */
-       char listed; /* was _list already run? */
-
-       void (*bad_callback)( void *aux );
-       void *bad_callback_aux;
-
-       /* currently open mailbox */
-       const char *orig_name; /* foreign! maybe preset? */
-       char *name; /* foreign! maybe preset? */
-       char *path; /* own */
-       message_t *msgs; /* own */
-       int uidvalidity;
-       int uidnext; /* from SELECT responses */
-       unsigned opts; /* maybe preset? */
-       /* note that the following do _not_ reflect stats from msgs, but 
mailbox totals */
-       int count; /* # of messages */
-       int recent; /* # of recent messages - don't trust this beyond the 
initial read */
-} store_t;
-
-/* When the callback is invoked (at most once per store), the store is fubar;
- * call the driver's cancel_store() to dispose of it. */
-static INLINE void
-set_bad_callback( store_t *ctx, void (*cb)( void *aux ), void *aux )
-{
-       ctx->bad_callback = cb;
-       ctx->bad_callback_aux = aux;
-}
-
-typedef struct {
-       char *data;
-       int len;
-       time_t date;
-       unsigned char flags;
-} msg_data_t;
-
-#define DRV_OK          0
-/* Message went missing, or mailbox is full, etc. */
-#define DRV_MSG_BAD     1
-/* Something is wrong with the current mailbox - probably it is somehow 
inaccessible. */
-#define DRV_BOX_BAD     2
-/* The command has been cancel()ed or cancel_store()d. */
-#define DRV_CANCELED    3
-
-/* All memory belongs to the driver's user, unless stated otherwise. */
-
-/*
-   This flag says that the driver CAN store messages with CRLFs,
-   not that it must. The lack of it OTOH implies that it CANNOT,
-   and as CRLF is the canonical format, we convert.
-*/
-#define DRV_CRLF        1
-/*
-   This flag says that the driver will act upon (DFlags & VERBOSE).
-*/
-#define DRV_VERBOSE     2
-
-#define LIST_PATH       1
-#define LIST_INBOX      2
-
-struct driver {
-       int flags;
-
-       /* Parse configuration. */
-       int (*parse_store)( conffile_t *cfg, store_conf_t **storep );
-
-       /* Close remaining server connections. All stores must be disowned 
first. */
-       void (*cleanup)( void );
-
-       /* Open a store with the given configuration. This may recycle existing
-        * server connections. Upon failure, a null store is passed to the 
callback. */
-       void (*open_store)( store_conf_t *conf, const char *label,
-                           void (*cb)( store_t *ctx, void *aux ), void *aux );
-
-       /* Mark the store as available for recycling. Server connection may be 
kept alive. */
-       void (*disown_store)( store_t *ctx );
-
-       /* Discard the store after a bad_callback. The server connections will 
be closed.
-        * Pending commands will have their callbacks synchronously invoked 
with DRV_CANCELED. */
-       void (*cancel_store)( store_t *ctx );
-
-       /* List the mailboxes in this store. Flags are ORed LIST_* values. */
-       void (*list)( store_t *ctx, int flags,
-                     void (*cb)( int sts, void *aux ), void *aux );
-
-       /* Invoked before select(), this informs the driver which operations 
(OP_*)
-        * will be performed on the mailbox. The driver may extend the set by 
implicitly
-        * needed or available operations. */
-       void (*prepare_opts)( store_t *ctx, int opts );
-
-       /* Open the mailbox ctx->name. Optionally create missing boxes.
-        * As a side effect, this should resolve ctx->path if applicable. */
-       void (*select)( store_t *ctx, int create,
-                      void (*cb)( int sts, void *aux ), void *aux );
-
-       /* Load the message attributes needed to perform the requested 
operations.
-        * Consider only messages with UIDs between minuid and maxuid 
(inclusive)
-        * and those named in the excs array (smaller than minuid).
-        * The driver takes ownership of the excs array. Messages below newuid 
do not need
-        * to have the TUID populated even if OPEN_FIND is set. */
-       void (*load)( store_t *ctx, int minuid, int maxuid, int newuid, int 
*excs, int nexcs,
-                     void (*cb)( int sts, void *aux ), void *aux );
-
-       /* Fetch the contents and flags of the given message from the current 
mailbox. */
-       void (*fetch_msg)( store_t *ctx, message_t *msg, msg_data_t *data,
-                          void (*cb)( int sts, void *aux ), void *aux );
-
-       /* Store the given message to either the current mailbox or the trash 
folder.
-        * If the new copy's UID can be immediately determined, return it, 
otherwise -2. */
-       void (*store_msg)( store_t *ctx, msg_data_t *data, int to_trash,
-                          void (*cb)( int sts, int uid, void *aux ), void *aux 
);
-
-       /* Index the messages which have newly appeared in the mailbox, 
including their
-        * temporary UID headers. This is needed if store_msg() does not 
guarantee returning
-        * a UID; otherwise the driver needs to implement only the OPEN_FIND 
flag. */
-       void (*find_new_msgs)( store_t *ctx,
-                              void (*cb)( int sts, void *aux ), void *aux );
-
-       /* Add/remove the named flags to/from the given message. The message 
may be either
-        * a pre-fetched one (in which case the in-memory representation is 
updated),
-        * or it may be identifed by UID only. The operation may be delayed 
until commit()
-        * is called. */
-       void (*set_flags)( store_t *ctx, message_t *msg, int uid, int add, int 
del, /* msg can be null, therefore uid as a fallback */
-                          void (*cb)( int sts, void *aux ), void *aux );
-
-       /* Move the given message from the current mailbox to the trash folder.
-        * This may expunge the original message immediately, but it needn't 
to. */
-       void (*trash_msg)( store_t *ctx, message_t *msg, /* This may expunge 
the original message immediately, but it needn't to */
-                          void (*cb)( int sts, void *aux ), void *aux );
-
-       /* Expunge deleted messages from the current mailbox and close it.
-        * There is no need to explicitly close a mailbox if no expunge is 
needed. */
-       void (*close)( store_t *ctx, /* IMAP-style: expunge inclusive */
-                      void (*cb)( int sts, void *aux ), void *aux );
-
-       /* Cancel queued commands which are not in flight yet; they will have 
their
-        * callbacks invoked with DRV_CANCELED. Afterwards, wait for the 
completion of
-        * the in-flight commands. If the store is canceled before this command 
completes,
-        * the callback will *not* be invoked. */
-       void (*cancel)( store_t *ctx,
-                       void (*cb)( void *aux ), void *aux );
-
-       /* Commit any pending set_flags() commands. */
-       void (*commit)( store_t *ctx );
-};
-
-
-/* main.c */
-
-extern int Pid;
-extern char Hostname[256];
-extern const char *Home;
-
-
-/* socket.c */
-
-/* call this before doing anything with the socket */
-static INLINE void socket_init( conn_t *conn,
-                                const server_conf_t *conf,
-                                void (*bad_callback)( void *aux ),
-                                void (*read_callback)( void *aux ),
-                                int (*write_callback)( void *aux ),
-                                void *aux )
-{
-       conn->conf = conf;
-       conn->bad_callback = bad_callback;
-       conn->read_callback = read_callback;
-       conn->write_callback = write_callback;
-       conn->callback_aux = aux;
-       conn->fd = -1;
-       conn->name = 0;
-       conn->write_buf_append = &conn->write_buf;
-}
-void socket_connect( conn_t *conn, void (*cb)( int ok, void *aux ) );
-void socket_start_tls(conn_t *conn, void (*cb)( int ok, void *aux ) );
-void socket_close( conn_t *sock );
-int socket_read( conn_t *sock, char *buf, int len ); /* never waits */
-char *socket_read_line( conn_t *sock ); /* don't free return value; never 
waits */
-typedef enum { KeepOwn = 0, GiveOwn } ownership_t;
-int socket_write( conn_t *sock, char *buf, int len, ownership_t takeOwn );
-
-void cram( const char *challenge, const char *user, const char *pass,
-           char **_final, int *_finallen );
-
-
-/* util.c */
-
-#define DEBUG        1
-#define VERBOSE      2
-#define XVERBOSE     4
-#define QUIET        8
-#define VERYQUIET    16
-#define KEEPJOURNAL  32
-#define ZERODELAY    64
-#define CRASHDEBUG   128
-
-extern int DFlags;
-
-void ATTR_PRINTFLIKE(1, 2) debug( const char *, ... );
-void ATTR_PRINTFLIKE(1, 2) debugn( const char *, ... );
-void ATTR_PRINTFLIKE(1, 2) info( const char *, ... );
-void ATTR_PRINTFLIKE(1, 2) infon( const char *, ... );
-void ATTR_PRINTFLIKE(1, 2) warn( const char *, ... );
-void ATTR_PRINTFLIKE(1, 2) error( const char *, ... );
-void ATTR_PRINTFLIKE(1, 2) sys_error( const char *, ... );
-void flushn( void );
-
-void add_string_list_n( string_list_t **list, const char *str, int len );
-void add_string_list( string_list_t **list, const char *str );
-void free_string_list( string_list_t *list );
-
-void free_generic_messages( message_t * );
-
-#ifndef HAVE_MEMRCHR
-void *memrchr( const void *s, int c, size_t n );
-#endif
-
-void *nfmalloc( size_t sz );
-void *nfcalloc( size_t sz );
-void *nfrealloc( void *mem, size_t sz );
-char *nfstrdup( const char *str );
-int nfvasprintf( char **str, const char *fmt, va_list va );
-int ATTR_PRINTFLIKE(2, 3) nfasprintf( char **str, const char *fmt, ... );
-int ATTR_PRINTFLIKE(3, 4) nfsnprintf( char *buf, int blen, const char *fmt, 
... );
-void ATTR_NORETURN oob( void );
-
-char *expand_strdup( const char *s );
-
-int map_name(const char *arg, char **result, int reserve, const char *in, 
const char *out );
-
-void sort_ints( int *arr, int len );
-
-void arc4_init( void );
-unsigned char arc4_getbyte( void );
-
-int bucketsForSize( int size );
-
-#ifdef HAVE_SYS_POLL_H
-# include <sys/poll.h>
-#else
-# define POLLIN 1
-# define POLLOUT 4
-# define POLLERR 8
-#endif
-
-void add_fd( int fd, void (*cb)( int events, void *aux ), void *aux );
-void conf_fd( int fd, int and_events, int or_events );
-void fake_fd( int fd, int events );
-void del_fd( int fd );
-void main_loop( void );
-
-/* sync.c */
-
-extern const char *str_ms[2], *str_hl[2];
-
-#define SYNC_OK       0 /* assumed to be 0 */
-#define SYNC_FAIL     1
-#define SYNC_FAIL_ALL 2
-#define SYNC_BAD(ms)  (4<<(ms))
-#define SYNC_NOGOOD   16 /* internal */
-#define SYNC_CANCELED 32 /* internal */
-
-/* All passed pointers must stay alive until cb is called. */
-void sync_boxes( store_t *ctx[], const char *names[], channel_conf_t *chan,
-                 void (*cb)( int sts, void *aux ), void *aux );
-
-/* config.c */
-
-#define N_DRIVERS 2
-extern driver_t *drivers[N_DRIVERS];
-
-extern channel_conf_t global_conf;
-extern channel_conf_t *channels;
-extern group_conf_t *groups;
-extern int UseFSync;
-
-int parse_bool( conffile_t *cfile );
-int parse_int( conffile_t *cfile );
-int parse_size( conffile_t *cfile );
-int getcline( conffile_t *cfile );
-int merge_ops( int cops, int ops[] );
-int load_config( const char *filename, int pseudo );
-void parse_generic_store( store_conf_t *store, conffile_t *cfg );
-
-/* drv_*.c */
-extern driver_t maildir_driver, imap_driver;
diff --git a/src/main.c b/src/main.c
index f16e068..f2faf25 100644
--- a/src/main.c
+++ b/src/main.c
@@ -20,7 +20,7 @@
  * despite that library's more restrictive license.
  */
 
-#include "isync.h"
+#include "sync.h"
 
 #include <stdlib.h>
 #include <stddef.h>
@@ -31,6 +31,9 @@
 #include <time.h>
 #include <sys/wait.h>
 
+int DFlags;
+int UseFSync = 1;
+
 int Pid;               /* for maildir and imap */
 char Hostname[256];    /* for maildir */
 const char *Home;      /* for config */
diff --git a/src/mdconvert.c b/src/mdconvert.c
index 2eefff6..7f6366f 100644
--- a/src/mdconvert.c
+++ b/src/mdconvert.c
@@ -16,7 +16,7 @@
  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-#include <config.h>
+#include <autodefs.h>
 
 #include <stdio.h>
 #include <stdlib.h>
diff --git a/src/socket.c b/src/socket.c
index c819395..044bd5f 100644
--- a/src/socket.c
+++ b/src/socket.c
@@ -21,17 +21,7 @@
  * despite that library's more restrictive license.
  */
 
-/* This must come before isync.h to avoid our #define S messing up
- * blowfish.h on MacOS X. */
-#include <config.h>
-#ifdef HAVE_LIBSSL
-# include <openssl/ssl.h>
-# include <openssl/err.h>
-# include <openssl/hmac.h>
-# include <openssl/x509v3.h>
-#endif
-
-#include "isync.h"
+#include "socket.h"
 
 #include <assert.h>
 #include <unistd.h>
@@ -46,6 +36,12 @@
 #include <netinet/tcp.h>
 #include <arpa/inet.h>
 #include <netdb.h>
+#ifdef HAVE_LIBSSL
+# include <openssl/ssl.h>
+# include <openssl/err.h>
+# include <openssl/hmac.h>
+# include <openssl/x509v3.h>
+#endif
 
 enum {
        SCK_CONNECTING,
diff --git a/src/socket.h b/src/socket.h
new file mode 100644
index 0000000..1545b39
--- /dev/null
+++ b/src/socket.h
@@ -0,0 +1,118 @@
+/*
+ * mbsync - mailbox synchronizer
+ * Copyright (C) 2000-2002 Michael R. Elkins <[email protected]>
+ * Copyright (C) 2002-2006,2010-2012 Oswald Buddenhagen <[email protected]>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * As a special exception, mbsync may be linked with the OpenSSL library,
+ * despite that library's more restrictive license.
+ */
+
+#ifndef SOCKET_H
+#define SOCKET_H
+
+#include "common.h"
+
+typedef struct ssl_st SSL;
+typedef struct ssl_ctx_st SSL_CTX;
+
+typedef struct server_conf {
+       char *tunnel;
+       char *host;
+       int port;
+#ifdef HAVE_LIBSSL
+       char *cert_file;
+       char use_imaps;
+       char use_sslv2, use_sslv3, use_tlsv1, use_tlsv11, use_tlsv12;
+
+       /* these are actually variables and are leaked at the end */
+       char ssl_ctx_valid;
+       unsigned num_trusted;
+       SSL_CTX *SSLContext;
+#endif
+} server_conf_t;
+
+typedef struct buff_chunk {
+       struct buff_chunk *next;
+       char *data;
+       int len;
+       char buf[1];
+} buff_chunk_t;
+
+typedef struct {
+       /* connection */
+       int fd;
+       int state;
+       const server_conf_t *conf; /* needed during connect */
+#ifdef HAVE_IPV6
+       struct addrinfo *addrs, *curr_addr; /* needed during connect */
+#else
+       char **curr_addr; /* needed during connect */
+#endif
+       char *name;
+#ifdef HAVE_LIBSSL
+       SSL *ssl;
+       int force_trusted;
+#endif
+
+       void (*bad_callback)( void *aux ); /* async fail while sending or 
listening */
+       void (*read_callback)( void *aux ); /* data available for reading */
+       int (*write_callback)( void *aux ); /* all *queued* data was sent */
+       union {
+               void (*connect)( int ok, void *aux );
+               void (*starttls)( int ok, void *aux );
+       } callbacks;
+       void *callback_aux;
+
+       /* writing */
+       buff_chunk_t *write_buf, **write_buf_append; /* buffer head & tail */
+       int write_offset; /* offset into buffer head */
+
+       /* reading */
+       int offset; /* start of filled bytes in buffer */
+       int bytes; /* number of filled bytes in buffer */
+       int scanoff; /* offset to continue scanning for newline at, relative to 
'offset' */
+       char buf[100000];
+} conn_t;
+
+/* call this before doing anything with the socket */
+static INLINE void socket_init( conn_t *conn,
+                                const server_conf_t *conf,
+                                void (*bad_callback)( void *aux ),
+                                void (*read_callback)( void *aux ),
+                                int (*write_callback)( void *aux ),
+                                void *aux )
+{
+       conn->conf = conf;
+       conn->bad_callback = bad_callback;
+       conn->read_callback = read_callback;
+       conn->write_callback = write_callback;
+       conn->callback_aux = aux;
+       conn->fd = -1;
+       conn->name = 0;
+       conn->write_buf_append = &conn->write_buf;
+}
+void socket_connect( conn_t *conn, void (*cb)( int ok, void *aux ) );
+void socket_start_tls(conn_t *conn, void (*cb)( int ok, void *aux ) );
+void socket_close( conn_t *sock );
+int socket_read( conn_t *sock, char *buf, int len ); /* never waits */
+char *socket_read_line( conn_t *sock ); /* don't free return value; never 
waits */
+typedef enum { KeepOwn = 0, GiveOwn } ownership_t;
+int socket_write( conn_t *sock, char *buf, int len, ownership_t takeOwn );
+
+void cram( const char *challenge, const char *user, const char *pass,
+           char **_final, int *_finallen );
+
+#endif
diff --git a/src/sync.c b/src/sync.c
index 2ae902b..bc55911 100644
--- a/src/sync.c
+++ b/src/sync.c
@@ -20,7 +20,7 @@
  * despite that library's more restrictive license.
  */
 
-#include "isync.h"
+#include "sync.h"
 
 #include <assert.h>
 #include <stdio.h>
@@ -39,6 +39,10 @@
 # define fdatasync fsync
 #endif
 
+channel_conf_t global_conf;
+channel_conf_t *channels;
+group_conf_t *groups;
+
 const char *str_ms[] = { "master", "slave" }, *str_hl[] = { "push", "pull" };
 
 void
diff --git a/src/sync.h b/src/sync.h
new file mode 100644
index 0000000..0abad8a
--- /dev/null
+++ b/src/sync.h
@@ -0,0 +1,81 @@
+/*
+ * mbsync - mailbox synchronizer
+ * Copyright (C) 2000-2002 Michael R. Elkins <[email protected]>
+ * Copyright (C) 2002-2006,2010-2012 Oswald Buddenhagen <[email protected]>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * As a special exception, mbsync may be linked with the OpenSSL library,
+ * despite that library's more restrictive license.
+ */
+
+#ifndef SYNC_H
+#define SYNC_H
+
+#include "driver.h"
+
+#define M 0 /* master */
+#define S 1 /* slave */
+
+#define OP_NEW             (1<<0)
+#define OP_RENEW           (1<<1)
+#define OP_DELETE          (1<<2)
+#define OP_FLAGS           (1<<3)
+#define  OP_MASK_TYPE      (OP_NEW|OP_RENEW|OP_DELETE|OP_FLAGS) /* asserted in 
the target ops */
+#define OP_EXPUNGE         (1<<4)
+#define OP_CREATE          (1<<5)
+#define XOP_PUSH           (1<<6)
+#define XOP_PULL           (1<<7)
+#define  XOP_MASK_DIR      (XOP_PUSH|XOP_PULL)
+#define XOP_HAVE_TYPE      (1<<8)
+#define XOP_HAVE_EXPUNGE   (1<<9)
+#define XOP_HAVE_CREATE    (1<<10)
+
+typedef struct channel_conf {
+       struct channel_conf *next;
+       const char *name;
+       store_conf_t *stores[2];
+       const char *boxes[2];
+       char *sync_state;
+       string_list_t *patterns;
+       int ops[2];
+       unsigned max_messages; /* for slave only */
+       signed char expire_unread;
+       char use_internal_date;
+} channel_conf_t;
+
+typedef struct group_conf {
+       struct group_conf *next;
+       const char *name;
+       string_list_t *channels;
+} group_conf_t;
+
+extern channel_conf_t global_conf;
+extern channel_conf_t *channels;
+extern group_conf_t *groups;
+
+extern const char *str_ms[2], *str_hl[2];
+
+#define SYNC_OK       0 /* assumed to be 0 */
+#define SYNC_FAIL     1
+#define SYNC_FAIL_ALL 2
+#define SYNC_BAD(ms)  (4<<(ms))
+#define SYNC_NOGOOD   16 /* internal */
+#define SYNC_CANCELED 32 /* internal */
+
+/* All passed pointers must stay alive until cb is called. */
+void sync_boxes( store_t *ctx[], const char *names[], channel_conf_t *chan,
+                 void (*cb)( int sts, void *aux ), void *aux );
+
+#endif
diff --git a/src/util.c b/src/util.c
index eec8a6d..1acbfc9 100644
--- a/src/util.c
+++ b/src/util.c
@@ -20,7 +20,7 @@
  * despite that library's more restrictive license.
  */
 
-#include "isync.h"
+#include "common.h"
 
 #include <assert.h>
 #include <stdlib.h>
@@ -29,7 +29,6 @@
 #include <string.h>
 #include <pwd.h>
 
-int DFlags;
 static int need_nl;
 
 void
@@ -174,17 +173,6 @@ free_string_list( string_list_t *list )
        }
 }
 
-void
-free_generic_messages( message_t *msgs )
-{
-       message_t *tmsg;
-
-       for (; msgs; msgs = tmsg) {
-               tmsg = msgs->next;
-               free( msgs );
-       }
-}
-
 #ifndef HAVE_VASPRINTF
 static int
 vasprintf( char **strp, const char *fmt, va_list ap )

------------------------------------------------------------------------------
Sponsored by Intel(R) XDK 
Develop, test and display web and hybrid apps with a single code base.
Download it for free now!
http://pubads.g.doubleclick.net/gampad/clk?id=111408631&iu=/4140/ostg.clktrk
_______________________________________________
isync-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/isync-devel

Reply via email to