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