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