15 #include <sys/types.h>
20 #ifdef THREADED_CLIENT
24 #include "citadel_ipc.h"
25 #include "citadel_decls.h"
26 #include "client_crypto.h"
29 #ifdef THREADED_CLIENT
30 pthread_mutex_t rwlock;
32 char express_msgs = 0;
36 * Does nothing. The server should always return 200.
38 int CtdlIPCNoop(CtdlIPC *ipc)
42 return CtdlIPCGenericCommand(ipc, "NOOP", NULL, 0, NULL, NULL, aaa);
47 * Does nothing interesting. The server should always return 200
48 * along with your string.
50 int CtdlIPCEcho(CtdlIPC *ipc, const char *arg, char *cret)
58 aaa = (char *)malloc((size_t)(strlen(arg) + 6));
61 sprintf(aaa, "ECHO %s", arg);
62 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
69 * Asks the server to close the connecction.
70 * Should always return 200.
72 int CtdlIPCQuit(CtdlIPC *ipc)
78 CtdlIPC_putline(ipc, "QUIT");
79 CtdlIPC_getline(ipc, aaa);
87 * Asks the server to logout. Should always return 200, even if no user
88 * was logged in. The user will not be logged in after this!
90 int CtdlIPCLogout(CtdlIPC *ipc)
96 CtdlIPC_putline(ipc, "LOUT");
97 CtdlIPC_getline(ipc, aaa);
105 * First stage of authentication - pass the username. Returns 300 if the
106 * username is able to log in, with the username correctly spelled in cret.
107 * Returns various 500 error codes if the user doesn't exist, etc.
109 int CtdlIPCTryLogin(CtdlIPC *ipc, const char *username, char *cret)
114 if (!username) return -2;
115 if (!cret) return -2;
117 aaa = (char *)malloc((size_t)(strlen(username) + 6));
120 sprintf(aaa, "USER %s", username);
121 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
128 * Second stage of authentication - provide password. The server returns
129 * 200 and several arguments in cret relating to the user's account.
131 int CtdlIPCTryPassword(CtdlIPC *ipc, const char *passwd, char *cret)
136 if (!passwd) return -2;
137 if (!cret) return -2;
139 aaa = (char *)malloc((size_t)(strlen(passwd) + 6));
142 sprintf(aaa, "PASS %s", passwd);
143 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
150 * Create a new user. This returns 200 plus the same arguments as TryPassword
151 * if selfservice is nonzero, unless there was a problem creating the account.
152 * If selfservice is zero, creates a new user but does not log out the existing
153 * user - intended for use by system administrators to create accounts on
154 * behalf of other users.
156 int CtdlIPCCreateUser(CtdlIPC *ipc, const char *username, int selfservice, char *cret)
161 if (!username) return -2;
162 if (!cret) return -2;
164 aaa = (char *)malloc((size_t)(strlen(username) + 6));
167 sprintf(aaa, "%s %s", selfservice ? "NEWU" : "CREU", username);
168 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
175 * Changes the user's password. Returns 200 if changed, errors otherwise.
177 int CtdlIPCChangePassword(CtdlIPC *ipc, const char *passwd, char *cret)
182 if (!passwd) return -2;
183 if (!cret) return -2;
185 aaa = (char *)malloc((size_t)(strlen(passwd) + 6));
188 sprintf(aaa, "SETP %s", passwd);
189 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
196 /* Caller must free the march list */
197 /* which is 0 = LRMS, 1 = LKRN, 2 = LKRO, 3 = LKRA, 4 = LZRM */
198 /* floor is -1 for all, or floornum */
199 int CtdlIPCKnownRooms(CtdlIPC *ipc, int which, int floor, struct march **listing, char *cret)
202 struct march *march = NULL;
203 static char *proto[] = {"LRMS", "LKRN", "LKRO", "LKRA", "LZRM" };
208 if (!listing) return -2;
209 if (*listing) return -2; /* Free the listing first */
210 if (!cret) return -2;
211 if (which < 0 || which > 4) return -2;
212 if (floor < -1) return -2; /* Can't validate upper bound, sorry */
214 sprintf(aaa, "%s %d", proto[which], floor);
215 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, &bbb, &bbbsize, cret);
216 if (ret / 100 == 1) {
219 while (bbb && strlen(bbb)) {
222 extract_token(aaa, bbb, 0, '\n');
224 memmove(bbb, bbb + a + 1, strlen(bbb) - a);
225 mptr = (struct march *) malloc(sizeof (struct march));
228 extract(mptr->march_name, aaa, 0);
229 mptr->march_floor = (char) extract_int(aaa, 2);
230 mptr->march_order = (char) extract_int(aaa, 3);
237 while (mptr2->next != NULL)
250 /* Caller must free the struct usersupp; caller may pass an existing one */
251 int CtdlIPCGetConfig(CtdlIPC *ipc, struct usersupp **uret, char *cret)
255 if (!cret) return -2;
256 if (!uret) return -2;
257 if (!*uret) *uret = (struct usersupp *)calloc(1, sizeof (struct usersupp));
258 if (!*uret) return -1;
260 ret = CtdlIPCGenericCommand(ipc, "GETU", NULL, 0, NULL, NULL, cret);
261 if (ret / 100 == 2) {
262 uret[0]->USscreenwidth = extract_int(cret, 0);
263 uret[0]->USscreenheight = extract_int(cret, 1);
264 uret[0]->flags = extract_int(cret, 2);
271 int CtdlIPCSetConfig(CtdlIPC *ipc, struct usersupp *uret, char *cret)
275 if (!uret) return -2;
276 if (!cret) return -2;
278 sprintf(aaa, "SETU %d|%d|%d",
279 uret->USscreenwidth, uret->USscreenheight,
281 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
286 int CtdlIPCGotoRoom(CtdlIPC *ipc, const char *room, const char *passwd,
287 struct ctdlipcroom **rret, char *cret)
292 if (!cret) return -2;
293 if (!rret) return -2;
294 if (!*rret) *rret = (struct ctdlipcroom *)calloc(1, sizeof (struct ctdlipcroom));
295 if (!*rret) return -1;
298 aaa = (char *)malloc(strlen(room) + strlen(passwd) + 7);
303 sprintf(aaa, "GOTO %s|%s", room, passwd);
305 aaa = (char *)malloc(strlen(room) + 6);
310 sprintf(aaa, "GOTO %s", room);
312 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
313 if (ret / 100 == 2) {
314 extract(rret[0]->RRname, cret, 0);
315 rret[0]->RRunread = extract_long(cret, 1);
316 rret[0]->RRtotal = extract_long(cret, 2);
317 rret[0]->RRinfoupdated = extract_int(cret, 3);
318 rret[0]->RRflags = extract_int(cret, 4);
319 rret[0]->RRhighest = extract_long(cret, 5);
320 rret[0]->RRlastread = extract_long(cret, 6);
321 rret[0]->RRismailbox = extract_int(cret, 7);
322 rret[0]->RRaide = extract_int(cret, 8);
323 rret[0]->RRnewmail = extract_long(cret, 9);
324 rret[0]->RRfloor = extract_int(cret, 10);
333 /* which is 0 = all, 1 = old, 2 = new, 3 = last, 4 = first, 5 = gt, 6 = lt */
334 /* whicharg is number of messages, applies to last, first, gt, lt */
335 int CtdlIPCGetMessages(CtdlIPC *ipc, int which, int whicharg, const char *template,
336 long **mret, char *cret)
339 register long count = 0;
340 static char *proto[] =
341 { "ALL", "OLD", "NEW", "LAST", "FIRST", "GT", "LT" };
346 if (!cret) return -2;
347 if (!mret) return -2;
348 if (*mret) return -2;
349 if (which < 0 || which > 6) return -2;
352 sprintf(aaa, "MSGS %s||%d", proto[which],
355 sprintf(aaa, "MSGS %s|%d|%d", proto[which], whicharg,
357 if (template) count = strlen(template);
358 ret = CtdlIPCGenericCommand(ipc, aaa, template, count, &bbb, &bbbsize, cret);
360 while (strlen(bbb)) {
363 extract_token(aaa, bbb, 0, '\n');
365 memmove(aaa, bbb + a + 1, strlen(bbb) - a - 1);
366 *mret = (long *)realloc(mret,
367 (size_t)((count + 1) * sizeof (long)));
369 *mret[count++] = atol(aaa);
377 int CtdlIPCGetSingleMessage(CtdlIPC *ipc, long msgnum, int headers, int as_mime,
378 struct ctdlipcmessage **mret, char *cret)
384 int multipart_hunting = 0;
385 char multipart_prefix[SIZ];
387 if (!cret) return -1;
388 if (!mret) return -1;
389 if (!*mret) *mret = (struct ctdlipcmessage *)calloc(1, sizeof (struct ctdlipcmessage));
390 if (!*mret) return -1;
391 if (!msgnum) return -1;
393 strcpy(mret[0]->content_type, "");
394 sprintf(aaa, "MSG%d %ld|%d", as_mime, msgnum, headers);
395 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, &bbb, &bbbsize, cret);
396 if (ret / 100 == 1) {
398 strcpy(mret[0]->mime_chosen, "1"); /* Default chosen-part is "1" */
399 while (strlen(bbb) > 4 && bbb[4] == '=') {
400 extract_token(aaa, bbb, 0, '\n');
401 remove_token(bbb, 0, '\n');
403 if (!strncasecmp(aaa, "nhdr=yes", 8))
405 else if (!strncasecmp(aaa, "from=", 5))
406 strcpy(mret[0]->author, &aaa[5]);
407 else if (!strncasecmp(aaa, "type=", 5))
408 mret[0]->type = atoi(&aaa[5]);
409 else if (!strncasecmp(aaa, "msgn=", 5))
410 strcpy(mret[0]->msgid, &aaa[5]);
411 else if (!strncasecmp(aaa, "subj=", 5))
412 strcpy(mret[0]->subject, &aaa[5]);
413 else if (!strncasecmp(aaa, "rfca=", 5))
414 strcpy(mret[0]->email, &aaa[5]);
415 else if (!strncasecmp(aaa, "hnod=", 5))
416 strcpy(mret[0]->hnod, &aaa[5]);
417 else if (!strncasecmp(aaa, "room=", 5))
418 strcpy(mret[0]->room, &aaa[5]);
419 else if (!strncasecmp(aaa, "node=", 5))
420 strcpy(mret[0]->node, &aaa[5]);
421 else if (!strncasecmp(aaa, "rcpt=", 5))
422 strcpy(mret[0]->recipient, &aaa[5]);
423 else if (!strncasecmp(aaa, "time=", 5))
424 mret[0]->time = atol(&aaa[5]);
426 /* Multipart/alternative prefix & suffix strings help
427 * us to determine which part we want to download.
429 else if (!strncasecmp(aaa, "pref=", 5)) {
430 extract(multipart_prefix, &aaa[5], 1);
431 if (!strcasecmp(multipart_prefix,
432 "multipart/alternative")) {
436 else if (!strncasecmp(aaa, "suff=", 5)) {
437 extract(multipart_prefix, &aaa[5], 1);
438 if (!strcasecmp(multipart_prefix,
439 "multipart/alternative")) {
444 else if (!strncasecmp(aaa, "part=", 5)) {
445 struct parts *ptr, *chain;
447 ptr = (struct parts *)calloc(1, sizeof (struct parts));
450 /* Fill the buffers for the caller */
451 extract(ptr->name, &aaa[5], 0);
452 extract(ptr->filename, &aaa[5], 1);
453 extract(ptr->number, &aaa[5], 2);
454 extract(ptr->disposition, &aaa[5], 3);
455 extract(ptr->mimetype, &aaa[5], 4);
456 ptr->length = extract_long(&aaa[5], 5);
457 if (!mret[0]->attachments)
458 mret[0]->attachments = ptr;
460 chain = mret[0]->attachments;
466 /* Now handle multipart/alternative */
467 if (multipart_hunting > 0) {
468 if ( (!strcasecmp(ptr->mimetype,
470 || (!strcasecmp(ptr->mimetype,
472 strcpy(mret[0]->mime_chosen,
480 /* Eliminate "text\n" */
481 remove_token(bbb, 0, '\n');
483 /* If doing a MIME thing, pull out the extra headers */
486 if (!strncasecmp(bbb, "Content-type: ", 14)) {
487 extract_token(mret[0]->content_type, bbb, 0, '\n');
488 strcpy(mret[0]->content_type,
489 &mret[0]->content_type[14]);
490 striplt(mret[0]->content_type);
492 remove_token(bbb, 0, '\n');
493 } while ((bbb[0] != 0) && (bbb[0] != '\n'));
499 /* Strip trailing whitespace */
500 bbb = (char *)realloc(bbb, (size_t)(strlen(bbb) + 1));
502 bbb = (char *)realloc(bbb, 1);
512 int CtdlIPCWhoKnowsRoom(CtdlIPC *ipc, char **listing, char *cret)
517 if (!cret) return -2;
518 if (!listing) return -2;
519 if (*listing) return -2;
521 ret = CtdlIPCGenericCommand(ipc, "WHOK", NULL, 0, listing, &bytes, cret);
527 int CtdlIPCServerInfo(CtdlIPC *ipc, struct CtdlServInfo *ServInfo, char *cret)
531 char *listing = NULL;
534 if (!cret) return -2;
535 if (!ServInfo) return -2;
537 ret = CtdlIPCGenericCommand(ipc, "INFO", NULL, 0, &listing, &bytes, cret);
538 if (ret / 100 == 1) {
541 while (*listing && strlen(listing)) {
542 extract_token(buf, listing, 0, '\n');
543 remove_token(listing, 0, '\n');
545 case 0: ServInfo->serv_pid = atoi(buf);
547 case 1: strcpy(ServInfo->serv_nodename,buf);
549 case 2: strcpy(ServInfo->serv_humannode,buf);
551 case 3: strcpy(ServInfo->serv_fqdn,buf);
553 case 4: strcpy(ServInfo->serv_software,buf);
555 case 5: ServInfo->serv_rev_level = atoi(buf);
557 case 6: strcpy(ServInfo->serv_bbs_city,buf);
559 case 7: strcpy(ServInfo->serv_sysadm,buf);
561 case 9: strcpy(ServInfo->serv_moreprompt,buf);
563 case 10: ServInfo->serv_ok_floors = atoi(buf);
565 case 11: ServInfo->serv_paging_level = atoi(buf);
567 case 13: ServInfo->serv_supports_qnop = atoi(buf);
578 int CtdlIPCReadDirectory(CtdlIPC *ipc, char **listing, char *cret)
583 if (!cret) return -2;
584 if (!listing) return -2;
585 if (*listing) return -2;
587 ret = CtdlIPCGenericCommand(ipc, "RDIR", NULL, 0, listing, &bytes, cret);
593 * Set last-read pointer in this room to msgnum, or 0 for HIGHEST.
595 int CtdlIPCSetLastRead(CtdlIPC *ipc, long msgnum, char *cret)
600 if (!cret) return -2;
603 sprintf(aaa, "SLRP %ld", msgnum);
605 sprintf(aaa, "SLRP HIGHEST");
606 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
612 int CtdlIPCInviteUserToRoom(CtdlIPC *ipc, const char *username, char *cret)
617 if (!cret) return -2;
618 if (!username) return -2;
620 aaa = (char *)malloc(strlen(username) + 6);
623 sprintf(aaa, "INVT %s", username);
624 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
631 int CtdlIPCKickoutUserFromRoom(CtdlIPC *ipc, const char *username, char *cret)
636 if (!cret) return -1;
637 if (!username) return -1;
639 aaa = (char *)malloc(strlen(username) + 6);
641 sprintf(aaa, "KICK %s", username);
642 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
649 int CtdlIPCGetRoomAttributes(CtdlIPC *ipc, struct quickroom **qret, char *cret)
653 if (!cret) return -2;
654 if (!qret) return -2;
655 if (!*qret) *qret = (struct quickroom *)calloc(1, sizeof (struct quickroom));
656 if (!*qret) return -1;
658 ret = CtdlIPCGenericCommand(ipc, "GETR", NULL, 0, NULL, NULL, cret);
659 if (ret / 100 == 2) {
660 extract(qret[0]->QRname, cret, 0);
661 extract(qret[0]->QRpasswd, cret, 1);
662 extract(qret[0]->QRdirname, cret, 2);
663 qret[0]->QRflags = extract_int(cret, 3);
664 qret[0]->QRfloor = extract_int(cret, 4);
665 qret[0]->QRorder = extract_int(cret, 5);
672 /* set forget to kick all users out of room */
673 int CtdlIPCSetRoomAttributes(CtdlIPC *ipc, int forget, struct quickroom *qret, char *cret)
678 if (!cret) return -2;
679 if (!qret) return -2;
681 aaa = (char *)malloc(strlen(qret->QRname) + strlen(qret->QRpasswd) +
682 strlen(qret->QRdirname) + 52);
685 sprintf(aaa, "SETR %s|%s|%s|%d|%d|%d|%d",
686 qret->QRname, qret->QRpasswd, qret->QRdirname,
687 qret->QRflags, forget, qret->QRfloor, qret->QRorder);
688 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
695 int CtdlIPCGetRoomAide(CtdlIPC *ipc, char *cret)
697 if (!cret) return -1;
699 return CtdlIPCGenericCommand(ipc, "GETA", NULL, 0, NULL, NULL, cret);
704 int CtdlIPCSetRoomAide(CtdlIPC *ipc, const char *username, char *cret)
709 if (!cret) return -2;
710 if (!username) return -2;
712 aaa = (char *)malloc(strlen(username) + 6);
715 sprintf(aaa, "SETA %s", username);
716 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
723 int CtdlIPCPostMessage(CtdlIPC *ipc, int flag, const struct ctdlipcmessage *mr, char *cret)
728 if (!cret) return -2;
731 aaa = (char *)malloc(strlen(mr->recipient) + strlen(mr->author) + 40);
734 sprintf(aaa, "ENT0 %d|%s|%d|%d|%s", flag, mr->recipient, mr->anonymous,
735 mr->type, mr->author);
736 ret = CtdlIPCGenericCommand(ipc, aaa, mr->text, strlen(mr->text), NULL,
744 int CtdlIPCRoomInfo(CtdlIPC *ipc, char **iret, char *cret)
748 if (!cret) return -2;
749 if (!iret) return -2;
750 if (*iret) return -2;
752 return CtdlIPCGenericCommand(ipc, "RINF", NULL, 0, iret, &bytes, cret);
757 int CtdlIPCDeleteMessage(CtdlIPC *ipc, long msgnum, char *cret)
761 if (!cret) return -2;
762 if (!msgnum) return -2;
764 sprintf(aaa, "DELE %ld", msgnum);
765 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
770 int CtdlIPCMoveMessage(CtdlIPC *ipc, int copy, long msgnum, const char *destroom, char *cret)
775 if (!cret) return -2;
776 if (!destroom) return -2;
777 if (!msgnum) return -2;
779 aaa = (char *)malloc(strlen(destroom) + 28);
782 sprintf(aaa, "MOVE %ld|%s|%d", msgnum, destroom, copy);
783 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
790 int CtdlIPCDeleteRoom(CtdlIPC *ipc, int for_real, char *cret)
794 if (!cret) return -2;
796 sprintf(aaa, "KILL %d", for_real);
797 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
802 int CtdlIPCCreateRoom(CtdlIPC *ipc, int for_real, const char *roomname, int type,
803 const char *password, int floor, char *cret)
808 if (!cret) return -2;
809 if (!roomname) return -2;
812 aaa = (char *)malloc(strlen(roomname) + strlen(password) + 40);
814 sprintf(aaa, "CRE8 %d|%s|%d|%s|%d", for_real, roomname, type,
817 aaa = (char *)malloc(strlen(roomname) + 40);
819 sprintf(aaa, "CRE8 %d|%s|%d||%d", for_real, roomname, type,
822 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
829 int CtdlIPCForgetRoom(CtdlIPC *ipc, char *cret)
831 if (!cret) return -2;
833 return CtdlIPCGenericCommand(ipc, "FORG", NULL, 0, NULL, NULL, cret);
838 int CtdlIPCSystemMessage(CtdlIPC *ipc, const char *message, char **mret, char *cret)
844 if (!cret) return -2;
845 if (!mret) return -2;
846 if (*mret) return -2;
847 if (!message) return -2;
849 aaa = (char *)malloc(strlen(message) + 6);
852 sprintf(aaa, "MESG %s", message);
853 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, mret, &bytes, cret);
860 int CtdlIPCNextUnvalidatedUser(CtdlIPC *ipc, char *cret)
862 if (!cret) return -2;
864 return CtdlIPCGenericCommand(ipc, "GNUR", NULL, 0, NULL, NULL, cret);
869 int CtdlIPCGetUserRegistration(CtdlIPC *ipc, const char *username, char **rret, char *cret)
875 if (!cret) return -2;
876 if (!rret) return -2;
877 if (*rret) return -2;
880 aaa = (char *)malloc(strlen(username) + 6);
882 aaa = (char *)malloc(12);
886 sprintf(aaa, "GREG %s", username);
888 sprintf(aaa, "GREG _SELF_");
889 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, rret, &bytes, cret);
896 int CtdlIPCValidateUser(CtdlIPC *ipc, const char *username, int axlevel, char *cret)
901 if (!cret) return -2;
902 if (!username) return -2;
903 if (axlevel < 0 || axlevel > 7) return -2;
905 aaa = (char *)malloc(strlen(username) + 17);
908 sprintf(aaa, "VALI %s|%d", username, axlevel);
909 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
916 int CtdlIPCSetRoomInfo(CtdlIPC *ipc, int for_real, const char *info, char *cret)
920 if (!cret) return -1;
921 if (!info) return -1;
923 sprintf(aaa, "EINF %d", for_real);
924 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
929 int CtdlIPCUserListing(CtdlIPC *ipc, char **listing, char *cret)
933 if (!cret) return -1;
934 if (!listing) return -1;
935 if (*listing) return -1;
937 return CtdlIPCGenericCommand(ipc, "LIST", NULL, 0, listing, &bytes, cret);
942 int CtdlIPCSetRegistration(CtdlIPC *ipc, const char *info, char *cret)
944 if (!cret) return -1;
945 if (!info) return -1;
947 return CtdlIPCGenericCommand(ipc, "REGI", info, strlen(info),
953 int CtdlIPCMiscCheck(CtdlIPC *ipc, struct ctdlipcmisc *chek, char *cret)
957 if (!cret) return -1;
958 if (!chek) return -1;
960 ret = CtdlIPCGenericCommand(ipc, "CHEK", NULL, 0, NULL, NULL, cret);
961 if (ret / 100 == 2) {
962 chek->newmail = extract_long(cret, 0);
963 chek->needregis = extract_int(cret, 1);
964 chek->needvalid = extract_int(cret, 2);
971 int CtdlIPCDeleteFile(CtdlIPC *ipc, const char *filename, char *cret)
976 if (!cret) return -2;
977 if (!filename) return -2;
979 aaa = (char *)malloc(strlen(filename) + 6);
982 sprintf(aaa, "DELF %s", filename);
983 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
990 int CtdlIPCMoveFile(CtdlIPC *ipc, const char *filename, const char *destroom, char *cret)
995 if (!cret) return -2;
996 if (!filename) return -2;
997 if (!destroom) return -2;
999 aaa = (char *)malloc(strlen(filename) + strlen(destroom) + 7);
1000 if (!aaa) return -1;
1002 sprintf(aaa, "MOVF %s|%s", filename, destroom);
1003 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1010 int CtdlIPCNetSendFile(CtdlIPC *ipc, const char *filename, const char *destnode, char *cret)
1015 if (!cret) return -2;
1016 if (!filename) return -2;
1017 if (!destnode) return -2;
1019 aaa = (char *)malloc(strlen(filename) + strlen(destnode) + 7);
1020 if (!aaa) return -1;
1022 sprintf(aaa, "NETF %s|%s", filename, destnode);
1023 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1030 int CtdlIPCOnlineUsers(CtdlIPC *ipc, char **listing, time_t *stamp, char *cret)
1035 if (!cret) return -1;
1036 if (!listing) return -1;
1037 if (*listing) return -1;
1039 *stamp = CtdlIPCServerTime(ipc, cret);
1041 *stamp = time(NULL);
1042 ret = CtdlIPCGenericCommand(ipc, "RWHO", NULL, 0, listing, &bytes, cret);
1048 int CtdlIPCFileDownload(CtdlIPC *ipc, const char *filename, void **buf,
1049 void (*progress_gauge_callback)(long, long), char *cret)
1057 if (!cret) return -2;
1058 if (!filename) return -2;
1059 if (!buf) return -2;
1060 if (*buf) return -2;
1061 if (ipc->downloading) return -2;
1063 aaa = (char *)malloc(strlen(filename) + 6);
1064 if (!aaa) return -1;
1066 sprintf(aaa, "OPEN %s", filename);
1067 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1069 if (ret / 100 == 2) {
1070 ipc->downloading = 1;
1071 bytes = extract_long(cret, 0);
1072 last_mod = extract_int(cret, 1);
1073 extract(mimetype, cret, 2);
1074 ret = CtdlIPCReadDownload(ipc, buf, bytes, progress_gauge_callback, cret);
1075 /* ret = CtdlIPCHighSpeedReadDownload(ipc, buf, bytes, progress_gauge_callback, cret); */
1076 ret = CtdlIPCEndDownload(ipc, cret);
1078 sprintf(cret, "%d|%ld|%s|%s", bytes, last_mod,
1079 filename, mimetype);
1086 int CtdlIPCAttachmentDownload(CtdlIPC *ipc, long msgnum, const char *part, void **buf,
1087 void (*progress_gauge_callback)(long, long), char *cret)
1096 if (!cret) return -2;
1097 if (!buf) return -2;
1098 if (*buf) return -2;
1099 if (!part) return -2;
1100 if (!msgnum) return -2;
1101 if (ipc->downloading) return -2;
1103 aaa = (char *)malloc(strlen(part) + 17);
1104 if (!aaa) return -1;
1106 sprintf(aaa, "OPNA %ld|%s", msgnum, part);
1107 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1109 if (ret / 100 == 2) {
1110 ipc->downloading = 1;
1111 bytes = extract_long(cret, 0);
1112 last_mod = extract_int(cret, 1);
1113 extract(mimetype, cret, 2);
1114 ret = CtdlIPCHighSpeedReadDownload(ipc, buf, bytes, progress_gauge_callback, cret);
1115 ret = CtdlIPCEndDownload(ipc, cret);
1117 sprintf(cret, "%d|%ld|%s|%s", bytes, last_mod,
1118 filename, mimetype);
1125 int CtdlIPCImageDownload(CtdlIPC *ipc, const char *filename, void **buf,
1126 void (*progress_gauge_callback)(long, long), char *cret)
1134 if (!cret) return -1;
1135 if (!buf) return -1;
1136 if (*buf) return -1;
1137 if (!filename) return -1;
1138 if (ipc->downloading) return -1;
1140 aaa = (char *)malloc(strlen(filename) + 6);
1141 if (!aaa) return -1;
1143 sprintf(aaa, "OIMG %s", filename);
1144 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1146 if (ret / 100 == 2) {
1147 ipc->downloading = 1;
1148 bytes = extract_long(cret, 0);
1149 last_mod = extract_int(cret, 1);
1150 extract(mimetype, cret, 2);
1151 ret = CtdlIPCReadDownload(ipc, buf, bytes, progress_gauge_callback, cret);
1152 ret = CtdlIPCEndDownload(ipc, cret);
1154 sprintf(cret, "%d|%ld|%s|%s", bytes, last_mod,
1155 filename, mimetype);
1162 int CtdlIPCFileUpload(CtdlIPC *ipc, const char *save_as, const char *comment,
1163 const char *path, void (*progress_gauge_callback)(long, long),
1169 if (!cret) return -1;
1170 if (!save_as) return -1;
1171 if (!comment) return -1;
1172 if (!path) return -1;
1173 if (!*path) return -1;
1174 if (ipc->uploading) return -1;
1176 aaa = (char *)malloc(strlen(save_as) + strlen(comment) + 7);
1177 if (!aaa) return -1;
1179 sprintf(aaa, "UOPN %s|%s", save_as, comment);
1180 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1182 if (ret / 100 == 2) {
1184 ret = CtdlIPCWriteUpload(ipc, path, progress_gauge_callback, cret);
1185 ret = CtdlIPCEndUpload(ipc, (ret == -2 ? 1 : 0), cret);
1193 int CtdlIPCImageUpload(CtdlIPC *ipc, int for_real, const char *path,
1194 const char *save_as,
1195 void (*progress_gauge_callback)(long, long), char *cret)
1200 if (!cret) return -1;
1201 if (!save_as) return -1;
1202 if (!path && for_real) return -1;
1203 if (!*path && for_real) return -1;
1204 if (ipc->uploading) return -1;
1206 aaa = (char *)malloc(strlen(save_as) + 17);
1207 if (!aaa) return -1;
1209 sprintf(aaa, "UIMG %d|%s", for_real, save_as);
1210 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1212 if (ret / 100 == 2 && for_real) {
1214 ret = CtdlIPCWriteUpload(ipc, path, progress_gauge_callback, cret);
1215 ret = CtdlIPCEndUpload(ipc, (ret == -2 ? 1 : 0), cret);
1223 int CtdlIPCQueryUsername(CtdlIPC *ipc, const char *username, char *cret)
1228 if (!cret) return -2;
1229 if (!username) return -2;
1231 aaa = (char *)malloc(strlen(username) + 6);
1232 if (!aaa) return -1;
1234 sprintf(aaa, "QUSR %s", username);
1235 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1242 int CtdlIPCFloorListing(CtdlIPC *ipc, char **listing, char *cret)
1246 if (!cret) return -2;
1247 if (!listing) return -2;
1248 if (*listing) return -2;
1250 return CtdlIPCGenericCommand(ipc, "LFLR", NULL, 0, listing, &bytes, cret);
1255 int CtdlIPCCreateFloor(CtdlIPC *ipc, int for_real, const char *name, char *cret)
1260 if (!cret) return -2;
1261 if (!name) return -2;
1263 aaa = (char *)malloc(strlen(name) + 17);
1264 if (!aaa) return -1;
1266 sprintf(aaa, "CFLR %s|%d", name, for_real);
1267 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1274 int CtdlIPCDeleteFloor(CtdlIPC *ipc, int for_real, int floornum, char *cret)
1278 if (!cret) return -1;
1279 if (floornum < 0) return -1;
1281 sprintf(aaa, "KFLR %d|%d", floornum, for_real);
1282 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1287 int CtdlIPCEditFloor(CtdlIPC *ipc, int floornum, const char *floorname, char *cret)
1292 if (!cret) return -2;
1293 if (!floorname) return -2;
1294 if (floornum < 0) return -2;
1296 aaa = (char *)malloc(strlen(floorname) + 17);
1297 if (!aaa) return -1;
1299 sprintf(aaa, "EFLR %d|%s", floornum, floorname);
1300 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1307 int CtdlIPCIdentifySoftware(CtdlIPC *ipc, int developerid, int clientid, int revision,
1308 const char *software_name, const char *hostname, char *cret)
1313 if (developerid < 0) return -2;
1314 if (clientid < 0) return -2;
1315 if (revision < 0) return -2;
1316 if (!software_name) return -2;
1317 if (!hostname) return -2;
1319 aaa = (char *)malloc(strlen(software_name) + strlen(hostname) + 29);
1320 if (!aaa) return -1;
1322 sprintf(aaa, "IDEN %d|%d|%d|%s|%s", developerid, clientid,
1323 revision, software_name, hostname);
1324 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1331 int CtdlIPCSendInstantMessage(CtdlIPC *ipc, const char *username, const char *text,
1337 if (!cret) return -2;
1338 if (!username) return -2;
1340 aaa = (char *)malloc(strlen(username) + 8);
1341 if (!aaa) return -1;
1344 sprintf(aaa, "SEXP %s|-", username);
1345 ret = CtdlIPCGenericCommand(ipc, aaa, text, strlen(text),
1348 sprintf(aaa, "SEXP %s||", username);
1349 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1357 int CtdlIPCGetInstantMessage(CtdlIPC *ipc, char **listing, char *cret)
1361 if (!cret) return -2;
1362 if (!listing) return -2;
1363 if (*listing) return -2;
1365 return CtdlIPCGenericCommand(ipc, "GEXP", NULL, 0, listing, &bytes, cret);
1370 /* mode is 0 = enable, 1 = disable, 2 = status */
1371 int CtdlIPCEnableInstantMessageReceipt(CtdlIPC *ipc, int mode, char *cret)
1375 if (!cret) return -2;
1377 sprintf(aaa, "DEXP %d", mode);
1378 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1383 int CtdlIPCSetBio(CtdlIPC *ipc, char *bio, char *cret)
1385 if (!cret) return -2;
1386 if (!bio) return -2;
1388 return CtdlIPCGenericCommand(ipc, "EBIO", bio, strlen(bio),
1394 int CtdlIPCGetBio(CtdlIPC *ipc, const char *username, char **listing, char *cret)
1400 if (!cret) return -2;
1401 if (!username) return -2;
1402 if (!listing) return -2;
1403 if (*listing) return -2;
1405 aaa = (char *)malloc(strlen(username) + 6);
1406 if (!aaa) return -1;
1408 sprintf(aaa, "RBIO %s", username);
1409 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, listing, &bytes, cret);
1416 int CtdlIPCListUsersWithBios(CtdlIPC *ipc, char **listing, char *cret)
1420 if (!cret) return -2;
1421 if (!listing) return -2;
1422 if (*listing) return -2;
1424 return CtdlIPCGenericCommand(ipc, "LBIO", NULL, 0, listing, &bytes, cret);
1429 int CtdlIPCStealthMode(CtdlIPC *ipc, int mode, char *cret)
1433 if (!cret) return -1;
1435 sprintf(aaa, "STEL %d", mode ? 1 : 0);
1436 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1441 int CtdlIPCTerminateSession(CtdlIPC *ipc, int sid, char *cret)
1445 if (!cret) return -1;
1447 sprintf(aaa, "TERM %d", sid);
1448 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1453 int CtdlIPCTerminateServerNow(CtdlIPC *ipc, char *cret)
1455 if (!cret) return -1;
1457 return CtdlIPCGenericCommand(ipc, "DOWN", NULL, 0, NULL, NULL, cret);
1462 int CtdlIPCTerminateServerScheduled(CtdlIPC *ipc, int mode, char *cret)
1466 if (!cret) return -1;
1468 sprintf(aaa, "SCDN %d", mode ? 1 : 0);
1469 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1474 int CtdlIPCEnterSystemMessage(CtdlIPC *ipc, const char *filename, const char *text,
1480 if (!cret) return -2;
1481 if (!text) return -2;
1482 if (!filename) return -2;
1484 aaa = (char *)malloc(strlen(filename) + 6);
1485 if (!aaa) return -1;
1487 sprintf(aaa, "EMSG %s", filename);
1488 ret = CtdlIPCGenericCommand(ipc, aaa, text, strlen(text), NULL, NULL, cret);
1495 int CtdlIPCChangeHostname(CtdlIPC *ipc, const char *hostname, char *cret)
1500 if (!cret) return -2;
1501 if (!hostname) return -2;
1503 aaa = (char *)malloc(strlen(hostname) + 6);
1504 if (!aaa) return -1;
1506 sprintf(aaa, "HCHG %s", hostname);
1507 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1514 int CtdlIPCChangeRoomname(CtdlIPC *ipc, const char *roomname, char *cret)
1519 if (!cret) return -2;
1520 if (!roomname) return -2;
1522 aaa = (char *)malloc(strlen(roomname) + 6);
1523 if (!aaa) return -1;
1525 sprintf(aaa, "RCHG %s", roomname);
1526 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1533 int CtdlIPCChangeUsername(CtdlIPC *ipc, const char *username, char *cret)
1538 if (!cret) return -2;
1539 if (!username) return -2;
1541 aaa = (char *)malloc(strlen(username) + 6);
1542 if (!aaa) return -1;
1544 sprintf(aaa, "UCHG %s", username);
1545 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1552 /* This function returns the actual server time reported, or 0 if error */
1553 time_t CtdlIPCServerTime(CtdlIPC *ipc, char *cret)
1555 register time_t tret;
1558 ret = CtdlIPCGenericCommand(ipc, "TIME", NULL, 0, NULL, NULL, cret);
1559 if (ret / 100 == 2) {
1560 tret = extract_long(cret, 0);
1569 int CtdlIPCAideGetUserParameters(CtdlIPC *ipc, const char *who,
1570 struct usersupp **uret, char *cret)
1575 if (!cret) return -2;
1576 if (!uret) return -2;
1577 if (!*uret) *uret = (struct usersupp *)calloc(1, sizeof(struct usersupp));
1578 if (!*uret) return -1;
1580 sprintf(aaa, "AGUP %s", who);
1581 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1583 if (ret / 100 == 2) {
1584 extract(uret[0]->fullname, cret, 0);
1585 extract(uret[0]->password, cret, 1);
1586 uret[0]->flags = extract_int(cret, 2);
1587 uret[0]->timescalled = extract_long(cret, 3);
1588 uret[0]->posted = extract_long(cret, 4);
1589 uret[0]->axlevel = extract_int(cret, 5);
1590 uret[0]->usernum = extract_long(cret, 6);
1591 uret[0]->lastcall = extract_long(cret, 7);
1592 uret[0]->USuserpurge = extract_int(cret, 8);
1599 int CtdlIPCAideSetUserParameters(CtdlIPC *ipc, const struct usersupp *uret, char *cret)
1604 if (!cret) return -2;
1605 if (!uret) return -2;
1607 aaa = (char *)malloc(strlen(uret->fullname) + strlen(uret->password) + 84);
1608 if (!aaa) return -1;
1610 sprintf(aaa, "ASUP %s|%s|%d|%ld|%ld|%d|%ld|%ld|%d",
1611 uret->fullname, uret->password, uret->flags,
1612 uret->timescalled, uret->posted, uret->axlevel,
1613 uret->usernum, uret->lastcall, uret->USuserpurge);
1614 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1621 /* which is 0 = room, 1 = floor, 2 = site */
1622 int CtdlIPCGetMessageExpirationPolicy(CtdlIPC *ipc, int which, char *cret)
1624 static char *proto[] = {"room", "floor", "site"};
1627 if (!cret) return -2;
1628 if (which < 0 || which > 2) return -2;
1630 sprintf(aaa, "GPEX %s", proto[which]);
1631 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1636 /* which is 0 = room, 1 = floor, 2 = site */
1637 /* policy is 0 = inherit, 1 = no purge, 2 = by count, 3 = by age (days) */
1638 int CtdlIPCSetMessageExpirationPolicy(CtdlIPC *ipc, int which, int policy, int value,
1643 if (!cret) return -2;
1644 if (which < 0 || which > 2) return -2;
1645 if (policy < 0 || policy > 3) return -2;
1646 if (policy >= 2 && value < 1) return -2;
1648 sprintf(aaa, "SPEX %d|%d|%d", which, policy, value);
1649 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1654 int CtdlGetSystemConfig(CtdlIPC *ipc, char **listing, char *cret)
1658 if (!cret) return -2;
1659 if (!listing) return -2;
1660 if (*listing) return -2;
1662 return CtdlIPCGenericCommand(ipc, "CONF GET", NULL, 0,
1663 listing, &bytes, cret);
1668 int CtdlSetSystemConfig(CtdlIPC *ipc, const char *listing, char *cret)
1670 if (!cret) return -2;
1671 if (!listing) return -2;
1673 return CtdlIPCGenericCommand(ipc, "CONF SET", listing, strlen(listing),
1679 int CtdlGetSystemConfigByType(CtdlIPC *ipc, const char *mimetype,
1680 char **listing, char *cret)
1685 if (!cret) return -2;
1686 if (!mimetype) return -2;
1687 if (!listing) return -2;
1688 if (*listing) return -2;
1690 aaa = malloc(strlen(mimetype) + 13);
1691 if (!aaa) return -1;
1692 sprintf(aaa, "CONF GETSYS|%s", mimetype);
1693 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0,
1694 listing, &bytes, cret);
1699 int CtdlSetSystemConfigByType(CtdlIPC *ipc, const char *mimetype,
1700 const char *listing, char *cret)
1704 if (!cret) return -2;
1705 if (!mimetype) return -2;
1706 if (!listing) return -2;
1708 aaa = malloc(strlen(mimetype) + 13);
1709 if (!aaa) return -1;
1710 sprintf(aaa, "CONF PUTSYS|%s", mimetype);
1711 return CtdlIPCGenericCommand(ipc, aaa, listing, strlen(listing),
1716 int CtdlIPCModerateMessage(CtdlIPC *ipc, long msgnum, int level, char *cret)
1720 if (!cret) return -2;
1721 if (!msgnum) return -2;
1723 sprintf(aaa, "MMOD %ld|%d", msgnum, level);
1724 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1729 int CtdlIPCRequestClientLogout(CtdlIPC *ipc, int session, char *cret)
1733 if (!cret) return -2;
1734 if (session < 0) return -2;
1736 sprintf(aaa, "REQT %d", session);
1737 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1742 int CtdlIPCSetMessageSeen(CtdlIPC *ipc, long msgnum, int seen, char *cret)
1746 if (!cret) return -2;
1747 if (msgnum < 0) return -2;
1749 sprintf(aaa, "SEEN %ld|%d", msgnum, seen ? 1 : 0);
1750 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1755 int CtdlIPCStartEncryption(CtdlIPC *ipc, char *cret)
1757 return CtdlIPCGenericCommand(ipc, "STLS", NULL, 0, NULL, NULL, cret);
1762 int CtdlIPCDirectoryLookup(CtdlIPC *ipc, const char *address, char *cret)
1766 if (!address) return -2;
1767 if (!cret) return -2;
1769 aaa = (char *)malloc(strlen(address) + 6);
1770 if (!aaa) return -1;
1772 sprintf(aaa, "QDIR %s", address);
1773 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1778 int CtdlIPCInternalProgram(CtdlIPC *ipc, int secret, char *cret)
1782 if (!cret) return -2;
1783 sprintf(aaa, "IPGM %d", secret);
1784 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1805 /* ************************************************************************** */
1806 /* Stuff below this line is not for public consumption */
1807 /* ************************************************************************** */
1810 inline void CtdlIPC_lock(CtdlIPC *ipc)
1812 #ifdef THREADED_CLIENT
1813 pthread_mutex_lock(&(ipc->mutex));
1818 inline void CtdlIPC_unlock(CtdlIPC *ipc)
1820 #ifdef THREADED_CLIENT
1821 pthread_mutex_unlock(&(ipc->mutex));
1826 /* Read a listing from the server up to 000. Append to dest if it exists */
1827 char *CtdlIPCReadListing(CtdlIPC *ipc, char *dest)
1836 length = strlen(ret);
1843 while (CtdlIPC_getline(ipc, aaa), strcmp(aaa, "000")) {
1844 linelength = strlen(aaa);
1845 ret = (char *)realloc(ret, (size_t)(length + linelength + 2));
1847 strcpy(&ret[length], aaa);
1848 length += linelength;
1849 strcpy(&ret[length++], "\n");
1857 /* Send a listing to the server; generate the ending 000. */
1858 int CtdlIPCSendListing(CtdlIPC *ipc, const char *listing)
1862 text = (char *)malloc(strlen(listing) + 6);
1864 strcpy(text, listing);
1865 while (text[strlen(text) - 1] == '\n')
1866 text[strlen(text) - 1] = '\0';
1867 strcat(text, "\n000");
1868 CtdlIPC_putline(ipc, text);
1872 /* Malloc failed but we are committed to send */
1873 /* This may result in extra blanks at the bottom */
1874 CtdlIPC_putline(ipc, text);
1875 CtdlIPC_putline(ipc, "000");
1881 /* Partial read of file from server */
1882 size_t CtdlIPCPartialRead(CtdlIPC *ipc, void **buf, size_t offset, size_t bytes, char *cret)
1884 register size_t len = 0;
1887 if (!buf) return -1;
1888 if (!cret) return -1;
1889 if (bytes < 1) return -1;
1890 if (offset < 0) return -1;
1893 sprintf(aaa, "READ %d|%d", offset, bytes);
1894 CtdlIPC_putline(ipc, aaa);
1895 CtdlIPC_getline(ipc, aaa);
1897 strcpy(cret, &aaa[4]);
1899 len = extract_long(&aaa[4], 0);
1900 *buf = (void *)realloc(*buf, (size_t)(offset + len));
1902 /* I know what I'm doing */
1903 serv_read(ipc, (*buf + offset), len);
1905 /* We have to read regardless */
1906 serv_read(ipc, aaa, len);
1910 CtdlIPC_unlock(ipc);
1916 int CtdlIPCEndDownload(CtdlIPC *ipc, char *cret)
1920 if (!cret) return -2;
1921 if (!ipc->downloading) return -2;
1923 ret = CtdlIPCGenericCommand(ipc, "CLOS", NULL, 0, NULL, NULL, cret);
1925 ipc->downloading = 0;
1931 int CtdlIPCSpecifyPreferredFormats(CtdlIPC *ipc, char *cret, char *formats) {
1935 snprintf(cmd, sizeof cmd, "MSGP %s", formats);
1936 ret = CtdlIPCGenericCommand(ipc, cmd, NULL, 0, NULL, NULL, cret);
1943 int CtdlIPCReadDownload(CtdlIPC *ipc, void **buf, size_t bytes,
1944 void (*progress_gauge_callback)(long, long), char *cret)
1946 register size_t len;
1948 if (!cret) return -1;
1949 if (!buf) return -1;
1950 if (*buf) return -1;
1951 if (!ipc->downloading) return -1;
1954 if (progress_gauge_callback)
1955 progress_gauge_callback(len, bytes);
1956 while (len < bytes) {
1957 register size_t block;
1959 block = CtdlIPCPartialRead(ipc, buf, len, 4096, cret);
1965 if (progress_gauge_callback)
1966 progress_gauge_callback(len, bytes);
1972 /* READ - pipelined */
1973 int CtdlIPCHighSpeedReadDownload(CtdlIPC *ipc, void **buf, size_t bytes,
1974 void (*progress_gauge_callback)(long, long), char *cret)
1976 register size_t len;
1977 register int calls; /* How many calls in the pipeline */
1978 register int i; /* iterator */
1981 if (!cret) return -1;
1982 if (!buf) return -1;
1983 if (*buf) return -1;
1984 if (!ipc->downloading) return -1;
1986 *buf = (void *)realloc(*buf, bytes);
1987 if (!*buf) return -1;
1991 if (progress_gauge_callback)
1992 progress_gauge_callback(len, bytes);
1994 /* How many calls will be in the pipeline? */
1995 calls = bytes / 4096;
1996 if (bytes % 4096) calls++;
1998 /* Send all requests at once */
1999 for (i = 0; i < calls; i++) {
2000 sprintf(aaa, "READ %d|4096", i * 4096);
2001 CtdlIPC_putline(ipc, aaa);
2004 /* Receive all responses at once */
2005 for (i = 0; i < calls; i++) {
2006 CtdlIPC_getline(ipc, aaa);
2008 strcpy(cret, &aaa[4]);
2010 len = extract_long(&aaa[4], 0);
2011 /* I know what I'm doing */
2012 serv_read(ipc, ((*buf) + (i * 4096)), len);
2014 if (progress_gauge_callback)
2015 progress_gauge_callback(i * 4096 + len, bytes);
2017 CtdlIPC_unlock(ipc);
2023 int CtdlIPCEndUpload(CtdlIPC *ipc, int discard, char *cret)
2028 if (!cret) return -1;
2029 if (!ipc->uploading) return -1;
2031 sprintf(cmd, "UCLS %d", discard ? 0 : 1);
2032 ret = CtdlIPCGenericCommand(ipc, cmd, NULL, 0, NULL, NULL, cret);
2039 int CtdlIPCWriteUpload(CtdlIPC *ipc, const char *path,
2040 void (*progress_gauge_callback)(long, long), char *cret)
2042 register int ret = -1;
2043 register size_t offset = 0;
2049 if (!cret) return -1;
2050 if (!path) return -1;
2051 if (!*path) return -1;
2053 fd = fopen(path, "r");
2056 fseek(fd, 0L, SEEK_END);
2060 if (progress_gauge_callback)
2061 progress_gauge_callback(0, bytes);
2063 while (offset < bytes) {
2064 register size_t to_write;
2066 /* Read some data in */
2067 to_write = fread(buf, 1, 4096, fd);
2069 if (feof(fd) || ferror(fd)) break;
2071 sprintf(aaa, "WRIT %d", to_write);
2072 CtdlIPC_putline(ipc, aaa);
2073 CtdlIPC_getline(ipc, aaa);
2074 strcpy(cret, &aaa[4]);
2076 if (aaa[0] == '7') {
2077 to_write = extract_long(&aaa[4], 0);
2079 serv_write(ipc, buf, to_write);
2081 if (progress_gauge_callback)
2082 progress_gauge_callback(offset, bytes);
2083 /* Detect short reads and back up if needed */
2084 fseek(fd, offset, SEEK_SET);
2089 if (progress_gauge_callback)
2090 progress_gauge_callback(1, 1);
2091 return (!ferror(fd) ? ret : -2);
2096 * Generic command method. This method should handle any server command
2097 * except for CHAT. It takes the following arguments:
2099 * ipc The server to speak with
2100 * command Preformatted command to send to server
2101 * to_send A text or binary file to send to server
2102 * (only sent if server requests it)
2103 * bytes_to_send The number of bytes in to_send (required if
2104 * sending binary, optional if sending listing)
2105 * to_receive Pointer to a NULL pointer, if the server
2106 * sends text or binary we will allocate memory
2107 * for the file and stuff it here
2108 * bytes_to_receive If a file is received, we will store its
2110 * proto_response The protocol response. Caller must provide
2111 * this buffer and ensure that it is at least
2112 * 128 bytes in length.
2114 * This function returns a number equal to the protocol response number,
2115 * -1 if an internal error occurred, -2 if caller provided bad values,
2116 * or 0 - the protocol response number if bad values were found during
2117 * the protocol exchange.
2118 * It stores the protocol response string (minus the number) in
2119 * protocol_response as described above. Some commands send additional
2120 * data in this string.
2122 int CtdlIPCGenericCommand(CtdlIPC *ipc,
2123 const char *command, const char *to_send,
2124 size_t bytes_to_send, char **to_receive,
2125 size_t *bytes_to_receive, char *proto_response)
2131 if (!command) return -2;
2132 if (!proto_response) return -2;
2134 if (ipc->ssl) watch_ssl = 1;
2137 CtdlIPC_putline(ipc, command);
2139 CtdlIPC_getline(ipc, proto_response);
2140 if (proto_response[3] == '*')
2142 ret = atoi(proto_response);
2143 strcpy(proto_response, &proto_response[4]);
2144 switch (ret / 100) {
2145 default: /* Unknown, punt */
2147 case 3: /* MORE_DATA */
2149 /* Don't need to do anything */
2151 case 1: /* LISTING_FOLLOWS */
2152 if (to_receive && !*to_receive && bytes_to_receive) {
2153 *to_receive = CtdlIPCReadListing(ipc, NULL);
2154 } else { /* Drain */
2155 while (CtdlIPC_getline(ipc, buf), strcmp(buf, "000")) ;
2159 case 4: /* SEND_LISTING */
2161 CtdlIPCSendListing(ipc, to_send);
2163 /* No listing given, fake it */
2164 CtdlIPC_putline(ipc, "000");
2168 case 6: /* BINARY_FOLLOWS */
2169 if (to_receive && !*to_receive && bytes_to_receive) {
2171 extract_long(proto_response, 0);
2172 *to_receive = (char *)
2173 malloc((size_t)*bytes_to_receive);
2177 serv_read(ipc, *to_receive,
2184 drain = extract_long(proto_response, 0);
2185 while (drain > SIZ) {
2186 serv_read(ipc, buf, SIZ);
2189 serv_read(ipc, buf, drain);
2193 case 7: /* SEND_BINARY */
2194 if (to_send && bytes_to_send) {
2195 serv_write(ipc, to_send, bytes_to_send);
2196 } else if (bytes_to_send) {
2197 /* Fake it, send nulls */
2200 fake = bytes_to_send;
2201 memset(buf, '\0', SIZ);
2202 while (fake > SIZ) {
2203 serv_write(ipc, buf, SIZ);
2206 serv_write(ipc, buf, fake);
2208 } /* else who knows? DANGER WILL ROBINSON */
2210 case 8: /* START_CHAT_MODE */
2211 if (!strncasecmp(command, "CHAT", 4)) {
2212 /* Don't call chatmode with generic! */
2213 CtdlIPC_putline(ipc, "/quit");
2216 /* In this mode we send then receive listing */
2218 CtdlIPCSendListing(ipc, to_send);
2220 /* No listing given, fake it */
2221 CtdlIPC_putline(ipc, "000");
2224 if (to_receive && !*to_receive
2225 && bytes_to_receive) {
2226 *to_receive = CtdlIPCReadListing(ipc, NULL);
2227 } else { /* Drain */
2228 while (CtdlIPC_getline(ipc, buf),
2229 strcmp(buf, "000")) ;
2234 case 9: /* ASYNC_MSG */
2235 /* CtdlIPCDoAsync(ret, proto_response); */
2236 free(CtdlIPCReadListing(ipc, NULL)); /* STUB FIXME */
2242 CtdlIPC_unlock(ipc);