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