6316a138e90c5857df29608b7932daf68cf9ed88
[citadel.git] / citadel / client_chat.c
1 /*
2  * Citadel/UX
3  *
4  * client_chat.c  --  front end for chat mode
5  *                    (the "single process" version - no more fork() anymore)
6  *
7  */
8
9 #include "sysdep.h"
10 #include <stdlib.h>
11 #include <unistd.h>
12 #include <stdio.h>
13 #include <fcntl.h>
14 #include <ctype.h>
15 #include <string.h>
16 #include <signal.h>
17 #include <errno.h>
18 #include <sys/time.h>
19 #include <sys/types.h>
20 #ifdef NEED_SELECT_H
21 #include <sys/select.h>
22 #endif
23 #include "citadel.h"
24
25 extern char fullname[];
26
27 int inkey();
28 void set_keepalives();
29 int num_parms();
30 void extract();
31 int struncmp();
32 int getsockfd();
33 char serv_getc();
34 void color();
35
36
37 void chatmode() {
38         char wbuf[256];
39         char buf[256];
40         char c_user[256];
41         char c_text[256];
42         char c_room[256];
43         char last_user[256];
44         int send_complete_line;
45         int recv_complete_line;
46         char ch;
47         int a,pos;
48         
49         fd_set rfds;
50         struct timeval tv;
51         int retval;
52
53         serv_puts("CHAT");
54         serv_gets(buf);
55         if (buf[0]!='8') {
56                 printf("%s\n",&buf[4]);
57                 return;
58                 }
59
60         printf("Entering chat mode (type /quit to exit, /help for other cmds)\n");
61         set_keepalives(KA_CHAT);
62
63         strcpy(buf,"");
64         strcpy(wbuf,"");
65         color(3);
66         printf("> ");
67         send_complete_line = 0;
68         recv_complete_line = 0;
69
70         while(1) {
71             fflush(stdout);
72             FD_ZERO(&rfds);
73             FD_SET(0,&rfds);
74             FD_SET(getsockfd(),&rfds);
75             tv.tv_sec = S_KEEPALIVE;
76             tv.tv_usec = 0;
77             retval = select(getsockfd()+1, &rfds, NULL, NULL, &tv);
78
79             if (FD_ISSET(getsockfd(), &rfds)) {
80                 ch = serv_getc();
81                 if (ch == 10) {
82                         recv_complete_line = 1;
83                         goto RCL; /* ugly, but we've gotta get out! */
84                         }
85                 else {
86                         buf[strlen(buf) + 1] = 0;
87                         buf[strlen(buf)] = ch;
88                         }
89                 goto RCL;
90                 }
91
92             if (FD_ISSET(0, &rfds)) {
93                 ch = inkey();
94                 if ((ch == 10) || (ch == 13)) {
95                         send_complete_line = 1;
96                         }
97                 else if ((ch == 8) || (ch == 127)) {
98                         if (strlen(wbuf) > 0) {
99                                 wbuf[strlen(wbuf)-1] = 0;
100                                 printf("%c %c",8,8);
101                                 }
102                         }
103                 else {
104                         putc(ch,stdout);
105                         wbuf[strlen(wbuf) + 1] = 0;
106                         wbuf[strlen(wbuf)] = ch;
107                         }
108                 }
109
110
111         /* if the user hit return, send the line */
112 RCL:        if (send_complete_line) {
113                 serv_puts(wbuf);
114                 strcpy(wbuf,"");
115                 send_complete_line = 0;
116                 }
117
118         /* if it's time to word wrap, send a partial line */
119             if ( strlen(wbuf) >= (77-strlen(fullname)) ) {
120                 pos = 0;
121                 for (a=0; a<strlen(wbuf); ++a) {
122                         if (wbuf[a] == 32) pos = a;
123                         }
124                 if (pos == 0) {
125                         serv_puts(wbuf);
126                         strcpy(wbuf, "");
127                         send_complete_line = 0;
128                         }
129                 else {
130                         wbuf[pos] = 0;
131                         serv_puts(wbuf);
132                         strcpy(wbuf,&wbuf[pos+1]);
133                         }
134                 }
135
136             if (recv_complete_line) {   
137                 printf("\r%79s\r","");
138                 if (!strcmp(buf,"000")) {
139                         color(7);
140                         printf("Exiting chat mode\n");
141
142                         fflush(stdout);
143                         set_keepalives(KA_YES);
144                         return;
145                         }
146                 if (num_parms(buf)>=2) {
147                         extract(c_user,buf,0);
148                         extract(c_text,buf,1);
149                         if (num_parms(buf)>2)
150                         {
151                            extract(c_room,buf,2);
152                            printf("Got room %s\n", c_room);
153                         }
154                            
155                         if (strucmp(c_text,"NOOP")) {
156                                 if (!strcmp(c_user, fullname)) {
157                                         color(3);
158                                         }
159                                 else if (!strcmp(c_user,":")) {
160                                         color(1);
161                                         }
162                                 else {
163                                         color(2);
164                                         }
165                                 if (strcmp(c_user,last_user)) {
166                                         sprintf(buf,"%s: %s",c_user,c_text);
167                                         }
168                                 else {
169                                         sprintf(buf,"%40s","");
170                                         sprintf(&buf[strlen(c_user)+2],
171                                                 "%s",c_text);
172                                         }
173                                 while (strlen(buf)<79) strcat(buf," ");
174                                 if (strcmp(c_user,last_user)) {
175                                         printf("\r%79s\n","");
176                                         strcpy(last_user,c_user);
177                                         }
178                                 printf("\r%s\n",buf);
179                                 fflush(stdout);
180                                 }
181                         }
182                 color(3);
183                 printf("> %s",wbuf);
184                 recv_complete_line = 0;
185                 strcpy(buf,"");
186                 }
187             }
188         }
189
190