4 * This module handles all "real time" communication between users. The
5 * modes of communication currently supported are Chat and Paging.
16 #include <sys/types.h>
18 #if TIME_WITH_SYS_TIME
19 # include <sys/time.h>
23 # include <sys/time.h>
35 #include "serv_chat.h"
36 #include "sysdep_decls.h"
37 #include "citserver.h"
40 #include "dynloader.h"
46 struct ChatLine *ChatQueue = NULL;
49 extern struct CitContext *ContextList;
53 void allwrite(char *cmdbuf, int flag, char *username)
58 struct ChatLine *clptr, *clnew;
61 if (CC->fake_username[0])
62 un = CC->fake_username;
64 un = CC->usersupp.fullname;
66 snprintf(bcast, sizeof bcast, ":|<%s %s>", un, cmdbuf);
67 } else if (flag == 0) {
68 snprintf(bcast, sizeof bcast, "%s|%s", un, cmdbuf);
69 } else if (flag == 2) {
70 snprintf(bcast, sizeof bcast, ":|<%s whispers %s>", un, cmdbuf);
72 if ((strcasecmp(cmdbuf, "NOOP")) && (flag != 2)) {
73 fp = fopen(CHATLOG, "a");
74 fprintf(fp, "%s\n", bcast);
77 clnew = (struct ChatLine *) mallok(sizeof(struct ChatLine));
78 memset(clnew, 0, sizeof(struct ChatLine));
80 fprintf(stderr, "citserver: cannot alloc chat line: %s\n",
86 clnew->chat_time = now;
87 safestrncpy(clnew->chat_room, CC->quickroom.QRname,
88 sizeof clnew->chat_room);
89 clnew->chat_room[sizeof clnew->chat_room - 1] = 0;
91 safestrncpy(clnew->chat_username, username,
92 sizeof clnew->chat_username);
93 clnew->chat_username[sizeof clnew->chat_username - 1] = 0;
95 clnew->chat_username[0] = '\0';
96 safestrncpy(clnew->chat_text, bcast, sizeof clnew->chat_text);
98 /* Here's the critical section.
99 * First, add the new message to the queue...
101 begin_critical_section(S_CHATQUEUE);
103 clnew->chat_seq = ChatLastMsg;
104 if (ChatQueue == NULL) {
107 for (clptr = ChatQueue; clptr->next != NULL; clptr = clptr->next);;
111 /* Then, before releasing the lock, free the expired messages */
112 while ((ChatQueue != NULL) && (now - ChatQueue->chat_time >= 120L)) {
114 ChatQueue = ChatQueue->next;
117 end_critical_section(S_CHATQUEUE);
121 t_context *find_context(char **unstr)
123 t_context *t_cc, *found_cc = NULL;
126 if ((!*unstr) || (!unstr))
129 begin_critical_section(S_SESSION_TABLE);
130 for (t_cc = ContextList; ((t_cc) && (!found_cc)); t_cc = t_cc->next) {
131 if (t_cc->fake_username[0])
132 name = t_cc->fake_username;
134 name = t_cc->curr_user;
136 if ((!strncasecmp(name, tptr, strlen(name))) && (tptr[strlen(name)] == ' ')) {
138 *unstr = &(tptr[strlen(name) + 1]);
141 end_critical_section(S_SESSION_TABLE);
147 * List users in chat.
148 * allflag == 0 = list users in chat
149 * 1 = list users in chat, followed by users not in chat
150 * 2 = display count only
153 void do_chat_listing(int allflag)
155 struct CitContext *ccptr;
157 int count_elsewhere = 0;
158 char roomname[ROOMNAMELEN];
160 if ((allflag == 0) || (allflag == 1))
161 cprintf(":|\n:| Users currently in chat:\n");
162 begin_critical_section(S_SESSION_TABLE);
163 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
164 if (ccptr->cs_flags & CS_CHAT) {
165 if (!strcasecmp(ccptr->quickroom.QRname,
166 CC->quickroom.QRname)) {
174 GenerateRoomDisplay(roomname, ccptr, CC);
175 if ((CC->usersupp.axlevel < 6)
176 && (strlen(ccptr->fake_roomname)>0)) {
177 strcpy(roomname, ccptr->fake_roomname);
180 if ((ccptr->cs_flags & CS_CHAT)
181 && ((ccptr->cs_flags & CS_STEALTH) == 0)) {
182 if ((allflag == 0) || (allflag == 1)) {
183 cprintf(":| %-25s <%s>:\n",
184 (ccptr->fake_username[0]) ? ccptr->fake_username : ccptr->curr_user,
191 cprintf(":|\n:| Users not in chat:\n");
192 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
194 GenerateRoomDisplay(roomname, ccptr, CC);
195 if ((CC->usersupp.axlevel < 6)
196 && (strlen(ccptr->fake_roomname)>0)) {
197 strcpy(roomname, ccptr->fake_roomname);
200 if (((ccptr->cs_flags & CS_CHAT) == 0)
201 && ((ccptr->cs_flags & CS_STEALTH) == 0)) {
202 cprintf(":| %-25s <%s>:\n",
203 (ccptr->fake_username[0]) ? ccptr->fake_username : ccptr->curr_user,
208 end_critical_section(S_SESSION_TABLE);
212 cprintf(":|There are %d users here.\n", count);
215 cprintf(":|Note: you are the only one here.\n");
217 if (count_elsewhere > 0) {
218 cprintf(":|There are %d users chatting in other rooms.\n", count_elsewhere);
226 void cmd_chat(char *argbuf)
231 int MyLastMsg, ThisLastMsg;
232 struct ChatLine *clptr;
233 struct CitContext *t_context;
236 if (!(CC->logged_in)) {
237 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
241 CC->cs_flags = CC->cs_flags | CS_CHAT;
242 cprintf("%d Entering chat mode (type '/help' for available commands)\n",
245 MyLastMsg = ChatLastMsg;
247 if ((CC->cs_flags & CS_STEALTH) == 0) {
248 allwrite("<entering chat>", 0, NULL);
259 linelen = strlen(cmdbuf);
260 if (linelen > 100) --linelen; /* truncate too-long lines */
261 cmdbuf[linelen + 1] = 0;
263 retval = client_read_to(&cmdbuf[linelen], 1, 2);
265 if (retval < 0) { /* socket broken? */
266 if ((CC->cs_flags & CS_STEALTH) == 0) {
267 allwrite("<disconnected>", 0, NULL);
272 /* if we have a complete line, do send processing */
273 if (strlen(cmdbuf) > 0)
274 if (cmdbuf[strlen(cmdbuf) - 1] == 10) {
275 cmdbuf[strlen(cmdbuf) - 1] = 0;
279 if ((!strcasecmp(cmdbuf, "exit"))
280 || (!strcasecmp(cmdbuf, "/exit"))
281 || (!strcasecmp(cmdbuf, "quit"))
282 || (!strcasecmp(cmdbuf, "logout"))
283 || (!strcasecmp(cmdbuf, "logoff"))
284 || (!strcasecmp(cmdbuf, "/q"))
285 || (!strcasecmp(cmdbuf, ".q"))
286 || (!strcasecmp(cmdbuf, "/quit"))
288 strcpy(cmdbuf, "000");
290 if (!strcmp(cmdbuf, "000")) {
291 if ((CC->cs_flags & CS_STEALTH) == 0) {
292 allwrite("<exiting chat>", 0, NULL);
296 CC->cs_flags = CC->cs_flags - CS_CHAT;
299 if ((!strcasecmp(cmdbuf, "/help"))
300 || (!strcasecmp(cmdbuf, "help"))
301 || (!strcasecmp(cmdbuf, "/?"))
302 || (!strcasecmp(cmdbuf, "?"))) {
304 cprintf(":|Available commands: \n");
305 cprintf(":|/help (prints this message) \n");
306 cprintf(":|/who (list users currently in chat) \n");
307 cprintf(":|/whobbs (list users in chat -and- elsewhere) \n");
308 cprintf(":|/me ('action' line, ala irc) \n");
309 cprintf(":|/msg (send private message, ala irc) \n");
310 cprintf(":|/quit (return to the BBS) \n");
314 if (!strcasecmp(cmdbuf, "/who")) {
318 if (!strcasecmp(cmdbuf, "/whobbs")) {
322 if (!strncasecmp(cmdbuf, "/me ", 4)) {
323 allwrite(&cmdbuf[4], 1, NULL);
326 if (!strncasecmp(cmdbuf, "/msg ", 5)) {
328 strptr1 = &cmdbuf[5];
329 if ((t_context = find_context(&strptr1))) {
330 allwrite(strptr1, 2, CC->curr_user);
331 if (strcasecmp(CC->curr_user, t_context->curr_user))
332 allwrite(strptr1, 2, t_context->curr_user);
334 cprintf(":|User not found.\n", cmdbuf);
337 if ((cmdbuf[0] != '/') && (strlen(cmdbuf) > 0)) {
339 allwrite(cmdbuf, 0, NULL);
341 if ((!ok_cmd) && (cmdbuf[0]) && (cmdbuf[0] != '\n'))
342 cprintf(":|Command %s is not understood.\n", cmdbuf);
347 /* now check the queue for new incoming stuff */
349 if (CC->fake_username[0])
350 un = CC->fake_username;
353 if (ChatLastMsg > MyLastMsg) {
354 ThisLastMsg = ChatLastMsg;
355 for (clptr = ChatQueue; clptr != NULL; clptr = clptr->next) {
356 if ((clptr->chat_seq > MyLastMsg) && ((!clptr->chat_username[0]) || (!strncasecmp(un, clptr->chat_username, 32)))) {
357 if ((!clptr->chat_room[0]) || (!strncasecmp(CC->quickroom.QRname, clptr->chat_room, ROOMNAMELEN))) {
358 cprintf("%s\n", clptr->chat_text);
362 MyLastMsg = ThisLastMsg;
370 * Delete any remaining express messages
372 void delete_express_messages(void) {
373 struct ExpressMessage *ptr;
375 begin_critical_section(S_SESSION_TABLE);
376 while (CC->FirstExpressMessage != NULL) {
377 ptr = CC->FirstExpressMessage->next;
378 if (CC->FirstExpressMessage->text != NULL)
379 phree(CC->FirstExpressMessage->text);
380 phree(CC->FirstExpressMessage);
381 CC->FirstExpressMessage = ptr;
383 end_critical_section(S_SESSION_TABLE);
390 * Poll for express messages (OLD METHOD -- ***DEPRECATED ***)
392 void cmd_pexp(char *argbuf)
394 struct ExpressMessage *ptr, *holdptr;
396 if (CC->FirstExpressMessage == NULL) {
397 cprintf("%d No express messages waiting.\n", ERROR);
400 begin_critical_section(S_SESSION_TABLE);
401 ptr = CC->FirstExpressMessage;
402 CC->FirstExpressMessage = NULL;
403 end_critical_section(S_SESSION_TABLE);
405 cprintf("%d Express msgs:\n", LISTING_FOLLOWS);
406 while (ptr != NULL) {
407 if (ptr->flags && EM_BROADCAST)
408 cprintf("Broadcast message ");
409 else if (ptr->flags && EM_CHAT)
410 cprintf("Chat request ");
411 else if (ptr->flags && EM_GO_AWAY)
412 cprintf("Please logoff now, as requested ");
415 cprintf("from %s:\n", ptr->sender);
416 if (ptr->text != NULL)
417 memfmout(80, ptr->text, 0, "\n");
420 if (ptr->text != NULL) phree(ptr->text);
429 * Get express messages (new method)
431 void cmd_gexp(char *argbuf) {
432 struct ExpressMessage *ptr;
434 if (CC->FirstExpressMessage == NULL) {
435 cprintf("%d No express messages waiting.\n", ERROR);
439 begin_critical_section(S_SESSION_TABLE);
440 ptr = CC->FirstExpressMessage;
441 CC->FirstExpressMessage = CC->FirstExpressMessage->next;
442 end_critical_section(S_SESSION_TABLE);
444 cprintf("%d %d|%ld|%d|%s|%s\n",
446 ((ptr->next != NULL) ? 1 : 0), /* more msgs? */
447 ptr->timestamp, /* time sent */
448 ptr->flags, /* flags */
449 ptr->sender, /* sender of msg */
450 config.c_nodename); /* static for now */
451 if (ptr->text != NULL) {
452 memfmout(80, ptr->text, 0, "\n");
453 if (ptr->text[strlen(ptr->text)-1] != '\n') cprintf("\n");
461 * Asynchronously deliver express messages'
463 void cmd_gexp_async(void) {
465 /* Only do this if the session can handle asynchronous protocol */
466 if (CC->is_async == 0) return;
468 /* And don't do it if there's nothing to send. */
469 if (CC->FirstExpressMessage == NULL) return;
471 cprintf("%d express msg\n", ASYNC_MSG + ASYNC_GEXP);
476 * Back end support function for send_express_message() and company
478 void add_xmsg_to_context(struct CitContext *ccptr,
479 struct ExpressMessage *newmsg)
481 struct ExpressMessage *findend;
483 if (ccptr->FirstExpressMessage == NULL) {
484 ccptr->FirstExpressMessage = newmsg;
487 findend = ccptr->FirstExpressMessage;
488 while (findend->next != NULL) {
489 findend = findend->next;
491 findend->next = newmsg;
499 * This is the back end to the express message sending function.
500 * Returns the number of users to which the message was sent.
501 * Sending a zero-length message tests for recipients without sending messages.
503 int send_express_message(char *lun, char *x_user, char *x_msg)
505 int message_sent = 0; /* number of successful sends */
507 struct CitContext *ccptr;
508 struct ExpressMessage *newmsg;
511 int do_send = 0; /* set to 1 to actually page, not
512 * just check to see if we can.
514 struct savelist *sl = NULL; /* list of rooms to save this page */
515 struct savelist *sptr;
516 struct CtdlMessage *logmsg;
519 if (strlen(x_msg) > 0) {
520 msglen = strlen(x_msg) + 4;
524 /* find the target user's context and append the message */
525 begin_critical_section(S_SESSION_TABLE);
526 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
528 if (ccptr->fake_username[0]) /* <bc> */
529 un = ccptr->fake_username;
531 un = ccptr->usersupp.fullname;
533 if ( ((!strcasecmp(un, x_user))
534 || (!strcasecmp(x_user, "broadcast")))
535 && ((ccptr->disable_exp == 0)
536 || (CC->usersupp.axlevel >= 6)) ) {
538 newmsg = (struct ExpressMessage *)
539 mallok(sizeof (struct ExpressMessage));
541 sizeof (struct ExpressMessage));
542 time(&(newmsg->timestamp));
543 safestrncpy(newmsg->sender, lun,
544 sizeof newmsg->sender);
545 if (!strcasecmp(x_user, "broadcast"))
546 newmsg->flags |= EM_BROADCAST;
547 newmsg->text = strdoop(x_msg);
549 add_xmsg_to_context(ccptr, newmsg);
553 sptr = (struct savelist *)
554 malloc(sizeof(struct savelist));
556 MailboxName(sptr->roomname,
557 &ccptr->usersupp, PAGELOGROOM);
564 end_critical_section(S_SESSION_TABLE);
566 /* Log the page to disk if configured to do so */
567 if ( (do_send) && (message_sent) ) {
569 logmsg = mallok(sizeof(struct CtdlMessage));
570 memset(logmsg, 0, sizeof(struct CtdlMessage));
571 logmsg->cm_magic = CTDLMESSAGE_MAGIC;
572 logmsg->cm_anon_type = MES_NORMAL;
573 logmsg->cm_format_type = 0;
574 logmsg->cm_fields['A'] = strdoop(lun);
575 logmsg->cm_fields['N'] = strdoop(NODENAME);
576 logmsg->cm_fields['O'] = strdoop(PAGELOGROOM);
577 logmsg->cm_fields['R'] = strdoop(x_user);
578 logmsg->cm_fields['M'] = strdoop(x_msg);
581 /* Save a copy of the message in the sender's log room,
582 * creating the room if necessary.
584 create_room(PAGELOGROOM, 4, "", 0, 1);
585 msgnum = CtdlSaveMsg(logmsg, "", PAGELOGROOM, MES_LOCAL);
587 /* Now save a copy in the global log room, if configured */
588 if (strlen(config.c_logpages) > 0) {
589 create_room(config.c_logpages, 3, "", 0, 1);
590 CtdlSaveMsgPointerInRoom(config.c_logpages, msgnum, 0);
593 /* Save a copy in each recipient's log room, creating those
594 * rooms if necessary. Note that we create as a type 5 room
595 * rather than 4, which indicates that it's a personal room
596 * but we've already supplied the namespace prefix.
599 create_room(sl->roomname, 5, "", 0, 1);
600 CtdlSaveMsgPointerInRoom(sl->roomname, msgnum, 0);
606 CtdlFreeMessage(logmsg);
609 return (message_sent);
613 * send express messages <bc>
615 void cmd_sexp(char *argbuf)
617 int message_sent = 0;
620 char *lun; /* <bc> */
621 char *x_big_msgbuf = NULL;
623 if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
624 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
627 if (CC->fake_username[0])
628 lun = CC->fake_username;
630 lun = CC->usersupp.fullname;
632 extract(x_user, argbuf, 0);
634 extract(x_msg, argbuf, 1);
637 cprintf("%d You were not previously paged.\n", ERROR);
640 if ((!strcasecmp(x_user, "broadcast")) && (CC->usersupp.axlevel < 6)) {
641 cprintf("%d Higher access required to send a broadcast.\n",
642 ERROR + HIGHER_ACCESS_REQUIRED);
645 /* This loop handles text-transfer pages */
646 if (!strcmp(x_msg, "-")) {
647 message_sent = PerformXmsgHooks(lun, x_user, "");
648 if (message_sent == 0) {
649 cprintf("%d '%s' is not logged in "
650 "or is not accepting pages.\n",
654 cprintf("%d Transmit message (will deliver to %d users)\n",
655 SEND_LISTING, message_sent);
656 x_big_msgbuf = mallok(SIZ);
657 memset(x_big_msgbuf, 0, SIZ);
658 while (client_gets(x_msg), strcmp(x_msg, "000")) {
659 x_big_msgbuf = reallok(x_big_msgbuf,
660 strlen(x_big_msgbuf) + strlen(x_msg) + 4);
661 if (strlen(x_big_msgbuf) > 0)
662 if (x_big_msgbuf[strlen(x_big_msgbuf)] != '\n')
663 strcat(x_big_msgbuf, "\n");
664 strcat(x_big_msgbuf, x_msg);
666 PerformXmsgHooks(lun, x_user, x_big_msgbuf);
669 /* This loop handles inline pages */
671 message_sent = PerformXmsgHooks(lun, x_user, x_msg);
673 if (message_sent > 0) {
674 if (strlen(x_msg) > 0)
675 cprintf("%d Message sent", OK);
677 cprintf("%d Ok to send message", OK);
678 if (message_sent > 1)
679 cprintf(" to %d users", message_sent);
682 cprintf("%d '%s' is not logged in "
683 "or is not accepting pages.\n",
694 * Enter or exit paging-disabled mode
696 void cmd_dexp(char *argbuf)
700 if (!CC->logged_in) {
701 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
705 new_state = extract_int(argbuf, 0);
706 if ((new_state == 0) || (new_state == 1)) {
707 CC->disable_exp = new_state;
709 cprintf("%d %d\n", OK, CC->disable_exp);
714 * Request client termination
716 void cmd_reqt(char *argbuf) {
717 struct CitContext *ccptr;
720 struct ExpressMessage *newmsg;
722 if (CtdlAccessCheck(ac_aide)) return;
723 which_session = extract_int(argbuf, 0);
725 begin_critical_section(S_SESSION_TABLE);
726 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
727 if ((ccptr->cs_pid == which_session) || (which_session == 0)) {
729 newmsg = (struct ExpressMessage *)
730 mallok(sizeof (struct ExpressMessage));
732 sizeof (struct ExpressMessage));
733 time(&(newmsg->timestamp));
734 safestrncpy(newmsg->sender, CC->usersupp.fullname,
735 sizeof newmsg->sender);
736 newmsg->flags |= EM_GO_AWAY;
737 newmsg->text = strdoop("Automatic logoff requested.");
739 add_xmsg_to_context(ccptr, newmsg);
744 end_critical_section(S_SESSION_TABLE);
745 cprintf("%d Sent termination request to %d sessions.\n", OK, sessions);
750 char *Dynamic_Module_Init(void)
752 CtdlRegisterProtoHook(cmd_chat, "CHAT", "Begin real-time chat");
753 CtdlRegisterProtoHook(cmd_pexp, "PEXP", "Poll for express messages");
754 CtdlRegisterProtoHook(cmd_gexp, "GEXP", "Get express messages");
755 CtdlRegisterProtoHook(cmd_sexp, "SEXP", "Send an express message");
756 CtdlRegisterProtoHook(cmd_dexp, "DEXP", "Disable express messages");
757 CtdlRegisterProtoHook(cmd_reqt, "REQT", "Request client termination");
758 CtdlRegisterSessionHook(cmd_gexp_async, EVT_CMD);
759 CtdlRegisterSessionHook(delete_express_messages, EVT_STOP);
760 CtdlRegisterXmsgHook(send_express_message, XMSG_PRI_LOCAL);