]> code.citadel.org Git - citadel.git/blob - citadel/serv_chat.c
irix fixen
[citadel.git] / citadel / serv_chat.c
1 /*
2  * serv_chat.c
3  * 
4  * This module handles all "real time" communication between users.  The
5  * modes of communication currently supported are Chat and Paging.
6  *
7  * $Id$
8  */
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <stdio.h>
12 #include <fcntl.h>
13 #include <signal.h>
14 #include <pwd.h>
15 #include <errno.h>
16 #include <sys/types.h>
17 #include <sys/time.h>
18 #include <sys/wait.h>
19 #include <string.h>
20 #include <limits.h>
21 #include <pthread.h>
22 #include "citadel.h"
23 #include "server.h"
24 #include <syslog.h>
25 #ifdef HAVE_SYS_SELECT_H
26 #include <sys/select.h>
27 #endif
28 #include "serv_chat.h"
29 #include "sysdep_decls.h"
30 #include "citserver.h"
31 #include "support.h"
32 #include "config.h"
33 #include "dynloader.h"
34 #include "tools.h"
35 #include "msgbase.h"
36
37 struct ChatLine *ChatQueue = NULL;
38 int ChatLastMsg = 0;
39
40 extern struct CitContext *ContextList;
41
42 #define MODULE_NAME     "Chat module"
43 #define MODULE_AUTHOR   "Art Cancro"
44 #define MODULE_EMAIL    "ajc@uncnsrd.mt-kisco.ny.us"
45 #define MAJOR_VERSION   1
46 #define MINOR_VERSION   0
47
48 static struct DLModule_Info info =
49 {
50         MODULE_NAME,
51         MODULE_AUTHOR,
52         MODULE_EMAIL,
53         MAJOR_VERSION,
54         MINOR_VERSION
55 };
56
57 struct DLModule_Info *Dynamic_Module_Init(void)
58 {
59         CtdlRegisterProtoHook(cmd_chat, "CHAT", "Begin real-time chat");
60         CtdlRegisterProtoHook(cmd_pexp, "PEXP", "Poll for express messages");
61         CtdlRegisterProtoHook(cmd_sexp, "SEXP", "Send an express message");
62         return &info;
63 }
64
65 void allwrite(char *cmdbuf, int flag, char *roomname, char *username)
66 {
67         FILE *fp;
68         char bcast[256];
69         char *un;
70         struct ChatLine *clptr, *clnew;
71         time_t now;
72
73         if (CC->fake_username[0])
74                 un = CC->fake_username;
75         else
76                 un = CC->usersupp.fullname;
77         if (flag == 1) {
78                 sprintf(bcast, ":|<%s %s>", un, cmdbuf);
79         } else if (flag == 0) {
80                 sprintf(bcast, "%s|%s", un, cmdbuf);
81         } else if (flag == 2) {
82                 sprintf(bcast, ":|<%s whispers %s>", un, cmdbuf);
83         }
84         if ((strcasecmp(cmdbuf, "NOOP")) && (flag != 2)) {
85                 fp = fopen(CHATLOG, "a");
86                 fprintf(fp, "%s\n", bcast);
87                 fclose(fp);
88         }
89         clnew = (struct ChatLine *) mallok(sizeof(struct ChatLine));
90         memset(clnew, 0, sizeof(struct ChatLine));
91         if (clnew == NULL) {
92                 fprintf(stderr, "citserver: cannot alloc chat line: %s\n",
93                         strerror(errno));
94                 return;
95         }
96         time(&now);
97         clnew->next = NULL;
98         clnew->chat_time = now;
99         strncpy(clnew->chat_room, roomname, sizeof clnew->chat_room);
100         clnew->chat_room[sizeof clnew->chat_room - 1] = 0;
101         if (username) {
102                 strncpy(clnew->chat_username, username,
103                         sizeof clnew->chat_username);
104                 clnew->chat_username[sizeof clnew->chat_username - 1] = 0;
105         } else
106                 clnew->chat_username[0] = '\0';
107         strcpy(clnew->chat_text, bcast);
108
109         /* Here's the critical section.
110          * First, add the new message to the queue...
111          */
112         begin_critical_section(S_CHATQUEUE);
113         ++ChatLastMsg;
114         clnew->chat_seq = ChatLastMsg;
115         if (ChatQueue == NULL) {
116                 ChatQueue = clnew;
117         } else {
118                 for (clptr = ChatQueue; clptr->next != NULL; clptr = clptr->next);;
119                 clptr->next = clnew;
120         }
121
122         /* Then, before releasing the lock, free the expired messages */
123         while (1) {
124                 if (ChatQueue == NULL)
125                         goto DONE_FREEING;
126                 if ((now - ChatQueue->chat_time) < 120L)
127                         goto DONE_FREEING;
128                 clptr = ChatQueue;
129                 ChatQueue = ChatQueue->next;
130                 phree(clptr);
131         }
132       DONE_FREEING:end_critical_section(S_CHATQUEUE);
133 }
134
135
136 t_context *find_context(char **unstr)
137 {
138         t_context *t_cc, *found_cc = NULL;
139         char *name, *tptr;
140
141         if ((!*unstr) || (!unstr))
142                 return (NULL);
143
144         begin_critical_section(S_SESSION_TABLE);
145         for (t_cc = ContextList; ((t_cc) && (!found_cc)); t_cc = t_cc->next) {
146                 if (t_cc->fake_username[0])
147                         name = t_cc->fake_username;
148                 else
149                         name = t_cc->curr_user;
150                 tptr = *unstr;
151                 if ((!strncasecmp(name, tptr, strlen(name))) && (tptr[strlen(name)] == ' ')) {
152                         found_cc = t_cc;
153                         *unstr = &(tptr[strlen(name) + 1]);
154                 }
155         }
156         end_critical_section(S_SESSION_TABLE);
157
158         return (found_cc);
159 }
160
161 /*
162  * List users in chat.  Setting allflag to 1 also lists users elsewhere.
163  */
164
165 void do_chat_listing(int allflag)
166 {
167         struct CitContext *ccptr;
168
169         cprintf(":|\n:| Users currently in chat:\n");
170         begin_critical_section(S_SESSION_TABLE);
171         for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
172                 if ((!strcasecmp(ccptr->cs_room, "<chat>"))
173                     && ((ccptr->cs_flags & CS_STEALTH) == 0)) {
174                         cprintf(":| %-25s <%s>\n", (ccptr->fake_username[0]) ? ccptr->fake_username : ccptr->curr_user, ccptr->chat_room);
175                 }
176         }
177
178         if (allflag == 1) {
179                 cprintf(":|\n:| Users not in chat:\n");
180                 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
181                         if ((strcasecmp(ccptr->cs_room, "<chat>"))
182                             && ((ccptr->cs_flags & CS_STEALTH) == 0)) {
183                                 cprintf(":| %-25s <%s>:\n", (ccptr->fake_username[0]) ? ccptr->fake_username : ccptr->curr_user, (ccptr->fake_roomname[0]) ? ccptr->fake_roomname : ccptr->cs_room);
184                         }
185                 }
186         }
187         end_critical_section(S_SESSION_TABLE);
188         cprintf(":|\n");
189 }
190
191
192 void cmd_chat(char *argbuf)
193 {
194         char cmdbuf[256];
195         char *un;
196         char *strptr1;
197         char hold_cs_room[ROOMNAMELEN];
198         int MyLastMsg, ThisLastMsg;
199         struct ChatLine *clptr;
200         struct CitContext *t_context;
201         int retval;
202
203         if (!(CC->logged_in)) {
204                 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
205                 return;
206         }
207         strcpy(CC->chat_room, "Main room");
208
209         strcpy(hold_cs_room, CC->cs_room);
210         CC->cs_flags = CC->cs_flags | CS_CHAT;
211         set_wtmpsupp("<chat>");
212         cprintf("%d Entering chat mode (type '/help' for available commands)\n",
213                 START_CHAT_MODE);
214
215         MyLastMsg = ChatLastMsg;
216
217         if ((CC->cs_flags & CS_STEALTH) == 0) {
218                 allwrite("<entering chat>", 0, CC->chat_room, NULL);
219         }
220         strcpy(cmdbuf, "");
221
222         while (1) {
223                 int ok_cmd;
224
225                 ok_cmd = 0;
226                 cmdbuf[strlen(cmdbuf) + 1] = 0;
227                 retval = client_read_to(&cmdbuf[strlen(cmdbuf)], 1, 2);
228
229                 /* if we have a complete line, do send processing */
230                 if (strlen(cmdbuf) > 0)
231                         if (cmdbuf[strlen(cmdbuf) - 1] == 10) {
232                                 cmdbuf[strlen(cmdbuf) - 1] = 0;
233                                 time(&CC->lastcmd);
234                                 time(&CC->lastidle);
235
236                                 if ((!strcasecmp(cmdbuf, "exit"))
237                                     || (!strcasecmp(cmdbuf, "/exit"))
238                                     || (!strcasecmp(cmdbuf, "quit"))
239                                     || (!strcasecmp(cmdbuf, "logout"))
240                                     || (!strcasecmp(cmdbuf, "logoff"))
241                                     || (!strcasecmp(cmdbuf, "/q"))
242                                     || (!strcasecmp(cmdbuf, ".q"))
243                                     || (!strcasecmp(cmdbuf, "/quit"))
244                                     )
245                                         strcpy(cmdbuf, "000");
246
247                                 if (!strcmp(cmdbuf, "000")) {
248                                         if ((CC->cs_flags & CS_STEALTH) == 0) {
249                                                 allwrite("<exiting chat>", 0, CC->chat_room, NULL);
250                                         }
251                                         sleep(1);
252                                         cprintf("000\n");
253                                         CC->cs_flags = CC->cs_flags - CS_CHAT;
254                                         set_wtmpsupp(hold_cs_room);
255                                         return;
256                                 }
257                                 if ((!strcasecmp(cmdbuf, "/help"))
258                                     || (!strcasecmp(cmdbuf, "help"))
259                                     || (!strcasecmp(cmdbuf, "/?"))
260                                     || (!strcasecmp(cmdbuf, "?"))) {
261                                         cprintf(":|\n");
262                                         cprintf(":|Available commands: \n");
263                                         cprintf(":|/help   (prints this message) \n");
264                                         cprintf(":|/who    (list users currently in chat) \n");
265                                         cprintf(":|/whobbs (list users in chat -and- elsewhere) \n");
266                                         cprintf(":|/me     ('action' line, ala irc) \n");
267                                         cprintf(":|/msg    (send private message, ala irc) \n");
268                                         cprintf(":|/join   (join new room) \n");
269                                         cprintf(":|/quit   (return to the BBS) \n");
270                                         cprintf(":|\n");
271                                         ok_cmd = 1;
272                                 }
273                                 if (!strcasecmp(cmdbuf, "/who")) {
274                                         do_chat_listing(0);
275                                         ok_cmd = 1;
276                                 }
277                                 if (!strcasecmp(cmdbuf, "/whobbs")) {
278                                         do_chat_listing(1);
279                                         ok_cmd = 1;
280                                 }
281                                 if (!strncasecmp(cmdbuf, "/me ", 4)) {
282                                         allwrite(&cmdbuf[4], 1, CC->chat_room, NULL);
283                                         ok_cmd = 1;
284                                 }
285                                 if (!strncasecmp(cmdbuf, "/msg ", 5)) {
286                                         ok_cmd = 1;
287                                         strptr1 = &cmdbuf[5];
288                                         if ((t_context = find_context(&strptr1))) {
289                                                 allwrite(strptr1, 2, "", CC->curr_user);
290                                                 if (strcasecmp(CC->curr_user, t_context->curr_user))
291                                                         allwrite(strptr1, 2, "", t_context->curr_user);
292                                         } else
293                                                 cprintf(":|User not found.\n", cmdbuf);
294                                         cprintf("\n");
295                                 }
296                                 if (!strncasecmp(cmdbuf, "/join ", 6)) {
297                                         ok_cmd = 1;
298                                         allwrite("<changing rooms>", 0, CC->chat_room, NULL);
299                                         if (!cmdbuf[6])
300                                                 strcpy(CC->chat_room, "Main room");
301                                         else {
302                                                 strncpy(CC->chat_room, &cmdbuf[6],
303                                                    sizeof CC->chat_room);
304                                                 CC->chat_room[sizeof CC->chat_room - 1] = 0;
305                                         }
306                                         allwrite("<joining room>", 0, CC->chat_room, NULL);
307                                         cprintf("\n");
308                                 }
309                                 if ((cmdbuf[0] != '/') && (strlen(cmdbuf) > 0)) {
310                                         ok_cmd = 1;
311                                         allwrite(cmdbuf, 0, CC->chat_room, NULL);
312                                 }
313                                 if ((!ok_cmd) && (cmdbuf[0]) && (cmdbuf[0] != '\n'))
314                                         cprintf(":|Command %s is not understood.\n", cmdbuf);
315
316                                 strcpy(cmdbuf, "");
317
318                         }
319                 /* now check the queue for new incoming stuff */
320
321                 if (CC->fake_username[0])
322                         un = CC->fake_username;
323                 else
324                         un = CC->curr_user;
325                 if (ChatLastMsg > MyLastMsg) {
326                         ThisLastMsg = ChatLastMsg;
327                         for (clptr = ChatQueue; clptr != NULL; clptr = clptr->next) {
328                                 if ((clptr->chat_seq > MyLastMsg) && ((!clptr->chat_username[0]) || (!strncasecmp(un, clptr->chat_username, 32)))) {
329                                         if ((!clptr->chat_room[0]) || (!strncasecmp(CC->chat_room, clptr->chat_room, ROOMNAMELEN))) {
330                                                 cprintf("%s\n", clptr->chat_text);
331                                         }
332                                 }
333                         }
334                         MyLastMsg = ThisLastMsg;
335                 }
336         }
337 }
338
339
340 /*
341  * poll for express messages
342  */
343 void cmd_pexp(char *argbuf)
344 {                               /* arg unused */
345         char *ptr;
346
347         if (CC->ExpressMessages == NULL) {
348                 cprintf("%d No express messages waiting.\n", ERROR);
349                 return;
350         }
351         begin_critical_section(S_SESSION_TABLE);
352         ptr = CC->ExpressMessages;
353         CC->ExpressMessages = NULL;
354         end_critical_section(S_SESSION_TABLE);
355
356         cprintf("%d Express msgs:\n%s", LISTING_FOLLOWS, ptr);
357         if (ptr[strlen(ptr)] != '\n')
358                 cprintf("\n");
359         cprintf("000\n");
360
361         phree(ptr);
362 }
363
364
365 /* 
366  * This is the back end to the express message sending function.  
367  * Returns the number of users to which the message was sent.
368  * Sending a zero-length message tests for recipients without sending messages.
369  */
370 int send_express_message(char *lun, char *x_user, char *x_msg)
371 {
372         int message_sent = 0;
373         struct CitContext *ccptr;
374         char *msgptr;
375         char *un;
376         int newlen;
377         FILE *fp;
378
379         /* find the target user's context and append the message */
380         begin_critical_section(S_SESSION_TABLE);
381         for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
382
383                 if (ccptr->fake_username[0])    /* <bc> */
384                         un = ccptr->fake_username;
385                 else
386                         un = ccptr->usersupp.fullname;
387
388                 if ((!strcasecmp(un, x_user))
389                     || (!strcasecmp(x_user, "broadcast"))) {
390                         if (strlen(x_msg) > 0) {
391                                 strcpy(ccptr->last_pager, CC->curr_user);
392                                 if (ccptr->ExpressMessages == NULL) {
393                                         newlen = strlen(x_msg) + 80;
394                                         msgptr = mallok(newlen);
395                                         memset(msgptr, 0, newlen);
396                                 } else {
397                                         newlen = (strlen(ccptr->ExpressMessages)
398                                                   + strlen(x_msg) + 80);
399                                         msgptr = reallok(
400                                          ccptr->ExpressMessages, newlen);
401                                 }
402                                 if (msgptr != NULL) {
403                                         sprintf(&msgptr[strlen(msgptr)],
404                                                 "%s from %s:\n %s\n",
405                                                 ((!strcasecmp(x_user, "broadcast")) ? "Broadcast message" : "Message"),
406                                                 lun, x_msg);
407                                         ccptr->ExpressMessages = msgptr;
408                                 }
409                         }
410                         ++message_sent;
411                 }
412         }
413         end_critical_section(S_SESSION_TABLE);
414
415         /* Log the page to disk if configured to do so */
416         if ((strlen(config.c_logpages) > 0) && (strlen(x_msg) > 0)) {
417                 fp = fopen(CC->temp, "wb");
418                 fprintf(fp, "%c%c%c", 255, MES_NORMAL, 0);
419                 fprintf(fp, "Psysop%c", 0);
420                 fprintf(fp, "T%ld%c", time(NULL), 0);
421                 fprintf(fp, "A%s%c", lun, 0);
422                 fprintf(fp, "R%s%c", x_user, 0);
423                 fprintf(fp, "O%s%c", config.c_logpages, 0);
424                 fprintf(fp, "N%s%c", NODENAME, 0);
425                 fprintf(fp, "M%s\n%c", x_msg, 0);
426                 fclose(fp);
427                 save_message(CC->temp, "", config.c_logpages, M_LOCAL, 1);
428                 unlink(CC->temp);
429         }
430         return (message_sent);
431 }
432
433 /*
434  * send express messages  <bc>
435  */
436 void cmd_sexp(char *argbuf)
437 {
438         int message_sent = 0;
439         char x_user[256];
440         char x_msg[256];
441         char *lun;              /* <bc> */
442         char *x_big_msgbuf = NULL;
443
444         if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
445                 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
446                 return;
447         }
448         if (CC->fake_username[0])
449                 lun = CC->fake_username;
450         else
451                 lun = CC->usersupp.fullname;
452
453         extract(x_user, argbuf, 0);
454
455         if (!strcmp(x_user, ".")) {
456                 strcpy(x_user, CC->last_pager);
457         }
458         extract(x_msg, argbuf, 1);
459
460         if (!x_user[0]) {
461                 cprintf("%d You were not previously paged.\n", ERROR);
462                 return;
463         }
464         if ((!strcasecmp(x_user, "broadcast")) && (CC->usersupp.axlevel < 6)) {
465                 cprintf("%d Higher access required to send a broadcast.\n",
466                         ERROR + HIGHER_ACCESS_REQUIRED);
467                 return;
468         }
469         /* This loop handles text-transfer pages */
470         if (!strcmp(x_msg, "-")) {
471                 message_sent = send_express_message(lun, x_user, "");
472                 if (message_sent == 0) {
473                         cprintf("%d No user '%s' logged in.\n", ERROR, x_user);
474                         return;
475                 }
476                 cprintf("%d Transmit message (will deliver to %d users)\n",
477                         SEND_LISTING, message_sent);
478                 x_big_msgbuf = mallok(256);
479                 memset(x_big_msgbuf, 0, 256);
480                 while (client_gets(x_msg), strcmp(x_msg, "000")) {
481                         x_big_msgbuf = reallok(x_big_msgbuf,
482                                strlen(x_big_msgbuf) + strlen(x_msg) + 4);
483                         if (strlen(x_big_msgbuf) > 0)
484                            if (x_big_msgbuf[strlen(x_big_msgbuf)] != '\n')
485                                 strcat(x_big_msgbuf, "\n");
486                         strcat(x_big_msgbuf, x_msg);
487                 }
488                 send_express_message(lun, x_user, x_big_msgbuf);
489                 phree(x_big_msgbuf);
490
491                 /* This loop handles inline pages */
492         } else {
493                 message_sent = send_express_message(lun, x_user, x_msg);
494
495                 if (message_sent > 0) {
496                         if (strlen(x_msg) > 0)
497                                 cprintf("%d Message sent", OK);
498                         else
499                                 cprintf("%d Ok to send message", OK);
500                         if (message_sent > 1)
501                                 cprintf(" to %d users", message_sent);
502                         cprintf(".\n");
503                 } else {
504                         cprintf("%d No user '%s' logged in.\n", ERROR, x_user);
505                 }
506
507
508         }
509 }