]> code.citadel.org Git - citadel.git/blobdiff - citadel/citadel_ipc.c
* Replaced all "Citadel/UX" references with "Citadel"
[citadel.git] / citadel / citadel_ipc.c
index 07f13ac9f77933f3634bd0a6993f8c5cefa4fb67..9a07aa2cdb403c95229bd7c74667f2adc6c0a81e 100644 (file)
 #  include <time.h>
 # endif
 #endif
+#include <unistd.h>
 #include <stdio.h>
 #include <sys/types.h>
 #include <string.h>
+#ifdef HAVE_MALLOC_H
 #include <malloc.h>
+#endif
 #include <stdlib.h>
 #include <ctype.h>
+#include <sys/socket.h>
+#include <arpa/inet.h>
+#include <netinet/in.h>
+#include <netdb.h>
+#include <sys/un.h>
+#include <errno.h>
 #ifdef THREADED_CLIENT
 #include <pthread.h>
 #endif
 #include "citadel.h"
 #include "citadel_ipc.h"
 #include "citadel_decls.h"
-#include "client_crypto.h"
 #include "tools.h"
 
 #ifdef THREADED_CLIENT
 pthread_mutex_t rwlock;
 #endif
-char express_msgs = 0;
+
+#ifdef HAVE_OPENSSL
+static SSL_CTX *ssl_ctx;
+char arg_encrypt;
+char rc_encrypt;
+#ifdef THREADED_CLIENT
+pthread_mutex_t **Critters;                    /* Things that need locking */
+#endif /* THREADED_CLIENT */
+
+#endif /* HAVE_OPENSSL */
+
+#ifndef INADDR_NONE
+#define INADDR_NONE 0xffffffff
+#endif
+
+static void (*status_hook)(char *s) = NULL;
+
+void setCryptoStatusHook(void (*hook)(char *s)) {
+       status_hook = hook;
+}
+
+void CtdlIPC_SetNetworkStatusCallback(CtdlIPC *ipc, void (*hook)(int state)) {
+       ipc->network_status_cb = hook;
+}
+
+
+char instant_msgs = 0;
+
+
+static void serv_read(CtdlIPC *ipc, char *buf, unsigned int bytes);
+static void serv_write(CtdlIPC *ipc, const char *buf, unsigned int nbytes);
+#ifdef HAVE_OPENSSL
+static void serv_read_ssl(CtdlIPC *ipc, char *buf, unsigned int bytes);
+static void serv_write_ssl(CtdlIPC *ipc, const char *buf, unsigned int nbytes);
+static void ssl_lock(int mode, int n, const char *file, int line);
+static void endtls(SSL *ssl);
+#ifdef THREADED_CLIENT
+static unsigned long id_callback(void);
+#endif /* THREADED_CLIENT */
+#endif /* HAVE_OPENSSL */
+static void CtdlIPC_getline(CtdlIPC* ipc, char *buf);
+static void CtdlIPC_putline(CtdlIPC *ipc, const char *buf);
 
 
 /*
@@ -71,13 +120,23 @@ int CtdlIPCEcho(CtdlIPC *ipc, const char *arg, char *cret)
  */
 int CtdlIPCQuit(CtdlIPC *ipc)
 {
-       register int ret;
+       register int ret = 221;         /* Default to successful quit */
        char aaa[128];
 
        CtdlIPC_lock(ipc);
-       CtdlIPC_putline(ipc, "QUIT");
-       CtdlIPC_getline(ipc, aaa);
-       ret = atoi(aaa);
+       if (ipc->sock > -1) {
+               CtdlIPC_putline(ipc, "QUIT");
+               CtdlIPC_getline(ipc, aaa);
+               ret = atoi(aaa);
+       }
+#ifdef HAVE_OPENSSL
+       if (ipc->ssl)
+               SSL_shutdown(ipc->ssl);
+       ipc->ssl = NULL;
+#endif
+       if (ipc->sock)
+               shutdown(ipc->sock, 2); /* Close connection; we're dead */
+       ipc->sock = -1;
        CtdlIPC_unlock(ipc);
        return ret;
 }
@@ -146,6 +205,28 @@ int CtdlIPCTryPassword(CtdlIPC *ipc, const char *passwd, char *cret)
 }
 
 
+/*
+ * Second stage of authentication - provide password.  The server returns
+ * 200 and several arguments in cret relating to the user's account.
+ */
+int CtdlIPCTryApopPassword(CtdlIPC *ipc, const char *response, char *cret)
+{
+       register int ret;
+       char *aaa;
+
+       if (!response) return -2;
+       if (!cret) return -2;
+
+       aaa = (char *)malloc((size_t)(strlen(response) + 6));
+       if (!aaa) return -1;
+
+       sprintf(aaa, "PAS2 %s", response);
+       ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
+       free(aaa);
+       return ret;
+}
+
+
 /*
  * Create a new user.  This returns 200 plus the same arguments as TryPassword
  * if selfservice is nonzero, unless there was a problem creating the account.
@@ -194,13 +275,14 @@ int CtdlIPCChangePassword(CtdlIPC *ipc, const char *passwd, char *cret)
 
 /* LKRN */
 /* Caller must free the march list */
-/* which is 0 = LRMS, 1 = LKRN, 2 = LKRO, 3 = LKRA, 4 = LZRM */
+/* Room types are defined in enum RoomList; keep these in sync! */
 /* floor is -1 for all, or floornum */
