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