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