]> code.citadel.org Git - citadel.git/blobdiff - citadel/citadel_ipc.c
* Variable names, comments, documentation, etc... removed the acronym 'BBS'
[citadel.git] / citadel / citadel_ipc.c
index 7f26170b9f9577365e17c023c988ba8384e81907..3a72ac180db1e815b73577264c5cb3ba89fea934 100644 (file)
@@ -63,7 +63,7 @@ void CtdlIPC_SetNetworkStatusCallback(CtdlIPC *ipc, void (*hook)(int state)) {
 }
 
 
-char express_msgs = 0;
+char instant_msgs = 0;
 
 
 static void serv_read(CtdlIPC *ipc, char *buf, unsigned int bytes);
@@ -77,6 +77,8 @@ static void endtls(SSL *ssl);
 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);
 
 
 /*
@@ -118,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;
 }
@@ -273,7 +285,7 @@ int CtdlIPCKnownRooms(CtdlIPC *ipc, enum RoomList which, int floor, struct march
                {"LKRA", "LKRN", "LKRO", "LZRM", "LRMS", "LPRM" };
        char aaa[SIZ];
        char *bbb = NULL;
-       size_t bbbsize;
+       size_t bbb_len;
 
        if (!listing) return -2;
        if (*listing) return -2;        /* Free the listing first */
