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