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 *filename, const char *comment, void *buf,
1163 size_t bytes, char *cret)
1168 if (!cret) return -1;
1169 if (!filename) return -1;
1170 if (!comment) return -1;
1171 if (ipc->uploading) return -1;
1173 aaa = (char *)malloc(strlen(filename) + strlen(comment) + 7);
1174 if (!aaa) return -1;
1176 sprintf(aaa, "UOPN %s|%s", filename, comment);
1177 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1181 ret = CtdlIPCWriteUpload(ipc, buf, bytes, cret);
1182 ret = CtdlIPCEndUpload(ipc, cret);
1188 int CtdlIPCImageUpload(CtdlIPC *ipc, int for_real, const char *filename, size_t bytes,
1194 if (!cret) return -1;
1195 if (!filename) return -1;
1196 if (ipc->uploading) return -1;
1198 aaa = (char *)malloc(strlen(filename) + 17);
1199 if (!aaa) return -1;
1201 sprintf(aaa, "UIMG %d|%s", for_real, filename);
1202 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1211 int CtdlIPCQueryUsername(CtdlIPC *ipc, const char *username, char *cret)
1216 if (!cret) return -2;
1217 if (!username) return -2;
1219 aaa = (char *)malloc(strlen(username) + 6);
1220 if (!aaa) return -1;
1222 sprintf(aaa, "QUSR %s", username);
1223 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1230 int CtdlIPCFloorListing(CtdlIPC *ipc, char **listing, char *cret)
1234 if (!cret) return -2;
1235 if (!listing) return -2;
1236 if (*listing) return -2;
1238 return CtdlIPCGenericCommand(ipc, "LFLR", NULL, 0, listing, &bytes, cret);
1243 int CtdlIPCCreateFloor(CtdlIPC *ipc, int for_real, const char *name, char *cret)
1248 if (!cret) return -2;
1249 if (!name) return -2;
1251 aaa = (char *)malloc(strlen(name) + 17);
1252 if (!aaa) return -1;
1254 sprintf(aaa, "CFLR %s|%d", name, for_real);
1255 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1262 int CtdlIPCDeleteFloor(CtdlIPC *ipc, int for_real, int floornum, char *cret)
1266 if (!cret) return -1;
1267 if (floornum < 0) return -1;
1269 sprintf(aaa, "KFLR %d|%d", floornum, for_real);
1270 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1275 int CtdlIPCEditFloor(CtdlIPC *ipc, int floornum, const char *floorname, char *cret)
1280 if (!cret) return -2;
1281 if (!floorname) return -2;
1282 if (floornum < 0) return -2;
1284 aaa = (char *)malloc(strlen(floorname) + 17);
1285 if (!aaa) return -1;
1287 sprintf(aaa, "EFLR %d|%s", floornum, floorname);
1288 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1295 int CtdlIPCIdentifySoftware(CtdlIPC *ipc, int developerid, int clientid, int revision,
1296 const char *software_name, const char *hostname, char *cret)
1301 if (developerid < 0) return -2;
1302 if (clientid < 0) return -2;
1303 if (revision < 0) return -2;
1304 if (!software_name) return -2;
1305 if (!hostname) return -2;
1307 aaa = (char *)malloc(strlen(software_name) + strlen(hostname) + 29);
1308 if (!aaa) return -1;
1310 sprintf(aaa, "IDEN %d|%d|%d|%s|%s", developerid, clientid,
1311 revision, software_name, hostname);
1312 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1319 int CtdlIPCSendInstantMessage(CtdlIPC *ipc, const char *username, const char *text,
1325 if (!cret) return -2;
1326 if (!username) return -2;
1328 aaa = (char *)malloc(strlen(username) + 8);
1329 if (!aaa) return -1;
1332 sprintf(aaa, "SEXP %s|-", username);
1333 ret = CtdlIPCGenericCommand(ipc, aaa, text, strlen(text),
1336 sprintf(aaa, "SEXP %s||", username);
1337 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1345 int CtdlIPCGetInstantMessage(CtdlIPC *ipc, char **listing, char *cret)
1349 if (!cret) return -2;
1350 if (!listing) return -2;
1351 if (*listing) return -2;
1353 return CtdlIPCGenericCommand(ipc, "GEXP", NULL, 0, listing, &bytes, cret);
1358 /* mode is 0 = enable, 1 = disable, 2 = status */
1359 int CtdlIPCEnableInstantMessageReceipt(CtdlIPC *ipc, int mode, char *cret)
1363 if (!cret) return -2;
1365 sprintf(aaa, "DEXP %d", mode);
1366 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1371 int CtdlIPCSetBio(CtdlIPC *ipc, char *bio, char *cret)
1373 if (!cret) return -2;
1374 if (!bio) return -2;
1376 return CtdlIPCGenericCommand(ipc, "EBIO", bio, strlen(bio),
1382 int CtdlIPCGetBio(CtdlIPC *ipc, const char *username, char **listing, char *cret)
1388 if (!cret) return -2;
1389 if (!username) return -2;
1390 if (!listing) return -2;
1391 if (*listing) return -2;
1393 aaa = (char *)malloc(strlen(username) + 6);
1394 if (!aaa) return -1;
1396 sprintf(aaa, "RBIO %s", username);
1397 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, listing, &bytes, cret);
1404 int CtdlIPCListUsersWithBios(CtdlIPC *ipc, char **listing, char *cret)
1408 if (!cret) return -2;
1409 if (!listing) return -2;
1410 if (*listing) return -2;
1412 return CtdlIPCGenericCommand(ipc, "LBIO", NULL, 0, listing, &bytes, cret);
1417 int CtdlIPCStealthMode(CtdlIPC *ipc, int mode, char *cret)
1421 if (!cret) return -1;
1423 sprintf(aaa, "STEL %d", mode ? 1 : 0);
1424 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1429 int CtdlIPCTerminateSession(CtdlIPC *ipc, int sid, char *cret)
1433 if (!cret) return -1;
1435 sprintf(aaa, "TERM %d", sid);
1436 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1441 int CtdlIPCTerminateServerNow(CtdlIPC *ipc, char *cret)
1443 if (!cret) return -1;
1445 return CtdlIPCGenericCommand(ipc, "DOWN", NULL, 0, NULL, NULL, cret);
1450 int CtdlIPCTerminateServerScheduled(CtdlIPC *ipc, int mode, char *cret)
1454 if (!cret) return -1;
1456 sprintf(aaa, "SCDN %d", mode ? 1 : 0);
1457 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1462 int CtdlIPCEnterSystemMessage(CtdlIPC *ipc, const char *filename, const char *text,
1468 if (!cret) return -2;
1469 if (!text) return -2;
1470 if (!filename) return -2;
1472 aaa = (char *)malloc(strlen(filename) + 6);
1473 if (!aaa) return -1;
1475 sprintf(aaa, "EMSG %s", filename);
1476 ret = CtdlIPCGenericCommand(ipc, aaa, text, strlen(text), NULL, NULL, cret);
1483 int CtdlIPCChangeHostname(CtdlIPC *ipc, const char *hostname, char *cret)
1488 if (!cret) return -2;
1489 if (!hostname) return -2;
1491 aaa = (char *)malloc(strlen(hostname) + 6);
1492 if (!aaa) return -1;
1494 sprintf(aaa, "HCHG %s", hostname);
1495 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1502 int CtdlIPCChangeRoomname(CtdlIPC *ipc, const char *roomname, char *cret)
1507 if (!cret) return -2;
1508 if (!roomname) return -2;
1510 aaa = (char *)malloc(strlen(roomname) + 6);
1511 if (!aaa) return -1;
1513 sprintf(aaa, "RCHG %s", roomname);
1514 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1521 int CtdlIPCChangeUsername(CtdlIPC *ipc, const char *username, char *cret)
1526 if (!cret) return -2;
1527 if (!username) return -2;
1529 aaa = (char *)malloc(strlen(username) + 6);
1530 if (!aaa) return -1;
1532 sprintf(aaa, "UCHG %s", username);
1533 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1540 /* This function returns the actual server time reported, or 0 if error */
1541 time_t CtdlIPCServerTime(CtdlIPC *ipc, char *cret)
1543 register time_t tret;
1546 ret = CtdlIPCGenericCommand(ipc, "TIME", NULL, 0, NULL, NULL, cret);
1547 if (ret / 100 == 2) {
1548 tret = extract_long(cret, 0);
1557 int CtdlIPCAideGetUserParameters(CtdlIPC *ipc, const char *who,
1558 struct usersupp **uret, char *cret)
1563 if (!cret) return -2;
1564 if (!uret) return -2;
1565 if (!*uret) *uret = (struct usersupp *)calloc(1, sizeof(struct usersupp));
1566 if (!*uret) return -1;
1568 sprintf(aaa, "AGUP %s", who);
1569 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1571 if (ret / 100 == 2) {
1572 extract(uret[0]->fullname, cret, 0);
1573 extract(uret[0]->password, cret, 1);
1574 uret[0]->flags = extract_int(cret, 2);
1575 uret[0]->timescalled = extract_long(cret, 3);
1576 uret[0]->posted = extract_long(cret, 4);
1577 uret[0]->axlevel = extract_int(cret, 5);
1578 uret[0]->usernum = extract_long(cret, 6);
1579 uret[0]->lastcall = extract_long(cret, 7);
1580 uret[0]->USuserpurge = extract_int(cret, 8);
1587 int CtdlIPCAideSetUserParameters(CtdlIPC *ipc, const struct usersupp *uret, char *cret)
1592 if (!cret) return -2;
1593 if (!uret) return -2;
1595 aaa = (char *)malloc(strlen(uret->fullname) + strlen(uret->password) + 84);
1596 if (!aaa) return -1;
1598 sprintf(aaa, "ASUP %s|%s|%d|%ld|%ld|%d|%ld|%ld|%d",
1599 uret->fullname, uret->password, uret->flags,
1600 uret->timescalled, uret->posted, uret->axlevel,
1601 uret->usernum, uret->lastcall, uret->USuserpurge);
1602 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1609 /* which is 0 = room, 1 = floor, 2 = site */
1610 int CtdlIPCGetMessageExpirationPolicy(CtdlIPC *ipc, int which, char *cret)
1612 static char *proto[] = {"room", "floor", "site"};
1615 if (!cret) return -2;
1616 if (which < 0 || which > 2) return -2;
1618 sprintf(aaa, "GPEX %s", proto[which]);
1619 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1624 /* which is 0 = room, 1 = floor, 2 = site */
1625 /* policy is 0 = inherit, 1 = no purge, 2 = by count, 3 = by age (days) */
1626 int CtdlIPCSetMessageExpirationPolicy(CtdlIPC *ipc, int which, int policy, int value,
1631 if (!cret) return -2;
1632 if (which < 0 || which > 2) return -2;
1633 if (policy < 0 || policy > 3) return -2;
1634 if (policy >= 2 && value < 1) return -2;
1636 sprintf(aaa, "SPEX %d|%d|%d", which, policy, value);
1637 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1642 int CtdlGetSystemConfig(CtdlIPC *ipc, char **listing, char *cret)
1646 if (!cret) return -2;
1647 if (!listing) return -2;
1648 if (*listing) return -2;
1650 return CtdlIPCGenericCommand(ipc, "CONF GET", NULL, 0,
1651 listing, &bytes, cret);
1656 int CtdlSetSystemConfig(CtdlIPC *ipc, const char *listing, char *cret)
1658 if (!cret) return -2;
1659 if (!listing) return -2;
1661 return CtdlIPCGenericCommand(ipc, "CONF SET", listing, strlen(listing),
1667 int CtdlGetSystemConfigByType(CtdlIPC *ipc, const char *mimetype,
1668 char **listing, char *cret)
1673 if (!cret) return -2;
1674 if (!mimetype) return -2;
1675 if (!listing) return -2;
1676 if (*listing) return -2;
1678 aaa = malloc(strlen(mimetype) + 13);
1679 if (!aaa) return -1;
1680 sprintf(aaa, "CONF GETSYS|%s", mimetype);
1681 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0,
1682 listing, &bytes, cret);
1687 int CtdlSetSystemConfigByType(CtdlIPC *ipc, const char *mimetype,
1688 const char *listing, char *cret)
1692 if (!cret) return -2;
1693 if (!mimetype) return -2;
1694 if (!listing) return -2;
1696 aaa = malloc(strlen(mimetype) + 13);
1697 if (!aaa) return -1;
1698 sprintf(aaa, "CONF PUTSYS|%s", mimetype);
1699 return CtdlIPCGenericCommand(ipc, aaa, listing, strlen(listing),
1704 int CtdlIPCModerateMessage(CtdlIPC *ipc, long msgnum, int level, char *cret)
1708 if (!cret) return -2;
1709 if (!msgnum) return -2;
1711 sprintf(aaa, "MMOD %ld|%d", msgnum, level);
1712 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1717 int CtdlIPCRequestClientLogout(CtdlIPC *ipc, int session, char *cret)
1721 if (!cret) return -2;
1722 if (session < 0) return -2;
1724 sprintf(aaa, "REQT %d", session);
1725 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1730 int CtdlIPCSetMessageSeen(CtdlIPC *ipc, long msgnum, int seen, char *cret)
1734 if (!cret) return -2;
1735 if (msgnum < 0) return -2;
1737 sprintf(aaa, "SEEN %ld|%d", msgnum, seen ? 1 : 0);
1738 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1743 int CtdlIPCStartEncryption(CtdlIPC *ipc, char *cret)
1745 return CtdlIPCGenericCommand(ipc, "STLS", NULL, 0, NULL, NULL, cret);
1750 int CtdlIPCDirectoryLookup(CtdlIPC *ipc, const char *address, char *cret)
1754 if (!address) return -2;
1755 if (!cret) return -2;
1757 aaa = (char *)malloc(strlen(address) + 6);
1758 if (!aaa) return -1;
1760 sprintf(aaa, "QDIR %s", address);
1761 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1766 int CtdlIPCInternalProgram(CtdlIPC *ipc, int secret, char *cret)
1770 if (!cret) return -2;
1771 sprintf(aaa, "IPGM %d", secret);
1772 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1793 /* ************************************************************************** */
1794 /* Stuff below this line is not for public consumption */
1795 /* ************************************************************************** */
1798 inline void CtdlIPC_lock(CtdlIPC *ipc)
1800 #ifdef THREADED_CLIENT
1801 pthread_mutex_lock(&(ipc->mutex));
1806 inline void CtdlIPC_unlock(CtdlIPC *ipc)
1808 #ifdef THREADED_CLIENT
1809 pthread_mutex_unlock(&(ipc->mutex));
1814 /* Read a listing from the server up to 000. Append to dest if it exists */
1815 char *CtdlIPCReadListing(CtdlIPC *ipc, char *dest)
1824 length = strlen(ret);
1831 while (CtdlIPC_getline(ipc, aaa), strcmp(aaa, "000")) {
1832 linelength = strlen(aaa);
1833 ret = (char *)realloc(ret, (size_t)(length + linelength + 2));
1835 strcpy(&ret[length], aaa);
1836 length += linelength;
1837 strcpy(&ret[length++], "\n");
1845 /* Send a listing to the server; generate the ending 000. */
1846 int CtdlIPCSendListing(CtdlIPC *ipc, const char *listing)
1850 text = (char *)malloc(strlen(listing) + 6);
1852 strcpy(text, listing);
1853 while (text[strlen(text) - 1] == '\n')
1854 text[strlen(text) - 1] = '\0';
1855 strcat(text, "\n000");
1856 CtdlIPC_putline(ipc, text);
1860 /* Malloc failed but we are committed to send */
1861 /* This may result in extra blanks at the bottom */
1862 CtdlIPC_putline(ipc, text);
1863 CtdlIPC_putline(ipc, "000");
1869 /* Partial read of file from server */
1870 size_t CtdlIPCPartialRead(CtdlIPC *ipc, void **buf, size_t offset, size_t bytes, char *cret)
1872 register size_t len = 0;
1875 if (!buf) return -1;
1876 if (!cret) return -1;
1877 if (bytes < 1) return -1;
1878 if (offset < 0) return -1;
1881 sprintf(aaa, "READ %d|%d", offset, bytes);
1882 CtdlIPC_putline(ipc, aaa);
1883 CtdlIPC_getline(ipc, aaa);
1885 strcpy(cret, &aaa[4]);
1887 len = extract_long(&aaa[4], 0);
1888 *buf = (void *)realloc(*buf, (size_t)(offset + len));
1890 /* I know what I'm doing */
1891 serv_read(ipc, (*buf + offset), len);
1893 /* We have to read regardless */
1894 serv_read(ipc, aaa, len);
1898 CtdlIPC_unlock(ipc);
1904 int CtdlIPCEndDownload(CtdlIPC *ipc, char *cret)
1908 if (!cret) return -2;
1909 if (!ipc->downloading) return -2;
1911 ret = CtdlIPCGenericCommand(ipc, "CLOS", NULL, 0, NULL, NULL, cret);
1913 ipc->downloading = 0;
1919 int CtdlIPCSpecifyPreferredFormats(CtdlIPC *ipc, char *cret, char *formats) {
1923 snprintf(cmd, sizeof cmd, "MSGP %s", formats);
1924 ret = CtdlIPCGenericCommand(ipc, cmd, NULL, 0, NULL, NULL, cret);
1931 int CtdlIPCReadDownload(CtdlIPC *ipc, void **buf, size_t bytes,
1932 void (*progress_gauge_callback)(long, long), char *cret)
1934 register size_t len;
1936 if (!cret) return -1;
1937 if (!buf) return -1;
1938 if (*buf) return -1;
1939 if (!ipc->downloading) return -1;
1942 if (progress_gauge_callback)
1943 progress_gauge_callback(len, bytes);
1944 while (len < bytes) {
1945 register size_t block;
1947 block = CtdlIPCPartialRead(ipc, buf, len, 4096, cret);
1953 if (progress_gauge_callback)
1954 progress_gauge_callback(len, bytes);
1960 /* READ - pipelined */
1961 int CtdlIPCHighSpeedReadDownload(CtdlIPC *ipc, void **buf, size_t bytes,
1962 void (*progress_gauge_callback)(long, long), char *cret)
1964 register size_t len;
1965 register int calls; /* How many calls in the pipeline */
1966 register int i; /* iterator */
1969 if (!cret) return -1;
1970 if (!buf) return -1;
1971 if (*buf) return -1;
1972 if (!ipc->downloading) return -1;
1974 *buf = (void *)realloc(*buf, bytes);
1975 if (!*buf) return -1;
1979 if (progress_gauge_callback)
1980 progress_gauge_callback(len, bytes);
1982 /* How many calls will be in the pipeline? */
1983 calls = bytes / 4096;
1984 if (bytes % 4096) calls++;
1986 /* Send all requests at once */
1987 for (i = 0; i < calls; i++) {
1988 sprintf(aaa, "READ %d|4096", i * 4096);
1989 CtdlIPC_putline(ipc, aaa);
1992 /* Receive all responses at once */
1993 for (i = 0; i < calls; i++) {
1994 CtdlIPC_getline(ipc, aaa);
1996 strcpy(cret, &aaa[4]);
1998 len = extract_long(&aaa[4], 0);
1999 /* I know what I'm doing */
2000 serv_read(ipc, ((*buf) + (i * 4096)), len);
2002 if (progress_gauge_callback)
2003 progress_gauge_callback(i * 4096 + len, bytes);
2005 CtdlIPC_unlock(ipc);
2011 int CtdlIPCEndUpload(CtdlIPC *ipc, char *cret)
2015 if (!cret) return -1;
2016 if (!ipc->uploading) return -1;
2018 ret = CtdlIPCGenericCommand(ipc, "UCLS", NULL, 0, NULL, NULL, cret);
2026 int CtdlIPCWriteUpload(CtdlIPC *ipc, void *buf, size_t bytes, char *cret)
2028 register int ret = -1;
2029 register size_t offset;
2032 if (!cret) return -1;
2033 if (!buf) return -1;
2034 if (bytes < 1) return -1;
2037 while (offset < bytes) {
2038 sprintf(aaa, "WRIT %d", bytes - offset);
2039 CtdlIPC_putline(ipc, aaa);
2040 CtdlIPC_getline(ipc, aaa);
2041 strcpy(cret, &aaa[4]);
2043 if (aaa[0] == '7') {
2044 register size_t to_write;
2046 to_write = extract_long(&aaa[4], 0);
2047 serv_write(ipc, buf + offset, to_write);
2058 * Generic command method. This method should handle any server command
2059 * except for CHAT. It takes the following arguments:
2061 * ipc The server to speak with
2062 * command Preformatted command to send to server
2063 * to_send A text or binary file to send to server
2064 * (only sent if server requests it)
2065 * bytes_to_send The number of bytes in to_send (required if
2066 * sending binary, optional if sending listing)
2067 * to_receive Pointer to a NULL pointer, if the server
2068 * sends text or binary we will allocate memory
2069 * for the file and stuff it here
2070 * bytes_to_receive If a file is received, we will store its
2072 * proto_response The protocol response. Caller must provide
2073 * this buffer and ensure that it is at least
2074 * 128 bytes in length.
2076 * This function returns a number equal to the protocol response number,
2077 * -1 if an internal error occurred, -2 if caller provided bad values,
2078 * or 0 - the protocol response number if bad values were found during
2079 * the protocol exchange.
2080 * It stores the protocol response string (minus the number) in
2081 * protocol_response as described above. Some commands send additional
2082 * data in this string.
2084 int CtdlIPCGenericCommand(CtdlIPC *ipc,
2085 const char *command, const char *to_send,
2086 size_t bytes_to_send, char **to_receive,
2087 size_t *bytes_to_receive, char *proto_response)
2093 if (!command) return -2;
2094 if (!proto_response) return -2;
2096 if (ipc->ssl) watch_ssl = 1;
2099 CtdlIPC_putline(ipc, command);
2101 CtdlIPC_getline(ipc, proto_response);
2102 if (proto_response[3] == '*')
2104 ret = atoi(proto_response);
2105 strcpy(proto_response, &proto_response[4]);
2106 switch (ret / 100) {
2107 default: /* Unknown, punt */
2109 case 3: /* MORE_DATA */
2111 /* Don't need to do anything */
2113 case 1: /* LISTING_FOLLOWS */
2114 if (to_receive && !*to_receive && bytes_to_receive) {
2115 *to_receive = CtdlIPCReadListing(ipc, NULL);
2116 } else { /* Drain */
2117 while (CtdlIPC_getline(ipc, buf), strcmp(buf, "000")) ;
2121 case 4: /* SEND_LISTING */
2123 CtdlIPCSendListing(ipc, to_send);
2125 /* No listing given, fake it */
2126 CtdlIPC_putline(ipc, "000");
2130 case 6: /* BINARY_FOLLOWS */
2131 if (to_receive && !*to_receive && bytes_to_receive) {
2133 extract_long(proto_response, 0);
2134 *to_receive = (char *)
2135 malloc((size_t)*bytes_to_receive);
2139 serv_read(ipc, *to_receive,
2146 drain = extract_long(proto_response, 0);
2147 while (drain > SIZ) {
2148 serv_read(ipc, buf, SIZ);
2151 serv_read(ipc, buf, drain);
2155 case 7: /* SEND_BINARY */
2156 if (to_send && bytes_to_send) {
2157 serv_write(ipc, to_send, bytes_to_send);
2158 } else if (bytes_to_send) {
2159 /* Fake it, send nulls */
2162 fake = bytes_to_send;
2163 memset(buf, '\0', SIZ);
2164 while (fake > SIZ) {
2165 serv_write(ipc, buf, SIZ);
2168 serv_write(ipc, buf, fake);
2170 } /* else who knows? DANGER WILL ROBINSON */
2172 case 8: /* START_CHAT_MODE */
2173 if (!strncasecmp(command, "CHAT", 4)) {
2174 /* Don't call chatmode with generic! */
2175 CtdlIPC_putline(ipc, "/quit");
2178 /* In this mode we send then receive listing */
2180 CtdlIPCSendListing(ipc, to_send);
2182 /* No listing given, fake it */
2183 CtdlIPC_putline(ipc, "000");
2186 if (to_receive && !*to_receive
2187 && bytes_to_receive) {
2188 *to_receive = CtdlIPCReadListing(ipc, NULL);
2189 } else { /* Drain */
2190 while (CtdlIPC_getline(ipc, buf),
2191 strcmp(buf, "000")) ;
2196 case 9: /* ASYNC_MSG */
2197 /* CtdlIPCDoAsync(ret, proto_response); */
2198 free(CtdlIPCReadListing(ipc, NULL)); /* STUB FIXME */
2204 CtdlIPC_unlock(ipc);