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