-int CtdlIPCKnownRooms(CtdlIPC *ipc, int which, int floor, struct march **listing, char *cret)
+int CtdlIPCKnownRooms(CtdlIPC *ipc, enum RoomList which, int floor, struct march **listing, char *cret)
 {
        register int ret;
        struct march *march = NULL;
-       static char *proto[] = {"LRMS", "LKRN", "LKRO", "LKRA", "LZRM" };
+       static char *proto[] =
+               {"LKRA", "LKRN", "LKRO", "LZRM", "LRMS", "LPRM" };
        char aaa[SIZ];
        char *bbb = NULL;
        size_t bbbsize;
@@ -208,7 +290,7 @@ int CtdlIPCKnownRooms(CtdlIPC *ipc, int which, int floor, struct march **listing
        if (!listing) return -2;
        if (*listing) return -2;        /* Free the listing first */
        if (!cret) return -2;
-       if (which < 0 || which > 4) return -2;
+       /* if (which < 0 || which > 4) return -2; */
        if (floor < -1) return -2;      /* Can't validate upper bound, sorry */
 
        sprintf(aaa, "%s %d", proto[which], floor);
@@ -226,8 +308,11 @@ int CtdlIPCKnownRooms(CtdlIPC *ipc, int which, int floor, struct march **listing
                        if (mptr) {
                                mptr->next = NULL;
                                extract(mptr->march_name, aaa, 0);
+                               mptr->march_flags = (unsigned int) extract_int(aaa, 1);
                                mptr->march_floor = (char) extract_int(aaa, 2);
                                mptr->march_order = (char) extract_int(aaa, 3);
+                               mptr->march_flags2 = (unsigned int) extract_int(aaa, 4);
+                               mptr->march_access = (char) extract_int(aaa, 5);
                                if (march == NULL)
                                        march = mptr;
                                else {
@@ -247,14 +332,14 @@ int CtdlIPCKnownRooms(CtdlIPC *ipc, int which, int floor, struct march **listing
 
 
 /* GETU */
-/* Caller must free the struct usersupp; caller may pass an existing one */
-int CtdlIPCGetConfig(CtdlIPC *ipc, struct usersupp **uret, char *cret)
+/* Caller must free the struct ctdluser; caller may pass an existing one */
+int CtdlIPCGetConfig(CtdlIPC *ipc, struct ctdluser **uret, char *cret)
 {
        register int ret;
 
        if (!cret) return -2;
        if (!uret) return -2;
-       if (!*uret) *uret = (struct usersupp *)calloc(1, sizeof (struct usersupp));
+       if (!*uret) *uret = (struct ctdluser *)calloc(1, sizeof (struct ctdluser));
        if (!*uret) return -1;
 
        ret = CtdlIPCGenericCommand(ipc, "GETU", NULL, 0, NULL, NULL, cret);
@@ -268,7 +353,7 @@ int CtdlIPCGetConfig(CtdlIPC *ipc, struct usersupp **uret, char *cret)
 
 
 /* SETU */
-int CtdlIPCSetConfig(CtdlIPC *ipc, struct usersupp *uret, char *cret)
+int CtdlIPCSetConfig(CtdlIPC *ipc, struct ctdluser *uret, char *cret)
 {
        char aaa[48];
 
@@ -332,15 +417,15 @@ int CtdlIPCGotoRoom(CtdlIPC *ipc, const char *room, const char *passwd,
 /* MSGS */
 /* which is 0 = all, 1 = old, 2 = new, 3 = last, 4 = first, 5 = gt, 6 = lt */
 /* whicharg is number of messages, applies to last, first, gt, lt */
-int CtdlIPCGetMessages(CtdlIPC *ipc, int which, int whicharg, const char *template,
-               long **mret, char *cret)
+int CtdlIPCGetMessages(CtdlIPC *ipc, enum MessageList which, int whicharg,
+               const char *mtemplate, unsigned long **mret, char *cret)
 {
        register int ret;
-       register long count = 0;
+       register unsigned long count = 0;
        static char *proto[] =
                { "ALL", "OLD", "NEW", "LAST", "FIRST", "GT", "LT" };
        char aaa[33];
-       char *bbb;
+       char *bbb = NULL;
        size_t bbbsize;
 
        if (!cret) return -2;
@@ -350,25 +435,31 @@ int CtdlIPCGetMessages(CtdlIPC *ipc, int which, int whicharg, const char *templa
 
        if (which <= 2)
                sprintf(aaa, "MSGS %s||%d", proto[which],
-                               (template) ? 1 : 0);
+                               (mtemplate) ? 1 : 0);
        else
                sprintf(aaa, "MSGS %s|%d|%d", proto[which], whicharg,
-                               (template) ? 1 : 0);
-       if (template) count = strlen(template);
-       ret = CtdlIPCGenericCommand(ipc, aaa, template, count, &bbb, &bbbsize, cret);
+                               (mtemplate) ? 1 : 0);
+       if (mtemplate) count = strlen(mtemplate);
+       ret = CtdlIPCGenericCommand(ipc, aaa, mtemplate, count, &bbb, &bbbsize, cret);
+       if (ret / 100 != 1)
+               return ret;
        count = 0;
-       while (strlen(bbb)) {
-               int a;
-
+       *mret = (unsigned long *)calloc(1, sizeof(unsigned long));
+       if (!*mret)
+               return -1;
+       while (bbb && strlen(bbb)) {
                extract_token(aaa, bbb, 0, '\n');
-               a = strlen(aaa);
-               memmove(aaa, bbb + a + 1, strlen(bbb) - a - 1);
-               *mret = (long *)realloc(mret,
-                                       (size_t)((count + 1) * sizeof (long)));
-               if (*mret)
-                       *mret[count++] = atol(aaa);
-               *mret[count] = 0L;
+               remove_token(bbb, 0, '\n');
+               *mret = (unsigned long *)realloc(*mret, (size_t)((count + 2) *
+                                       sizeof (unsigned long)));
+               if (*mret) {
+                       (*mret)[count++] = atol(aaa);
+                       (*mret)[count] = 0L;
+               } else {
+                       break;
+               }
        }
+       if (bbb) free(bbb);
        return ret;
 }
 
@@ -403,23 +494,23 @@ int CtdlIPCGetSingleMessage(CtdlIPC *ipc, long msgnum, int headers, int as_mime,
                                if (!strncasecmp(aaa, "nhdr=yes", 8))
                                        mret[0]->nhdr = 1;
                                else if (!strncasecmp(aaa, "from=", 5))
-                                       strcpy(mret[0]->author, &aaa[5]);
+                                       safestrncpy(mret[0]->author, &aaa[5], SIZ);
                                else if (!strncasecmp(aaa, "type=", 5))
                                        mret[0]->type = atoi(&aaa[5]);
                                else if (!strncasecmp(aaa, "msgn=", 5))
-                                       strcpy(mret[0]->msgid, &aaa[5]);
+                                       safestrncpy(mret[0]->msgid, &aaa[5], SIZ);
                                else if (!strncasecmp(aaa, "subj=", 5))
-                                       strcpy(mret[0]->subject, &aaa[5]);
+                                       safestrncpy(mret[0]->subject, &aaa[5], SIZ);
                                else if (!strncasecmp(aaa, "rfca=", 5))
-                                       strcpy(mret[0]->email, &aaa[5]);
+                                       safestrncpy(mret[0]->email, &aaa[5], SIZ);
                                else if (!strncasecmp(aaa, "hnod=", 5))
-                                       strcpy(mret[0]->hnod, &aaa[5]);
+                                       safestrncpy(mret[0]->hnod, &aaa[5], SIZ);
                                else if (!strncasecmp(aaa, "room=", 5))
-                                       strcpy(mret[0]->room, &aaa[5]);
+                                       safestrncpy(mret[0]->room, &aaa[5], SIZ);
                                else if (!strncasecmp(aaa, "node=", 5))
-                                       strcpy(mret[0]->node, &aaa[5]);
+                                       safestrncpy(mret[0]->node, &aaa[5], SIZ);
                                else if (!strncasecmp(aaa, "rcpt=", 5))
-                                       strcpy(mret[0]->recipient, &aaa[5]);
+                                       safestrncpy(mret[0]->recipient, &aaa[5], SIZ);
                                else if (!strncasecmp(aaa, "time=", 5))
                                        mret[0]->time = atol(&aaa[5]);
 
@@ -491,12 +582,13 @@ int CtdlIPCGetSingleMessage(CtdlIPC *ipc, long msgnum, int headers, int as_mime,
                                        }
                                        remove_token(bbb, 0, '\n');
                                } while ((bbb[0] != 0) && (bbb[0] != '\n'));
+                               remove_token(bbb, 0, '\n');
                        }
 
 
                }
                if (strlen(bbb)) {
-                       /* Strip trailing whitespace */
+                       /* FIXME: Strip trailing whitespace */
                        bbb = (char *)realloc(bbb, (size_t)(strlen(bbb) + 1));
                } else {
                        bbb = (char *)realloc(bbb, 1);
@@ -524,7 +616,7 @@ int CtdlIPCWhoKnowsRoom(CtdlIPC *ipc, char **listing, char *cret)
 
 
 /* INFO */
-int CtdlIPCServerInfo(CtdlIPC *ipc, struct CtdlServInfo *ServInfo, char *cret)
+int CtdlIPCServerInfo(CtdlIPC *ipc, char *cret)
 {
        register int ret;
        size_t bytes;
@@ -532,7 +624,6 @@ int CtdlIPCServerInfo(CtdlIPC *ipc, struct CtdlServInfo *ServInfo, char *cret)
        char buf[SIZ];
 
        if (!cret) return -2;
-       if (!ServInfo) return -2;
 
        ret = CtdlIPCGenericCommand(ipc, "INFO", NULL, 0, &listing, &bytes, cret);
        if (ret / 100 == 1) {
@@ -542,29 +633,31 @@ int CtdlIPCServerInfo(CtdlIPC *ipc, struct CtdlServInfo *ServInfo, char *cret)
                        extract_token(buf, listing, 0, '\n');
                        remove_token(listing, 0, '\n');
                        switch (line++) {
-                       case 0:         ServInfo->serv_pid = atoi(buf);
+                       case 0:         ipc->ServInfo.pid = atoi(buf);
+                                       break;
+                       case 1:         strcpy(ipc->ServInfo.nodename,buf);
                                        break;
-                       case 1:         strcpy(ServInfo->serv_nodename,buf);
+                       case 2:         strcpy(ipc->ServInfo.humannode,buf);
                                        break;
-                       case 2:         strcpy(ServInfo->serv_humannode,buf);
+                       case 3:         strcpy(ipc->ServInfo.fqdn,buf);
                                        break;
-                       case 3:         strcpy(ServInfo->serv_fqdn,buf);
+                       case 4:         strcpy(ipc->ServInfo.software,buf);
                                        break;
-                       case 4:         strcpy(ServInfo->serv_software,buf);
+                       case 5:         ipc->ServInfo.rev_level = atoi(buf);
                                        break;
-                       case 5:         ServInfo->serv_rev_level = atoi(buf);
+                       case 6:         strcpy(ipc->ServInfo.bbs_city,buf);
                                        break;
-                       case 6:         strcpy(ServInfo->serv_bbs_city,buf);
+                       case 7:         strcpy(ipc->ServInfo.sysadm,buf);
                                        break;
-                       case 7:         strcpy(ServInfo->serv_sysadm,buf);
+                       case 9:         strcpy(ipc->ServInfo.moreprompt,buf);
                                        break;
-                       case 9:         strcpy(ServInfo->serv_moreprompt,buf);
+                       case 10:        ipc->ServInfo.ok_floors = atoi(buf);
                                        break;
-                       case 10:        ServInfo->serv_ok_floors = atoi(buf);
+                       case 11:        ipc->ServInfo.paging_level = atoi(buf);
                                        break;
-                       case 11:        ServInfo->serv_paging_level = atoi(buf);
+                       case 13:        ipc->ServInfo.supports_qnop = atoi(buf);
                                        break;
-                       case 13:        ServInfo->serv_supports_qnop = atoi(buf);
+                       case 14:        ipc->ServInfo.supports_ldap = atoi(buf);
                                        break;
                        }
                }
@@ -599,10 +692,12 @@ int CtdlIPCSetLastRead(CtdlIPC *ipc, long msgnum, char *cret)
 
        if (!cret) return -2;
 
-       if (msgnum)
+       if (msgnum) {
                sprintf(aaa, "SLRP %ld", msgnum);
-       else
+       }
+       else {
                sprintf(aaa, "SLRP HIGHEST");
+       }
        ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
        return ret;
 }
@@ -646,13 +741,13 @@ int CtdlIPCKickoutUserFromRoom(CtdlIPC *ipc, const char *username, char *cret)
 
 
 /* GETR */
-int CtdlIPCGetRoomAttributes(CtdlIPC *ipc, struct quickroom **qret, char *cret)
+int CtdlIPCGetRoomAttributes(CtdlIPC *ipc, struct ctdlroom **qret, char *cret)
 {
        register int ret;
 
        if (!cret) return -2;
        if (!qret) return -2;
-       if (!*qret) *qret = (struct quickroom *)calloc(1, sizeof (struct quickroom));
+       if (!*qret) *qret = (struct ctdlroom *)calloc(1, sizeof (struct ctdlroom));
        if (!*qret) return -1;
 
        ret = CtdlIPCGenericCommand(ipc, "GETR", NULL, 0, NULL, NULL, cret);
@@ -663,6 +758,8 @@ int CtdlIPCGetRoomAttributes(CtdlIPC *ipc, struct quickroom **qret, char *cret)
                qret[0]->QRflags = extract_int(cret, 3);
                qret[0]->QRfloor = extract_int(cret, 4);
                qret[0]->QRorder = extract_int(cret, 5);
+               qret[0]->QRdefaultview = extract_int(cret, 6);
+               qret[0]->QRflags2 = extract_int(cret, 7);
        }
        return ret;
 }
@@ -670,7 +767,7 @@ int CtdlIPCGetRoomAttributes(CtdlIPC *ipc, struct quickroom **qret, char *cret)
 
 /* SETR */
 /* set forget to kick all users out of room */
-int CtdlIPCSetRoomAttributes(CtdlIPC *ipc, int forget, struct quickroom *qret, char *cret)
+int CtdlIPCSetRoomAttributes(CtdlIPC *ipc, int forget, struct ctdlroom *qret, char *cret)
 {
        register int ret;
        char *aaa;
@@ -679,12 +776,13 @@ int CtdlIPCSetRoomAttributes(CtdlIPC *ipc, int forget, struct quickroom *qret, c
        if (!qret) return -2;
 
        aaa = (char *)malloc(strlen(qret->QRname) + strlen(qret->QRpasswd) +
-                       strlen(qret->QRdirname) + 52);
+                       strlen(qret->QRdirname) + 64);
        if (!aaa) return -1;
 
-       sprintf(aaa, "SETR %s|%s|%s|%d|%d|%d|%d",
+       sprintf(aaa, "SETR %s|%s|%s|%d|%d|%d|%d|%d|%d",
                        qret->QRname, qret->QRpasswd, qret->QRdirname,
-                       qret->QRflags, forget, qret->QRfloor, qret->QRorder);
+                       qret->QRflags, forget, qret->QRfloor, qret->QRorder,
+                       qret->QRdefaultview, qret->QRflags2);
        ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
        free(aaa);
        return ret;
@@ -723,19 +821,16 @@ int CtdlIPCSetRoomAide(CtdlIPC *ipc, const char *username, char *cret)
 int CtdlIPCPostMessage(CtdlIPC *ipc, int flag, const struct ctdlipcmessage *mr, char *cret)
 {
        register int ret;
-       char *aaa;
+       char cmd[SIZ];
 
        if (!cret) return -2;
        if (!mr) return -2;
 
-       aaa = (char *)malloc(strlen(mr->recipient) + strlen(mr->author) + 40);
-       if (!aaa) return -1;
-
-       sprintf(aaa, "ENT0 %d|%s|%d|%d|%s", flag, mr->recipient, mr->anonymous,
-                       mr->type, mr->author);
-       ret = CtdlIPCGenericCommand(ipc, aaa, mr->text, strlen(mr->text), NULL,
+       snprintf(cmd, sizeof cmd,
+                       "ENT0 %d|%s|%d|%d|%s|%s", flag, mr->recipient,
+                       mr->anonymous, mr->type, mr->subject, mr->author);
+       ret = CtdlIPCGenericCommand(ipc, cmd, mr->text, strlen(mr->text), NULL,
                        NULL, cret);
-       free(aaa);
        return ret;
 }
 
@@ -1046,7 +1141,10 @@ int CtdlIPCOnlineUsers(CtdlIPC *ipc, char **listing, time_t *stamp, char *cret)
 
 /* OPEN */
 int CtdlIPCFileDownload(CtdlIPC *ipc, const char *filename, void **buf,
-               void (*progress_gauge_callback)(long, long), char *cret)
+               size_t resume,
+               void (*progress_gauge_callback)
+                       (CtdlIPC*, unsigned long, unsigned long),
+               char *cret)
 {
        register int ret;
        size_t bytes;
@@ -1071,11 +1169,17 @@ int CtdlIPCFileDownload(CtdlIPC *ipc, const char *filename, void **buf,
                bytes = extract_long(cret, 0);
                last_mod = extract_int(cret, 1);
                extract(mimetype, cret, 2);
-               ret = CtdlIPCReadDownload(ipc, buf, bytes, progress_gauge_callback, cret);
-/*             ret = CtdlIPCHighSpeedReadDownload(ipc, buf, bytes, progress_gauge_callback, cret); */
+
+               ret = CtdlIPCReadDownload(ipc, buf, bytes, resume,
+                                       progress_gauge_callback, cret);
+               /*
+               ret = CtdlIPCHighSpeedReadDownload(ipc, buf, bytes, resume,
+                                       progress_gauge_callback, cret);
+               */
+
                ret = CtdlIPCEndDownload(ipc, cret);
                if (ret / 100 == 2)
-                       sprintf(cret, "%d|%ld|%s|%s", bytes, last_mod,
+                       sprintf(cret, "%d|%ld|%s|%s", (int)bytes, last_mod,
                                        filename, mimetype);
        }
        return ret;
@@ -1083,15 +1187,18 @@ int CtdlIPCFileDownload(CtdlIPC *ipc, const char *filename, void **buf,
 
 
 /* OPNA */
-int CtdlIPCAttachmentDownload(CtdlIPC *ipc, long msgnum, const char *part, void **buf,
-               void (*progress_gauge_callback)(long, long), char *cret)
+int CtdlIPCAttachmentDownload(CtdlIPC *ipc, long msgnum, const char *part,
+               void **buf,
+               void (*progress_gauge_callback)
+                       (CtdlIPC*, unsigned long, unsigned long),
+               char *cret)
 {
        register int ret;
        size_t bytes;
        time_t last_mod;
        char filename[SIZ];
        char mimetype[SIZ];
-       char *aaa;
+       char aaa[SIZ];
 
        if (!cret) return -2;
        if (!buf) return -2;
@@ -1100,21 +1207,19 @@ int CtdlIPCAttachmentDownload(CtdlIPC *ipc, long msgnum, const char *part, void
        if (!msgnum) return -2;
        if (ipc->downloading) return -2;
 
-       aaa = (char *)malloc(strlen(part) + 17);
-       if (!aaa) return -1;
-
        sprintf(aaa, "OPNA %ld|%s", msgnum, part);
        ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
-       free(aaa);
        if (ret / 100 == 2) {
                ipc->downloading = 1;
                bytes = extract_long(cret, 0);
                last_mod = extract_int(cret, 1);
-               extract(mimetype, cret, 2);
-               ret = CtdlIPCHighSpeedReadDownload(ipc, buf, bytes, progress_gauge_callback, cret);
+               extract(filename, cret, 2);
+               extract(mimetype, cret, 3);
+               /* ret = CtdlIPCReadDownload(ipc, buf, bytes, 0, progress_gauge_callback, cret); */
+               ret = CtdlIPCHighSpeedReadDownload(ipc, buf, bytes, 0, progress_gauge_callback, cret);
                ret = CtdlIPCEndDownload(ipc, cret);
                if (ret / 100 == 2)
-                       sprintf(cret, "%d|%ld|%s|%s", bytes, last_mod,
+                       sprintf(cret, "%d|%ld|%s|%s", (int)bytes, last_mod,
                                        filename, mimetype);
        }
        return ret;
@@ -1123,7 +1228,9 @@ int CtdlIPCAttachmentDownload(CtdlIPC *ipc, long msgnum, const char *part, void
 
 /* OIMG */
 int CtdlIPCImageDownload(CtdlIPC *ipc, const char *filename, void **buf,
-               void (*progress_gauge_callback)(long, long), char *cret)
+               void (*progress_gauge_callback)
+                       (CtdlIPC*, unsigned long, unsigned long),
+               char *cret)
 {
        register int ret;
        size_t bytes;
@@ -1148,10 +1255,11 @@ int CtdlIPCImageDownload(CtdlIPC *ipc, const char *filename, void **buf,
                bytes = extract_long(cret, 0);
                last_mod = extract_int(cret, 1);
                extract(mimetype, cret, 2);
-               ret = CtdlIPCReadDownload(ipc, buf, bytes, progress_gauge_callback, cret);
+/*             ret = CtdlIPCReadDownload(ipc, buf, bytes, 0, progress_gauge_callback, cret); */
+               ret = CtdlIPCHighSpeedReadDownload(ipc, buf, bytes, 0, progress_gauge_callback, cret);
                ret = CtdlIPCEndDownload(ipc, cret);
                if (ret / 100 == 2)
-                       sprintf(cret, "%d|%ld|%s|%s", bytes, last_mod,
+                       sprintf(cret, "%d|%ld|%s|%s", (int)bytes, last_mod,
                                        filename, mimetype);
        }
        return ret;
@@ -1159,50 +1267,66 @@ int CtdlIPCImageDownload(CtdlIPC *ipc, const char *filename, void **buf,
 
 
 /* UOPN */
-int CtdlIPCFileUpload(CtdlIPC *ipc, const char *filename, const char *comment, void *buf,
-               size_t bytes, char *cret)
+int CtdlIPCFileUpload(CtdlIPC *ipc, const char *save_as, const char *comment,
+               const char *path,
+               void (*progress_gauge_callback)
+                       (CtdlIPC*, unsigned long, unsigned long),
+               char *cret)
 {
        register int ret;
        char *aaa;
 
        if (!cret) return -1;
-       if (!filename) return -1;
+       if (!save_as) return -1;
        if (!comment) return -1;
+       if (!path) return -1;
+       if (!*path) return -1;
        if (ipc->uploading) return -1;
 
-       aaa = (char *)malloc(strlen(filename) + strlen(comment) + 7);
+       aaa = (char *)malloc(strlen(save_as) + strlen(comment) + 7);
        if (!aaa) return -1;
 
-       sprintf(aaa, "UOPN %s|%s", filename, comment);
+       sprintf(aaa, "UOPN %s|%s", save_as, comment);
        ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
        free(aaa);
-       if (ret / 100 == 2)
+       if (ret / 100 == 2) {
                ipc->uploading = 1;
-       ret = CtdlIPCWriteUpload(ipc, buf, bytes, cret);
-       ret = CtdlIPCEndUpload(ipc, cret);
+               ret = CtdlIPCWriteUpload(ipc, path, progress_gauge_callback, cret);
+               ret = CtdlIPCEndUpload(ipc, (ret == -2 ? 1 : 0), cret);
+               ipc->uploading = 0;
+       }
        return ret;
 }
 
 
 /* UIMG */
-int CtdlIPCImageUpload(CtdlIPC *ipc, int for_real, const char *filename, size_t bytes,
+int CtdlIPCImageUpload(CtdlIPC *ipc, int for_real, const char *path,
+               const char *save_as,
+               void (*progress_gauge_callback)
+                       (CtdlIPC*, unsigned long, unsigned long),
                char *cret)
 {
        register int ret;
        char *aaa;
 
        if (!cret) return -1;
-       if (!filename) return -1;
+       if (!save_as) return -1;
+       if (!path && for_real) return -1;
+       if (!*path && for_real) return -1;
        if (ipc->uploading) return -1;
 
-       aaa = (char *)malloc(strlen(filename) + 17);
+       aaa = (char *)malloc(strlen(save_as) + 17);
        if (!aaa) return -1;
 
-       sprintf(aaa, "UIMG %d|%s", for_real, filename);
+       sprintf(aaa, "UIMG %d|%s", for_real, save_as);
        ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
        free(aaa);
-       if (ret / 100 == 2)
+       if (ret / 100 == 2 && for_real) {
                ipc->uploading = 1;
+               ret = CtdlIPCWriteUpload(ipc, path, progress_gauge_callback, cret);
+               ret = CtdlIPCEndUpload(ipc, (ret == -2 ? 1 : 0), cret);
+               ipc->uploading = 0;
+       }
        return ret;
 }
 
@@ -1243,17 +1367,13 @@ int CtdlIPCFloorListing(CtdlIPC *ipc, char **listing, char *cret)
 int CtdlIPCCreateFloor(CtdlIPC *ipc, int for_real, const char *name, char *cret)
 {
        register int ret;
-       char *aaa;
+       char aaa[SIZ];
 
        if (!cret) return -2;
        if (!name) return -2;
 
-       aaa = (char *)malloc(strlen(name) + 17);
-       if (!aaa) return -1;
-
        sprintf(aaa, "CFLR %s|%d", name, for_real);
        ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
-       free(aaa);
        return ret;
 }
 
@@ -1261,7 +1381,7 @@ int CtdlIPCCreateFloor(CtdlIPC *ipc, int for_real, const char *name, char *cret)
 /* KFLR */
 int CtdlIPCDeleteFloor(CtdlIPC *ipc, int for_real, int floornum, char *cret)
 {
-       char aaa[27];
+       char aaa[SIZ];
 
        if (!cret) return -1;
        if (floornum < 0) return -1;
@@ -1275,33 +1395,38 @@ int CtdlIPCDeleteFloor(CtdlIPC *ipc, int for_real, int floornum, char *cret)
 int CtdlIPCEditFloor(CtdlIPC *ipc, int floornum, const char *floorname, char *cret)
 {
        register int ret;
-       char *aaa;
+       char aaa[SIZ];
 
        if (!cret) return -2;
        if (!floorname) return -2;
        if (floornum < 0) return -2;
 
-       aaa = (char *)malloc(strlen(floorname) + 17);
-       if (!aaa) return -1;
-
        sprintf(aaa, "EFLR %d|%s", floornum, floorname);
        ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
-       free(aaa);
        return ret;
 }
 
 
-/* IDEN */
-int CtdlIPCIdentifySoftware(CtdlIPC *ipc, int developerid, int clientid, int revision,
-               const char *software_name, const char *hostname, char *cret)
+/*
+ * IDEN 
+ *
+ * You only need to fill out hostname, the defaults will be used if any of the
+ * other fields are not set properly.
+ */
+int CtdlIPCIdentifySoftware(CtdlIPC *ipc, int developerid, int clientid,
+               int revision, const char *software_name, const char *hostname,
+               char *cret)
 {
        register int ret;
        char *aaa;
 
-       if (developerid < 0) return -2;
-       if (clientid < 0) return -2;
-       if (revision < 0) return -2;
-       if (!software_name) return -2;
+       if (developerid < 0 || clientid < 0 || revision < 0 ||
+           !software_name) {
+               developerid = 8;
+               clientid = 0;
+               revision = REV_LEVEL - 600;
+               software_name = "Citadel (libcitadel)";
+       }
        if (!hostname) return -2;
 
        aaa = (char *)malloc(strlen(software_name) + strlen(hostname) + 29);
@@ -1555,14 +1680,14 @@ time_t CtdlIPCServerTime(CtdlIPC *ipc, char *cret)
 
 /* AGUP */
 int CtdlIPCAideGetUserParameters(CtdlIPC *ipc, const char *who,
-                                struct usersupp **uret, char *cret)
+                                struct ctdluser **uret, char *cret)
 {
        register int ret;
        char aaa[SIZ];
 
        if (!cret) return -2;
        if (!uret) return -2;
-       if (!*uret) *uret = (struct usersupp *)calloc(1, sizeof(struct usersupp));
+       if (!*uret) *uret = (struct ctdluser *)calloc(1, sizeof(struct ctdluser));
        if (!*uret) return -1;
 
        sprintf(aaa, "AGUP %s", who);
@@ -1584,7 +1709,7 @@ int CtdlIPCAideGetUserParameters(CtdlIPC *ipc, const char *who,
 
 
 /* ASUP */
-int CtdlIPCAideSetUserParameters(CtdlIPC *ipc, const struct usersupp *uret, char *cret)
+int CtdlIPCAideSetUserParameters(CtdlIPC *ipc, const struct ctdluser *uret, char *cret)
 {
        register int ret;
        char *aaa;
@@ -1606,40 +1731,55 @@ int CtdlIPCAideSetUserParameters(CtdlIPC *ipc, const struct usersupp *uret, char
 
 
 /* GPEX */
-/* which is 0 = room, 1 = floor, 2 = site */
-int CtdlIPCGetMessageExpirationPolicy(CtdlIPC *ipc, int which, char *cret)
+/* which is 0 = room, 1 = floor, 2 = site, 3 = default for mailboxes */
+/* caller must free the struct ExpirePolicy */
+int CtdlIPCGetMessageExpirationPolicy(CtdlIPC *ipc, int which,
+               struct ExpirePolicy **policy, char *cret)
 {
-       static char *proto[] = {"room", "floor", "site"};
+       static char *proto[] = {"room", "floor", "site", "mailboxes" };
        char aaa[11];
+       register int ret;
 
        if (!cret) return -2;
-       if (which < 0 || which > 2) return -2;
+       if (!policy) return -2;
+       if (!*policy) *policy = (struct ExpirePolicy *)calloc(1, sizeof(struct ExpirePolicy));
+       if (!*policy) return -1;
+       if (which < 0 || which > 3) return -2;
        
        sprintf(aaa, "GPEX %s", proto[which]);
-       return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
+       ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
+       if (ret / 100 == 2) {
+               policy[0]->expire_mode = extract_int(cret, 0);
+               policy[0]->expire_value = extract_int(cret, 1);
+       }
+       return ret;
+
 }
 
 
 /* SPEX */
-/* which is 0 = room, 1 = floor, 2 = site */
+/* which is 0 = room, 1 = floor, 2 = site, 3 = default for mailboxes */
 /* policy is 0 = inherit, 1 = no purge, 2 = by count, 3 = by age (days) */
-int CtdlIPCSetMessageExpirationPolicy(CtdlIPC *ipc, int which, int policy, int value,
-               char *cret)
+int CtdlIPCSetMessageExpirationPolicy(CtdlIPC *ipc, int which,
+               struct ExpirePolicy *policy, char *cret)
 {
        char aaa[38];
+       char *whichvals[] = { "room", "floor", "site", "mailboxes" };
 
        if (!cret) return -2;
-       if (which < 0 || which > 2) return -2;
-       if (policy < 0 || policy > 3) return -2;
-       if (policy >= 2 && value < 1) return -2;
+       if (which < 0 || which > 3) return -2;
+       if (!policy) return -2;
+       if (policy->expire_mode < 0 || policy->expire_mode > 3) return -2;
+       if (policy->expire_mode >= 2 && policy->expire_value < 1) return -2;
 
-       sprintf(aaa, "SPEX %d|%d|%d", which, policy, value);
+       sprintf(aaa, "SPEX %s|%d|%d", whichvals[which],
+                       policy->expire_mode, policy->expire_value);
        return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
 }
 
 
 /* CONF GET */
-int CtdlGetSystemConfig(CtdlIPC *ipc, char **listing, char *cret)
+int CtdlIPCGetSystemConfig(CtdlIPC *ipc, char **listing, char *cret)
 {
        size_t bytes;
 
@@ -1653,7 +1793,7 @@ int CtdlGetSystemConfig(CtdlIPC *ipc, char **listing, char *cret)
 
 
 /* CONF SET */
-int CtdlSetSystemConfig(CtdlIPC *ipc, const char *listing, char *cret)
+int CtdlIPCSetSystemConfig(CtdlIPC *ipc, const char *listing, char *cret)
 {
        if (!cret) return -2;
        if (!listing) return -2;
@@ -1664,7 +1804,7 @@ int CtdlSetSystemConfig(CtdlIPC *ipc, const char *listing, char *cret)
 
 
 /* CONF GETSYS */
-int CtdlGetSystemConfigByType(CtdlIPC *ipc, const char *mimetype,
+int CtdlIPCGetSystemConfigByType(CtdlIPC *ipc, const char *mimetype,
                char **listing, char *cret)
 {
        char *aaa;
@@ -1684,7 +1824,7 @@ int CtdlGetSystemConfigByType(CtdlIPC *ipc, const char *mimetype,
 
 
 /* CONF PUTSYS */
-int CtdlSetSystemConfigByType(CtdlIPC *ipc, const char *mimetype,
+int CtdlIPCSetSystemConfigByType(CtdlIPC *ipc, const char *mimetype,
               const char *listing, char *cret)
 {
        char *aaa;
@@ -1700,16 +1840,29 @@ int CtdlSetSystemConfigByType(CtdlIPC *ipc, const char *mimetype,
                        NULL, NULL, cret);
 }
 
-/* MMOD */
-int CtdlIPCModerateMessage(CtdlIPC *ipc, long msgnum, int level, char *cret)
+
+/* GNET */
+int CtdlIPCGetRoomNetworkConfig(CtdlIPC *ipc, char **listing, char *cret)
 {
-       char aaa[27];
+       size_t bytes;
 
        if (!cret) return -2;
-       if (!msgnum) return -2;
+       if (!listing) return -2;
+       if (*listing) return -2;
 
-       sprintf(aaa, "MMOD %ld|%d", msgnum, level);
-       return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
+       return CtdlIPCGenericCommand(ipc, "GNET", NULL, 0,
+                       listing, &bytes, cret);
+}
+
+
+/* SNET */
+int CtdlIPCSetRoomNetworkConfig(CtdlIPC *ipc, const char *listing, char *cret)
+{
+       if (!cret) return -2;
+       if (!listing) return -2;
+
+       return CtdlIPCGenericCommand(ipc, "SNET", listing, strlen(listing),
+                       NULL, NULL, cret);
 }
 
 
@@ -1742,8 +1895,95 @@ int CtdlIPCSetMessageSeen(CtdlIPC *ipc, long msgnum, int seen, char *cret)
 /* STLS */
 int CtdlIPCStartEncryption(CtdlIPC *ipc, char *cret)
 {
-       return CtdlIPCGenericCommand(ipc, "STLS", NULL, 0, NULL, NULL, cret);
+       int a;
+       int r;
+       char buf[SIZ];
+
+#ifdef HAVE_OPENSSL
+       SSL *temp_ssl;
+
+       /* New SSL object */
+       temp_ssl = SSL_new(ssl_ctx);
+       if (!temp_ssl) {
+               error_printf("SSL_new failed: %s\n",
+                               ERR_reason_error_string(ERR_get_error()));
+               return -2;
+       }
+       /* Pointless flag waving */
+#if SSLEAY_VERSION_NUMBER >= 0x0922
+       SSL_set_session_id_context(temp_ssl, "Citadel SID", 14);
+#endif
+
+       if (!access("/var/run/egd-pool", F_OK))
+               RAND_egd("/var/run/egd-pool");
+
+       if (!RAND_status()) {
+               error_printf("PRNG not properly seeded\n");
+               return -2;
+       }
+
+       /* Associate network connection with SSL object */
+       if (SSL_set_fd(temp_ssl, ipc->sock) < 1) {
+               error_printf("SSL_set_fd failed: %s\n",
+                               ERR_reason_error_string(ERR_get_error()));
+               return -2;
+       }
+
+       if (status_hook != NULL)
+               status_hook("Requesting encryption...\r");
+
+       /* Ready to start SSL/TLS */
+       /* Old code
+       CtdlIPC_putline(ipc, "STLS");
+       CtdlIPC_getline(ipc, buf);
+       if (buf[0] != '2') {
+               error_printf("Server can't start TLS: %s\n", buf);
+               return 0;
+       }
+       */
+       r = CtdlIPCGenericCommand(ipc,
+                                 "STLS", NULL, 0, NULL, NULL, cret);
+       if (r / 100 != 2) {
+               error_printf("Server can't start TLS: %s\n", buf);
+               endtls(temp_ssl);
+               return r;
+       }
+
+       /* Do SSL/TLS handshake */
+       if ((a = SSL_connect(temp_ssl)) < 1) {
+               error_printf("SSL_connect failed: %s\n",
+                               ERR_reason_error_string(ERR_get_error()));
+               endtls(temp_ssl);
+               return -2;
+       }
+       ipc->ssl = temp_ssl;
+
+       BIO_set_close(ipc->ssl->rbio, BIO_NOCLOSE);
+       {
+               int bits, alg_bits;
+
+               bits = SSL_CIPHER_get_bits(SSL_get_current_cipher(ipc->ssl), &alg_bits);
+               error_printf("Encrypting with %s cipher %s (%d of %d bits)\n",
+                               SSL_CIPHER_get_version(SSL_get_current_cipher(ipc->ssl)),
+                               SSL_CIPHER_get_name(SSL_get_current_cipher(ipc->ssl)),
+                               bits, alg_bits);
+       }
+       return r;
+#else
+       return 0;
+#endif /* HAVE_OPENSSL */
+}
+
+
+#ifdef HAVE_OPENSSL
+static void endtls(SSL *ssl)
+{
+       if (ssl) {
+               SSL_shutdown(ssl);
+               SSL_free(ssl);
+       }
 }
+#endif
 
 
 /* QDIR */
@@ -1773,6 +2013,19 @@ int CtdlIPCInternalProgram(CtdlIPC *ipc, int secret, char *cret)
 }
 
 
+/* FSCK */
+int CtdlIPCMessageBaseCheck(CtdlIPC *ipc, char **mret, char *cret)
+{
+       size_t size = 0;
+
+       if (!cret) return -2;
+       if (!mret) return -2;
+       if (*mret) return -2;
+
+       return CtdlIPCGenericCommand(ipc, "FSCK", NULL, 0, mret, &size, cret);
+}
+
+
 /*
  * Not implemented:
  * 
@@ -1795,19 +2048,21 @@ int CtdlIPCInternalProgram(CtdlIPC *ipc, int secret, char *cret)
 /* ************************************************************************** */
 
 
-inline void CtdlIPC_lock(CtdlIPC *ipc)
+INLINE void CtdlIPC_lock(CtdlIPC *ipc)
 {
+       if (ipc->network_status_cb) ipc->network_status_cb(1);
 #ifdef THREADED_CLIENT
        pthread_mutex_lock(&(ipc->mutex));
 #endif
 }
 
 
-inline void CtdlIPC_unlock(CtdlIPC *ipc)
+INLINE void CtdlIPC_unlock(CtdlIPC *ipc)
 {
 #ifdef THREADED_CLIENT
        pthread_mutex_unlock(&(ipc->mutex));
 #endif
+       if (ipc->network_status_cb) ipc->network_status_cb(0);
 }
 
 
@@ -1816,15 +2071,13 @@ char *CtdlIPCReadListing(CtdlIPC *ipc, char *dest)
 {
        size_t length = 0;
        size_t linelength;
-       char *ret;
+       char *ret = NULL;
        char aaa[SIZ];
 
        ret = dest;
        if (ret != NULL) {
                length = strlen(ret);
-       }
-       else {
-               ret = strdup("");
+       } else {
                length = 0;
        }
 
@@ -1872,13 +2125,13 @@ size_t CtdlIPCPartialRead(CtdlIPC *ipc, void **buf, size_t offset, size_t bytes,
        register size_t len = 0;
        char aaa[SIZ];
 
-       if (!buf) return -1;
-       if (!cret) return -1;
-       if (bytes < 1) return -1;
-       if (offset < 0) return -1;
+       if (!buf) return 0;
+       if (!cret) return 0;
+       if (bytes < 1) return 0;
+       if (offset < 0) return 0;
 
        CtdlIPC_lock(ipc);
-       sprintf(aaa, "READ %d|%d", offset, bytes);
+       sprintf(aaa, "READ %d|%d", (int)offset, (int)bytes);
        CtdlIPC_putline(ipc, aaa);
        CtdlIPC_getline(ipc, aaa);
        if (aaa[0] != '6')
@@ -1892,7 +2145,7 @@ size_t CtdlIPCPartialRead(CtdlIPC *ipc, void **buf, size_t offset, size_t bytes,
                } else {
                        /* We have to read regardless */
                        serv_read(ipc, aaa, len);
-                       len = -1;
+                       len = 0;
                }
        }
        CtdlIPC_unlock(ipc);
@@ -1928,8 +2181,10 @@ int CtdlIPCSpecifyPreferredFormats(CtdlIPC *ipc, char *cret, char *formats) {
 
 
 /* READ */
-int CtdlIPCReadDownload(CtdlIPC *ipc, void **buf, size_t bytes,
-              void (*progress_gauge_callback)(long, long), char *cret)
+int CtdlIPCReadDownload(CtdlIPC *ipc, void **buf, size_t bytes, size_t resume,
+               void (*progress_gauge_callback)
+                       (CtdlIPC*, unsigned long, unsigned long),
+              char *cret)
 {
        register size_t len;
 
@@ -1938,28 +2193,30 @@ int CtdlIPCReadDownload(CtdlIPC *ipc, void **buf, size_t bytes,
        if (*buf) return -1;
        if (!ipc->downloading) return -1;
 
-       len = 0;
+       len = resume;
        if (progress_gauge_callback)
-               progress_gauge_callback(len, bytes);
+               progress_gauge_callback(ipc, len, bytes);
        while (len < bytes) {
                register size_t block;
 
                block = CtdlIPCPartialRead(ipc, buf, len, 4096, cret);
-               if (block == -1) {
+               if (block == 0) {
                        free(*buf);
                        return 0;
                }
                len += block;
                if (progress_gauge_callback)
-                       progress_gauge_callback(len, bytes);
+                       progress_gauge_callback(ipc, len, bytes);
        }
        return len;
 }
 
-
 /* READ - pipelined */
 int CtdlIPCHighSpeedReadDownload(CtdlIPC *ipc, void **buf, size_t bytes,
-              void (*progress_gauge_callback)(long, long), char *cret)
+              size_t resume,
+               void (*progress_gauge_callback)
+                       (CtdlIPC*, unsigned long, unsigned long),
+              char *cret)
 {
        register size_t len;
        register int calls;     /* How many calls in the pipeline */
@@ -1971,21 +2228,21 @@ int CtdlIPCHighSpeedReadDownload(CtdlIPC *ipc, void **buf, size_t bytes,
        if (*buf) return -1;
        if (!ipc->downloading) return -1;
 
-       *buf = (void *)realloc(*buf, bytes);
+       *buf = (void *)realloc(*buf, bytes - resume);
        if (!*buf) return -1;
 
        len = 0;
        CtdlIPC_lock(ipc);
        if (progress_gauge_callback)
-               progress_gauge_callback(len, bytes);
+               progress_gauge_callback(ipc, len, bytes);
 
        /* How many calls will be in the pipeline? */
-       calls = bytes / 4096;
-       if (bytes % 4096) calls++;
+       calls = (bytes - resume) / 4096;
+       if ((bytes - resume) % 4096) calls++;
 
        /* Send all requests at once */
        for (i = 0; i < calls; i++) {
-               sprintf(aaa, "READ %d|4096", i * 4096);
+               sprintf(aaa, "READ %d|4096", (int)(i * 4096 + resume) );
                CtdlIPC_putline(ipc, aaa);
        }
 
@@ -2000,7 +2257,7 @@ int CtdlIPCHighSpeedReadDownload(CtdlIPC *ipc, void **buf, size_t bytes,
                        serv_read(ipc, ((*buf) + (i * 4096)), len);
                }
                if (progress_gauge_callback)
-                       progress_gauge_callback(i * 4096 + len, bytes);
+                       progress_gauge_callback(ipc, i * 4096 + len, bytes);
        }
        CtdlIPC_unlock(ipc);
        return len;
@@ -2008,49 +2265,78 @@ int CtdlIPCHighSpeedReadDownload(CtdlIPC *ipc, void **buf, size_t bytes,
 
 
 /* UCLS */
-int CtdlIPCEndUpload(CtdlIPC *ipc, char *cret)
+int CtdlIPCEndUpload(CtdlIPC *ipc, int discard, char *cret)
 {
        register int ret;
+       char cmd[8];
 
        if (!cret) return -1;
        if (!ipc->uploading) return -1;
 
-       ret = CtdlIPCGenericCommand(ipc, "UCLS", NULL, 0, NULL, NULL, cret);
-       if (ret / 100 == 2)
-               ipc->uploading = 0;
+       sprintf(cmd, "UCLS %d", discard ? 0 : 1);
+       ret = CtdlIPCGenericCommand(ipc, cmd, NULL, 0, NULL, NULL, cret);
+       ipc->uploading = 0;
        return ret;
 }
 
 
 /* WRIT */
-int CtdlIPCWriteUpload(CtdlIPC *ipc, void *buf, size_t bytes, char *cret)
+int CtdlIPCWriteUpload(CtdlIPC *ipc, const char *path,
+               void (*progress_gauge_callback)
+                       (CtdlIPC*, unsigned long, unsigned long),
+               char *cret)
 {
        register int ret = -1;
-       register size_t offset;
+       register size_t offset = 0;
+       size_t bytes;
        char aaa[SIZ];
+       char buf[4096];
+       FILE *fd;
 
        if (!cret) return -1;
-       if (!buf) return -1;
-       if (bytes < 1) return -1;
+       if (!path) return -1;
+       if (!*path) return -1;
+
+       fd = fopen(path, "r");
+       if (!fd) return -2;
+
+       fseek(fd, 0L, SEEK_END);
+       bytes = ftell(fd);
+       rewind(fd);
+
+       if (progress_gauge_callback)
+               progress_gauge_callback(ipc, 0, bytes);
 
-       offset = 0;
        while (offset < bytes) {
-               sprintf(aaa, "WRIT %d", bytes - offset);
+               register size_t to_write;
+
+               /* Read some data in */
+               to_write = fread(buf, 1, 4096, fd);
+               if (!to_write) {
+                       if (feof(fd) || ferror(fd)) break;
+               }
+               sprintf(aaa, "WRIT %d", (int)to_write);
                CtdlIPC_putline(ipc, aaa);
                CtdlIPC_getline(ipc, aaa);
                strcpy(cret, &aaa[4]);
                ret = atoi(aaa);
                if (aaa[0] == '7') {
-                       register size_t to_write;
-
                        to_write = extract_long(&aaa[4], 0);
-                       serv_write(ipc, buf + offset, to_write);
+                       
+                       serv_write(ipc, buf, to_write);
                        offset += to_write;
+                       if (progress_gauge_callback)
+                               progress_gauge_callback(ipc, offset, bytes);
+                       /* Detect short reads and back up if needed */
+                       /* offset will never be negative anyway */
+                       fseek(fd, (signed)offset, SEEK_SET);
                } else {
                        break;
                }
        }
-       return ret;
+       if (progress_gauge_callback)
+               progress_gauge_callback(ipc, 1, 1);
+       return (!ferror(fd) ? ret : -2);
 }
 
 
@@ -2093,14 +2379,16 @@ int CtdlIPCGenericCommand(CtdlIPC *ipc,
        if (!command) return -2;
        if (!proto_response) return -2;
 
+#ifdef HAVE_OPENSSL
        if (ipc->ssl) watch_ssl = 1;
+#endif
 
        CtdlIPC_lock(ipc);
        CtdlIPC_putline(ipc, command);
        while (1) {
                CtdlIPC_getline(ipc, proto_response);
                if (proto_response[3] == '*')
-                       express_msgs = 1;
+                       instant_msgs = 1;
                ret = atoi(proto_response);
                strcpy(proto_response, &proto_response[4]);
                switch (ret / 100) {
@@ -2204,3 +2492,508 @@ int CtdlIPCGenericCommand(CtdlIPC *ipc,
        CtdlIPC_unlock(ipc);
        return ret;
 }
+
+
+static int connectsock(char *host, char *service, char *protocol, int defaultPort)
+{
+       struct hostent *phe;
+       struct servent *pse;
+       struct protoent *ppe;
+       struct sockaddr_in sin;
+       int s, type;
+
+       memset(&sin, 0, sizeof(sin));
+       sin.sin_family = AF_INET;
+
+       pse = getservbyname(service, protocol);
+       if (pse != NULL) {
+               sin.sin_port = pse->s_port;
+       }
+       else if (atoi(service) > 0) {
+               sin.sin_port = htons(atoi(service));
+       }
+       else {
+               sin.sin_port = htons(defaultPort);
+       }
+       phe = gethostbyname(host);
+       if (phe) {
+               memcpy(&sin.sin_addr, phe->h_addr, phe->h_length);
+       } else if ((sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE) {
+               return -1;
+       }
+       if ((ppe = getprotobyname(protocol)) == 0) {
+               return -1;
+       }
+       if (!strcmp(protocol, "udp")) {
+               type = SOCK_DGRAM;
+       } else {
+               type = SOCK_STREAM;
+       }
+
+       s = socket(PF_INET, type, ppe->p_proto);
+       if (s < 0) {
+               return -1;
+       }
+
+       if (connect(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
+               close(s);
+               return -1;
+       }
+
+       return (s);
+}
+
+static int uds_connectsock(int *isLocal, char *sockpath)
+{
+       struct sockaddr_un addr;
+       int s;
+
+       memset(&addr, 0, sizeof(addr));
+       addr.sun_family = AF_UNIX;
+       safestrncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
+
+       s = socket(AF_UNIX, SOCK_STREAM, 0);
+       if (s < 0) {
+               return -1;
+       }
+
+       if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
+               close(s);
+               return -1;
+       }
+
+       *isLocal = 1;
+       return s;
+}
+
+
+/*
+ * input binary data from socket
+ */
+static void serv_read(CtdlIPC *ipc, char *buf, unsigned int bytes)
+{
+       unsigned int len, rlen;
+
+#if defined(HAVE_OPENSSL)
+       if (ipc->ssl) {
+               serv_read_ssl(ipc, buf, bytes);
+               return;
+       }
+#endif
+       len = 0;
+       while (len < bytes) {
+               rlen = read(ipc->sock, &buf[len], bytes - len);
+               if (rlen < 1) {
+                       connection_died(ipc, 0);
+                       return;
+               }
+               len += rlen;
+       }
+}
+
+
+/*
+ * send binary to server
+ */
+static void serv_write(CtdlIPC *ipc, const char *buf, unsigned int nbytes)
+{
+       unsigned int bytes_written = 0;
+       int retval;
+
+#if defined(HAVE_OPENSSL)
+       if (ipc->ssl) {
+               serv_write_ssl(ipc, buf, nbytes);
+               return;
+       }
+#endif
+       while (bytes_written < nbytes) {
+               retval = write(ipc->sock, &buf[bytes_written],
+                              nbytes - bytes_written);
+               if (retval < 1) {
+                       connection_died(ipc, 0);
+                       return;
+               }
+               bytes_written += retval;
+       }
+}
+
+
+#ifdef HAVE_OPENSSL
+/*
+ * input binary data from encrypted connection
+ */
+static void serv_read_ssl(CtdlIPC* ipc, char *buf, unsigned int bytes)
+{
+       int len, rlen;
+       char junk[1];
+
+       len = 0;
+       while (len < bytes) {
+               if (SSL_want_read(ipc->ssl)) {
+                       if ((SSL_write(ipc->ssl, junk, 0)) < 1) {
+                               error_printf("SSL_write in serv_read:\n");
+                               ERR_print_errors_fp(stderr);
+                       }
+               }
+               rlen = SSL_read(ipc->ssl, &buf[len], bytes - len);
+               if (rlen < 1) {
+                       long errval;
+
+                       errval = SSL_get_error(ipc->ssl, rlen);
+                       if (errval == SSL_ERROR_WANT_READ ||
+                                       errval == SSL_ERROR_WANT_WRITE) {
+                               sleep(1);
+                               continue;
+                       }
+                       if (errval == SSL_ERROR_ZERO_RETURN ||
+                                       errval == SSL_ERROR_SSL) {
+                               serv_read(ipc, &buf[len], bytes - len);
+                               return;
+                       }
+                       error_printf("SSL_read in serv_read: %s\n",
+                                       ERR_reason_error_string(ERR_peek_error()));
+                       connection_died(ipc, 1);
+                       return;
+               }
+               len += rlen;
+       }
+}
+
+
+/*
+ * send binary to server encrypted
+ */
+static void serv_write_ssl(CtdlIPC *ipc, const char *buf, unsigned int nbytes)
+{
+       unsigned int bytes_written = 0;
+       int retval;
+       char junk[1];
+
+       while (bytes_written < nbytes) {
+               if (SSL_want_write(ipc->ssl)) {
+                       if ((SSL_read(ipc->ssl, junk, 0)) < 1) {
+                               error_printf("SSL_read in serv_write:\n");
+                               ERR_print_errors_fp(stderr);
+                       }
+               }
+               retval = SSL_write(ipc->ssl, &buf[bytes_written],
+                               nbytes - bytes_written);
+               if (retval < 1) {
+                       long errval;
+
+                       errval = SSL_get_error(ipc->ssl, retval);
+                       if (errval == SSL_ERROR_WANT_READ ||
+                                       errval == SSL_ERROR_WANT_WRITE) {
+                               sleep(1);
+                               continue;
+                       }
+                       if (errval == SSL_ERROR_ZERO_RETURN ||
+                                       errval == SSL_ERROR_SSL) {
+                               serv_write(ipc, &buf[bytes_written],
+                                               nbytes - bytes_written);
+                               return;
+                       }
+                       error_printf("SSL_write in serv_write: %s\n",
+                                       ERR_reason_error_string(ERR_peek_error()));
+                       connection_died(ipc, 1);
+                       return;
+               }
+               bytes_written += retval;
+       }
+}
+
+
+static void CtdlIPC_init_OpenSSL(void)
+{
+       int a;
+       SSL_METHOD *ssl_method;
+       DH *dh;
+       
+       /* already done init */
+       if (ssl_ctx) {
+               return;
+       }
+
+       /* Get started */
+       ssl_ctx = NULL;
+       dh = NULL;
+       SSL_load_error_strings();
+       SSLeay_add_ssl_algorithms();
+
+       /* Set up the SSL context in which we will oeprate */
+       ssl_method = SSLv23_client_method();
+       ssl_ctx = SSL_CTX_new(ssl_method);
+       if (!ssl_ctx) {
+               error_printf("SSL_CTX_new failed: %s\n",
+                               ERR_reason_error_string(ERR_get_error()));
+               return;
+       }
+       /* Any reasonable cipher we can get */
+       if (!(SSL_CTX_set_cipher_list(ssl_ctx, CIT_CIPHERS))) {
+               error_printf("No ciphers available for encryption\n");
+               return;
+       }
+       SSL_CTX_set_session_cache_mode(ssl_ctx, SSL_SESS_CACHE_BOTH);
+       
+       /* Load DH parameters into the context */
+       dh = DH_new();
+       if (!dh) {
+               error_printf("Can't allocate a DH object: %s\n",
+                               ERR_reason_error_string(ERR_get_error()));
+               return;
+       }
+       if (!(BN_hex2bn(&(dh->p), DH_P))) {
+               error_printf("Can't assign DH_P: %s\n",
+                               ERR_reason_error_string(ERR_get_error()));
+               DH_free(dh);
+               return;
+       }
+       if (!(BN_hex2bn(&(dh->g), DH_G))) {
+               error_printf("Can't assign DH_G: %s\n",
+                               ERR_reason_error_string(ERR_get_error()));
+               DH_free(dh);
+               return;
+       }
+       dh->length = DH_L;
+       SSL_CTX_set_tmp_dh(ssl_ctx, dh);
+       DH_free(dh);
+
+#ifdef THREADED_CLIENT
+       /* OpenSSL requires callbacks for threaded clients */
+       CRYPTO_set_locking_callback(ssl_lock);
+       CRYPTO_set_id_callback(id_callback);
+
+       /* OpenSSL requires us to do semaphores for threaded clients */
+       Critters = malloc(CRYPTO_num_locks() * sizeof (pthread_mutex_t *));
+       if (!Critters) {
+               perror("malloc failed");
+               exit(1);
+       } else {
+               for (a = 0; a < CRYPTO_num_locks(); a++) {
+                       Critters[a] = malloc(sizeof (pthread_mutex_t));
+                       if (!Critters[a]) {
+                               perror("malloc failed");
+                               exit(1);
+                       }
+                       pthread_mutex_init(Critters[a], NULL);
+               }
+       }
+#endif /* THREADED_CLIENT */       
+}
+
+
+static void ssl_lock(int mode, int n, const char *file, int line)
+{
+#ifdef THREADED_CLIENT
+       if (mode & CRYPTO_LOCK)
+               pthread_mutex_lock(Critters[n]);
+       else
+               pthread_mutex_unlock(Critters[n]);
+#endif /* THREADED_CLIENT */
+}
+
+#ifdef THREADED_CLIENT
+static unsigned long id_callback(void) {
+       return (unsigned long)pthread_self();
+}
+#endif /* THREADED_CLIENT */
+#endif /* HAVE_OPENSSL */
+
+
+/*
+ * input string from socket - implemented in terms of serv_read()
+ */
+static void CtdlIPC_getline(CtdlIPC* ipc, char *buf)
+{
+       int i;
+
+       /* Read one character at a time. */
+       for (i = 0;; i++) {
+               serv_read(ipc, &buf[i], 1);
+               if (buf[i] == '\n' || i == (SIZ-1))
+                       break;
+       }
+
+       /* If we got a long line, discard characters until the newline. */
+       if (i == (SIZ-1))
+               while (buf[i] != '\n')
+                       serv_read(ipc, &buf[i], 1);
+
+       /* Strip the trailing newline (and carriage return, if present) */
+       if (buf[i] == 10) buf[i--] = 0;
+       if (buf[i] == 13) buf[i--] = 0;
+}
+
+void CtdlIPC_chat_recv(CtdlIPC* ipc, char* buf)
+{
+       return CtdlIPC_getline(ipc, buf);
+}
+
+/*
+ * send line to server - implemented in terms of serv_write()
+ */
+static void CtdlIPC_putline(CtdlIPC *ipc, const char *buf)
+{
+       /* error_printf("< %s\n", buf); */
+       serv_write(ipc, buf, strlen(buf));
+       serv_write(ipc, "\n", 1);
+
+       ipc->last_command_sent = time(NULL);
+}
+
+void CtdlIPC_chat_send(CtdlIPC* ipc, const char* buf)
+{
+       return CtdlIPC_putline(ipc, buf);
+}
+
+
+/*
+ * attach to server
+ */
+CtdlIPC* CtdlIPC_new(int argc, char **argv, char *hostbuf, char *portbuf)
+{
+       int a;
+       char cithost[SIZ];
+       char citport[SIZ];
+       char sockpath[SIZ];
+       CtdlIPC* ipc;
+
+       ipc = ialloc(CtdlIPC);
+       if (!ipc) {
+               return 0;
+       }
+#if defined(HAVE_OPENSSL)
+       ipc->ssl = NULL;
+       CtdlIPC_init_OpenSSL();
+#endif
+#if defined(HAVE_PTHREAD_H)
+       pthread_mutex_init(&(ipc->mutex), NULL); /* Default fast mutex */
+#endif
+       ipc->sock = -1;                 /* Not connected */
+       ipc->isLocal = 0;               /* Not local, of course! */
+       ipc->downloading = 0;
+       ipc->uploading = 0;
+       ipc->last_command_sent = 0L;
+       ipc->network_status_cb = NULL;
+
+       strcpy(cithost, DEFAULT_HOST);  /* default host */
+       strcpy(citport, DEFAULT_PORT);  /* default port */
+
+       /* Allow caller to supply our values (Windows) */
+       if (hostbuf && strlen(hostbuf) > 0)
+               strcpy(cithost, hostbuf);
+       if (portbuf && strlen(portbuf) > 0)
+               strcpy(citport, portbuf);
+
+       /* Read host/port from command line if present */
+       for (a = 0; a < argc; ++a) {
+               if (a == 0) {
+                       /* do nothing */
+               } else if (a == 1) {
+                       strcpy(cithost, argv[a]);
+               } else if (a == 2) {
+                       strcpy(citport, argv[a]);
+               } else {
+                       error_printf("%s: usage: ",argv[0]);
+                       error_printf("%s [host] [port] ",argv[0]);
+                       ifree(ipc);
+                       errno = EINVAL;
+                       return 0;
+               }
+       }
+
+       if ((!strcmp(cithost, "localhost"))
+          || (!strcmp(cithost, "127.0.0.1"))) {
+               ipc->isLocal = 1;
+       }
+
+       /* If we're using a unix domain socket we can do a bunch of stuff */
+       if (!strcmp(cithost, UDS)) {
+               if (!strcasecmp(citport, DEFAULT_PORT)) {
+                       snprintf(sockpath, sizeof sockpath, "%s%s",
+                               BBSDIR, "/citadel.socket");
+               }
+               else {
+                       snprintf(sockpath, sizeof sockpath, "%s%s",
+                               citport, "/citadel.socket");
+               }
+               ipc->sock = uds_connectsock(&(ipc->isLocal), sockpath);
+               if (ipc->sock == -1) {
+                       ifree(ipc);
+                       return 0;
+               }
+               if (hostbuf != NULL) strcpy(hostbuf, cithost);
+               if (portbuf != NULL) strcpy(portbuf, sockpath);
+               return ipc;
+       }
+
+       ipc->sock = connectsock(cithost, citport, "tcp", 504);
+       if (ipc->sock == -1) {
+               ifree(ipc);
+               return 0;
+       }
+       if (hostbuf != NULL) strcpy(hostbuf, cithost);
+       if (portbuf != NULL) strcpy(portbuf, citport);
+       return ipc;
+}
+
+
+/*
+ * Disconnect and delete the IPC class (destructor)
+ */
+void CtdlIPC_delete(CtdlIPC* ipc)
+{
+#ifdef HAVE_OPENSSL
+       if (ipc->ssl) {
+               SSL_shutdown(ipc->ssl);
+               SSL_free(ipc->ssl);
+               ipc->ssl = NULL;
+       }
+#endif
+       if (ipc->sock > -1) {
+               shutdown(ipc->sock, 2); /* Close it up */
+               ipc->sock = -1;
+       }
+       ifree(ipc);
+}
+
+
+/*
+ * Disconnect and delete the IPC class (destructor)
+ * Also NULLs out the pointer
+ */
+void CtdlIPC_delete_ptr(CtdlIPC** pipc)
+{
+       CtdlIPC_delete(*pipc);
+       *pipc = NULL;
+}
+
+
+/*
+ * return the file descriptor of the server socket so we can select() on it.
+ *
+ * FIXME: This is only used in chat mode; eliminate it when chat mode gets
+ * rewritten...
+ */
+int CtdlIPC_getsockfd(CtdlIPC* ipc)
+{
+       return ipc->sock;
+}
+
+
+/*
+ * return one character
+ *
+ * FIXME: This is only used in chat mode; eliminate it when chat mode gets
+ * rewritten...
+ */
+char CtdlIPC_get(CtdlIPC* ipc)
+{
+       char buf[2];
+       char ch;
+
+       serv_read(ipc, buf, 1);
+       ch = (int) buf[0];
+
+       return (ch);
+}