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