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