irix fixen
[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 extern struct CtdlServInfo serv_info;
36 extern char temp[];
37 void citedit(FILE *fp, long int base_pos);
38 void getline(char *, int);
39
40 void chatmode(void) {
41         char wbuf[256];
42         char buf[256];
43         char c_user[256];
44         char c_text[256];
45         char c_room[256];
46         char last_user[256];
47         int send_complete_line;
48         int recv_complete_line;
49         char ch;
50         int a,pos;
51         
52         fd_set rfds;
53         struct timeval tv;
54         int retval;
55
56         serv_puts("CHAT");
57         serv_gets(buf);
58         if (buf[0]!='8') {
59                 printf("%s\n",&buf[4]);
60                 return;
61                 }
62
63         printf("Entering chat mode (type /quit to exit, /help for other cmds)\n");
64         set_keepalives(KA_CHAT);
65
66         strcpy(buf,"");
67         strcpy(wbuf,"");
68         color(3);
69         printf("> ");
70         send_complete_line = 0;
71         recv_complete_line = 0;
72
73         while(1) {
74             fflush(stdout);
75             FD_ZERO(&rfds);
76             FD_SET(0,&rfds);
77             FD_SET(getsockfd(),&rfds);
78             tv.tv_sec = S_KEEPALIVE;
79             tv.tv_usec = 0;
80             retval = select(getsockfd()+1, &rfds, NULL, NULL, &tv);
81
82             if (FD_ISSET(getsockfd(), &rfds)) {
83                 ch = serv_getc();
84                 if (ch == 10) {
85                         recv_complete_line = 1;
86                         goto RCL; /* ugly, but we've gotta get out! */
87                         }
88                 else {
89                         buf[strlen(buf) + 1] = 0;
90                         buf[strlen(buf)] = ch;
91                         }
92                 goto RCL;
93                 }
94
95             if (FD_ISSET(0, &rfds)) {
96                 ch = inkey();
97                 if ((ch == 10) || (ch == 13)) {
98                         send_complete_line = 1;
99                         }
100                 else if ((ch == 8) || (ch == 127)) {
101                         if (strlen(wbuf) > 0) {
102                                 wbuf[strlen(wbuf)-1] = 0;
103                                 printf("%c %c",8,8);
104                                 }
105                         }
106                 else {
107                         putc(ch,stdout);
108                         wbuf[strlen(wbuf) + 1] = 0;
109                         wbuf[strlen(wbuf)] = ch;
110                         }
111                 }
112
113
114         /* if the user hit return, send the line */
115 RCL:        if (send_complete_line) {
116                 serv_puts(wbuf);
117                 strcpy(wbuf,"");
118                 send_complete_line = 0;
119                 }
120
121         /* if it's time to word wrap, send a partial line */
122             if ( strlen(wbuf) >= (77-strlen(fullname)) ) {
123                 pos = 0;
124                 for (a=0; a<strlen(wbuf); ++a) {
125                         if (wbuf[a] == 32) pos = a;
126                         }
127                 if (pos == 0) {
128                         serv_puts(wbuf);
129                         strcpy(wbuf, "");
130                         send_complete_line = 0;
131                         }
132                 else {
133                         wbuf[pos] = 0;
134                         serv_puts(wbuf);
135                         strcpy(wbuf,&wbuf[pos+1]);
136                         }
137                 }
138
139             if (recv_complete_line) {   
140                 printf("\r%79s\r","");
141                 if (!strcmp(buf,"000")) {
142                         color(7);
143                         printf("Exiting chat mode\n");
144
145                         fflush(stdout);
146                         set_keepalives(KA_YES);
147                         return;
148                         }
149                 if (num_parms(buf)>=2) {
150                         extract(c_user,buf,0);
151                         extract(c_text,buf,1);
152                         if (num_parms(buf)>2)
153                         {
154                            extract(c_room,buf,2);
155                            printf("Got room %s\n", c_room);
156                         }
157                            
158                         if (strucmp(c_text,"NOOP")) {
159                                 if (!strcmp(c_user, fullname)) {
160                                         color(3);
161                                         }
162                                 else if (!strcmp(c_user,":")) {
163                                         color(1);
164                                         }
165                                 else {
166                                         color(2);
167                                         }
168                                 if (strcmp(c_user,last_user)) {
169                                         snprintf(buf,sizeof buf,"%s: %s",c_user,c_text);
170                                         }
171                                 else {
172                                         size_t i = MIN(sizeof buf - 1,
173                                                        strlen(c_user) + 2);
174
175                                         memset(buf, ' ', i);
176                                         safestrncpy(&buf[i], c_text,
177                                                     sizeof buf - i);
178                                         }
179                                 while (strlen(buf)<79) strcat(buf," ");
180                                 if (strcmp(c_user,last_user)) {
181                                         printf("\r%79s\n","");
182                                         strcpy(last_user,c_user);
183                                         }
184                                 printf("\r%s\n",buf);
185                                 fflush(stdout);
186                                 }
187                         }
188                 color(3);
189                 printf("> %s",wbuf);
190                 recv_complete_line = 0;
191                 strcpy(buf,"");
192                 }
193             }
194         }
195
196 /*
197  * send an express message
198  */
199 void page_user() {
200         static char last_paged[32] = "";
201         char buf[256], touser[256], msg[256];
202         char longmsg[5][80];
203         int i;
204
205         strcpy(touser, last_paged);
206         strprompt("Page who", touser, 30);
207
208         /* old server -- use inline paging */
209         if (serv_info.serv_paging_level == 0) {
210                 newprompt("Message:  ",msg,69);
211                 snprintf(buf,sizeof buf,"SEXP %s|%s",touser,msg);
212                 serv_puts(buf);
213                 serv_gets(buf);
214                 if (!strncmp(buf, "200", 3)) {
215                         strcpy(last_paged, touser);
216                         }
217                 printf("%s\n", &buf[4]);
218                 return;
219         }
220
221         /* new server -- use extended paging */ 
222         else if (serv_info.serv_paging_level >= 1) {
223                 snprintf(buf, sizeof buf, "SEXP %s||", touser);
224                 serv_puts(buf);
225                 serv_gets(buf);
226                 if (buf[0] != '2') {
227                         printf("%s\n", &buf[4]);
228                         return;
229                         }
230
231                 printf("Type message to send.  Enter a blank line when finished.\n");
232                 memset(longmsg, 0, sizeof longmsg);
233                 for (i=0; i<5; ++i) {
234                         printf("> ");
235                         getline(&longmsg[i][0], 77);
236                         if (strlen(&longmsg[i][0])==0) i=5;
237                         }
238                 snprintf(buf, sizeof buf, "SEXP %s|-", touser);
239                 serv_puts(buf);
240                 serv_gets(buf);
241                 if (buf[0]=='4') {
242                         strcpy(last_paged, touser);
243                         for (i=0; i<5; ++i)
244                                 if (strlen(&longmsg[i][0])>0)
245                                         serv_puts(&longmsg[i][0]);
246                         serv_puts("000");
247                         printf("Message sent.\n");
248                 }
249                 else {
250                         printf("%s\n", &buf[4]);
251                 }
252         }
253 }
254
255
256