91ef66187090d561cfb9719d536a8df754ad36dc
[doldaconnect.git] / clients / tty / dcsh.c
1 /*
2  *  Dolda Connect - Modular multiuser Direct Connect-style client
3  *  Copyright (C) 2007 Fredrik Tolf (fredrik@dolda2000.com)
4  *  
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.
9  *  
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.
14  *  
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
18 */
19
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <unistd.h>
23 #include <wchar.h>
24 #include <sys/poll.h>
25 #include <errno.h>
26
27 #ifdef HAVE_CONFIG_H
28 #include <config.h>
29 #endif
30 #include <doldaconnect/uilib.h>
31 #include <doldaconnect/uimisc.h>
32 #include <doldaconnect/utils.h>
33
34 int verbose = 0;
35 int dcfd;
36
37 int main(int argc, char **argv)
38 {
39     int c;
40     char *server;
41     char *username;
42     int authless, authia;
43     struct pollfd pfd[2];
44     int done;
45     char cmdbuf[128];
46     int cmddata;
47     
48     server = username = NULL;
49     authless = authia = 1;
50     while((c = getopt(argc, argv, "hvIAs:u:")) != -1) {
51         switch(c) {
52         case 's':
53             server = optarg;
54             break;
55         case 'u':
56             username = optarg;
57             break;
58         case 'I':
59             authia = 0;
60             break;
61         case 'A':
62             authless = 0;
63             break;
64         case 'v':
65             verbose++;
66             break;
67         default:
68             fprintf(stderr, "usage: dcsh [-AIhv] [-s SERVER] [-u USERNAME]\n");
69             exit((c == 'h')?0:1);
70         }
71     }
72     dc_init();
73     
74     if(verbose)
75         fprintf(stderr, "connecting...\n");
76     if((dcfd = dc_connectsync2(server, 1)) < 0) {
77         perror("dcsh: could not connect to server");
78         exit(1);
79     }
80     if(verbose)
81         fprintf(stderr, "authenticating...\n");
82     if(dc_login(username, authless, authia?dc_convtty:dc_convnone, NULL) != DC_LOGIN_ERR_SUCCESS) {
83         fprintf(stderr, "dcsh: authentication unsuccessful\n");
84         exit(1);
85     }
86     if(verbose)
87         fprintf(stderr, "done\n");
88     done = 0;
89     cmddata = 0;
90     while(!done) {
91         pfd[0].fd = dcfd;
92         pfd[0].events = POLLIN;
93         if(dc_wantwrite())
94             pfd[0].events |= POLLOUT;
95         pfd[1].fd = 0;
96         pfd[1].events = POLLIN;
97         pfd[1].revents = 0;
98         if(poll(pfd, (cmddata < sizeof(cmdbuf)) ? 2 : 1, -1) < 0) {
99             if(errno != EINTR) {
100                 perror("dcsh: poll");
101                 exit(1);
102             }
103         }
104         
105     }
106     return(0);
107 }