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