@@ -282,20 +294,20 @@ int CtdlIPCKnownRooms(CtdlIPC *ipc, enum RoomList which, int floor, struct march
        if (floor < -1) return -2;      /* Can't validate upper bound, sorry */
 
        sprintf(aaa, "%s %d", proto[which], floor);
-       ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, &bbb, &bbbsize, cret);
+       ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, &bbb, &bbb_len, cret);
        if (ret / 100 == 1) {
                struct march *mptr;
 
                while (bbb && strlen(bbb)) {
                        int a;
 
-                       extract_token(aaa, bbb, 0, '\n');
+                       extract_token(aaa, bbb, 0, '\n', sizeof aaa);
                        a = strlen(aaa);
                        memmove(bbb, bbb + a + 1, strlen(bbb) - a);
                        mptr = (struct march *) malloc(sizeof (struct march));
                        if (mptr) {
                                mptr->next = NULL;
-                               extract(mptr->march_name, aaa, 0);
+                               extract_token(mptr->march_name, aaa, 0, '|', sizeof mptr->march_name);
                                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);
@@ -384,7 +396,7 @@ int CtdlIPCGotoRoom(CtdlIPC *ipc, const char *room, const char *passwd,
        }
        ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
        if (ret / 100 == 2) {
-               extract(rret[0]->RRname, cret, 0);
+               extract_token(rret[0]->RRname, cret, 0, '|', sizeof rret[0]->RRname);
                rret[0]->RRunread = extract_long(cret, 1);
                rret[0]->RRtotal = extract_long(cret, 2);
                rret[0]->RRinfoupdated = extract_int(cret, 3);
@@ -414,7 +426,7 @@ int CtdlIPCGetMessages(CtdlIPC *ipc, enum MessageList which, int whicharg,
                { "ALL", "OLD", "NEW", "LAST", "FIRST", "GT", "LT" };
        char aaa[33];
        char *bbb = NULL;
-       size_t bbbsize;
+       size_t bbb_len;
 
        if (!cret) return -2;
        if (!mret) return -2;
@@ -428,7 +440,7 @@ int CtdlIPCGetMessages(CtdlIPC *ipc, enum MessageList which, int whicharg,
                sprintf(aaa, "MSGS %s|%d|%d", proto[which], whicharg,
                                (mtemplate) ? 1 : 0);
        if (mtemplate) count = strlen(mtemplate);
-       ret = CtdlIPCGenericCommand(ipc, aaa, mtemplate, count, &bbb, &bbbsize, cret);
+       ret = CtdlIPCGenericCommand(ipc, aaa, mtemplate, count, &bbb, &bbb_len, cret);
        if (ret / 100 != 1)
                return ret;
        count = 0;
@@ -436,7 +448,7 @@ int CtdlIPCGetMessages(CtdlIPC *ipc, enum MessageList which, int whicharg,
        if (!*mret)
                return -1;
        while (bbb && strlen(bbb)) {
-               extract_token(aaa, bbb, 0, '\n');
+               extract_token(aaa, bbb, 0, '\n', sizeof aaa);
                remove_token(bbb, 0, '\n');
                *mret = (unsigned long *)realloc(*mret, (size_t)((count + 2) *
                                        sizeof (unsigned long)));
@@ -459,9 +471,9 @@ int CtdlIPCGetSingleMessage(CtdlIPC *ipc, long msgnum, int headers, int as_mime,
        register int ret;
        char aaa[SIZ];
        char *bbb = NULL;
-       size_t bbbsize;
+       size_t bbb_len;
        int multipart_hunting = 0;
-       char multipart_prefix[SIZ];
+       char multipart_prefix[128];
 
        if (!cret) return -1;
        if (!mret) return -1;
@@ -471,12 +483,12 @@ int CtdlIPCGetSingleMessage(CtdlIPC *ipc, long msgnum, int headers, int as_mime,
 
        strcpy(mret[0]->content_type, "");
        sprintf(aaa, "MSG%d %ld|%d", as_mime, msgnum, headers);
-       ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, &bbb, &bbbsize, cret);
+       ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, &bbb, &bbb_len, cret);
        if (ret / 100 == 1) {
                if (as_mime != 2) {
                        strcpy(mret[0]->mime_chosen, "1");      /* Default chosen-part is "1" */
                        while (strlen(bbb) > 4 && bbb[4] == '=') {
-                               extract_token(aaa, bbb, 0, '\n');
+                               extract_token(aaa, bbb, 0, '\n', sizeof aaa);
                                remove_token(bbb, 0, '\n');
 
                                if (!strncasecmp(aaa, "nhdr=yes", 8))
@@ -506,14 +518,14 @@ int CtdlIPCGetSingleMessage(CtdlIPC *ipc, long msgnum, int headers, int as_mime,
                                 * us to determine which part we want to download.
                                 */
                                else if (!strncasecmp(aaa, "pref=", 5)) {
-                                       extract(multipart_prefix, &aaa[5], 1);
+                                       extract_token(multipart_prefix, &aaa[5], 1, '|', sizeof multipart_prefix);
                                        if (!strcasecmp(multipart_prefix,
                                           "multipart/alternative")) {
                                                ++multipart_hunting;
                                        }
                                }
                                else if (!strncasecmp(aaa, "suff=", 5)) {
-                                       extract(multipart_prefix, &aaa[5], 1);
+                                       extract_token(multipart_prefix, &aaa[5], 1, '|', sizeof multipart_prefix);
                                        if (!strcasecmp(multipart_prefix,
                                           "multipart/alternative")) {
                                                ++multipart_hunting;
@@ -527,11 +539,11 @@ int CtdlIPCGetSingleMessage(CtdlIPC *ipc, long msgnum, int headers, int as_mime,
                                        if (ptr) {
 
                                                /* Fill the buffers for the caller */
-                                               extract(ptr->name, &aaa[5], 0);
-                                               extract(ptr->filename, &aaa[5], 1);
-                                               extract(ptr->number, &aaa[5], 2);
-                                               extract(ptr->disposition, &aaa[5], 3);
-                                               extract(ptr->mimetype, &aaa[5], 4);
+                                               extract_token(ptr->name, &aaa[5], 0, '|', sizeof ptr->name);
+                                               extract_token(ptr->filename, &aaa[5], 1, '|', sizeof ptr->filename);
+                                               extract_token(ptr->number, &aaa[5], 2, '|', sizeof ptr->number);
+                                               extract_token(ptr->disposition, &aaa[5], 3, '|', sizeof ptr->disposition);
+                                               extract_token(ptr->mimetype, &aaa[5], 4, '|', sizeof ptr->mimetype);
                                                ptr->length = extract_long(&aaa[5], 5);
                                                if (!mret[0]->attachments)
                                                        mret[0]->attachments = ptr;
@@ -563,7 +575,7 @@ int CtdlIPCGetSingleMessage(CtdlIPC *ipc, long msgnum, int headers, int as_mime,
                        if (as_mime == 4) {
                                do {
                                        if (!strncasecmp(bbb, "Content-type: ", 14)) {
-                                               extract_token(mret[0]->content_type, bbb, 0, '\n');
+                                               extract_token(mret[0]->content_type, bbb, 0, '\n', sizeof mret[0]->content_type);
                                                strcpy(mret[0]->content_type,
                                                        &mret[0]->content_type[14]);
                                                striplt(mret[0]->content_type);
@@ -604,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;
@@ -612,39 +624,40 @@ 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) {
                int line = 0;
 
                while (*listing && strlen(listing)) {
-                       extract_token(buf, listing, 0, '\n');
+                       extract_token(buf, listing, 0, '\n', sizeof buf);
                        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(ServInfo->serv_nodename,buf);
+                       case 1:         strcpy(ipc->ServInfo.nodename,buf);
                                        break;
-                       case 2:         strcpy(ServInfo->serv_humannode,buf);
+                       case 2:         strcpy(ipc->ServInfo.humannode,buf);
                                        break;
-                       case 3:         strcpy(ServInfo->serv_fqdn,buf);
+                       case 3:         strcpy(ipc->ServInfo.fqdn,buf);
                                        break;
-                       case 4:         strcpy(ServInfo->serv_software,buf);
+                       case 4:         strcpy(ipc->ServInfo.software,buf);
                                        break;
-                       case 5:         ServInfo->serv_rev_level = atoi(buf);
+                       case 5:         ipc->ServInfo.rev_level = atoi(buf);
                                        break;
-                       case 6:         strcpy(ServInfo->serv_bbs_city,buf);
+                       case 6:         strcpy(ipc->ServInfo.site_location,buf);
                                        break;
-                       case 7:         strcpy(ServInfo->serv_sysadm,buf);
+                       case 7:         strcpy(ipc->ServInfo.sysadm,buf);
                                        break;
-                       case 9:         strcpy(ServInfo->serv_moreprompt,buf);
+                       case 9:         strcpy(ipc->ServInfo.moreprompt,buf);
                                        break;
-                       case 10:        ServInfo->serv_ok_floors = atoi(buf);
+                       case 10:        ipc->ServInfo.ok_floors = atoi(buf);
                                        break;
-                       case 11:        ServInfo->serv_paging_level = atoi(buf);
+                       case 11:        ipc->ServInfo.paging_level = atoi(buf);
                                        break;
-                       case 13:        ServInfo->serv_supports_qnop = atoi(buf);
+                       case 13:        ipc->ServInfo.supports_qnop = atoi(buf);
+                                       break;
+                       case 14:        ipc->ServInfo.supports_ldap = atoi(buf);
                                        break;
                        }
                }
@@ -739,9 +752,9 @@ int CtdlIPCGetRoomAttributes(CtdlIPC *ipc, struct ctdlroom **qret, char *cret)
 
        ret = CtdlIPCGenericCommand(ipc, "GETR", NULL, 0, NULL, NULL, cret);
        if (ret / 100 == 2) {
-               extract(qret[0]->QRname, cret, 0);
-               extract(qret[0]->QRpasswd, cret, 1);
-               extract(qret[0]->QRdirname, cret, 2);
+               extract_token(qret[0]->QRname, cret, 0, '|', sizeof qret[0]->QRname);
+               extract_token(qret[0]->QRpasswd, cret, 1, '|', sizeof qret[0]->QRpasswd);
+               extract_token(qret[0]->QRdirname, cret, 2, '|', sizeof qret[0]->QRdirname);
                qret[0]->QRflags = extract_int(cret, 3);
                qret[0]->QRfloor = extract_int(cret, 4);
                qret[0]->QRorder = extract_int(cret, 5);
@@ -763,12 +776,13 @@ int CtdlIPCSetRoomAttributes(CtdlIPC *ipc, int forget, struct ctdlroom *qret, ch
        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;
@@ -1128,7 +1142,8 @@ int CtdlIPCOnlineUsers(CtdlIPC *ipc, char **listing, time_t *stamp, char *cret)
 /* OPEN */
 int CtdlIPCFileDownload(CtdlIPC *ipc, const char *filename, void **buf,
                size_t resume,
-               void (*progress_gauge_callback)(unsigned long, unsigned long),
+               void (*progress_gauge_callback)
+                       (CtdlIPC*, unsigned long, unsigned long),
                char *cret)
 {
        register int ret;
@@ -1153,7 +1168,7 @@ int CtdlIPCFileDownload(CtdlIPC *ipc, const char *filename, void **buf,
                ipc->downloading = 1;
                bytes = extract_long(cret, 0);
                last_mod = extract_int(cret, 1);
-               extract(mimetype, cret, 2);
+               extract_token(mimetype, cret, 2, '|', sizeof mimetype);
 
                ret = CtdlIPCReadDownload(ipc, buf, bytes, resume,
                                        progress_gauge_callback, cret);
@@ -1174,7 +1189,8 @@ 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)(unsigned long, unsigned long),
+               void (*progress_gauge_callback)
+                       (CtdlIPC*, unsigned long, unsigned long),
                char *cret)
 {
        register int ret;
@@ -1197,8 +1213,8 @@ int CtdlIPCAttachmentDownload(CtdlIPC *ipc, long msgnum, const char *part,
                ipc->downloading = 1;
                bytes = extract_long(cret, 0);
                last_mod = extract_int(cret, 1);
-               extract(filename, cret, 2);
-               extract(mimetype, cret, 3);
+               extract_token(filename, cret, 2, '|', sizeof filename);
+               extract_token(mimetype, cret, 3, '|', sizeof mimetype);
                /* ret = CtdlIPCReadDownload(ipc, buf, bytes, 0, progress_gauge_callback, cret); */
                ret = CtdlIPCHighSpeedReadDownload(ipc, buf, bytes, 0, progress_gauge_callback, cret);
                ret = CtdlIPCEndDownload(ipc, cret);
@@ -1212,7 +1228,8 @@ int CtdlIPCAttachmentDownload(CtdlIPC *ipc, long msgnum, const char *part,
 
 /* OIMG */
 int CtdlIPCImageDownload(CtdlIPC *ipc, const char *filename, void **buf,
-               void (*progress_gauge_callback)(unsigned long, unsigned long),
+               void (*progress_gauge_callback)
+                       (CtdlIPC*, unsigned long, unsigned long),
                char *cret)
 {
        register int ret;
@@ -1237,7 +1254,7 @@ int CtdlIPCImageDownload(CtdlIPC *ipc, const char *filename, void **buf,
                ipc->downloading = 1;
                bytes = extract_long(cret, 0);
                last_mod = extract_int(cret, 1);
-               extract(mimetype, cret, 2);
+               extract_token(mimetype, cret, 2, '|', sizeof mimetype);
 /*             ret = CtdlIPCReadDownload(ipc, buf, bytes, 0, progress_gauge_callback, cret); */
                ret = CtdlIPCHighSpeedReadDownload(ipc, buf, bytes, 0, progress_gauge_callback, cret);
                ret = CtdlIPCEndDownload(ipc, cret);
@@ -1252,7 +1269,8 @@ int CtdlIPCImageDownload(CtdlIPC *ipc, const char *filename, void **buf,
 /* UOPN */
 int CtdlIPCFileUpload(CtdlIPC *ipc, const char *save_as, const char *comment,
                const char *path,
-               void (*progress_gauge_callback)(unsigned long, unsigned long),
+               void (*progress_gauge_callback)
+                       (CtdlIPC*, unsigned long, unsigned long),
                char *cret)
 {
        register int ret;
@@ -1284,7 +1302,8 @@ int CtdlIPCFileUpload(CtdlIPC *ipc, const char *save_as, const char *comment,
 /* UIMG */
 int CtdlIPCImageUpload(CtdlIPC *ipc, int for_real, const char *path,
                const char *save_as,
-               void (*progress_gauge_callback)(unsigned long, unsigned long),
+               void (*progress_gauge_callback)
+                       (CtdlIPC*, unsigned long, unsigned long),
                char *cret)
 {
        register int ret;
@@ -1406,7 +1425,7 @@ int CtdlIPCIdentifySoftware(CtdlIPC *ipc, int developerid, int clientid,
                developerid = 8;
                clientid = 0;
                revision = REV_LEVEL - 600;
-               software_name = "Citadel/UX (libcitadel)";
+               software_name = "Citadel (libcitadel)";
        }
        if (!hostname) return -2;
 
@@ -1675,8 +1694,8 @@ int CtdlIPCAideGetUserParameters(CtdlIPC *ipc, const char *who,
        ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
 
        if (ret / 100 == 2) {
-               extract(uret[0]->fullname, cret, 0);
-               extract(uret[0]->password, cret, 1);
+               extract_token(uret[0]->fullname, cret, 0, '|', sizeof uret[0]->fullname);
+               extract_token(uret[0]->password, cret, 1, '|', sizeof uret[0]->password);
                uret[0]->flags = extract_int(cret, 2);
                uret[0]->timescalled = extract_long(cret, 3);
                uret[0]->posted = extract_long(cret, 4);
@@ -1822,6 +1841,31 @@ int CtdlIPCSetSystemConfigByType(CtdlIPC *ipc, const char *mimetype,
 }
 
 
+/* GNET */
+int CtdlIPCGetRoomNetworkConfig(CtdlIPC *ipc, char **listing, char *cret)
+{
+       size_t bytes;
+
+       if (!cret) return -2;
+       if (!listing) return -2;
+       if (*listing) return -2;
+
+       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);
+}
+
+
 /* REQT */
 int CtdlIPCRequestClientLogout(CtdlIPC *ipc, int session, char *cret)
 {
@@ -1867,7 +1911,7 @@ int CtdlIPCStartEncryption(CtdlIPC *ipc, char *cret)
        }
        /* Pointless flag waving */
 #if SSLEAY_VERSION_NUMBER >= 0x0922
-       SSL_set_session_id_context(temp_ssl, "Citadel/UX SID", 14);
+       SSL_set_session_id_context(temp_ssl, "Citadel SID", 14);
 #endif
 
        if (!access("/var/run/egd-pool", F_OK))
@@ -2084,7 +2128,6 @@ size_t CtdlIPCPartialRead(CtdlIPC *ipc, void **buf, size_t offset, size_t bytes,
        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", (int)offset, (int)bytes);
@@ -2097,7 +2140,7 @@ size_t CtdlIPCPartialRead(CtdlIPC *ipc, void **buf, size_t offset, size_t bytes,
                *buf = (void *)realloc(*buf, (size_t)(offset + len));
                if (*buf) {
                        /* I know what I'm doing */
-                       serv_read(ipc, (*buf + offset), len);
+                       serv_read(ipc, ((char *)(*buf) + offset), len);
                } else {
                        /* We have to read regardless */
                        serv_read(ipc, aaa, len);
@@ -2138,7 +2181,8 @@ int CtdlIPCSpecifyPreferredFormats(CtdlIPC *ipc, char *cret, char *formats) {
 
 /* READ */
 int CtdlIPCReadDownload(CtdlIPC *ipc, void **buf, size_t bytes, size_t resume,
-              void (*progress_gauge_callback)(unsigned long, unsigned long),
+               void (*progress_gauge_callback)
+                       (CtdlIPC*, unsigned long, unsigned long),
               char *cret)
 {
        register size_t len;
@@ -2150,7 +2194,7 @@ int CtdlIPCReadDownload(CtdlIPC *ipc, void **buf, size_t bytes, size_t resume,
 
        len = resume;
        if (progress_gauge_callback)
-               progress_gauge_callback(len, bytes);
+               progress_gauge_callback(ipc, len, bytes);
        while (len < bytes) {
                register size_t block;
 
@@ -2161,7 +2205,7 @@ int CtdlIPCReadDownload(CtdlIPC *ipc, void **buf, size_t bytes, size_t resume,
                }
                len += block;
                if (progress_gauge_callback)
-                       progress_gauge_callback(len, bytes);
+                       progress_gauge_callback(ipc, len, bytes);
        }
        return len;
 }
@@ -2169,7 +2213,8 @@ int CtdlIPCReadDownload(CtdlIPC *ipc, void **buf, size_t bytes, size_t resume,
 /* READ - pipelined */
 int CtdlIPCHighSpeedReadDownload(CtdlIPC *ipc, void **buf, size_t bytes,
               size_t resume,
-              void (*progress_gauge_callback)(unsigned long, unsigned long),
+               void (*progress_gauge_callback)
+                       (CtdlIPC*, unsigned long, unsigned long),
               char *cret)
 {
        register size_t len;
@@ -2188,7 +2233,7 @@ int CtdlIPCHighSpeedReadDownload(CtdlIPC *ipc, void **buf, size_t bytes,
        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 - resume) / 4096;
@@ -2208,10 +2253,10 @@ int CtdlIPCHighSpeedReadDownload(CtdlIPC *ipc, void **buf, size_t bytes,
                else {
                        len = extract_long(&aaa[4], 0);
                        /* I know what I'm doing */
-                       serv_read(ipc, ((*buf) + (i * 4096)), len);
+                       serv_read(ipc, ((char *)(*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;
@@ -2236,7 +2281,8 @@ int CtdlIPCEndUpload(CtdlIPC *ipc, int discard, char *cret)
 
 /* WRIT */
 int CtdlIPCWriteUpload(CtdlIPC *ipc, const char *path,
-               void (*progress_gauge_callback)(unsigned long, unsigned long),
+               void (*progress_gauge_callback)
+                       (CtdlIPC*, unsigned long, unsigned long),
                char *cret)
 {
        register int ret = -1;
@@ -2258,7 +2304,7 @@ int CtdlIPCWriteUpload(CtdlIPC *ipc, const char *path,
        rewind(fd);
 
        if (progress_gauge_callback)
-               progress_gauge_callback(0, bytes);
+               progress_gauge_callback(ipc, 0, bytes);
 
        while (offset < bytes) {
                register size_t to_write;
@@ -2279,7 +2325,7 @@ int CtdlIPCWriteUpload(CtdlIPC *ipc, const char *path,
                        serv_write(ipc, buf, to_write);
                        offset += to_write;
                        if (progress_gauge_callback)
-                               progress_gauge_callback(offset, bytes);
+                               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);
@@ -2288,7 +2334,7 @@ int CtdlIPCWriteUpload(CtdlIPC *ipc, const char *path,
                }
        }
        if (progress_gauge_callback)
-               progress_gauge_callback(1, 1);
+               progress_gauge_callback(ipc, 1, 1);
        return (!ferror(fd) ? ret : -2);
 }
 
@@ -2341,7 +2387,7 @@ int CtdlIPCGenericCommand(CtdlIPC *ipc,
        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) {
@@ -2537,7 +2583,7 @@ static void serv_read(CtdlIPC *ipc, char *buf, unsigned int bytes)
        while (len < bytes) {
                rlen = read(ipc->sock, &buf[len], bytes - len);
                if (rlen < 1) {
-                       connection_died(ipc);
+                       connection_died(ipc, 0);
                        return;
                }
                len += rlen;
@@ -2563,7 +2609,7 @@ static void serv_write(CtdlIPC *ipc, const char *buf, unsigned int nbytes)
                retval = write(ipc->sock, &buf[bytes_written],
                               nbytes - bytes_written);
                if (retval < 1) {
-                       connection_died(ipc);
+                       connection_died(ipc, 0);
                        return;
                }
                bytes_written += retval;
@@ -2603,9 +2649,9 @@ static void serv_read_ssl(CtdlIPC* ipc, char *buf, unsigned int bytes)
                                serv_read(ipc, &buf[len], bytes - len);
                                return;
                        }
-                       error_printf("SSL_read in serv_read:\n");
-                       ERR_print_errors_fp(stderr);
-                       connection_died(ipc);
+                       error_printf("SSL_read in serv_read: %s\n",
+                                       ERR_reason_error_string(ERR_peek_error()));
+                       connection_died(ipc, 1);
                        return;
                }
                len += rlen;
@@ -2646,9 +2692,9 @@ static void serv_write_ssl(CtdlIPC *ipc, const char *buf, unsigned int nbytes)
                                                nbytes - bytes_written);
                                return;
                        }
-                       error_printf("SSL_write in serv_write:\n");
-                       ERR_print_errors_fp(stderr);
-                       connection_died(ipc);
+                       error_printf("SSL_write in serv_write: %s\n",
+                                       ERR_reason_error_string(ERR_peek_error()));
+                       connection_died(ipc, 1);
                        return;
                }
                bytes_written += retval;
@@ -2668,6 +2714,7 @@ static void CtdlIPC_init_OpenSSL(void)
        }
 
        /* Get started */
+       a = 0;
        ssl_ctx = NULL;
        dh = NULL;
        SSL_load_error_strings();
@@ -2756,7 +2803,7 @@ static unsigned long id_callback(void) {
 /*
  * input string from socket - implemented in terms of serv_read()
  */
-void CtdlIPC_getline(CtdlIPC* ipc, char *buf)
+static void CtdlIPC_getline(CtdlIPC* ipc, char *buf)
 {
        int i;
 
@@ -2777,19 +2824,41 @@ void CtdlIPC_getline(CtdlIPC* ipc, char *buf)
        if (buf[i] == 13) buf[i--] = 0;
 }
 
+void CtdlIPC_chat_recv(CtdlIPC* ipc, char* buf)
+{
+       CtdlIPC_getline(ipc, buf);
+}
 
 /*
  * send line to server - implemented in terms of serv_write()
  */
-void CtdlIPC_putline(CtdlIPC *ipc, const char *buf)
+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);
+       char *cmd = NULL;
+       int len;
+
+       len = strlen(buf);
+       cmd = malloc(len + 2);
+       if (!cmd) {
+               /* This requires no extra memory */
+               serv_write(ipc, buf, len);
+               serv_write(ipc, "\n", 1);
+       } else {
+               /* This is network-optimized */
+               strncpy(cmd, buf, len);
+               strcpy(cmd + len, "\n");
+               serv_write(ipc, cmd, len + 1);
+               free(cmd);
+       }
 
        ipc->last_command_sent = time(NULL);
 }
 
+void CtdlIPC_chat_send(CtdlIPC* ipc, const char* buf)
+{
+       CtdlIPC_putline(ipc, buf);
+}
+
 
 /*
  * attach to server
@@ -2800,8 +2869,9 @@ CtdlIPC* CtdlIPC_new(int argc, char **argv, char *hostbuf, char *portbuf)
        char cithost[SIZ];
        char citport[SIZ];
        char sockpath[SIZ];
+       CtdlIPC* ipc;
 
-       CtdlIPC *ipc = ialloc(CtdlIPC);
+       ipc = ialloc(CtdlIPC);
        if (!ipc) {
                return 0;
        }
@@ -2854,7 +2924,7 @@ CtdlIPC* CtdlIPC_new(int argc, char **argv, char *hostbuf, char *portbuf)
        if (!strcmp(cithost, UDS)) {
                if (!strcasecmp(citport, DEFAULT_PORT)) {
                        snprintf(sockpath, sizeof sockpath, "%s%s",
-                               BBSDIR, "/citadel.socket");
+                               CTDLDIR, "/citadel.socket");
                }
                else {
                        snprintf(sockpath, sizeof sockpath, "%s%s",
@@ -2880,6 +2950,38 @@ CtdlIPC* CtdlIPC_new(int argc, char **argv, char *hostbuf, char *portbuf)
        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.
  *