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