]> code.citadel.org Git - citadel.git/blob - citadel/citadel_ipc.c
* Mac OS X build fixes (everything but serv_imap now builds)
[citadel.git] / citadel / citadel_ipc.c
1 /* $Id$ */
2
3 #define UDS                     "_UDS_"
4 #define DEFAULT_HOST            UDS
5 #define DEFAULT_PORT            "citadel"
6
7 #include "sysdep.h"
8 #if TIME_WITH_SYS_TIME
9 # include <sys/time.h>
10 # include <time.h>
11 #else
12 # if HAVE_SYS_TIME_H
13 #  include <sys/time.h>
14 # else
15 #  include <time.h>
16 # endif
17 #endif
18 #include <unistd.h>
19 #include <stdio.h>
20 #include <sys/types.h>
21 #include <string.h>
22 #ifdef HAVE_MALLOC_H
23 #include <malloc.h>
24 #endif
25 #include <stdlib.h>
26 #include <ctype.h>
27 #include <sys/socket.h>
28 #include <arpa/inet.h>
29 #include <netinet/in.h>
30 #include <netdb.h>
31 #include <sys/un.h>
32 #include <errno.h>
33 #ifdef THREADED_CLIENT
34 #include <pthread.h>
35 #endif
36 #include "citadel.h"
37 #include "citadel_ipc.h"
38 #include "citadel_decls.h"
39 #include "tools.h"
40
41 #ifdef THREADED_CLIENT
42 pthread_mutex_t rwlock;
43 #endif
44
45 #ifdef HAVE_OPENSSL
46 static SSL_CTX *ssl_ctx;
47 char arg_encrypt;
48 char rc_encrypt;
49 #ifdef THREADED_CLIENT
50 pthread_mutex_t **Critters;                     /* Things that need locking */
51 #endif /* THREADED_CLIENT */
52
53 #endif /* HAVE_OPENSSL */
54
55 #ifndef INADDR_NONE
56 #define INADDR_NONE 0xffffffff
57 #endif
58
59 static void (*status_hook)(char *s) = NULL;
60
61 void setCryptoStatusHook(void (*hook)(char *s)) {
62         status_hook = hook;
63 }
64
65
66 char express_msgs = 0;
67
68
69 static void serv_read(CtdlIPC *ipc, char *buf, int bytes);
70 static void serv_write(CtdlIPC *ipc, const char *buf, int nbytes);
71 #ifdef HAVE_OPENSSL
72 static void serv_read_ssl(CtdlIPC *ipc, char *buf, int bytes);
73 static void serv_write_ssl(CtdlIPC *ipc, const char *buf, int nbytes);
74 static void ssl_lock(int mode, int n, const char *file, int line);
75 static void endtls(SSL *ssl);
76 #ifdef THREADED_CLIENT
77 static unsigned long id_callback(void);
78 #endif /* THREADED_CLIENT */
79 #endif /* HAVE_OPENSSL */
80
81
82 /*
83  * Does nothing.  The server should always return 200.
84  */
85 int CtdlIPCNoop(CtdlIPC *ipc)
86 {
87         char aaa[128];
88
89         return CtdlIPCGenericCommand(ipc, "NOOP", NULL, 0, NULL, NULL, aaa);
90 }
91
92
93 /*
94  * Does nothing interesting.  The server should always return 200
95  * along with your string.
96  */
97 int CtdlIPCEcho(CtdlIPC *ipc, const char *arg, char *cret)
98 {
99         register int ret;
100         char *aaa;
101         
102         if (!arg) return -2;
103         if (!cret) return -2;
104
105         aaa = (char *)malloc((size_t)(strlen(arg) + 6));
106         if (!aaa) return -1;
107
108         sprintf(aaa, "ECHO %s", arg);
109         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
110         free(aaa);
111         return ret;
112 }
113
114
115 /*
116  * Asks the server to close the connecction.
117  * Should always return 200.
118  */
119 int CtdlIPCQuit(CtdlIPC *ipc)
120 {
121         register int ret;
122         char aaa[128];
123
124         CtdlIPC_lock(ipc);
125         CtdlIPC_putline(ipc, "QUIT");
126         CtdlIPC_getline(ipc, aaa);
127         ret = atoi(aaa);
128         CtdlIPC_unlock(ipc);
129         return ret;
130 }
131
132
133 /*
134  * Asks the server to logout.  Should always return 200, even if no user
135  * was logged in.  The user will not be logged in after this!
136  */
137 int CtdlIPCLogout(CtdlIPC *ipc)
138 {
139         register int ret;
140         char aaa[128];
141
142         CtdlIPC_lock(ipc);
143         CtdlIPC_putline(ipc, "LOUT");
144         CtdlIPC_getline(ipc, aaa);
145         ret = atoi(aaa);
146         CtdlIPC_unlock(ipc);
147         return ret;
148 }
149
150
151 /*
152  * First stage of authentication - pass the username.  Returns 300 if the
153  * username is able to log in, with the username correctly spelled in cret.
154  * Returns various 500 error codes if the user doesn't exist, etc.
155  */
156 int CtdlIPCTryLogin(CtdlIPC *ipc, const char *username, char *cret)
157 {
158         register int ret;
159         char *aaa;
160
161         if (!username) return -2;
162         if (!cret) return -2;
163
164         aaa = (char *)malloc((size_t)(strlen(username) + 6));
165         if (!aaa) return -1;
166
167         sprintf(aaa, "USER %s", username);
168         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
169         free(aaa);
170         return ret;
171 }
172
173
174 /*
175  * Second stage of authentication - provide password.  The server returns
176  * 200 and several arguments in cret relating to the user's account.
177  */
178 int CtdlIPCTryPassword(CtdlIPC *ipc, const char *passwd, char *cret)
179 {
180         register int ret;
181         char *aaa;
182
183         if (!passwd) return -2;
184         if (!cret) return -2;
185
186         aaa = (char *)malloc((size_t)(strlen(passwd) + 6));
187         if (!aaa) return -1;
188
189         sprintf(aaa, "PASS %s", passwd);
190         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
191         free(aaa);
192         return ret;
193 }
194
195
196 /*
197  * Create a new user.  This returns 200 plus the same arguments as TryPassword
198  * if selfservice is nonzero, unless there was a problem creating the account.
199  * If selfservice is zero, creates a new user but does not log out the existing
200  * user - intended for use by system administrators to create accounts on
201  * behalf of other users.
202  */
203 int CtdlIPCCreateUser(CtdlIPC *ipc, const char *username, int selfservice, char *cret)
204 {
205         register int ret;
206         char *aaa;
207
208         if (!username) return -2;
209         if (!cret) return -2;
210
211         aaa = (char *)malloc((size_t)(strlen(username) + 6));
212         if (!aaa) return -1;
213
214         sprintf(aaa, "%s %s", selfservice ? "NEWU" : "CREU",  username);
215         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
216         free(aaa);
217         return ret;
218 }
219
220
221 /*
222  * Changes the user's password.  Returns 200 if changed, errors otherwise.
223  */
224 int CtdlIPCChangePassword(CtdlIPC *ipc, const char *passwd, char *cret)
225 {
226         register int ret;
227         char *aaa;
228
229         if (!passwd) return -2;
230         if (!cret) return -2;
231
232         aaa = (char *)malloc((size_t)(strlen(passwd) + 6));
233         if (!aaa) return -1;
234
235         sprintf(aaa, "SETP %s", passwd);
236         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
237         free(aaa);
238         return ret;
239 }
240
241
242 /* LKRN */
243 /* Caller must free the march list */
244 /* which is 0 = LRMS, 1 = LKRN, 2 = LKRO, 3 = LKRA, 4 = LZRM */
245 /* floor is -1 for all, or floornum */
246 int CtdlIPCKnownRooms(CtdlIPC *ipc, int which, int floor, struct march **listing, char *cret)
247 {
248         register int ret;
249         struct march *march = NULL;
250         static char *proto[] = {"LRMS", "LKRN", "LKRO", "LKRA", "LZRM" };
251         char aaa[SIZ];
252         char *bbb = NULL;
253         size_t bbbsize;
254
255         if (!listing) return -2;
256         if (*listing) return -2;        /* Free the listing first */
257         if (!cret) return -2;
258         if (which < 0 || which > 4) return -2;
259         if (floor < -1) return -2;      /* Can't validate upper bound, sorry */
260
261         sprintf(aaa, "%s %d", proto[which], floor);
262         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, &bbb, &bbbsize, cret);
263         if (ret / 100 == 1) {
264                 struct march *mptr;
265
266                 while (bbb && strlen(bbb)) {
267                         int a;
268
269                         extract_token(aaa, bbb, 0, '\n');
270                         a = strlen(aaa);
271                         memmove(bbb, bbb + a + 1, strlen(bbb) - a);
272                         mptr = (struct march *) malloc(sizeof (struct march));
273                         if (mptr) {
274                                 mptr->next = NULL;
275                                 extract(mptr->march_name, aaa, 0);
276                                 mptr->march_floor = (char) extract_int(aaa, 2);
277                                 mptr->march_order = (char) extract_int(aaa, 3);
278                                 if (march == NULL)
279                                         march = mptr;
280                                 else {
281                                         struct march *mptr2;
282
283                                         mptr2 = march;
284                                         while (mptr2->next != NULL)
285                                                 mptr2 = mptr2->next;
286                                         mptr2->next = mptr;
287                                 }
288                         }
289                 }
290         }
291         *listing = march;
292         return ret;
293 }
294
295
296 /* GETU */
297 /* Caller must free the struct usersupp; caller may pass an existing one */
298 int CtdlIPCGetConfig(CtdlIPC *ipc, struct usersupp **uret, char *cret)
299 {
300         register int ret;
301
302         if (!cret) return -2;
303         if (!uret) return -2;
304         if (!*uret) *uret = (struct usersupp *)calloc(1, sizeof (struct usersupp));
305         if (!*uret) return -1;
306
307         ret = CtdlIPCGenericCommand(ipc, "GETU", NULL, 0, NULL, NULL, cret);
308         if (ret / 100 == 2) {
309                 uret[0]->USscreenwidth = extract_int(cret, 0);
310                 uret[0]->USscreenheight = extract_int(cret, 1);
311                 uret[0]->flags = extract_int(cret, 2);
312         }
313         return ret;
314 }
315
316
317 /* SETU */
318 int CtdlIPCSetConfig(CtdlIPC *ipc, struct usersupp *uret, char *cret)
319 {
320         char aaa[48];
321
322         if (!uret) return -2;
323         if (!cret) return -2;
324
325         sprintf(aaa, "SETU %d|%d|%d",
326                         uret->USscreenwidth, uret->USscreenheight,
327                         uret->flags);
328         return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
329 }
330
331
332 /* GOTO */
333 int CtdlIPCGotoRoom(CtdlIPC *ipc, const char *room, const char *passwd,
334                 struct ctdlipcroom **rret, char *cret)
335 {
336         register int ret;
337         char *aaa;
338
339         if (!cret) return -2;
340         if (!rret) return -2;
341         if (!*rret) *rret = (struct ctdlipcroom *)calloc(1, sizeof (struct ctdlipcroom));
342         if (!*rret) return -1;
343
344         if (passwd) {
345                 aaa = (char *)malloc(strlen(room) + strlen(passwd) + 7);
346                 if (!aaa) {
347                         free(*rret);
348                         return -1;
349                 }
350                 sprintf(aaa, "GOTO %s|%s", room, passwd);
351         } else {
352                 aaa = (char *)malloc(strlen(room) + 6);
353                 if (!aaa) {
354                         free(*rret);
355                         return -1;
356                 }
357                 sprintf(aaa, "GOTO %s", room);
358         }
359         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
360         if (ret / 100 == 2) {
361                 extract(rret[0]->RRname, cret, 0);
362                 rret[0]->RRunread = extract_long(cret, 1);
363                 rret[0]->RRtotal = extract_long(cret, 2);
364                 rret[0]->RRinfoupdated = extract_int(cret, 3);
365                 rret[0]->RRflags = extract_int(cret, 4);
366                 rret[0]->RRhighest = extract_long(cret, 5);
367                 rret[0]->RRlastread = extract_long(cret, 6);
368                 rret[0]->RRismailbox = extract_int(cret, 7);
369                 rret[0]->RRaide = extract_int(cret, 8);
370                 rret[0]->RRnewmail = extract_long(cret, 9);
371                 rret[0]->RRfloor = extract_int(cret, 10);
372         } else {
373                 free(*rret);
374         }
375         return ret;
376 }
377
378
379 /* MSGS */
380 /* which is 0 = all, 1 = old, 2 = new, 3 = last, 4 = first, 5 = gt, 6 = lt */
381 /* whicharg is number of messages, applies to last, first, gt, lt */
382 int CtdlIPCGetMessages(CtdlIPC *ipc, int which, int whicharg, const char *template,
383                 long **mret, char *cret)
384 {
385         register int ret;
386         register long count = 0;
387         static char *proto[] =
388                 { "ALL", "OLD", "NEW", "LAST", "FIRST", "GT", "LT" };
389         char aaa[33];
390         char *bbb;
391         size_t bbbsize;
392
393         if (!cret) return -2;
394         if (!mret) return -2;
395         if (*mret) return -2;
396         if (which < 0 || which > 6) return -2;
397
398         if (which <= 2)
399                 sprintf(aaa, "MSGS %s||%d", proto[which],
400                                 (template) ? 1 : 0);
401         else
402                 sprintf(aaa, "MSGS %s|%d|%d", proto[which], whicharg,
403                                 (template) ? 1 : 0);
404         if (template) count = strlen(template);
405         ret = CtdlIPCGenericCommand(ipc, aaa, template, count, &bbb, &bbbsize, cret);
406         count = 0;
407         while (strlen(bbb)) {
408                 int a;
409
410                 extract_token(aaa, bbb, 0, '\n');
411                 a = strlen(aaa);
412                 memmove(aaa, bbb + a + 1, strlen(bbb) - a - 1);
413                 *mret = (long *)realloc(mret,
414                                         (size_t)((count + 1) * sizeof (long)));
415                 if (*mret)
416                         *mret[count++] = atol(aaa);
417                 *mret[count] = 0L;
418         }
419         return ret;
420 }
421
422
423 /* MSG0, MSG2 */
424 int CtdlIPCGetSingleMessage(CtdlIPC *ipc, long msgnum, int headers, int as_mime,
425                 struct ctdlipcmessage **mret, char *cret)
426 {
427         register int ret;
428         char aaa[SIZ];
429         char *bbb = NULL;
430         size_t bbbsize;
431         int multipart_hunting = 0;
432         char multipart_prefix[SIZ];
433
434         if (!cret) return -1;
435         if (!mret) return -1;
436         if (!*mret) *mret = (struct ctdlipcmessage *)calloc(1, sizeof (struct ctdlipcmessage));
437         if (!*mret) return -1;
438         if (!msgnum) return -1;
439
440         strcpy(mret[0]->content_type, "");
441         sprintf(aaa, "MSG%d %ld|%d", as_mime, msgnum, headers);
442         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, &bbb, &bbbsize, cret);
443         if (ret / 100 == 1) {
444                 if (as_mime != 2) {
445                         strcpy(mret[0]->mime_chosen, "1");      /* Default chosen-part is "1" */
446                         while (strlen(bbb) > 4 && bbb[4] == '=') {
447                                 extract_token(aaa, bbb, 0, '\n');
448                                 remove_token(bbb, 0, '\n');
449
450                                 if (!strncasecmp(aaa, "nhdr=yes", 8))
451                                         mret[0]->nhdr = 1;
452                                 else if (!strncasecmp(aaa, "from=", 5))
453                                         strcpy(mret[0]->author, &aaa[5]);
454                                 else if (!strncasecmp(aaa, "type=", 5))
455                                         mret[0]->type = atoi(&aaa[5]);
456                                 else if (!strncasecmp(aaa, "msgn=", 5))
457                                         strcpy(mret[0]->msgid, &aaa[5]);
458                                 else if (!strncasecmp(aaa, "subj=", 5))
459                                         strcpy(mret[0]->subject, &aaa[5]);
460                                 else if (!strncasecmp(aaa, "rfca=", 5))
461                                         strcpy(mret[0]->email, &aaa[5]);
462                                 else if (!strncasecmp(aaa, "hnod=", 5))
463                                         strcpy(mret[0]->hnod, &aaa[5]);
464                                 else if (!strncasecmp(aaa, "room=", 5))
465                                         strcpy(mret[0]->room, &aaa[5]);
466                                 else if (!strncasecmp(aaa, "node=", 5))
467                                         strcpy(mret[0]->node, &aaa[5]);
468                                 else if (!strncasecmp(aaa, "rcpt=", 5))
469                                         strcpy(mret[0]->recipient, &aaa[5]);
470                                 else if (!strncasecmp(aaa, "time=", 5))
471                                         mret[0]->time = atol(&aaa[5]);
472
473                                 /* Multipart/alternative prefix & suffix strings help
474                                  * us to determine which part we want to download.
475                                  */
476                                 else if (!strncasecmp(aaa, "pref=", 5)) {
477                                         extract(multipart_prefix, &aaa[5], 1);
478                                         if (!strcasecmp(multipart_prefix,
479                                            "multipart/alternative")) {
480                                                 ++multipart_hunting;
481                                         }
482                                 }
483                                 else if (!strncasecmp(aaa, "suff=", 5)) {
484                                         extract(multipart_prefix, &aaa[5], 1);
485                                         if (!strcasecmp(multipart_prefix,
486                                            "multipart/alternative")) {
487                                                 ++multipart_hunting;
488                                         }
489                                 }
490
491                                 else if (!strncasecmp(aaa, "part=", 5)) {
492                                         struct parts *ptr, *chain;
493         
494                                         ptr = (struct parts *)calloc(1, sizeof (struct parts));
495                                         if (ptr) {
496
497                                                 /* Fill the buffers for the caller */
498                                                 extract(ptr->name, &aaa[5], 0);
499                                                 extract(ptr->filename, &aaa[5], 1);
500                                                 extract(ptr->number, &aaa[5], 2);
501                                                 extract(ptr->disposition, &aaa[5], 3);
502                                                 extract(ptr->mimetype, &aaa[5], 4);
503                                                 ptr->length = extract_long(&aaa[5], 5);
504                                                 if (!mret[0]->attachments)
505                                                         mret[0]->attachments = ptr;
506                                                 else {
507                                                         chain = mret[0]->attachments;
508                                                         while (chain->next)
509                                                                 chain = chain->next;
510                                                         chain->next = ptr;
511                                                 }
512
513                                                 /* Now handle multipart/alternative */
514                                                 if (multipart_hunting > 0) {
515                                                         if ( (!strcasecmp(ptr->mimetype,
516                                                              "text/plain"))
517                                                            || (!strcasecmp(ptr->mimetype,
518                                                               "text/html")) ) {
519                                                                 strcpy(mret[0]->mime_chosen,
520                                                                         ptr->number);
521                                                         }
522                                                 }
523
524                                         }
525                                 }
526                         }
527                         /* Eliminate "text\n" */
528                         remove_token(bbb, 0, '\n');
529
530                         /* If doing a MIME thing, pull out the extra headers */
531                         if (as_mime == 4) {
532                                 do {
533                                         if (!strncasecmp(bbb, "Content-type: ", 14)) {
534                                                 extract_token(mret[0]->content_type, bbb, 0, '\n');
535                                                 strcpy(mret[0]->content_type,
536                                                         &mret[0]->content_type[14]);
537                                                 striplt(mret[0]->content_type);
538                                         }
539                                         remove_token(bbb, 0, '\n');
540                                 } while ((bbb[0] != 0) && (bbb[0] != '\n'));
541                         }
542
543
544                 }
545                 if (strlen(bbb)) {
546                         /* Strip trailing whitespace */
547                         bbb = (char *)realloc(bbb, (size_t)(strlen(bbb) + 1));
548                 } else {
549                         bbb = (char *)realloc(bbb, 1);
550                         *bbb = '\0';
551                 }
552                 mret[0]->text = bbb;
553         }
554         return ret;
555 }
556
557
558 /* WHOK */
559 int CtdlIPCWhoKnowsRoom(CtdlIPC *ipc, char **listing, char *cret)
560 {
561         register int ret;
562         size_t bytes;
563
564         if (!cret) return -2;
565         if (!listing) return -2;
566         if (*listing) return -2;
567
568         ret = CtdlIPCGenericCommand(ipc, "WHOK", NULL, 0, listing, &bytes, cret);
569         return ret;
570 }
571
572
573 /* INFO */
574 int CtdlIPCServerInfo(CtdlIPC *ipc, struct CtdlServInfo *ServInfo, char *cret)
575 {
576         register int ret;
577         size_t bytes;
578         char *listing = NULL;
579         char buf[SIZ];
580
581         if (!cret) return -2;
582         if (!ServInfo) return -2;
583
584         ret = CtdlIPCGenericCommand(ipc, "INFO", NULL, 0, &listing, &bytes, cret);
585         if (ret / 100 == 1) {
586                 int line = 0;
587
588                 while (*listing && strlen(listing)) {
589                         extract_token(buf, listing, 0, '\n');
590                         remove_token(listing, 0, '\n');
591                         switch (line++) {
592                         case 0:         ServInfo->serv_pid = atoi(buf);
593                                         break;
594                         case 1:         strcpy(ServInfo->serv_nodename,buf);
595                                         break;
596                         case 2:         strcpy(ServInfo->serv_humannode,buf);
597                                         break;
598                         case 3:         strcpy(ServInfo->serv_fqdn,buf);
599                                         break;
600                         case 4:         strcpy(ServInfo->serv_software,buf);
601                                         break;
602                         case 5:         ServInfo->serv_rev_level = atoi(buf);
603                                         break;
604                         case 6:         strcpy(ServInfo->serv_bbs_city,buf);
605                                         break;
606                         case 7:         strcpy(ServInfo->serv_sysadm,buf);
607                                         break;
608                         case 9:         strcpy(ServInfo->serv_moreprompt,buf);
609                                         break;
610                         case 10:        ServInfo->serv_ok_floors = atoi(buf);
611                                         break;
612                         case 11:        ServInfo->serv_paging_level = atoi(buf);
613                                         break;
614                         case 13:        ServInfo->serv_supports_qnop = atoi(buf);
615                                         break;
616                         }
617                 }
618
619         }
620         return ret;
621 }
622
623
624 /* RDIR */
625 int CtdlIPCReadDirectory(CtdlIPC *ipc, char **listing, char *cret)
626 {
627         register int ret;
628         size_t bytes;
629
630         if (!cret) return -2;
631         if (!listing) return -2;
632         if (*listing) return -2;
633
634         ret = CtdlIPCGenericCommand(ipc, "RDIR", NULL, 0, listing, &bytes, cret);
635         return ret;
636 }
637
638
639 /*
640  * Set last-read pointer in this room to msgnum, or 0 for HIGHEST.
641  */
642 int CtdlIPCSetLastRead(CtdlIPC *ipc, long msgnum, char *cret)
643 {
644         register int ret;
645         char aaa[16];
646
647         if (!cret) return -2;
648
649         if (msgnum)
650                 sprintf(aaa, "SLRP %ld", msgnum);
651         else
652                 sprintf(aaa, "SLRP HIGHEST");
653         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
654         return ret;
655 }
656
657
658 /* INVT */
659 int CtdlIPCInviteUserToRoom(CtdlIPC *ipc, const char *username, char *cret)
660 {
661         register int ret;
662         char *aaa;
663
664         if (!cret) return -2;
665         if (!username) return -2;
666
667         aaa = (char *)malloc(strlen(username) + 6);
668         if (!aaa) return -1;
669
670         sprintf(aaa, "INVT %s", username);
671         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
672         free(aaa);
673         return ret;
674 }
675
676
677 /* KICK */
678 int CtdlIPCKickoutUserFromRoom(CtdlIPC *ipc, const char *username, char *cret)
679 {
680         register int ret;
681         char *aaa;
682
683         if (!cret) return -1;
684         if (!username) return -1;
685
686         aaa = (char *)malloc(strlen(username) + 6);
687
688         sprintf(aaa, "KICK %s", username);
689         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
690         free(aaa);
691         return ret;
692 }
693
694
695 /* GETR */
696 int CtdlIPCGetRoomAttributes(CtdlIPC *ipc, struct quickroom **qret, char *cret)
697 {
698         register int ret;
699
700         if (!cret) return -2;
701         if (!qret) return -2;
702         if (!*qret) *qret = (struct quickroom *)calloc(1, sizeof (struct quickroom));
703         if (!*qret) return -1;
704
705         ret = CtdlIPCGenericCommand(ipc, "GETR", NULL, 0, NULL, NULL, cret);
706         if (ret / 100 == 2) {
707                 extract(qret[0]->QRname, cret, 0);
708                 extract(qret[0]->QRpasswd, cret, 1);
709                 extract(qret[0]->QRdirname, cret, 2);
710                 qret[0]->QRflags = extract_int(cret, 3);
711                 qret[0]->QRfloor = extract_int(cret, 4);
712                 qret[0]->QRorder = extract_int(cret, 5);
713         }
714         return ret;
715 }
716
717
718 /* SETR */
719 /* set forget to kick all users out of room */
720 int CtdlIPCSetRoomAttributes(CtdlIPC *ipc, int forget, struct quickroom *qret, char *cret)
721 {
722         register int ret;
723         char *aaa;
724
725         if (!cret) return -2;
726         if (!qret) return -2;
727
728         aaa = (char *)malloc(strlen(qret->QRname) + strlen(qret->QRpasswd) +
729                         strlen(qret->QRdirname) + 52);
730         if (!aaa) return -1;
731
732         sprintf(aaa, "SETR %s|%s|%s|%d|%d|%d|%d",
733                         qret->QRname, qret->QRpasswd, qret->QRdirname,
734                         qret->QRflags, forget, qret->QRfloor, qret->QRorder);
735         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
736         free(aaa);
737         return ret;
738 }
739
740
741 /* GETA */
742 int CtdlIPCGetRoomAide(CtdlIPC *ipc, char *cret)
743 {
744         if (!cret) return -1;
745
746         return CtdlIPCGenericCommand(ipc, "GETA", NULL, 0, NULL, NULL, cret);
747 }
748
749
750 /* SETA */
751 int CtdlIPCSetRoomAide(CtdlIPC *ipc, const char *username, char *cret)
752 {
753         register int ret;
754         char *aaa;
755
756         if (!cret) return -2;
757         if (!username) return -2;
758
759         aaa = (char *)malloc(strlen(username) + 6);
760         if (!aaa) return -1;
761
762         sprintf(aaa, "SETA %s", username);
763         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
764         free(aaa);
765         return ret;
766 }
767
768
769 /* ENT0 */
770 int CtdlIPCPostMessage(CtdlIPC *ipc, int flag, const struct ctdlipcmessage *mr, char *cret)
771 {
772         register int ret;
773         char *aaa;
774
775         if (!cret) return -2;
776         if (!mr) return -2;
777
778         aaa = (char *)malloc(strlen(mr->recipient) + strlen(mr->author) + 40);
779         if (!aaa) return -1;
780
781         sprintf(aaa, "ENT0 %d|%s|%d|%d|%s", flag, mr->recipient, mr->anonymous,
782                         mr->type, mr->author);
783         ret = CtdlIPCGenericCommand(ipc, aaa, mr->text, strlen(mr->text), NULL,
784                         NULL, cret);
785         free(aaa);
786         return ret;
787 }
788
789
790 /* RINF */
791 int CtdlIPCRoomInfo(CtdlIPC *ipc, char **iret, char *cret)
792 {
793         size_t bytes;
794
795         if (!cret) return -2;
796         if (!iret) return -2;
797         if (*iret) return -2;
798
799         return CtdlIPCGenericCommand(ipc, "RINF", NULL, 0, iret, &bytes, cret);
800 }
801
802
803 /* DELE */
804 int CtdlIPCDeleteMessage(CtdlIPC *ipc, long msgnum, char *cret)
805 {
806         char aaa[16];
807
808         if (!cret) return -2;
809         if (!msgnum) return -2;
810
811         sprintf(aaa, "DELE %ld", msgnum);
812         return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
813 }
814
815
816 /* MOVE */
817 int CtdlIPCMoveMessage(CtdlIPC *ipc, int copy, long msgnum, const char *destroom, char *cret)
818 {
819         register int ret;
820         char *aaa;
821
822         if (!cret) return -2;
823         if (!destroom) return -2;
824         if (!msgnum) return -2;
825
826         aaa = (char *)malloc(strlen(destroom) + 28);
827         if (!aaa) return -1;
828
829         sprintf(aaa, "MOVE %ld|%s|%d", msgnum, destroom, copy);
830         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
831         free(aaa);
832         return ret;
833 }
834
835
836 /* KILL */
837 int CtdlIPCDeleteRoom(CtdlIPC *ipc, int for_real, char *cret)
838 {
839         char aaa[16];
840
841         if (!cret) return -2;
842
843         sprintf(aaa, "KILL %d", for_real);
844         return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
845 }
846
847
848 /* CRE8 */
849 int CtdlIPCCreateRoom(CtdlIPC *ipc, int for_real, const char *roomname, int type,
850                 const char *password, int floor, char *cret)
851 {
852         register int ret;
853         char *aaa;
854
855         if (!cret) return -2;
856         if (!roomname) return -2;
857
858         if (password) {
859                 aaa = (char *)malloc(strlen(roomname) + strlen(password) + 40);
860                 if (!aaa) return -1;
861                 sprintf(aaa, "CRE8 %d|%s|%d|%s|%d", for_real, roomname, type,
862                                 password, floor);
863         } else {
864                 aaa = (char *)malloc(strlen(roomname) + 40);
865                 if (!aaa) return -1;
866                 sprintf(aaa, "CRE8 %d|%s|%d||%d", for_real, roomname, type,
867                                 floor);
868         }
869         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
870         free(aaa);
871         return ret;
872 }
873
874
875 /* FORG */
876 int CtdlIPCForgetRoom(CtdlIPC *ipc, char *cret)
877 {
878         if (!cret) return -2;
879
880         return CtdlIPCGenericCommand(ipc, "FORG", NULL, 0, NULL, NULL, cret);
881 }
882
883
884 /* MESG */
885 int CtdlIPCSystemMessage(CtdlIPC *ipc, const char *message, char **mret, char *cret)
886 {
887         register int ret;
888         char *aaa;
889         size_t bytes;
890
891         if (!cret) return -2;
892         if (!mret) return -2;
893         if (*mret) return -2;
894         if (!message) return -2;
895
896         aaa = (char *)malloc(strlen(message) + 6);
897         if (!aaa) return -1;
898
899         sprintf(aaa, "MESG %s", message);
900         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, mret, &bytes, cret);
901         free(aaa);
902         return ret;
903 }
904
905
906 /* GNUR */
907 int CtdlIPCNextUnvalidatedUser(CtdlIPC *ipc, char *cret)
908 {
909         if (!cret) return -2;
910
911         return CtdlIPCGenericCommand(ipc, "GNUR", NULL, 0, NULL, NULL, cret);
912 }
913
914
915 /* GREG */
916 int CtdlIPCGetUserRegistration(CtdlIPC *ipc, const char *username, char **rret, char *cret)
917 {
918         register int ret;
919         char *aaa;
920         size_t bytes;
921
922         if (!cret) return -2;
923         if (!rret) return -2;
924         if (*rret) return -2;
925
926         if (username)
927                 aaa = (char *)malloc(strlen(username) + 6);
928         else
929                 aaa = (char *)malloc(12);
930         if (!aaa) return -1;
931
932         if (username)
933                 sprintf(aaa, "GREG %s", username);
934         else
935                 sprintf(aaa, "GREG _SELF_");
936         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, rret, &bytes, cret);
937         free(aaa);
938         return ret;
939 }
940
941
942 /* VALI */
943 int CtdlIPCValidateUser(CtdlIPC *ipc, const char *username, int axlevel, char *cret)
944 {
945         register int ret;
946         char *aaa;
947
948         if (!cret) return -2;
949         if (!username) return -2;
950         if (axlevel < 0 || axlevel > 7) return -2;
951
952         aaa = (char *)malloc(strlen(username) + 17);
953         if (!aaa) return -1;
954
955         sprintf(aaa, "VALI %s|%d", username, axlevel);
956         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
957         free(aaa);
958         return ret;
959 }
960
961
962 /* EINF */
963 int CtdlIPCSetRoomInfo(CtdlIPC *ipc, int for_real, const char *info, char *cret)
964 {
965         char aaa[16];
966
967         if (!cret) return -1;
968         if (!info) return -1;
969
970         sprintf(aaa, "EINF %d", for_real);
971         return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
972 }
973
974
975 /* LIST */
976 int CtdlIPCUserListing(CtdlIPC *ipc, char **listing, char *cret)
977 {
978         size_t bytes;
979
980         if (!cret) return -1;
981         if (!listing) return -1;
982         if (*listing) return -1;
983
984         return CtdlIPCGenericCommand(ipc, "LIST", NULL, 0, listing, &bytes, cret);
985 }
986
987
988 /* REGI */
989 int CtdlIPCSetRegistration(CtdlIPC *ipc, const char *info, char *cret)
990 {
991         if (!cret) return -1;
992         if (!info) return -1;
993
994         return CtdlIPCGenericCommand(ipc, "REGI", info, strlen(info),
995                         NULL, NULL, cret);
996 }
997
998
999 /* CHEK */
1000 int CtdlIPCMiscCheck(CtdlIPC *ipc, struct ctdlipcmisc *chek, char *cret)
1001 {
1002         register int ret;
1003
1004         if (!cret) return -1;
1005         if (!chek) return -1;
1006
1007         ret = CtdlIPCGenericCommand(ipc, "CHEK", NULL, 0, NULL, NULL, cret);
1008         if (ret / 100 == 2) {
1009                 chek->newmail = extract_long(cret, 0);
1010                 chek->needregis = extract_int(cret, 1);
1011                 chek->needvalid = extract_int(cret, 2);
1012         }
1013         return ret;
1014 }
1015
1016
1017 /* DELF */
1018 int CtdlIPCDeleteFile(CtdlIPC *ipc, const char *filename, char *cret)
1019 {
1020         register int ret;
1021         char *aaa;
1022
1023         if (!cret) return -2;
1024         if (!filename) return -2;
1025         
1026         aaa = (char *)malloc(strlen(filename) + 6);
1027         if (!aaa) return -1;
1028
1029         sprintf(aaa, "DELF %s", filename);
1030         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1031         free(aaa);
1032         return ret;
1033 }
1034
1035
1036 /* MOVF */
1037 int CtdlIPCMoveFile(CtdlIPC *ipc, const char *filename, const char *destroom, char *cret)
1038 {
1039         register int ret;
1040         char *aaa;
1041
1042         if (!cret) return -2;
1043         if (!filename) return -2;
1044         if (!destroom) return -2;
1045
1046         aaa = (char *)malloc(strlen(filename) + strlen(destroom) + 7);
1047         if (!aaa) return -1;
1048
1049         sprintf(aaa, "MOVF %s|%s", filename, destroom);
1050         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1051         free(aaa);
1052         return ret;
1053 }
1054
1055
1056 /* NETF */
1057 int CtdlIPCNetSendFile(CtdlIPC *ipc, const char *filename, const char *destnode, char *cret)
1058 {
1059         register int ret;
1060         char *aaa;
1061
1062         if (!cret) return -2;
1063         if (!filename) return -2;
1064         if (!destnode) return -2;
1065
1066         aaa = (char *)malloc(strlen(filename) + strlen(destnode) + 7);
1067         if (!aaa) return -1;
1068
1069         sprintf(aaa, "NETF %s|%s", filename, destnode);
1070         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1071         free(aaa);
1072         return ret;
1073 }
1074
1075
1076 /* RWHO */
1077 int CtdlIPCOnlineUsers(CtdlIPC *ipc, char **listing, time_t *stamp, char *cret)
1078 {
1079         register int ret;
1080         size_t bytes;
1081
1082         if (!cret) return -1;
1083         if (!listing) return -1;
1084         if (*listing) return -1;
1085
1086         *stamp = CtdlIPCServerTime(ipc, cret);
1087         if (!*stamp)
1088                 *stamp = time(NULL);
1089         ret = CtdlIPCGenericCommand(ipc, "RWHO", NULL, 0, listing, &bytes, cret);
1090         return ret;
1091 }
1092
1093
1094 /* OPEN */
1095 int CtdlIPCFileDownload(CtdlIPC *ipc, const char *filename, void **buf,
1096                 void (*progress_gauge_callback)(long, long), char *cret)
1097 {
1098         register int ret;
1099         size_t bytes;
1100         time_t last_mod;
1101         char mimetype[SIZ];
1102         char *aaa;
1103
1104         if (!cret) return -2;
1105         if (!filename) return -2;
1106         if (!buf) return -2;
1107         if (*buf) return -2;
1108         if (ipc->downloading) return -2;
1109
1110         aaa = (char *)malloc(strlen(filename) + 6);
1111         if (!aaa) return -1;
1112
1113         sprintf(aaa, "OPEN %s", filename);
1114         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1115         free(aaa);
1116         if (ret / 100 == 2) {
1117                 ipc->downloading = 1;
1118                 bytes = extract_long(cret, 0);
1119                 last_mod = extract_int(cret, 1);
1120                 extract(mimetype, cret, 2);
1121                 ret = CtdlIPCReadDownload(ipc, buf, bytes, progress_gauge_callback, cret);
1122 /*              ret = CtdlIPCHighSpeedReadDownload(ipc, buf, bytes, progress_gauge_callback, cret); */
1123                 ret = CtdlIPCEndDownload(ipc, cret);
1124                 if (ret / 100 == 2)
1125                         sprintf(cret, "%d|%ld|%s|%s", bytes, last_mod,
1126                                         filename, mimetype);
1127         }
1128         return ret;
1129 }
1130
1131
1132 /* OPNA */
1133 int CtdlIPCAttachmentDownload(CtdlIPC *ipc, long msgnum, const char *part, void **buf,
1134                 void (*progress_gauge_callback)(long, long), char *cret)
1135 {
1136         register int ret;
1137         size_t bytes;
1138         time_t last_mod;
1139         char filename[SIZ];
1140         char mimetype[SIZ];
1141         char *aaa;
1142
1143         if (!cret) return -2;
1144         if (!buf) return -2;
1145         if (*buf) return -2;
1146         if (!part) return -2;
1147         if (!msgnum) return -2;
1148         if (ipc->downloading) return -2;
1149
1150         aaa = (char *)malloc(strlen(part) + 17);
1151         if (!aaa) return -1;
1152
1153         sprintf(aaa, "OPNA %ld|%s", msgnum, part);
1154         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1155         free(aaa);
1156         if (ret / 100 == 2) {
1157                 ipc->downloading = 1;
1158                 bytes = extract_long(cret, 0);
1159                 last_mod = extract_int(cret, 1);
1160                 extract(mimetype, cret, 2);
1161                 ret = CtdlIPCHighSpeedReadDownload(ipc, buf, bytes, progress_gauge_callback, cret);
1162                 ret = CtdlIPCEndDownload(ipc, cret);
1163                 if (ret / 100 == 2)
1164                         sprintf(cret, "%d|%ld|%s|%s", bytes, last_mod,
1165                                         filename, mimetype);
1166         }
1167         return ret;
1168 }
1169
1170
1171 /* OIMG */
1172 int CtdlIPCImageDownload(CtdlIPC *ipc, const char *filename, void **buf,
1173                 void (*progress_gauge_callback)(long, long), char *cret)
1174 {
1175         register int ret;
1176         size_t bytes;
1177         time_t last_mod;
1178         char mimetype[SIZ];
1179         char *aaa;
1180
1181         if (!cret) return -1;
1182         if (!buf) return -1;
1183         if (*buf) return -1;
1184         if (!filename) return -1;
1185         if (ipc->downloading) return -1;
1186
1187         aaa = (char *)malloc(strlen(filename) + 6);
1188         if (!aaa) return -1;
1189
1190         sprintf(aaa, "OIMG %s", filename);
1191         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1192         free(aaa);
1193         if (ret / 100 == 2) {
1194                 ipc->downloading = 1;
1195                 bytes = extract_long(cret, 0);
1196                 last_mod = extract_int(cret, 1);
1197                 extract(mimetype, cret, 2);
1198                 ret = CtdlIPCReadDownload(ipc, buf, bytes, progress_gauge_callback, cret);
1199                 ret = CtdlIPCEndDownload(ipc, cret);
1200                 if (ret / 100 == 2)
1201                         sprintf(cret, "%d|%ld|%s|%s", bytes, last_mod,
1202                                         filename, mimetype);
1203         }
1204         return ret;
1205 }
1206
1207
1208 /* UOPN */
1209 int CtdlIPCFileUpload(CtdlIPC *ipc, const char *save_as, const char *comment,
1210                 const char *path, void (*progress_gauge_callback)(long, long),
1211                 char *cret)
1212 {
1213         register int ret;
1214         char *aaa;
1215
1216         if (!cret) return -1;
1217         if (!save_as) return -1;
1218         if (!comment) return -1;
1219         if (!path) return -1;
1220         if (!*path) return -1;
1221         if (ipc->uploading) return -1;
1222
1223         aaa = (char *)malloc(strlen(save_as) + strlen(comment) + 7);
1224         if (!aaa) return -1;
1225
1226         sprintf(aaa, "UOPN %s|%s", save_as, comment);
1227         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1228         free(aaa);
1229         if (ret / 100 == 2) {
1230                 ipc->uploading = 1;
1231                 ret = CtdlIPCWriteUpload(ipc, path, progress_gauge_callback, cret);
1232                 ret = CtdlIPCEndUpload(ipc, (ret == -2 ? 1 : 0), cret);
1233                 ipc->uploading = 0;
1234         }
1235         return ret;
1236 }
1237
1238
1239 /* UIMG */
1240 int CtdlIPCImageUpload(CtdlIPC *ipc, int for_real, const char *path,
1241                 const char *save_as,
1242                 void (*progress_gauge_callback)(long, long), char *cret)
1243 {
1244         register int ret;
1245         char *aaa;
1246
1247         if (!cret) return -1;
1248         if (!save_as) return -1;
1249         if (!path && for_real) return -1;
1250         if (!*path && for_real) return -1;
1251         if (ipc->uploading) return -1;
1252
1253         aaa = (char *)malloc(strlen(save_as) + 17);
1254         if (!aaa) return -1;
1255
1256         sprintf(aaa, "UIMG %d|%s", for_real, save_as);
1257         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1258         free(aaa);
1259         if (ret / 100 == 2 && for_real) {
1260                 ipc->uploading = 1;
1261                 ret = CtdlIPCWriteUpload(ipc, path, progress_gauge_callback, cret);
1262                 ret = CtdlIPCEndUpload(ipc, (ret == -2 ? 1 : 0), cret);
1263                 ipc->uploading = 0;
1264         }
1265         return ret;
1266 }
1267
1268
1269 /* QUSR */
1270 int CtdlIPCQueryUsername(CtdlIPC *ipc, const char *username, char *cret)
1271 {
1272         register int ret;
1273         char *aaa;
1274
1275         if (!cret) return -2;
1276         if (!username) return -2;
1277
1278         aaa = (char *)malloc(strlen(username) + 6);
1279         if (!aaa) return -1;
1280
1281         sprintf(aaa, "QUSR %s", username);
1282         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1283         free(aaa);
1284         return ret;
1285 }
1286
1287
1288 /* LFLR */
1289 int CtdlIPCFloorListing(CtdlIPC *ipc, char **listing, char *cret)
1290 {
1291         size_t bytes;
1292
1293         if (!cret) return -2;
1294         if (!listing) return -2;
1295         if (*listing) return -2;
1296
1297         return CtdlIPCGenericCommand(ipc, "LFLR", NULL, 0, listing, &bytes, cret);
1298 }
1299
1300
1301 /* CFLR */
1302 int CtdlIPCCreateFloor(CtdlIPC *ipc, int for_real, const char *name, char *cret)
1303 {
1304         register int ret;
1305         char *aaa;
1306
1307         if (!cret) return -2;
1308         if (!name) return -2;
1309
1310         aaa = (char *)malloc(strlen(name) + 17);
1311         if (!aaa) return -1;
1312
1313         sprintf(aaa, "CFLR %s|%d", name, for_real);
1314         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1315         free(aaa);
1316         return ret;
1317 }
1318
1319
1320 /* KFLR */
1321 int CtdlIPCDeleteFloor(CtdlIPC *ipc, int for_real, int floornum, char *cret)
1322 {
1323         char aaa[27];
1324
1325         if (!cret) return -1;
1326         if (floornum < 0) return -1;
1327
1328         sprintf(aaa, "KFLR %d|%d", floornum, for_real);
1329         return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1330 }
1331
1332
1333 /* EFLR */
1334 int CtdlIPCEditFloor(CtdlIPC *ipc, int floornum, const char *floorname, char *cret)
1335 {
1336         register int ret;
1337         char *aaa;
1338
1339         if (!cret) return -2;
1340         if (!floorname) return -2;
1341         if (floornum < 0) return -2;
1342
1343         aaa = (char *)malloc(strlen(floorname) + 17);
1344         if (!aaa) return -1;
1345
1346         sprintf(aaa, "EFLR %d|%s", floornum, floorname);
1347         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1348         free(aaa);
1349         return ret;
1350 }
1351
1352
1353 /* IDEN */
1354 int CtdlIPCIdentifySoftware(CtdlIPC *ipc, int developerid, int clientid, int revision,
1355                 const char *software_name, const char *hostname, char *cret)
1356 {
1357         register int ret;
1358         char *aaa;
1359
1360         if (developerid < 0) return -2;
1361         if (clientid < 0) return -2;
1362         if (revision < 0) return -2;
1363         if (!software_name) return -2;
1364         if (!hostname) return -2;
1365
1366         aaa = (char *)malloc(strlen(software_name) + strlen(hostname) + 29);
1367         if (!aaa) return -1;
1368
1369         sprintf(aaa, "IDEN %d|%d|%d|%s|%s", developerid, clientid,
1370                         revision, software_name, hostname);
1371         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1372         free(aaa);
1373         return ret;
1374 }
1375
1376
1377 /* SEXP */
1378 int CtdlIPCSendInstantMessage(CtdlIPC *ipc, const char *username, const char *text,
1379                 char *cret)
1380 {
1381         register int ret;
1382         char *aaa;
1383
1384         if (!cret) return -2;
1385         if (!username) return -2;
1386
1387         aaa = (char *)malloc(strlen(username) + 8);
1388         if (!aaa) return -1;
1389
1390         if (text) {
1391                 sprintf(aaa, "SEXP %s|-", username);
1392                 ret = CtdlIPCGenericCommand(ipc, aaa, text, strlen(text),
1393                                 NULL, NULL, cret);
1394         } else {
1395                 sprintf(aaa, "SEXP %s||", username);
1396                 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1397         }
1398         free(aaa);
1399         return ret;
1400 }
1401
1402
1403 /* GEXP */
1404 int CtdlIPCGetInstantMessage(CtdlIPC *ipc, char **listing, char *cret)
1405 {
1406         size_t bytes;
1407
1408         if (!cret) return -2;
1409         if (!listing) return -2;
1410         if (*listing) return -2;
1411
1412         return CtdlIPCGenericCommand(ipc, "GEXP", NULL, 0, listing, &bytes, cret);
1413 }
1414
1415
1416 /* DEXP */
1417 /* mode is 0 = enable, 1 = disable, 2 = status */
1418 int CtdlIPCEnableInstantMessageReceipt(CtdlIPC *ipc, int mode, char *cret)
1419 {
1420         char aaa[16];
1421
1422         if (!cret) return -2;
1423
1424         sprintf(aaa, "DEXP %d", mode);
1425         return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1426 }
1427
1428
1429 /* EBIO */
1430 int CtdlIPCSetBio(CtdlIPC *ipc, char *bio, char *cret)
1431 {
1432         if (!cret) return -2;
1433         if (!bio) return -2;
1434
1435         return CtdlIPCGenericCommand(ipc, "EBIO", bio, strlen(bio),
1436                         NULL, NULL, cret);
1437 }
1438
1439
1440 /* RBIO */
1441 int CtdlIPCGetBio(CtdlIPC *ipc, const char *username, char **listing, char *cret)
1442 {
1443         register int ret;
1444         size_t bytes;
1445         char *aaa;
1446
1447         if (!cret) return -2;
1448         if (!username) return -2;
1449         if (!listing) return -2;
1450         if (*listing) return -2;
1451
1452         aaa = (char *)malloc(strlen(username) + 6);
1453         if (!aaa) return -1;
1454
1455         sprintf(aaa, "RBIO %s", username);
1456         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, listing, &bytes, cret);
1457         free(aaa);
1458         return ret;
1459 }
1460
1461
1462 /* LBIO */
1463 int CtdlIPCListUsersWithBios(CtdlIPC *ipc, char **listing, char *cret)
1464 {
1465         size_t bytes;
1466
1467         if (!cret) return -2;
1468         if (!listing) return -2;
1469         if (*listing) return -2;
1470
1471         return CtdlIPCGenericCommand(ipc, "LBIO", NULL, 0, listing, &bytes, cret);
1472 }
1473
1474
1475 /* STEL */
1476 int CtdlIPCStealthMode(CtdlIPC *ipc, int mode, char *cret)
1477 {
1478         char aaa[16];
1479
1480         if (!cret) return -1;
1481
1482         sprintf(aaa, "STEL %d", mode ? 1 : 0);
1483         return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1484 }
1485
1486
1487 /* TERM */
1488 int CtdlIPCTerminateSession(CtdlIPC *ipc, int sid, char *cret)
1489 {
1490         char aaa[16];
1491
1492         if (!cret) return -1;
1493
1494         sprintf(aaa, "TERM %d", sid);
1495         return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1496 }
1497
1498
1499 /* DOWN */
1500 int CtdlIPCTerminateServerNow(CtdlIPC *ipc, char *cret)
1501 {
1502         if (!cret) return -1;
1503
1504         return CtdlIPCGenericCommand(ipc, "DOWN", NULL, 0, NULL, NULL, cret);
1505 }
1506
1507
1508 /* SCDN */
1509 int CtdlIPCTerminateServerScheduled(CtdlIPC *ipc, int mode, char *cret)
1510 {
1511         char aaa[16];
1512
1513         if (!cret) return -1;
1514
1515         sprintf(aaa, "SCDN %d", mode ? 1 : 0);
1516         return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1517 }
1518
1519
1520 /* EMSG */
1521 int CtdlIPCEnterSystemMessage(CtdlIPC *ipc, const char *filename, const char *text,
1522                 char *cret)
1523 {
1524         register int ret;
1525         char *aaa;
1526
1527         if (!cret) return -2;
1528         if (!text) return -2;
1529         if (!filename) return -2;
1530
1531         aaa = (char *)malloc(strlen(filename) + 6);
1532         if (!aaa) return -1;
1533
1534         sprintf(aaa, "EMSG %s", filename);
1535         ret = CtdlIPCGenericCommand(ipc, aaa, text, strlen(text), NULL, NULL, cret);
1536         free(aaa);
1537         return ret;
1538 }
1539
1540
1541 /* HCHG */
1542 int CtdlIPCChangeHostname(CtdlIPC *ipc, const char *hostname, char *cret)
1543 {
1544         register int ret;
1545         char *aaa;
1546
1547         if (!cret) return -2;
1548         if (!hostname) return -2;
1549
1550         aaa = (char *)malloc(strlen(hostname) + 6);
1551         if (!aaa) return -1;
1552
1553         sprintf(aaa, "HCHG %s", hostname);
1554         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1555         free(aaa);
1556         return ret;
1557 }
1558
1559
1560 /* RCHG */
1561 int CtdlIPCChangeRoomname(CtdlIPC *ipc, const char *roomname, char *cret)
1562 {
1563         register int ret;
1564         char *aaa;
1565
1566         if (!cret) return -2;
1567         if (!roomname) return -2;
1568
1569         aaa = (char *)malloc(strlen(roomname) + 6);
1570         if (!aaa) return -1;
1571
1572         sprintf(aaa, "RCHG %s", roomname);
1573         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1574         free(aaa);
1575         return ret;
1576 }
1577
1578
1579 /* UCHG */
1580 int CtdlIPCChangeUsername(CtdlIPC *ipc, const char *username, char *cret)
1581 {
1582         register int ret;
1583         char *aaa;
1584
1585         if (!cret) return -2;
1586         if (!username) return -2;
1587
1588         aaa = (char *)malloc(strlen(username) + 6);
1589         if (!aaa) return -1;
1590
1591         sprintf(aaa, "UCHG %s", username);
1592         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1593         free(aaa);
1594         return ret;
1595 }
1596
1597
1598 /* TIME */
1599 /* This function returns the actual server time reported, or 0 if error */
1600 time_t CtdlIPCServerTime(CtdlIPC *ipc, char *cret)
1601 {
1602         register time_t tret;
1603         register int ret;
1604
1605         ret = CtdlIPCGenericCommand(ipc, "TIME", NULL, 0, NULL, NULL, cret);
1606         if (ret / 100 == 2) {
1607                 tret = extract_long(cret, 0);
1608         } else {
1609                 tret = 0L;
1610         }
1611         return tret;
1612 }
1613
1614
1615 /* AGUP */
1616 int CtdlIPCAideGetUserParameters(CtdlIPC *ipc, const char *who,
1617                                  struct usersupp **uret, char *cret)
1618 {
1619         register int ret;
1620         char aaa[SIZ];
1621
1622         if (!cret) return -2;
1623         if (!uret) return -2;
1624         if (!*uret) *uret = (struct usersupp *)calloc(1, sizeof(struct usersupp));
1625         if (!*uret) return -1;
1626
1627         sprintf(aaa, "AGUP %s", who);
1628         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1629
1630         if (ret / 100 == 2) {
1631                 extract(uret[0]->fullname, cret, 0);
1632                 extract(uret[0]->password, cret, 1);
1633                 uret[0]->flags = extract_int(cret, 2);
1634                 uret[0]->timescalled = extract_long(cret, 3);
1635                 uret[0]->posted = extract_long(cret, 4);
1636                 uret[0]->axlevel = extract_int(cret, 5);
1637                 uret[0]->usernum = extract_long(cret, 6);
1638                 uret[0]->lastcall = extract_long(cret, 7);
1639                 uret[0]->USuserpurge = extract_int(cret, 8);
1640         }
1641         return ret;
1642 }
1643
1644
1645 /* ASUP */
1646 int CtdlIPCAideSetUserParameters(CtdlIPC *ipc, const struct usersupp *uret, char *cret)
1647 {
1648         register int ret;
1649         char *aaa;
1650
1651         if (!cret) return -2;
1652         if (!uret) return -2;
1653
1654         aaa = (char *)malloc(strlen(uret->fullname) + strlen(uret->password) + 84);
1655         if (!aaa) return -1;
1656
1657         sprintf(aaa, "ASUP %s|%s|%d|%ld|%ld|%d|%ld|%ld|%d",
1658                         uret->fullname, uret->password, uret->flags,
1659                         uret->timescalled, uret->posted, uret->axlevel,
1660                         uret->usernum, uret->lastcall, uret->USuserpurge);
1661         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1662         free(aaa);
1663         return ret;
1664 }
1665
1666
1667 /* GPEX */
1668 /* which is 0 = room, 1 = floor, 2 = site */
1669 int CtdlIPCGetMessageExpirationPolicy(CtdlIPC *ipc, int which, char *cret)
1670 {
1671         static char *proto[] = {"room", "floor", "site"};
1672         char aaa[11];
1673
1674         if (!cret) return -2;
1675         if (which < 0 || which > 2) return -2;
1676         
1677         sprintf(aaa, "GPEX %s", proto[which]);
1678         return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1679 }
1680
1681
1682 /* SPEX */
1683 /* which is 0 = room, 1 = floor, 2 = site */
1684 /* policy is 0 = inherit, 1 = no purge, 2 = by count, 3 = by age (days) */
1685 int CtdlIPCSetMessageExpirationPolicy(CtdlIPC *ipc, int which, int policy, int value,
1686                 char *cret)
1687 {
1688         char aaa[38];
1689
1690         if (!cret) return -2;
1691         if (which < 0 || which > 2) return -2;
1692         if (policy < 0 || policy > 3) return -2;
1693         if (policy >= 2 && value < 1) return -2;
1694
1695         sprintf(aaa, "SPEX %d|%d|%d", which, policy, value);
1696         return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1697 }
1698
1699
1700 /* CONF GET */
1701 int CtdlGetSystemConfig(CtdlIPC *ipc, char **listing, char *cret)
1702 {
1703         size_t bytes;
1704
1705         if (!cret) return -2;
1706         if (!listing) return -2;
1707         if (*listing) return -2;
1708
1709         return CtdlIPCGenericCommand(ipc, "CONF GET", NULL, 0,
1710                         listing, &bytes, cret);
1711 }
1712
1713
1714 /* CONF SET */
1715 int CtdlSetSystemConfig(CtdlIPC *ipc, const char *listing, char *cret)
1716 {
1717         if (!cret) return -2;
1718         if (!listing) return -2;
1719
1720         return CtdlIPCGenericCommand(ipc, "CONF SET", listing, strlen(listing),
1721                         NULL, NULL, cret);
1722 }
1723
1724
1725 /* CONF GETSYS */
1726 int CtdlGetSystemConfigByType(CtdlIPC *ipc, const char *mimetype,
1727                 char **listing, char *cret)
1728 {
1729         char *aaa;
1730         size_t bytes;
1731
1732         if (!cret) return -2;
1733         if (!mimetype) return -2;
1734         if (!listing) return -2;
1735         if (*listing) return -2;
1736
1737         aaa = malloc(strlen(mimetype) + 13);
1738         if (!aaa) return -1;
1739         sprintf(aaa, "CONF GETSYS|%s", mimetype);
1740         return CtdlIPCGenericCommand(ipc, aaa, NULL, 0,
1741                         listing, &bytes, cret);
1742 }
1743
1744
1745 /* CONF PUTSYS */
1746 int CtdlSetSystemConfigByType(CtdlIPC *ipc, const char *mimetype,
1747                const char *listing, char *cret)
1748 {
1749         char *aaa;
1750
1751         if (!cret) return -2;
1752         if (!mimetype) return -2;
1753         if (!listing) return -2;
1754
1755         aaa = malloc(strlen(mimetype) + 13);
1756         if (!aaa) return -1;
1757         sprintf(aaa, "CONF PUTSYS|%s", mimetype);
1758         return CtdlIPCGenericCommand(ipc, aaa, listing, strlen(listing),
1759                         NULL, NULL, cret);
1760 }
1761
1762 /* MMOD */
1763 int CtdlIPCModerateMessage(CtdlIPC *ipc, long msgnum, int level, char *cret)
1764 {
1765         char aaa[27];
1766
1767         if (!cret) return -2;
1768         if (!msgnum) return -2;
1769
1770         sprintf(aaa, "MMOD %ld|%d", msgnum, level);
1771         return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1772 }
1773
1774
1775 /* REQT */
1776 int CtdlIPCRequestClientLogout(CtdlIPC *ipc, int session, char *cret)
1777 {
1778         char aaa[16];
1779
1780         if (!cret) return -2;
1781         if (session < 0) return -2;
1782
1783         sprintf(aaa, "REQT %d", session);
1784         return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1785 }
1786
1787
1788 /* SEEN */
1789 int CtdlIPCSetMessageSeen(CtdlIPC *ipc, long msgnum, int seen, char *cret)
1790 {
1791         char aaa[27];
1792
1793         if (!cret) return -2;
1794         if (msgnum < 0) return -2;
1795
1796         sprintf(aaa, "SEEN %ld|%d", msgnum, seen ? 1 : 0);
1797         return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1798 }
1799
1800
1801 /* STLS */
1802 int CtdlIPCStartEncryption(CtdlIPC *ipc, char *cret)
1803 {
1804         int a;
1805         int r;
1806         char buf[SIZ];
1807
1808 #ifdef HAVE_OPENSSL
1809         SSL *temp_ssl;
1810
1811         /* New SSL object */
1812         temp_ssl = SSL_new(ssl_ctx);
1813         if (!temp_ssl) {
1814                 error_printf("SSL_new failed: %s\n",
1815                                 ERR_reason_error_string(ERR_get_error()));
1816                 return -2;
1817         }
1818         /* Pointless flag waving */
1819 #if SSLEAY_VERSION_NUMBER >= 0x0922
1820         SSL_set_session_id_context(temp_ssl, "Citadel/UX SID", 14);
1821 #endif
1822
1823         if (!access("/var/run/egd-pool", F_OK))
1824                 RAND_egd("/var/run/egd-pool");
1825
1826         if (!RAND_status()) {
1827                 error_printf("PRNG not properly seeded\n");
1828                 return -2;
1829         }
1830
1831         /* Associate network connection with SSL object */
1832         if (SSL_set_fd(temp_ssl, ipc->sock) < 1) {
1833                 error_printf("SSL_set_fd failed: %s\n",
1834                                 ERR_reason_error_string(ERR_get_error()));
1835                 return -2;
1836         }
1837
1838         if (status_hook != NULL)
1839                 status_hook("Requesting encryption...\r");
1840
1841         /* Ready to start SSL/TLS */
1842         /* Old code
1843         CtdlIPC_putline(ipc, "STLS");
1844         CtdlIPC_getline(ipc, buf);
1845         if (buf[0] != '2') {
1846                 error_printf("Server can't start TLS: %s\n", buf);
1847                 return 0;
1848         }
1849         */
1850         r = CtdlIPCGenericCommand(ipc,
1851                                   "STLS", NULL, 0, NULL, NULL, cret);
1852         if (r / 100 != 2) {
1853                 error_printf("Server can't start TLS: %s\n", buf);
1854                 endtls(temp_ssl);
1855                 return r;
1856         }
1857
1858         /* Do SSL/TLS handshake */
1859         if ((a = SSL_connect(temp_ssl)) < 1) {
1860                 error_printf("SSL_connect failed: %s\n",
1861                                 ERR_reason_error_string(ERR_get_error()));
1862                 endtls(temp_ssl);
1863                 return -2;
1864         }
1865         ipc->ssl = temp_ssl;
1866
1867         BIO_set_close(ipc->ssl->rbio, BIO_NOCLOSE);
1868         {
1869                 int bits, alg_bits;
1870
1871                 bits = SSL_CIPHER_get_bits(SSL_get_current_cipher(ipc->ssl), &alg_bits);
1872                 error_printf("Encrypting with %s cipher %s (%d of %d bits)\n",
1873                                 SSL_CIPHER_get_version(SSL_get_current_cipher(ipc->ssl)),
1874                                 SSL_CIPHER_get_name(SSL_get_current_cipher(ipc->ssl)),
1875                                 bits, alg_bits);
1876         }
1877         return r;
1878 #else
1879         return 0;
1880 #endif /* HAVE_OPENSSL */
1881 }
1882
1883
1884 #ifdef HAVE_OPENSSL
1885 static void endtls(SSL *ssl)
1886 {
1887         if (ssl) {
1888                 SSL_shutdown(ssl);
1889                 SSL_free(ssl);
1890         }
1891 }
1892 #endif
1893
1894
1895 /* QDIR */
1896 int CtdlIPCDirectoryLookup(CtdlIPC *ipc, const char *address, char *cret)
1897 {
1898         char *aaa;
1899
1900         if (!address) return -2;
1901         if (!cret) return -2;
1902
1903         aaa = (char *)malloc(strlen(address) + 6);
1904         if (!aaa) return -1;
1905
1906         sprintf(aaa, "QDIR %s", address);
1907         return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1908 }
1909
1910
1911 /* IPGM */
1912 int CtdlIPCInternalProgram(CtdlIPC *ipc, int secret, char *cret)
1913 {
1914         char aaa[30];
1915
1916         if (!cret) return -2;
1917         sprintf(aaa, "IPGM %d", secret);
1918         return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1919 }
1920
1921
1922 /*
1923  * Not implemented:
1924  * 
1925  * CHAT
1926  * ETLS
1927  * EXPI
1928  * GTLS
1929  * IGAB
1930  * MSG3
1931  * MSG4
1932  * NDOP
1933  * NETP
1934  * NUOP
1935  * SMTP
1936  */
1937
1938
1939 /* ************************************************************************** */
1940 /*             Stuff below this line is not for public consumption            */
1941 /* ************************************************************************** */
1942
1943
1944 inline void CtdlIPC_lock(CtdlIPC *ipc)
1945 {
1946 #ifdef THREADED_CLIENT
1947         pthread_mutex_lock(&(ipc->mutex));
1948 #endif
1949 }
1950
1951
1952 inline void CtdlIPC_unlock(CtdlIPC *ipc)
1953 {
1954 #ifdef THREADED_CLIENT
1955         pthread_mutex_unlock(&(ipc->mutex));
1956 #endif
1957 }
1958
1959
1960 /* Read a listing from the server up to 000.  Append to dest if it exists */
1961 char *CtdlIPCReadListing(CtdlIPC *ipc, char *dest)
1962 {
1963         size_t length = 0;
1964         size_t linelength;
1965         char *ret;
1966         char aaa[SIZ];
1967
1968         ret = dest;
1969         if (ret != NULL) {
1970                 length = strlen(ret);
1971         }
1972         else {
1973                 ret = strdup("");
1974                 length = 0;
1975         }
1976
1977         while (CtdlIPC_getline(ipc, aaa), strcmp(aaa, "000")) {
1978                 linelength = strlen(aaa);
1979                 ret = (char *)realloc(ret, (size_t)(length + linelength + 2));
1980                 if (ret) {
1981                         strcpy(&ret[length], aaa);
1982                         length += linelength;
1983                         strcpy(&ret[length++], "\n");
1984                 }
1985         }
1986
1987         return(ret);
1988 }
1989
1990
1991 /* Send a listing to the server; generate the ending 000. */
1992 int CtdlIPCSendListing(CtdlIPC *ipc, const char *listing)
1993 {
1994         char *text;
1995
1996         text = (char *)malloc(strlen(listing) + 6);
1997         if (text) {
1998                 strcpy(text, listing);
1999                 while (text[strlen(text) - 1] == '\n')
2000                         text[strlen(text) - 1] = '\0';
2001                 strcat(text, "\n000");
2002                 CtdlIPC_putline(ipc, text);
2003                 free(text);
2004                 text = NULL;
2005         } else {
2006                 /* Malloc failed but we are committed to send */
2007                 /* This may result in extra blanks at the bottom */
2008                 CtdlIPC_putline(ipc, text);
2009                 CtdlIPC_putline(ipc, "000");
2010         }
2011         return 0;
2012 }
2013
2014
2015 /* Partial read of file from server */
2016 size_t CtdlIPCPartialRead(CtdlIPC *ipc, void **buf, size_t offset, size_t bytes, char *cret)
2017 {
2018         register size_t len = 0;
2019         char aaa[SIZ];
2020
2021         if (!buf) return -1;
2022         if (!cret) return -1;
2023         if (bytes < 1) return -1;
2024         if (offset < 0) return -1;
2025
2026         CtdlIPC_lock(ipc);
2027         sprintf(aaa, "READ %d|%d", offset, bytes);
2028         CtdlIPC_putline(ipc, aaa);
2029         CtdlIPC_getline(ipc, aaa);
2030         if (aaa[0] != '6')
2031                 strcpy(cret, &aaa[4]);
2032         else {
2033                 len = extract_long(&aaa[4], 0);
2034                 *buf = (void *)realloc(*buf, (size_t)(offset + len));
2035                 if (*buf) {
2036                         /* I know what I'm doing */
2037                         serv_read(ipc, (*buf + offset), len);
2038                 } else {
2039                         /* We have to read regardless */
2040                         serv_read(ipc, aaa, len);
2041                         len = -1;
2042                 }
2043         }
2044         CtdlIPC_unlock(ipc);
2045         return len;
2046 }
2047
2048
2049 /* CLOS */
2050 int CtdlIPCEndDownload(CtdlIPC *ipc, char *cret)
2051 {
2052         register int ret;
2053
2054         if (!cret) return -2;
2055         if (!ipc->downloading) return -2;
2056
2057         ret = CtdlIPCGenericCommand(ipc, "CLOS", NULL, 0, NULL, NULL, cret);
2058         if (ret / 100 == 2)
2059                 ipc->downloading = 0;
2060         return ret;
2061 }
2062
2063
2064 /* MSGP */
2065 int CtdlIPCSpecifyPreferredFormats(CtdlIPC *ipc, char *cret, char *formats) {
2066         register int ret;
2067         char cmd[SIZ];
2068         
2069         snprintf(cmd, sizeof cmd, "MSGP %s", formats);
2070         ret = CtdlIPCGenericCommand(ipc, cmd, NULL, 0, NULL, NULL, cret);
2071         return ret;
2072 }
2073
2074
2075
2076 /* READ */
2077 int CtdlIPCReadDownload(CtdlIPC *ipc, void **buf, size_t bytes,
2078                void (*progress_gauge_callback)(long, long), char *cret)
2079 {
2080         register size_t len;
2081
2082         if (!cret) return -1;
2083         if (!buf) return -1;
2084         if (*buf) return -1;
2085         if (!ipc->downloading) return -1;
2086
2087         len = 0;
2088         if (progress_gauge_callback)
2089                 progress_gauge_callback(len, bytes);
2090         while (len < bytes) {
2091                 register size_t block;
2092
2093                 block = CtdlIPCPartialRead(ipc, buf, len, 4096, cret);
2094                 if (block == -1) {
2095                         free(*buf);
2096                         return 0;
2097                 }
2098                 len += block;
2099                 if (progress_gauge_callback)
2100                         progress_gauge_callback(len, bytes);
2101         }
2102         return len;
2103 }
2104
2105
2106 /* READ - pipelined */
2107 int CtdlIPCHighSpeedReadDownload(CtdlIPC *ipc, void **buf, size_t bytes,
2108                void (*progress_gauge_callback)(long, long), char *cret)
2109 {
2110         register size_t len;
2111         register int calls;     /* How many calls in the pipeline */
2112         register int i;         /* iterator */
2113         char aaa[4096];
2114
2115         if (!cret) return -1;
2116         if (!buf) return -1;
2117         if (*buf) return -1;
2118         if (!ipc->downloading) return -1;
2119
2120         *buf = (void *)realloc(*buf, bytes);
2121         if (!*buf) return -1;
2122
2123         len = 0;
2124         CtdlIPC_lock(ipc);
2125         if (progress_gauge_callback)
2126                 progress_gauge_callback(len, bytes);
2127
2128         /* How many calls will be in the pipeline? */
2129         calls = bytes / 4096;
2130         if (bytes % 4096) calls++;
2131
2132         /* Send all requests at once */
2133         for (i = 0; i < calls; i++) {
2134                 sprintf(aaa, "READ %d|4096", i * 4096);
2135                 CtdlIPC_putline(ipc, aaa);
2136         }
2137
2138         /* Receive all responses at once */
2139         for (i = 0; i < calls; i++) {
2140                 CtdlIPC_getline(ipc, aaa);
2141                 if (aaa[0] != '6')
2142                         strcpy(cret, &aaa[4]);
2143                 else {
2144                         len = extract_long(&aaa[4], 0);
2145                         /* I know what I'm doing */
2146                         serv_read(ipc, ((*buf) + (i * 4096)), len);
2147                 }
2148                 if (progress_gauge_callback)
2149                         progress_gauge_callback(i * 4096 + len, bytes);
2150         }
2151         CtdlIPC_unlock(ipc);
2152         return len;
2153 }
2154
2155
2156 /* UCLS */
2157 int CtdlIPCEndUpload(CtdlIPC *ipc, int discard, char *cret)
2158 {
2159         register int ret;
2160         char cmd[8];
2161
2162         if (!cret) return -1;
2163         if (!ipc->uploading) return -1;
2164
2165         sprintf(cmd, "UCLS %d", discard ? 0 : 1);
2166         ret = CtdlIPCGenericCommand(ipc, cmd, NULL, 0, NULL, NULL, cret);
2167         ipc->uploading = 0;
2168         return ret;
2169 }
2170
2171
2172 /* WRIT */
2173 int CtdlIPCWriteUpload(CtdlIPC *ipc, const char *path,
2174                 void (*progress_gauge_callback)(long, long), char *cret)
2175 {
2176         register int ret = -1;
2177         register size_t offset = 0;
2178         size_t bytes;
2179         char aaa[SIZ];
2180         char buf[4096];
2181         FILE *fd;
2182
2183         if (!cret) return -1;
2184         if (!path) return -1;
2185         if (!*path) return -1;
2186
2187         fd = fopen(path, "r");
2188         if (!fd) return -2;
2189
2190         fseek(fd, 0L, SEEK_END);
2191         bytes = ftell(fd);
2192         rewind(fd);
2193
2194         if (progress_gauge_callback)
2195                 progress_gauge_callback(0, bytes);
2196
2197         while (offset < bytes) {
2198                 register size_t to_write;
2199
2200                 /* Read some data in */
2201                 to_write = fread(buf, 1, 4096, fd);
2202                 if (!to_write) {
2203                         if (feof(fd) || ferror(fd)) break;
2204                 }
2205                 sprintf(aaa, "WRIT %d", to_write);
2206                 CtdlIPC_putline(ipc, aaa);
2207                 CtdlIPC_getline(ipc, aaa);
2208                 strcpy(cret, &aaa[4]);
2209                 ret = atoi(aaa);
2210                 if (aaa[0] == '7') {
2211                         to_write = extract_long(&aaa[4], 0);
2212                         
2213                         serv_write(ipc, buf, to_write);
2214                         offset += to_write;
2215                         if (progress_gauge_callback)
2216                                 progress_gauge_callback(offset, bytes);
2217                         /* Detect short reads and back up if needed */
2218                         fseek(fd, offset, SEEK_SET);
2219                 } else {
2220                         break;
2221                 }
2222         }
2223         if (progress_gauge_callback)
2224                 progress_gauge_callback(1, 1);
2225         return (!ferror(fd) ? ret : -2);
2226 }
2227
2228
2229 /*
2230  * Generic command method.  This method should handle any server command
2231  * except for CHAT.  It takes the following arguments:
2232  *
2233  * ipc                  The server to speak with
2234  * command              Preformatted command to send to server
2235  * to_send              A text or binary file to send to server
2236  *                      (only sent if server requests it)
2237  * bytes_to_send        The number of bytes in to_send (required if
2238  *                      sending binary, optional if sending listing)
2239  * to_receive           Pointer to a NULL pointer, if the server
2240  *                      sends text or binary we will allocate memory
2241  *                      for the file and stuff it here
2242  * bytes_to_receive     If a file is received, we will store its
2243  *                      byte count here
2244  * proto_response       The protocol response.  Caller must provide
2245  *                      this buffer and ensure that it is at least
2246  *                      128 bytes in length.
2247  *
2248  * This function returns a number equal to the protocol response number,
2249  * -1 if an internal error occurred, -2 if caller provided bad values,
2250  * or 0 - the protocol response number if bad values were found during
2251  * the protocol exchange.
2252  * It stores the protocol response string (minus the number) in 
2253  * protocol_response as described above.  Some commands send additional
2254  * data in this string.
2255  */
2256 int CtdlIPCGenericCommand(CtdlIPC *ipc,
2257                 const char *command, const char *to_send,
2258                 size_t bytes_to_send, char **to_receive, 
2259                 size_t *bytes_to_receive, char *proto_response)
2260 {
2261         char buf[SIZ];
2262         register int ret;
2263         int watch_ssl = 0;
2264
2265         if (!command) return -2;
2266         if (!proto_response) return -2;
2267
2268 #ifdef HAVE_OPENSSL
2269         if (ipc->ssl) watch_ssl = 1;
2270 #endif
2271
2272         CtdlIPC_lock(ipc);
2273         CtdlIPC_putline(ipc, command);
2274         while (1) {
2275                 CtdlIPC_getline(ipc, proto_response);
2276                 if (proto_response[3] == '*')
2277                         express_msgs = 1;
2278                 ret = atoi(proto_response);
2279                 strcpy(proto_response, &proto_response[4]);
2280                 switch (ret / 100) {
2281                 default:                        /* Unknown, punt */
2282                 case 2:                         /* OK */
2283                 case 3:                         /* MORE_DATA */
2284                 case 5:                         /* ERROR */
2285                         /* Don't need to do anything */
2286                         break;
2287                 case 1:                         /* LISTING_FOLLOWS */
2288                         if (to_receive && !*to_receive && bytes_to_receive) {
2289                                 *to_receive = CtdlIPCReadListing(ipc, NULL);
2290                         } else { /* Drain */
2291                                 while (CtdlIPC_getline(ipc, buf), strcmp(buf, "000")) ;
2292                                 ret = -ret;
2293                         }
2294                         break;
2295                 case 4:                         /* SEND_LISTING */
2296                         if (to_send) {
2297                                 CtdlIPCSendListing(ipc, to_send);
2298                         } else {
2299                                 /* No listing given, fake it */
2300                                 CtdlIPC_putline(ipc, "000");
2301                                 ret = -ret;
2302                         }
2303                         break;
2304                 case 6:                         /* BINARY_FOLLOWS */
2305                         if (to_receive && !*to_receive && bytes_to_receive) {
2306                                 *bytes_to_receive =
2307                                         extract_long(proto_response, 0);
2308                                 *to_receive = (char *)
2309                                         malloc((size_t)*bytes_to_receive);
2310                                 if (!*to_receive) {
2311                                         ret = -1;
2312                                 } else {
2313                                         serv_read(ipc, *to_receive,
2314                                                         *bytes_to_receive);
2315                                 }
2316                         } else {
2317                                 /* Drain */
2318                                 size_t drain;
2319
2320                                 drain = extract_long(proto_response, 0);
2321                                 while (drain > SIZ) {
2322                                         serv_read(ipc, buf, SIZ);
2323                                         drain -= SIZ;
2324                                 }
2325                                 serv_read(ipc, buf, drain);
2326                                 ret = -ret;
2327                         }
2328                         break;
2329                 case 7:                         /* SEND_BINARY */
2330                         if (to_send && bytes_to_send) {
2331                                 serv_write(ipc, to_send, bytes_to_send);
2332                         } else if (bytes_to_send) {
2333                                 /* Fake it, send nulls */
2334                                 size_t fake;
2335
2336                                 fake = bytes_to_send;
2337                                 memset(buf, '\0', SIZ);
2338                                 while (fake > SIZ) {
2339                                         serv_write(ipc, buf, SIZ);
2340                                         fake -= SIZ;
2341                                 }
2342                                 serv_write(ipc, buf, fake);
2343                                 ret = -ret;
2344                         } /* else who knows?  DANGER WILL ROBINSON */
2345                         break;
2346                 case 8:                         /* START_CHAT_MODE */
2347                         if (!strncasecmp(command, "CHAT", 4)) {
2348                                 /* Don't call chatmode with generic! */
2349                                 CtdlIPC_putline(ipc, "/quit");
2350                                 ret = -ret;
2351                         } else {
2352                                 /* In this mode we send then receive listing */
2353                                 if (to_send) {
2354                                         CtdlIPCSendListing(ipc, to_send);
2355                                 } else {
2356                                         /* No listing given, fake it */
2357                                         CtdlIPC_putline(ipc, "000");
2358                                         ret = -ret;
2359                                 }
2360                                 if (to_receive && !*to_receive
2361                                                 && bytes_to_receive) {
2362                                         *to_receive = CtdlIPCReadListing(ipc, NULL);
2363                                 } else { /* Drain */
2364                                         while (CtdlIPC_getline(ipc, buf),
2365                                                         strcmp(buf, "000")) ;
2366                                         ret = -ret;
2367                                 }
2368                         }
2369                         break;
2370                 case 9:                         /* ASYNC_MSG */
2371                         /* CtdlIPCDoAsync(ret, proto_response); */
2372                         free(CtdlIPCReadListing(ipc, NULL));    /* STUB FIXME */
2373                         break;
2374                 }
2375                 if (ret / 100 != 9)
2376                         break;
2377         }
2378         CtdlIPC_unlock(ipc);
2379         return ret;
2380 }
2381
2382
2383 static int connectsock(char *host, char *service, char *protocol, int defaultPort)
2384 {
2385         struct hostent *phe;
2386         struct servent *pse;
2387         struct protoent *ppe;
2388         struct sockaddr_in sin;
2389         int s, type;
2390
2391         memset(&sin, 0, sizeof(sin));
2392         sin.sin_family = AF_INET;
2393
2394         pse = getservbyname(service, protocol);
2395         if (pse != NULL) {
2396                 sin.sin_port = pse->s_port;
2397         }
2398         else if (atoi(service) > 0) {
2399                 sin.sin_port = htons(atoi(service));
2400         }
2401         else {
2402                 sin.sin_port = htons(defaultPort);
2403         }
2404         phe = gethostbyname(host);
2405         if (phe) {
2406                 memcpy(&sin.sin_addr, phe->h_addr, phe->h_length);
2407         } else if ((sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE) {
2408                 return -1;
2409         }
2410         if ((ppe = getprotobyname(protocol)) == 0) {
2411                 return -1;
2412         }
2413         if (!strcmp(protocol, "udp")) {
2414                 type = SOCK_DGRAM;
2415         } else {
2416                 type = SOCK_STREAM;
2417         }
2418
2419         s = socket(PF_INET, type, ppe->p_proto);
2420         if (s < 0) {
2421                 return -1;
2422         }
2423
2424         if (connect(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
2425                 return -1;
2426         }
2427
2428         return (s);
2429 }
2430
2431 static int uds_connectsock(int *isLocal, char *sockpath)
2432 {
2433         struct sockaddr_un addr;
2434         int s;
2435
2436         memset(&addr, 0, sizeof(addr));
2437         addr.sun_family = AF_UNIX;
2438         safestrncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
2439
2440         s = socket(AF_UNIX, SOCK_STREAM, 0);
2441         if (s < 0) {
2442                 return -1;
2443         }
2444
2445         if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
2446                 return -1;
2447         }
2448
2449         *isLocal = 1;
2450         return s;
2451 }
2452
2453
2454 /*
2455  * input binary data from socket
2456  */
2457 static void serv_read(CtdlIPC *ipc, char *buf, int bytes)
2458 {
2459         int len, rlen;
2460
2461 #if defined(HAVE_OPENSSL)
2462         if (ipc->ssl) {
2463                 serv_read_ssl(ipc, buf, bytes);
2464                 return;
2465         }
2466 #endif
2467         len = 0;
2468         while (len < bytes) {
2469                 rlen = read(ipc->sock, &buf[len], bytes - len);
2470                 if (rlen < 1) {
2471                         connection_died(ipc);
2472                         return;
2473                 }
2474                 len += rlen;
2475         }
2476 }
2477
2478
2479 /*
2480  * send binary to server
2481  */
2482 static void serv_write(CtdlIPC *ipc, const char *buf, int nbytes)
2483 {
2484         int bytes_written = 0;
2485         int retval;
2486
2487 #if defined(HAVE_OPENSSL)
2488         if (ipc->ssl) {
2489                 serv_write_ssl(ipc, buf, nbytes);
2490                 return;
2491         }
2492 #endif
2493         while (bytes_written < nbytes) {
2494                 retval = write(ipc->sock, &buf[bytes_written],
2495                                nbytes - bytes_written);
2496                 if (retval < 1) {
2497                         connection_died(ipc);
2498                         return;
2499                 }
2500                 bytes_written += retval;
2501         }
2502 }
2503
2504
2505 #ifdef HAVE_OPENSSL
2506 /*
2507  * input binary data from encrypted connection
2508  */
2509 static void serv_read_ssl(CtdlIPC* ipc, char *buf, int bytes)
2510 {
2511         int len, rlen;
2512         char junk[1];
2513
2514         len = 0;
2515         while (len < bytes) {
2516                 if (SSL_want_read(ipc->ssl)) {
2517                         if ((SSL_write(ipc->ssl, junk, 0)) < 1) {
2518                                 error_printf("SSL_write in serv_read:\n");
2519                                 ERR_print_errors_fp(stderr);
2520                         }
2521                 }
2522                 rlen = SSL_read(ipc->ssl, &buf[len], bytes - len);
2523                 if (rlen < 1) {
2524                         long errval;
2525
2526                         errval = SSL_get_error(ipc->ssl, rlen);
2527                         if (errval == SSL_ERROR_WANT_READ ||
2528                                         errval == SSL_ERROR_WANT_WRITE) {
2529                                 sleep(1);
2530                                 continue;
2531                         }
2532                         if (errval == SSL_ERROR_ZERO_RETURN ||
2533                                         errval == SSL_ERROR_SSL) {
2534                                 serv_read(ipc, &buf[len], bytes - len);
2535                                 return;
2536                         }
2537                         error_printf("SSL_read in serv_read:\n");
2538                         ERR_print_errors_fp(stderr);
2539                         connection_died();
2540                         return;
2541                 }
2542                 len += rlen;
2543         }
2544 }
2545
2546
2547 /*
2548  * send binary to server encrypted
2549  */
2550 static void serv_write_ssl(CtdlIPC *ipc, const char *buf, int nbytes)
2551 {
2552         int bytes_written = 0;
2553         int retval;
2554         char junk[1];
2555
2556         while (bytes_written < nbytes) {
2557                 if (SSL_want_write(ipc->ssl)) {
2558                         if ((SSL_read(ipc->ssl, junk, 0)) < 1) {
2559                                 error_printf("SSL_read in serv_write:\n");
2560                                 ERR_print_errors_fp(stderr);
2561                         }
2562                 }
2563                 retval = SSL_write(ipc->ssl, &buf[bytes_written],
2564                                 nbytes - bytes_written);
2565                 if (retval < 1) {
2566                         long errval;
2567
2568                         errval = SSL_get_error(ipc->ssl, retval);
2569                         if (errval == SSL_ERROR_WANT_READ ||
2570                                         errval == SSL_ERROR_WANT_WRITE) {
2571                                 sleep(1);
2572                                 continue;
2573                         }
2574                         if (errval == SSL_ERROR_ZERO_RETURN ||
2575                                         errval == SSL_ERROR_SSL) {
2576                                 serv_write(ipc, &buf[bytes_written],
2577                                                 nbytes - bytes_written);
2578                                 return;
2579                         }
2580                         error_printf("SSL_write in serv_write:\n");
2581                         ERR_print_errors_fp(stderr);
2582                         connection_died();
2583                         return;
2584                 }
2585                 bytes_written += retval;
2586         }
2587 }
2588
2589
2590 static void CtdlIPC_init_OpenSSL(void)
2591 {
2592         int a;
2593         SSL_METHOD *ssl_method;
2594         DH *dh;
2595         
2596         /* already done init */
2597         if (ssl_ctx) {
2598                 return;
2599         }
2600
2601         /* Get started */
2602         ssl_ctx = NULL;
2603         dh = NULL;
2604         SSL_load_error_strings();
2605         SSLeay_add_ssl_algorithms();
2606
2607         /* Set up the SSL context in which we will oeprate */
2608         ssl_method = SSLv23_client_method();
2609         ssl_ctx = SSL_CTX_new(ssl_method);
2610         if (!ssl_ctx) {
2611                 error_printf("SSL_CTX_new failed: %s\n",
2612                                 ERR_reason_error_string(ERR_get_error()));
2613                 return;
2614         }
2615         /* Any reasonable cipher we can get */
2616         if (!(SSL_CTX_set_cipher_list(ssl_ctx, CIT_CIPHERS))) {
2617                 error_printf("No ciphers available for encryption\n");
2618                 return;
2619         }
2620         SSL_CTX_set_session_cache_mode(ssl_ctx, SSL_SESS_CACHE_BOTH);
2621         
2622         /* Load DH parameters into the context */
2623         dh = DH_new();
2624         if (!dh) {
2625                 error_printf("Can't allocate a DH object: %s\n",
2626                                 ERR_reason_error_string(ERR_get_error()));
2627                 return;
2628         }
2629         if (!(BN_hex2bn(&(dh->p), DH_P))) {
2630                 error_printf("Can't assign DH_P: %s\n",
2631                                 ERR_reason_error_string(ERR_get_error()));
2632                 DH_free(dh);
2633                 return;
2634         }
2635         if (!(BN_hex2bn(&(dh->g), DH_G))) {
2636                 error_printf("Can't assign DH_G: %s\n",
2637                                 ERR_reason_error_string(ERR_get_error()));
2638                 DH_free(dh);
2639                 return;
2640         }
2641         dh->length = DH_L;
2642         SSL_CTX_set_tmp_dh(ssl_ctx, dh);
2643         DH_free(dh);
2644
2645 #ifdef THREADED_CLIENT
2646         /* OpenSSL requires callbacks for threaded clients */
2647         CRYPTO_set_locking_callback(ssl_lock);
2648         CRYPTO_set_id_callback(id_callback);
2649
2650         /* OpenSSL requires us to do semaphores for threaded clients */
2651         Critters = malloc(CRYPTO_num_locks() * sizeof (pthread_mutex_t *));
2652         if (!Critters) {
2653                 perror("malloc failed");
2654                 exit(1);
2655         } else {
2656                 for (a = 0; a < CRYPTO_num_locks(); a++) {
2657                         Critters[a] = malloc(sizeof (pthread_mutex_t));
2658                         if (!Critters[a]) {
2659                                 perror("malloc failed");
2660                                 exit(1);
2661                         }
2662                         pthread_mutex_init(Critters[a], NULL);
2663                 }
2664         }
2665 #endif /* THREADED_CLIENT */       
2666 }
2667
2668
2669 static void ssl_lock(int mode, int n, const char *file, int line)
2670 {
2671 #ifdef THREADED_CLIENT
2672         if (mode & CRYPTO_LOCK)
2673                 pthread_mutex_lock(Critters[n]);
2674         else
2675                 pthread_mutex_unlock(Critters[n]);
2676 #endif /* THREADED_CLIENT */
2677 }
2678
2679 #ifdef THREADED_CLIENT
2680 static unsigned long id_callback(void) {
2681         return (unsigned long)pthread_self();
2682 }
2683 #endif /* THREADED_CLIENT */
2684 #endif /* HAVE_OPENSSL */
2685
2686
2687 /*
2688  * input string from socket - implemented in terms of serv_read()
2689  */
2690 void CtdlIPC_getline(CtdlIPC* ipc, char *buf)
2691 {
2692         int i;
2693
2694         /* Read one character at a time. */
2695         for (i = 0;; i++) {
2696                 serv_read(ipc, &buf[i], 1);
2697                 if (buf[i] == '\n' || i == (SIZ-1))
2698                         break;
2699         }
2700
2701         /* If we got a long line, discard characters until the newline. */
2702         if (i == (SIZ-1))
2703                 while (buf[i] != '\n')
2704                         serv_read(ipc, &buf[i], 1);
2705
2706         /* Strip the trailing newline.
2707          */
2708         buf[i] = 0;
2709 }
2710
2711
2712 /*
2713  * send line to server - implemented in terms of serv_write()
2714  */
2715 void CtdlIPC_putline(CtdlIPC *ipc, const char *buf)
2716 {
2717         /* error_printf("< %s\n", buf); */
2718         serv_write(ipc, buf, strlen(buf));
2719         serv_write(ipc, "\n", 1);
2720
2721         ipc->last_command_sent = time(NULL);
2722 }
2723
2724
2725 /*
2726  * attach to server
2727  */
2728 CtdlIPC* CtdlIPC_new(int argc, char **argv, char *hostbuf, char *portbuf)
2729 {
2730         int a;
2731         char cithost[SIZ];
2732         char citport[SIZ];
2733         char sockpath[SIZ];
2734
2735         CtdlIPC *ipc = ialloc(CtdlIPC);
2736         if (!ipc) {
2737                 return 0;
2738         }
2739 #if defined(HAVE_OPENSSL)
2740         ipc->ssl = NULL;
2741         CtdlIPC_init_OpenSSL();
2742 #endif
2743 #if defined(HAVE_PTHREAD_H)
2744         pthread_mutex_init(&(ipc->mutex), NULL); /* Default fast mutex */
2745 #endif
2746         ipc->sock = -1;                 /* Not connected */
2747         ipc->isLocal = 0;               /* Not local, of course! */
2748         ipc->downloading = 0;
2749         ipc->uploading = 0;
2750         ipc->last_command_sent = 0L;
2751
2752         strcpy(cithost, DEFAULT_HOST);  /* default host */
2753         strcpy(citport, DEFAULT_PORT);  /* default port */
2754
2755         for (a = 0; a < argc; ++a) {
2756                 if (a == 0) {
2757                         /* do nothing */
2758                 } else if (a == 1) {
2759                         strcpy(cithost, argv[a]);
2760                 } else if (a == 2) {
2761                         strcpy(citport, argv[a]);
2762                 } else {
2763                         error_printf("%s: usage: ",argv[0]);
2764                         error_printf("%s [host] [port] ",argv[0]);
2765                         ifree(ipc);
2766                         errno = EINVAL;
2767                         return 0;
2768                 }
2769         }
2770
2771         if ((!strcmp(cithost, "localhost"))
2772            || (!strcmp(cithost, "127.0.0.1"))) {
2773                 ipc->isLocal = 1;
2774         }
2775
2776         /* If we're using a unix domain socket we can do a bunch of stuff */
2777         if (!strcmp(cithost, UDS)) {
2778                 snprintf(sockpath, sizeof sockpath, "citadel.socket");
2779                 ipc->sock = uds_connectsock(&(ipc->isLocal), sockpath);
2780                 if (ipc->sock == -1) {
2781                         ifree(ipc);
2782                         return 0;
2783                 }
2784                 if (hostbuf != NULL) strcpy(hostbuf, cithost);
2785                 if (portbuf != NULL) strcpy(portbuf, sockpath);
2786                 return ipc;
2787         }
2788
2789         ipc->sock = connectsock(cithost, citport, "tcp", 504);
2790         if (ipc->sock == -1) {
2791                 ifree(ipc);
2792                 return 0;
2793         }
2794         if (hostbuf != NULL) strcpy(hostbuf, cithost);
2795         if (portbuf != NULL) strcpy(portbuf, citport);
2796         return ipc;
2797 }
2798
2799 /*
2800  * return the file descriptor of the server socket so we can select() on it.
2801  *
2802  * FIXME: This is only used in chat mode; eliminate it when chat mode gets
2803  * rewritten...
2804  */
2805 int CtdlIPC_getsockfd(CtdlIPC* ipc)
2806 {
2807         return ipc->sock;
2808 }
2809
2810
2811 /*
2812  * return one character
2813  *
2814  * FIXME: This is only used in chat mode; eliminate it when chat mode gets
2815  * rewritten...
2816  */
2817 char CtdlIPC_get(CtdlIPC* ipc)
2818 {
2819         char buf[2];
2820         char ch;
2821
2822         serv_read(ipc, buf, 1);
2823         ch = (int) buf[0];
2824
2825         return (ch);
2826 }