2 * Dolda Connect - Modular multiuser Direct Connect-style client
3 * Copyright (C) 2007 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
33 #include <doldaconnect/uilib.h>
34 #include <doldaconnect/uimisc.h>
35 #include <doldaconnect/utils.h>
43 int (*handler)(int argc, wchar_t **argv);
46 void wcslimit(wchar_t *wcs, int limit) {
49 if(wcslen(wcs) > limit) {
50 for(i = limit - 3; i < limit; i++)
56 void wcslimitr(wchar_t *wcs, int limit) {
59 if((i = wcslen(wcs)) > limit) {
60 memmove(wcs + 3, wcs + (i - limit + 3), sizeof(wchar_t) * (limit - 2));
61 for(i = 0; i < 3; i++)
66 int cmd_lsnodes(int argc, wchar_t **argv)
68 struct dc_response *resp;
69 struct dc_intresp *ires;
71 resp = dc_gettaggedrespsync(dc_queuecmd(NULL, NULL, L"lsnodes", NULL));
72 if(resp->code == 200) {
74 printf("ID NET USERS S NAME\n");
75 printf("----- ---- ------ - ----------------------------------------------------------\n");
77 while((ires = dc_interpret(resp)) != NULL) {
79 wcslimit(ires->argv[2].val.str, 58);
80 printf("%-5i %-4ls %-6i %c %ls\n", ires->argv[0].val.num, ires->argv[1].val.str, ires->argv[3].val.num, "SHED"[ires->argv[4].val.num], ires->argv[2].val.str);
83 } else if(resp->code == 201) {
85 fprintf(stderr, "dcsh: %ls\n", resp->rlines[0].argv[0]);
91 int cmd_lsdl(int argc, wchar_t **argv)
93 struct dc_response *resp;
94 struct dc_intresp *ires;
97 resp = dc_gettaggedrespsync(dc_queuecmd(NULL, NULL, L"lstrans", NULL));
98 if(resp->code == 200) {
100 printf("ID S USER PROGRESS FILE\n");
101 printf("------- - ---------- ------------- -------------------------------------------\n");
103 while((ires = dc_interpret(resp)) != NULL) {
104 if(ires->argv[1].val.num == DC_TRNSD_DOWN) {
105 file = ires->argv[5].val.str;
106 if((p = wcsrchr(file, L'/')) != NULL)
109 wcslimit(ires->argv[4].val.str, 10);
112 printf("%-7i %c %-10ls %'-13ji %ls\n", ires->argv[0].val.num, "SHED"[ires->argv[2].val.num], ires->argv[4].val.str, (intmax_t)ires->argv[7].val.lnum, file);
116 } else if(resp->code == 201) {
118 fprintf(stderr, "dcsh: %ls\n", resp->rlines[0].argv[0]);
124 struct cmd commands[] = {
125 {L"hubs", cmd_lsnodes},
130 int run1(int argc, wchar_t **argv)
134 for(c = commands; c->handler != NULL; c++) {
135 if(!wcscmp(argv[0], c->name))
136 return(c->handler(argc, argv));
138 fprintf(stderr, "dcsh: no such command\n");
142 int runchar(int argc, char **argv) {
144 wchar_t **wargv, *buf;
145 size_t wargvsize, wargvdata;
148 wargvsize = wargvdata = 0;
149 for(i = 0; i < argc; i++) {
150 if((buf = icmbstowcs(argv[i], NULL)) == NULL) {
151 fprintf(stderr, "dcsh: could not convert %s to wide char: %s\n", argv[i], strerror(errno));
152 for(i = 0; i < wargvdata; i++)
158 addtobuf(wargv, buf);
160 addtobuf(wargv, NULL);
161 rv = run1(wargvdata - 1, wargv);
162 dc_freewcsarr(wargv);
169 wchar_t *buf, **argv;
170 char *p, cmdbuf[128];
172 struct pollfd pfd[2];
175 fprintf(stderr, "dcsh> ");
181 pfd[0].events = POLLIN;
183 pfd[0].events |= POLLOUT;
185 pfd[1].events = POLLIN;
187 if(poll(pfd, 2, -1) < 0) {
189 perror("dcsh: poll");
193 if(((pfd[0].revents & POLLIN) && dc_handleread()) || ((pfd[0].revents & POLLOUT) && dc_handlewrite()))
196 ret = read(0, cmdbuf + cmddata, sizeof(cmdbuf) - cmddata);
198 fprintf(stderr, "dcsh: stdin: %s\n", strerror(errno));
200 } else if(ret == 0) {
202 fprintf(stderr, "\n");
206 while((p = memchr(cmdbuf, '\n', cmddata)) != NULL) {
208 if((buf = icmbstowcs(cmdbuf, NULL)) == NULL) {
209 fprintf(stderr, "dcsh: could not convert command to wide chars: %s\n", strerror(errno));
211 argv = dc_lexsexpr(buf);
213 for(argc = 0; argv[argc] != NULL; argc++);
218 memmove(cmdbuf, p, cmddata -= (p - cmdbuf));
220 fprintf(stderr, "dcsh> ");
228 int main(int argc, char **argv)
233 int authless, authia;
235 setlocale(LC_ALL, "");
236 server = username = NULL;
237 authless = authia = 1;
238 while((c = getopt(argc, argv, "hvIAs:u:")) != -1) {
256 fprintf(stderr, "usage: dcsh [-AIhv] [-s SERVER] [-u USERNAME] [COMMAND [ARGS...]]\n");
257 exit((c == 'h')?0:1);
263 fprintf(stderr, "connecting...\n");
264 if((dcfd = dc_connectsync2(server, 1)) < 0) {
265 perror("dcsh: could not connect to server");
269 fprintf(stderr, "authenticating...\n");
270 if(dc_login(username, authless, authia?dc_convtty:dc_convnone, NULL) != DC_LOGIN_ERR_SUCCESS) {
271 fprintf(stderr, "dcsh: authentication unsuccessful\n");
275 fprintf(stderr, "done\n");
277 interactive = isatty(1);
278 rv = runchar(argc - optind, argv + optind);
280 interactive = isatty(0);