2 * Dolda Connect - Modular multiuser Direct Connect-style client
3 * Copyright (C) 2004 Fredrik Tolf <fredrik@dolda2000.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 /* I'm very unsure about this, but for now it defines wcstoll (which
23 * should be defined anyway) and doesn't break anything... let's keep
24 * two eyes wide open, though. */
36 #include <doldaconnect/uilib.h>
37 #include <doldaconnect/uimisc.h>
50 void (*process)(struct dc_response *resp, struct logindata *data);
51 int (*init)(struct logindata *data);
52 void (*release)(struct logindata *data);
57 int (*conv)(int type, wchar_t *text, char **resp, void *data);
58 void (*callback)(int err, wchar_t *reason, void *data);
64 struct authmech *mech;
76 void (*callback)(int resp, void *data);
82 void (*callback)(struct dc_fnetnode *fn, int resp, void *data);
87 struct dc_fnetnode *dc_fnetnodes = NULL;
88 struct dc_transfer *dc_transfers = NULL;
90 static void message(int bits, char *format, ...)
99 if((v = getenv("LIBDCUI_MSG")) != NULL)
100 hb = strtol(v, NULL, 0) & 65535;
104 va_start(args, format);
105 vfprintf(stderr, format, args);
110 static void freelogindata(struct logindata *data)
112 if((data->mech != NULL) && (data->mech->release != NULL))
113 data->mech->release(data);
114 if(data->freeusername)
115 free(data->username);
119 static int logincallback(struct dc_response *resp);
121 static void process_authless(struct dc_response *resp, struct logindata *data)
126 data->callback(DC_LOGIN_ERR_SUCCESS, NULL, data->data);
132 if((ires = dc_interpret(resp)) != NULL)
135 if(data->conv((resp->code == 303)?DC_LOGIN_CONV_INFO:DC_LOGIN_CONV_ERROR, ires->argv[0].val.str, &buf, data->data))
137 data->callback(DC_LOGIN_ERR_CONV, NULL, data->data);
140 dc_queuecmd(logincallback, data, L"pass", L"", NULL);
144 memset(buf, 0, strlen(buf));
152 data->callback(DC_LOGIN_ERR_SERVER, NULL, data->data);
156 data->callback(DC_LOGIN_ERR_AUTHFAIL, NULL, data->data);
160 data->callback(DC_LOGIN_ERR_USER, NULL, data->data);
166 static void process_pam(struct dc_response *resp, struct logindata *data)
168 struct dc_intresp *ires;
175 data->callback(DC_LOGIN_ERR_SUCCESS, NULL, data->data);
182 if(resp->code == 301)
183 convtype = DC_LOGIN_CONV_NOECHO;
184 else if(resp->code == 302)
185 convtype = DC_LOGIN_CONV_ECHO;
186 else if(resp->code == 303)
187 convtype = DC_LOGIN_CONV_INFO;
188 else if(resp->code == 304)
189 convtype = DC_LOGIN_CONV_ERROR;
190 if((ires = dc_interpret(resp)) != NULL)
193 if(data->conv(convtype, ires->argv[0].val.str, &buf, data->data))
195 data->callback(DC_LOGIN_ERR_CONV, NULL, data->data);
198 dc_queuecmd(logincallback, data, L"pass", L"%s", buf, NULL);
202 memset(buf, 0, strlen(buf));
209 data->callback(DC_LOGIN_ERR_SERVER, NULL, data->data);
213 data->callback(DC_LOGIN_ERR_AUTHFAIL, NULL, data->data);
217 data->callback(DC_LOGIN_ERR_USER, NULL, data->data);
227 krb5_context context;
228 krb5_principal sprinc, myprinc;
230 krb5_auth_context authcon;
232 krb5_creds *servcreds;
233 int valid, fwd, fwded;
236 static void process_krb5(struct dc_response *resp, struct logindata *data)
239 struct dc_intresp *ires;
240 struct krb5data *krb;
242 krb5_ap_rep_enc_part *repl;
245 krb = data->mechdata;
249 data->callback(DC_LOGIN_ERR_SUCCESS, NULL, data->data);
256 buf = hexencode(krb->reqbuf.data, krb->reqbuf.length);
257 dc_queuecmd(logincallback, data, L"pass", L"%s", buf, NULL);
262 if((ires = dc_interpret(resp)) != NULL)
264 k5d.data = hexdecode(icswcstombs(ires->argv[0].val.str, NULL, NULL), &k5d.length);
267 if((ret = krb5_rd_rep(krb->context, krb->authcon, &k5d, &repl)) != 0)
269 data->callback(DC_LOGIN_ERR_SERVER, NULL, data->data);
273 /* XXX: Do I need to do something with this? */
275 krb5_free_ap_rep_enc_part(krb->context, repl);
277 if(krb->fwd && !krb->fwded)
279 if(krb->reqbuf.data != NULL)
280 free(krb->reqbuf.data);
281 krb->reqbuf.data = NULL;
282 if((ret = krb5_fwd_tgt_creds(krb->context, krb->authcon, NULL, krb->servcreds->client, krb->servcreds->server, 0, 1, &krb->reqbuf)) != 0)
284 message(1, "krb5_fwd_tgt_creds reported an error: %s\n", error_message(ret));
285 dc_queuecmd(logincallback, data, L"pass", L"31", NULL);
289 dc_queuecmd(logincallback, data, L"pass", L"32", NULL);
294 dc_queuecmd(logincallback, data, L"pass", L"31", NULL);
302 data->callback(DC_LOGIN_ERR_USER, NULL, data->data);
308 data->callback(DC_LOGIN_ERR_SERVER, NULL, data->data);
312 data->callback(DC_LOGIN_ERR_AUTHFAIL, NULL, data->data);
316 data->callback(DC_LOGIN_ERR_USER, NULL, data->data);
322 static int init_krb5(struct logindata *data)
325 struct krb5data *krb;
329 if(dc_gethostname() == NULL)
331 message(1, "cannot use krb5 without a host name\n");
334 krb = smalloc(sizeof(*krb));
335 memset(krb, 0, sizeof(*krb));
338 data->mechdata = krb;
339 if((ret = krb5_init_context(&krb->context)) != 0)
341 message(1, "krb5_init_context reported an error: %s\n", error_message(ret));
344 if((ret = krb5_auth_con_init(krb->context, &krb->authcon)) != 0)
346 message(1, "krb5_auth_con_init reported an error: %s\n", error_message(ret));
349 krb5_auth_con_setflags(krb->context, krb->authcon, KRB5_AUTH_CONTEXT_DO_SEQUENCE);
350 if((ret = krb5_sname_to_principal(krb->context, dc_gethostname(), "doldacond", KRB5_NT_SRV_HST, &krb->sprinc)) != 0)
352 message(1, "krb5_sname_to_principal reported an error: %s\n", error_message(ret));
355 if((ret = krb5_cc_default(krb->context, &krb->ccache)) != 0)
357 message(1, "krb5_cc_default reported an error: %s\n", error_message(ret));
360 if((ret = krb5_cc_get_principal(krb->context, krb->ccache, &krb->myprinc)) != 0)
362 message(1, "krb5_cc_default reported an error: %s\n", error_message(ret));
365 memset(&creds, 0, sizeof(creds));
366 creds.client = krb->myprinc;
367 creds.server = krb->sprinc;
368 if((ret = krb5_get_credentials(krb->context, 0, krb->ccache, &creds, &krb->servcreds)) != 0)
370 message(1, "krb5_get_credentials reported an error: %s\n", error_message(ret));
373 /* WTF is this checksum stuff?! The Krb docs don't say a word about it! */
374 cksum.data = sstrdup(dc_gethostname());
375 cksum.length = strlen(cksum.data);
376 if((ret = krb5_mk_req_extended(krb->context, &krb->authcon, AP_OPTS_MUTUAL_REQUIRED, &cksum, krb->servcreds, &krb->reqbuf)) != 0)
378 message(1, "krb5_mk_req_extended reported an error: %s\n", error_message(ret));
386 static void release_krb5(struct logindata *data)
388 struct krb5data *krb;
390 if((krb = data->mechdata) == NULL)
392 if(krb->servcreds != NULL)
393 krb5_free_creds(krb->context, krb->servcreds);
394 if(krb->reqbuf.data != NULL)
395 free(krb->reqbuf.data);
396 if(krb->sprinc != NULL)
397 krb5_free_principal(krb->context, krb->sprinc);
398 if(krb->myprinc != NULL)
399 krb5_free_principal(krb->context, krb->myprinc);
400 if(krb->ccache != NULL)
401 krb5_cc_close(krb->context, krb->ccache);
402 if(krb->authcon != NULL)
403 krb5_auth_con_free(krb->context, krb->authcon);
404 if(krb->context != NULL)
405 krb5_free_context(krb->context);
410 /* Arranged in order of priority */
411 static struct authmech authmechs[] =
416 .process = process_krb5,
418 .release = release_krb5
423 .process = process_authless,
429 .process = process_authless,
435 .process = process_pam,
444 int dc_convtty(int type, wchar_t *text, char **resp, void *data)
450 if((buf = icwcstombs(text, NULL)) == NULL)
454 *resp = sstrdup(pass);
455 memset(pass, 0, strlen(pass));
461 int dc_convnone(int type, wchar_t *text, char **resp, void *data)
466 static int logincallback(struct dc_response *resp)
469 struct dc_intresp *ires;
470 struct logindata *data;
473 struct passwd *pwent;
477 if(!wcscmp(resp->cmdname, L"lsauth"))
479 if(resp->code == 201)
481 data->callback(DC_LOGIN_ERR_NOLOGIN, NULL, data->data);
485 while((ires = dc_interpret(resp)) != NULL)
487 if(!data->useauthless && !wcscmp(ires->argv[0].val.str, L"authless"))
492 for(i = 0; authmechs[i].name != NULL; i++)
494 if(!wcscmp(authmechs[i].name, ires->argv[0].val.str) && ((i < mech) || (mech == -1)))
496 odata = data->mechdata;
497 data->mechdata = NULL;
498 message(4, "trying auth mech %ls\n", authmechs[i].name);
499 if((authmechs[i].init != NULL) && authmechs[i].init(data))
501 if(authmechs[i].release != NULL)
502 authmechs[i].release(data);
503 data->mechdata = odata;
504 message(2, "authentication mechanism %ls failed, trying further...\n", authmechs[i].name);
506 if((data->mech != NULL) && data->mech->release != NULL)
508 ndata = data->mechdata;
509 data->mechdata = odata;
510 data->mech->release(data);
511 data->mechdata = ndata;
514 data->mech = authmechs + i;
523 data->callback(DC_LOGIN_ERR_NOLOGIN, NULL, data->data);
526 if((username = data->username) == NULL)
528 if((pwent = getpwuid(getuid())) == NULL)
530 data->callback(DC_LOGIN_ERR_USER, NULL, data->data);
534 username = pwent->pw_name;
536 dc_queuecmd(logincallback, data, L"login", data->mech->name, L"%s", username, NULL);
539 } else if(!wcscmp(resp->cmdname, L"login") || !wcscmp(resp->cmdname, L"pass")) {
540 data->mech->process(resp, data);
545 void dc_loginasync(char *username, int useauthless, int (*conv)(int, wchar_t *, char **, void *), void (*callback)(int, wchar_t *, void *), void *udata)
547 struct logindata *data;
549 data = smalloc(sizeof(*data));
555 data->mechdata = NULL;
556 data->callback = callback;
557 data->useauthless = useauthless;
558 data->freeusername = 0;
561 data->username = NULL;
563 data->username = sstrdup(username);
564 data->freeusername = 1;
566 dc_queuecmd(logincallback, data, L"lsauth", NULL);
569 static void synclogincb(int err, wchar_t *reason, struct synclogindata *data)
580 data->reason = swcsdup(reason);
583 int dc_login(char *username, int useauthless, int (*conv)(int, wchar_t *, char **, void *), wchar_t **reason)
586 struct synclogindata *dbuf;
589 dbuf = smalloc(sizeof(*dbuf));
590 memset(dbuf, 0, sizeof(*dbuf));
592 dc_loginasync(username, useauthless, conv, (void (*)(int, wchar_t *, void *))synclogincb, dbuf);
593 while(dbuf->err == -1)
598 pfd.events |= POLLOUT;
600 if(poll(&pfd, 1, -1) < 0)
602 if(!abort && (pfd.revents & POLLIN) && dc_handleread())
604 if(!abort && (pfd.revents & POLLOUT) && dc_handlewrite())
613 *reason = dbuf->reason;
614 else if(dbuf->reason != NULL)
621 static struct dc_fnetpeerdatum *finddatum(struct dc_fnetnode *fn, wchar_t *id)
623 struct dc_fnetpeerdatum *datum;
625 for(datum = fn->peerdata; datum != NULL; datum = datum->next)
627 if(!wcscmp(datum->id, id))
633 static struct dc_fnetpeerdatum *adddatum(struct dc_fnetnode *fn, wchar_t *id, int dt)
635 struct dc_fnetpeerdatum *datum;
637 datum = smalloc(sizeof(*datum));
638 memset(datum, 0, sizeof(*datum));
641 datum->id = swcsdup(id);
643 datum->next = fn->peerdata;
644 if(fn->peerdata != NULL)
645 fn->peerdata->prev = datum;
646 fn->peerdata = datum;
650 static struct dc_fnetpeerdi *difindoradd(struct dc_fnetpeer *peer, struct dc_fnetpeerdatum *datum)
654 for(i = 0; i < peer->dinum; i++)
656 if(peer->di[i].datum == datum)
657 return(&peer->di[i]);
659 peer->di = srealloc(peer->di, sizeof(struct dc_fnetpeerdi) * ++(peer->dinum));
660 memset(&peer->di[i], 0, sizeof(struct dc_fnetpeerdi));
661 peer->di[i].datum = datum;
663 return(&peer->di[i]);
666 static void putdatum(struct dc_fnetnode *fn, struct dc_fnetpeerdatum *datum)
668 if(--datum->refcount > 0)
670 if(datum->next != NULL)
671 datum->next->prev = datum->prev;
672 if(datum->prev != NULL)
673 datum->prev->next = datum->next;
674 if(fn->peerdata == datum)
675 fn->peerdata = datum->next;
680 static void peersetnum(struct dc_fnetpeer *peer, wchar_t *id, int value)
682 struct dc_fnetpeerdatum *datum;
683 struct dc_fnetpeerdi *di;
685 if((datum = finddatum(peer->fn, id)) == NULL)
686 datum = adddatum(peer->fn, id, DC_FNPD_INT);
687 di = difindoradd(peer, datum);
691 static void peersetlnum(struct dc_fnetpeer *peer, wchar_t *id, long long value)
693 struct dc_fnetpeerdatum *datum;
694 struct dc_fnetpeerdi *di;
696 if((datum = finddatum(peer->fn, id)) == NULL)
697 datum = adddatum(peer->fn, id, DC_FNPD_INT);
698 di = difindoradd(peer, datum);
702 static void peersetstr(struct dc_fnetpeer *peer, wchar_t *id, wchar_t *value)
704 struct dc_fnetpeerdatum *datum;
705 struct dc_fnetpeerdi *di;
707 if((datum = finddatum(peer->fn, id)) == NULL)
708 datum = adddatum(peer->fn, id, DC_FNPD_INT);
709 di = difindoradd(peer, datum);
710 if(di->d.str != NULL)
712 di->d.str = swcsdup(value);
715 struct dc_fnetpeer *dc_fnetfindpeer(struct dc_fnetnode *fn, wchar_t *id)
717 struct dc_fnetpeer *peer;
719 for(peer = fn->peers; peer != NULL; peer = peer->next)
721 if(!wcscmp(peer->id, id))
727 static struct dc_fnetpeer *addpeer(struct dc_fnetnode *fn, wchar_t *id, wchar_t *nick)
729 struct dc_fnetpeer *peer;
731 peer = smalloc(sizeof(*peer));
732 memset(peer, 0, sizeof(*peer));
734 peer->id = swcsdup(id);
735 peer->nick = swcsdup(nick);
736 peer->next = fn->peers;
738 if(fn->peers != NULL)
739 fn->peers->prev = peer;
744 static void delpeer(struct dc_fnetpeer *peer)
748 if(peer->next != NULL)
749 peer->next->prev = peer->prev;
750 if(peer->prev != NULL)
751 peer->prev->next = peer->next;
752 if(peer->fn->peers == peer)
753 peer->fn->peers = peer->next;
756 for(i = 0; i < peer->dinum; i++)
758 if((peer->di[i].datum->dt == DC_FNPD_STR) && (peer->di[i].d.str != NULL))
759 free(peer->di[i].d.str);
760 putdatum(peer->fn, peer->di[i].datum);
766 static struct dc_fnetnode *newfn(void)
768 struct dc_fnetnode *fn;
770 fn = smalloc(sizeof(*fn));
771 memset(fn, 0, sizeof(*fn));
775 fn->state = fn->numusers = fn->found = 0;
776 fn->destroycb = NULL;
778 fn->next = dc_fnetnodes;
780 if(dc_fnetnodes != NULL)
781 dc_fnetnodes->prev = fn;
786 static void freefn(struct dc_fnetnode *fn)
789 fn->next->prev = fn->prev;
791 fn->prev->next = fn->next;
792 if(fn == dc_fnetnodes)
793 dc_fnetnodes = fn->next;
794 if(fn->destroycb != NULL)
796 while(fn->peers != NULL)
798 while(fn->peerdata != NULL)
800 fn->peerdata->refcount = 0;
801 putdatum(fn, fn->peerdata);
810 struct dc_fnetnode *dc_findfnetnode(int id)
812 struct dc_fnetnode *fn;
814 for(fn = dc_fnetnodes; fn != NULL; fn = fn->next)
822 static struct dc_transfer *newtransfer(void)
824 struct dc_transfer *transfer;
826 transfer = smalloc(sizeof(*transfer));
827 memset(transfer, 0, sizeof(*transfer));
829 transfer->peerid = transfer->peernick = transfer->path = NULL;
830 transfer->state = DC_TRNS_WAITING;
831 transfer->dir = DC_TRNSD_UNKNOWN;
833 transfer->curpos = -1;
834 transfer->destroycb = NULL;
835 transfer->udata = NULL;
836 transfer->next = dc_transfers;
837 transfer->prev = NULL;
838 if(dc_transfers != NULL)
839 dc_transfers->prev = transfer;
840 dc_transfers = transfer;
844 static void freetransfer(struct dc_transfer *transfer)
846 if(transfer->next != NULL)
847 transfer->next->prev = transfer->prev;
848 if(transfer->prev != NULL)
849 transfer->prev->next = transfer->next;
850 if(transfer == dc_transfers)
851 dc_transfers = transfer->next;
852 if(transfer->destroycb != NULL)
853 transfer->destroycb(transfer);
854 if(transfer->peerid != NULL)
855 free(transfer->peerid);
856 if(transfer->peernick != NULL)
857 free(transfer->peernick);
858 if(transfer->path != NULL)
859 free(transfer->path);
863 struct dc_transfer *dc_findtransfer(int id)
865 struct dc_transfer *transfer;
867 for(transfer = dc_transfers; transfer != NULL; transfer = transfer->next)
869 if(transfer->id == id)
875 static int getfnlistcallback(struct dc_response *resp)
877 struct dc_intresp *ires;
878 struct gencbdata *data;
879 struct dc_fnetnode *fn, *next;
882 if(resp->code == 200)
884 for(fn = dc_fnetnodes; fn != NULL; fn = fn->next)
886 while((ires = dc_interpret(resp)) != NULL)
888 if((fn = dc_findfnetnode(ires->argv[0].val.num)) != NULL)
893 fn->fnet = swcsdup(ires->argv[1].val.str);
896 fn->name = swcsdup(ires->argv[2].val.str);
897 fn->numusers = ires->argv[3].val.num;
898 fn->state = ires->argv[4].val.num;
899 if(fn->pubid != NULL)
901 fn->pubid = swcsdup(ires->argv[5].val.str);
904 fn->id = ires->argv[0].val.num;
905 fn->fnet = swcsdup(ires->argv[1].val.str);
906 fn->name = swcsdup(ires->argv[2].val.str);
907 fn->numusers = ires->argv[3].val.num;
908 fn->state = ires->argv[4].val.num;
909 fn->pubid = swcsdup(ires->argv[5].val.str);
914 for(fn = dc_fnetnodes; fn != NULL; fn = next)
920 data->callback(200, data->data);
922 } else if(resp->code == 201) {
923 while(dc_fnetnodes != NULL)
924 freefn(dc_fnetnodes);
925 data->callback(201, data->data);
927 } else if(resp->code == 502) {
928 while(dc_fnetnodes != NULL)
929 freefn(dc_fnetnodes);
930 data->callback(502, data->data);
936 static int gettrlistcallback(struct dc_response *resp)
938 struct dc_intresp *ires;
939 struct gencbdata *data;
940 struct dc_transfer *transfer, *next;
943 if(resp->code == 200)
945 for(transfer = dc_transfers; transfer != NULL; transfer = transfer->next)
947 while((ires = dc_interpret(resp)) != NULL)
949 if((transfer = dc_findtransfer(ires->argv[0].val.num)) != NULL)
952 if((transfer->path == NULL) || wcscmp(transfer->path, ires->argv[5].val.str))
954 if(transfer->path != NULL)
955 free(transfer->path);
956 transfer->path = swcsdup(ires->argv[5].val.str);
958 if((transfer->peerid == NULL) || wcscmp(transfer->peerid, ires->argv[3].val.str))
960 if(transfer->peerid != NULL)
961 free(transfer->peerid);
962 transfer->peerid = swcsdup(ires->argv[3].val.str);
964 if((transfer->peernick == NULL) || wcscmp(transfer->peernick, ires->argv[4].val.str))
966 if(transfer->peernick != NULL)
967 free(transfer->peernick);
968 transfer->peernick = swcsdup(ires->argv[4].val.str);
970 transfer->dir = ires->argv[1].val.num;
971 transfer->state = ires->argv[2].val.num;
972 transfer->size = ires->argv[6].val.num;
973 transfer->curpos = ires->argv[7].val.num;
974 if(transfer->hash != NULL)
976 free(transfer->hash);
977 transfer->hash = NULL;
979 if(wcslen(ires->argv[8].val.str) > 0)
980 transfer->hash = swcsdup(ires->argv[8].val.str);
982 transfer = newtransfer();
983 transfer->id = ires->argv[0].val.num;
984 transfer->dir = ires->argv[1].val.num;
985 transfer->state = ires->argv[2].val.num;
986 transfer->peerid = swcsdup(ires->argv[3].val.str);
987 transfer->peernick = swcsdup(ires->argv[4].val.str);
988 transfer->path = swcsdup(ires->argv[5].val.str);
989 transfer->size = ires->argv[6].val.num;
990 transfer->curpos = ires->argv[7].val.num;
991 if(wcslen(ires->argv[8].val.str) > 0)
992 transfer->hash = swcsdup(ires->argv[8].val.str);
997 for(transfer = dc_transfers; transfer != NULL; transfer = next)
999 next = transfer->next;
1000 if(!transfer->found)
1001 freetransfer(transfer);
1003 data->callback(200, data->data);
1005 } else if(resp->code == 201) {
1006 while(dc_transfers != NULL)
1007 freetransfer(dc_transfers);
1008 data->callback(201, data->data);
1010 } else if(resp->code == 502) {
1011 while(dc_transfers != NULL)
1012 freetransfer(dc_transfers);
1013 data->callback(502, data->data);
1019 static int sortlist1(const struct dc_respline *l1, const struct dc_respline *l2)
1021 return(wcscmp(l1->argv[1], l2->argv[1]));
1024 static int sortlist2(const struct dc_fnetpeer **p1, const struct dc_fnetpeer **p2)
1026 return(wcscmp((*p1)->id, (*p2)->id));
1029 static void fillpeer(struct dc_fnetpeer *peer, struct dc_respline *r)
1032 struct dc_fnetpeerdatum *datum;
1034 for(i = 3; i < r->argc; i += 2)
1036 if((datum = finddatum(peer->fn, r->argv[i])) != NULL)
1041 peersetnum(peer, datum->id, wcstol(r->argv[i + 1], NULL, 10));
1044 peersetlnum(peer, datum->id, wcstoll(r->argv[i + 1], NULL, 10));
1047 peersetstr(peer, datum->id, r->argv[i + 1]);
1054 static int getpeerlistcallback(struct dc_response *resp)
1057 struct dc_fnetnode *fn;
1058 struct fnetcbdata *data;
1059 struct dc_fnetpeer *peer;
1060 struct dc_fnetpeer **plist;
1061 size_t plistsize, plistdata;
1064 if((fn = dc_findfnetnode(data->fnid)) == NULL)
1066 data->callback(NULL, -1, data->data);
1070 if(resp->code == 200)
1072 qsort(resp->rlines, resp->numlines, sizeof(*resp->rlines), (int (*)(const void *, const void *))sortlist1);
1074 plistsize = plistdata = 0;
1075 for(i = 0, peer = fn->peers; peer != NULL; peer = peer->next)
1076 addtobuf(plist, peer);
1077 qsort(plist, plistdata, sizeof(*plist), (int (*)(const void *, const void *))sortlist2);
1081 if((i < resp->numlines) && (o < plistdata))
1083 c = wcscmp(resp->rlines[i].argv[1], plist[o]->id);
1086 peer = addpeer(fn, resp->rlines[i].argv[1], resp->rlines[i].argv[2]);
1087 fillpeer(peer, resp->rlines + i);
1093 fillpeer(plist[o], resp->rlines + i);
1097 } else if(i < resp->numlines) {
1098 peer = addpeer(fn, resp->rlines[i].argv[1], resp->rlines[i].argv[2]);
1099 fillpeer(peer, resp->rlines + i);
1101 } else if(o < plistdata) {
1109 } else if(resp->code == 201) {
1110 while(fn->peers != NULL)
1113 data->callback(fn, resp->code, data->data);
1118 static int getpalistcallback(struct dc_response *resp)
1120 struct dc_fnetnode *fn;
1121 struct dc_intresp *ires;
1122 struct fnetcbdata *data;
1125 if((fn = dc_findfnetnode(data->fnid)) == NULL)
1127 data->callback(NULL, -1, data->data);
1131 if(resp->code == 200)
1133 while((ires = dc_interpret(resp)) != NULL)
1135 adddatum(fn, ires->argv[0].val.str, ires->argv[1].val.num);
1138 dc_queuecmd(getpeerlistcallback, data, L"lspeers", L"%i", fn->id, NULL);
1139 } else if(resp->code == 201) {
1140 dc_queuecmd(getpeerlistcallback, data, L"lspeers", L"%i", fn->id, NULL);
1142 data->callback(fn, resp->code, data->data);
1148 void dc_getfnlistasync(void (*callback)(int, void *), void *udata)
1150 struct gencbdata *data;
1152 data = smalloc(sizeof(*data));
1153 data->callback = callback;
1155 dc_queuecmd(getfnlistcallback, data, L"lsnodes", NULL);
1158 void dc_gettrlistasync(void (*callback)(int, void *), void *udata)
1160 struct gencbdata *data;
1162 data = smalloc(sizeof(*data));
1163 data->callback = callback;
1165 dc_queuecmd(gettrlistcallback, data, L"lstrans", NULL);
1168 void dc_getpeerlistasync(struct dc_fnetnode *fn, void (*callback)(struct dc_fnetnode *, int, void *), void *udata)
1170 struct fnetcbdata *data;
1172 data = smalloc(sizeof(*data));
1173 data->callback = callback;
1174 data->fnid = fn->id;
1176 dc_queuecmd(getpalistcallback, data, L"lspa", L"%i", fn->id, NULL);
1179 void dc_uimisc_disconnected(void)
1181 while(dc_fnetnodes != NULL)
1182 freefn(dc_fnetnodes);
1183 while(dc_transfers != NULL)
1184 freetransfer(dc_transfers);
1187 void dc_uimisc_handlenotify(struct dc_response *resp)
1190 struct dc_fnetnode *fn;
1191 struct dc_transfer *transfer;
1192 struct dc_fnetpeer *peer;
1193 struct dc_intresp *ires;
1195 if((ires = dc_interpret(resp)) == NULL)
1200 if((fn = dc_findfnetnode(ires->argv[0].val.num)) != NULL)
1201 fn->state = ires->argv[1].val.num;
1204 if((fn = dc_findfnetnode(ires->argv[0].val.num)) != NULL)
1206 if(fn->name != NULL)
1208 fn->name = swcsdup(ires->argv[1].val.str);
1212 if((fn = dc_findfnetnode(ires->argv[0].val.num)) != NULL)
1217 fn->id = ires->argv[0].val.num;
1218 if(fn->fnet != NULL)
1220 fn->fnet = swcsdup(ires->argv[1].val.str);
1221 fn->state = DC_FNN_STATE_SYN;
1225 if((fn = dc_findfnetnode(ires->argv[0].val.num)) != NULL)
1226 fn->numusers = ires->argv[1].val.num;
1229 transfer = newtransfer();
1230 transfer->id = ires->argv[0].val.num;
1231 transfer->dir = ires->argv[1].val.num;
1232 if(transfer->dir == DC_TRNSD_UP)
1233 transfer->state = DC_TRNS_HS;
1234 transfer->peerid = swcsdup(ires->argv[2].val.str);
1235 if(ires->argv[3].val.str[0])
1236 transfer->path = swcsdup(ires->argv[3].val.str);
1239 if((transfer = dc_findtransfer(ires->argv[0].val.num)) != NULL)
1240 transfer->state = ires->argv[1].val.num;
1243 if((transfer = dc_findtransfer(ires->argv[0].val.num)) != NULL)
1245 if(transfer->peernick != NULL)
1246 free(transfer->peernick);
1247 transfer->peernick = swcsdup(ires->argv[1].val.str);
1251 if((transfer = dc_findtransfer(ires->argv[0].val.num)) != NULL)
1252 transfer->size = ires->argv[1].val.num;
1255 if((transfer = dc_findtransfer(ires->argv[0].val.num)) != NULL)
1257 transfer->error = ires->argv[1].val.num;
1258 time(&transfer->errortime);
1262 if((transfer = dc_findtransfer(ires->argv[0].val.num)) != NULL)
1263 transfer->curpos = ires->argv[1].val.num;
1266 if((transfer = dc_findtransfer(ires->argv[0].val.num)) != NULL)
1268 if(transfer->path != NULL)
1269 free(transfer->path);
1270 transfer->path = swcsdup(ires->argv[1].val.str);
1274 if((transfer = dc_findtransfer(ires->argv[0].val.num)) != NULL)
1275 freetransfer(transfer);
1278 if((transfer = dc_findtransfer(ires->argv[0].val.num)) != NULL)
1280 if(transfer->hash != NULL)
1282 free(transfer->hash);
1283 transfer->hash = NULL;
1285 if(wcslen(ires->argv[1].val.str) > 0)
1286 transfer->hash = swcsdup(ires->argv[1].val.str);
1290 if((fn = dc_findfnetnode(ires->argv[0].val.num)) != NULL)
1292 if((peer = dc_fnetfindpeer(fn, ires->argv[1].val.str)) == NULL)
1294 peer = addpeer(fn, ires->argv[1].val.str, ires->argv[2].val.str);
1295 if(fn->newpeercb != NULL)
1296 fn->newpeercb(peer);
1301 if((fn = dc_findfnetnode(ires->argv[0].val.num)) != NULL)
1303 if((peer = dc_fnetfindpeer(fn, ires->argv[1].val.str)) != NULL)
1305 if(fn->delpeercb != NULL)
1306 fn->delpeercb(peer);
1312 if((fn = dc_findfnetnode(ires->argv[0].val.num)) != NULL)
1314 if((peer = dc_fnetfindpeer(fn, ires->argv[1].val.str)) != NULL)
1316 if(wcscmp(ires->argv[2].val.str, peer->nick))
1319 peer->nick = swcsdup(ires->argv[2].val.str);
1321 for(i = 4; i < resp->rlines[0].argc; i += 3)
1323 switch(wcstol(resp->rlines[0].argv[i + 1], NULL, 10))
1326 peersetnum(peer, resp->rlines[0].argv[i], wcstol(resp->rlines[0].argv[i + 2], NULL, 10));
1329 peersetlnum(peer, resp->rlines[0].argv[i], wcstoll(resp->rlines[0].argv[i + 2], NULL, 10));
1332 peersetstr(peer, resp->rlines[0].argv[i], resp->rlines[0].argv[i + 2]);
1336 if(fn->chpeercb != NULL)
1348 /* Note the backspace handling - it's not as elegant as possible, but
1349 * it helps avoid the "box-of-toothpicks" syndrome when writing search
1350 * expressions manually. */
1351 wchar_t **dc_lexsexpr(wchar_t *sexpr)
1355 size_t retsize, retdata, bufsize, bufdata;
1360 retsize = retdata = bufsize = bufdata = 0;
1362 while(*sexpr != L'\0')
1367 if(!iswspace(*sexpr))
1373 if(iswspace(*sexpr))
1377 addtobuf(buf, L'\0');
1380 bufsize = bufdata = 0;
1383 } else if((*sexpr == L'(') ||
1390 addtobuf(buf, L'\0');
1393 bufsize = bufdata = 0;
1395 addtobuf(buf, *sexpr);
1396 addtobuf(buf, L'\0');
1399 bufsize = bufdata = 0;
1401 } else if(*sexpr == L'\"') {
1404 } else if(*sexpr == L'\\') {
1408 addtobuf(buf, *sexpr);
1409 } else if((*sexpr == L'\\') || (*sexpr == L'\"')) {
1410 addtobuf(buf, *sexpr);
1413 addtobuf(buf, L'\\');
1414 addtobuf(buf, *sexpr);
1418 addtobuf(buf, *(sexpr++));
1427 addtobuf(buf, *sexpr);
1428 } else if((*sexpr == L'\\') || (*sexpr == L'\"')) {
1429 addtobuf(buf, *sexpr);
1432 addtobuf(buf, L'\\');
1433 addtobuf(buf, *sexpr);
1436 } else if(*sexpr == L'\"') {
1440 addtobuf(buf, *(sexpr++));
1447 addtobuf(buf, L'\0');
1450 addtobuf(ret, NULL);
1454 void dc_freewcsarr(wchar_t **arr)
1460 for(buf = arr; *buf != NULL; buf++)