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>
34 #include "serv_extensions.h"
35 #include "serv_chat.h"
36 #include "sysdep_decls.h"
37 #include "citserver.h"
49 struct ChatLine *ChatQueue = NULL;
53 * This message can be set to anything you want, but it is
54 * checked for consistency so don't move it away from here.
56 #define KICKEDMSG "You have been kicked out of this room."
58 void allwrite(char *cmdbuf, int flag, char *username)
63 struct ChatLine *clptr, *clnew;
66 if (CC->fake_username[0])
67 un = CC->fake_username;
69 un = CC->user.fullname;
71 snprintf(bcast, sizeof bcast, ":|<%s %s>", un, cmdbuf);
72 } else if (flag == 0) {
73 snprintf(bcast, sizeof bcast, "%s|%s", un, cmdbuf);
74 } else if (flag == 2) {
75 snprintf(bcast, sizeof bcast, ":|<%s whispers %s>", un, cmdbuf);
76 } else if (flag == 3) {
77 snprintf(bcast, sizeof bcast, ":|%s", KICKEDMSG);
79 if ((strcasecmp(cmdbuf, "NOOP")) && (flag != 2)) {
80 fp = fopen(CHATLOG, "a");
82 fprintf(fp, "%s\n", bcast);
85 clnew = (struct ChatLine *) malloc(sizeof(struct ChatLine));
86 memset(clnew, 0, sizeof(struct ChatLine));
88 fprintf(stderr, "citserver: cannot alloc chat line: %s\n",
94 clnew->chat_time = now;
95 safestrncpy(clnew->chat_room, CC->room.QRname,
96 sizeof clnew->chat_room);
97 clnew->chat_room[sizeof clnew->chat_room - 1] = 0;
99 safestrncpy(clnew->chat_username, username,
100 sizeof clnew->chat_username);
101 clnew->chat_username[sizeof clnew->chat_username - 1] = 0;
103 clnew->chat_username[0] = '\0';
104 safestrncpy(clnew->chat_text, bcast, sizeof clnew->chat_text);
106 /* Here's the critical section.
107 * First, add the new message to the queue...
109 begin_critical_section(S_CHATQUEUE);
111 clnew->chat_seq = ChatLastMsg;
112 if (ChatQueue == NULL) {
115 for (clptr = ChatQueue; clptr->next != NULL; clptr = clptr->next);;
119 /* Then, before releasing the lock, free the expired messages */
120 while ((ChatQueue != NULL) && (now - ChatQueue->chat_time >= 120L)) {
122 ChatQueue = ChatQueue->next;
125 end_critical_section(S_CHATQUEUE);
129 t_context *find_context(char **unstr)
131 t_context *t_cc, *found_cc = NULL;
134 if ((!*unstr) || (!unstr))
137 begin_critical_section(S_SESSION_TABLE);
138 for (t_cc = ContextList; ((t_cc) && (!found_cc)); t_cc = t_cc->next) {
139 if (t_cc->fake_username[0])
140 name = t_cc->fake_username;
142 name = t_cc->curr_user;
144 if ((!strncasecmp(name, tptr, strlen(name))) && (tptr[strlen(name)] == ' ')) {
146 *unstr = &(tptr[strlen(name) + 1]);
149 end_critical_section(S_SESSION_TABLE);
155 * List users in chat.
156 * allflag == 0 = list users in chat
157 * 1 = list users in chat, followed by users not in chat
158 * 2 = display count only
161 void do_chat_listing(int allflag)
163 struct CitContext *ccptr;
165 int count_elsewhere = 0;
166 char roomname[ROOMNAMELEN];
168 if ((allflag == 0) || (allflag == 1))
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 (ccptr->cs_flags & CS_CHAT) {
173 if (!strcasecmp(ccptr->room.QRname,
182 GenerateRoomDisplay(roomname, ccptr, CC);
183 if ((CC->user.axlevel < 6)
184 && (strlen(ccptr->fake_roomname)>0)) {
185 strcpy(roomname, ccptr->fake_roomname);
188 if ((ccptr->cs_flags & CS_CHAT)
189 && ((ccptr->cs_flags & CS_STEALTH) == 0)) {
190 if ((allflag == 0) || (allflag == 1)) {
191 cprintf(":| %-25s <%s>:\n",
192 (ccptr->fake_username[0]) ? ccptr->fake_username : ccptr->curr_user,
199 cprintf(":|\n:| Users not in chat:\n");
200 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
202 GenerateRoomDisplay(roomname, ccptr, CC);
203 if ((CC->user.axlevel < 6)
204 && (strlen(ccptr->fake_roomname)>0)) {
205 strcpy(roomname, ccptr->fake_roomname);
208 if (((ccptr->cs_flags & CS_CHAT) == 0)
209 && ((ccptr->cs_flags & CS_STEALTH) == 0)) {
210 cprintf(":| %-25s <%s>:\n",
211 (ccptr->fake_username[0]) ? ccptr->fake_username : ccptr->curr_user,
216 end_critical_section(S_SESSION_TABLE);
220 cprintf(":|There are %d users here.\n", count);
223 cprintf(":|Note: you are the only one here.\n");
225 if (count_elsewhere > 0) {
226 cprintf(":|There are %d users chatting in other rooms.\n", count_elsewhere);
234 void cmd_chat(char *argbuf)
239 int MyLastMsg, ThisLastMsg;
240 struct ChatLine *clptr;
241 struct CitContext *t_context;
244 if (!(CC->logged_in)) {
245 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
249 CC->cs_flags = CC->cs_flags | CS_CHAT;
250 cprintf("%d Entering chat mode (type '/help' for available commands)\n",
253 MyLastMsg = ChatLastMsg;
255 if ((CC->cs_flags & CS_STEALTH) == 0) {
256 allwrite("<entering chat>", 0, NULL);
267 linelen = strlen(cmdbuf);
268 if (linelen > 100) --linelen; /* truncate too-long lines */
269 cmdbuf[linelen + 1] = 0;
271 retval = client_read_to(&cmdbuf[linelen], 1, 2);
273 if (retval < 0 || CC->kill_me) { /* socket broken? */
274 if ((CC->cs_flags & CS_STEALTH) == 0) {
275 allwrite("<disconnected>", 0, NULL);
280 /* if we have a complete line, do send processing */
281 if (strlen(cmdbuf) > 0)
282 if (cmdbuf[strlen(cmdbuf) - 1] == 10) {
283 cmdbuf[strlen(cmdbuf) - 1] = 0;
287 if ((!strcasecmp(cmdbuf, "exit"))
288 || (!strcasecmp(cmdbuf, "/exit"))
289 || (!strcasecmp(cmdbuf, "quit"))
290 || (!strcasecmp(cmdbuf, "logout"))
291 || (!strcasecmp(cmdbuf, "logoff"))
292 || (!strcasecmp(cmdbuf, "/q"))
293 || (!strcasecmp(cmdbuf, ".q"))
294 || (!strcasecmp(cmdbuf, "/quit"))
296 strcpy(cmdbuf, "000");
298 if (!strcmp(cmdbuf, "000")) {
299 if ((CC->cs_flags & CS_STEALTH) == 0) {
300 allwrite("<exiting chat>", 0, NULL);
304 CC->cs_flags = CC->cs_flags - CS_CHAT;
307 if ((!strcasecmp(cmdbuf, "/help"))
308 || (!strcasecmp(cmdbuf, "help"))
309 || (!strcasecmp(cmdbuf, "/?"))
310 || (!strcasecmp(cmdbuf, "?"))) {
312 cprintf(":|Available commands: \n");
313 cprintf(":|/help (prints this message) \n");
314 cprintf(":|/who (list users currently in chat) \n");
315 cprintf(":|/whobbs (list users in chat -and- elsewhere) \n");
316 cprintf(":|/me ('action' line, ala irc) \n");
317 cprintf(":|/msg (send private message, ala irc) \n");
318 if (is_room_aide()) {
319 cprintf(":|/kick (kick another user out of this room) \n");
321 cprintf(":|/quit (return to the BBS) \n");
325 if (!strcasecmp(cmdbuf, "/who")) {
329 if (!strcasecmp(cmdbuf, "/whobbs")) {
333 if (!strncasecmp(cmdbuf, "/me ", 4)) {
334 allwrite(&cmdbuf[4], 1, NULL);
337 if (!strncasecmp(cmdbuf, "/msg ", 5)) {
339 strptr1 = &cmdbuf[5];
340 if ((t_context = find_context(&strptr1))) {
341 allwrite(strptr1, 2, CC->curr_user);
342 if (strcasecmp(CC->curr_user, t_context->curr_user))
343 allwrite(strptr1, 2, t_context->curr_user);
345 cprintf(":|User not found.\n");
348 /* The /kick function is implemented by sending a specific
349 * message to the kicked-out user's context. When that message
350 * is processed by the read loop, that context will exit.
352 if ( (!strncasecmp(cmdbuf, "/kick ", 6)) && (is_room_aide()) ) {
354 strptr1 = &cmdbuf[6];
355 strcat(strptr1, " ");
356 if ((t_context = find_context(&strptr1))) {
357 if (strcasecmp(CC->curr_user, t_context->curr_user))
358 allwrite(strptr1, 3, t_context->curr_user);
360 cprintf(":|User not found.\n");
363 if ((cmdbuf[0] != '/') && (strlen(cmdbuf) > 0)) {
365 allwrite(cmdbuf, 0, NULL);
367 if ((!ok_cmd) && (cmdbuf[0]) && (cmdbuf[0] != '\n'))
368 cprintf(":|Command %s is not understood.\n", cmdbuf);
373 /* now check the queue for new incoming stuff */
375 if (CC->fake_username[0])
376 un = CC->fake_username;
379 if (ChatLastMsg > MyLastMsg) {
380 ThisLastMsg = ChatLastMsg;
381 for (clptr = ChatQueue; clptr != NULL; clptr = clptr->next) {
382 if ((clptr->chat_seq > MyLastMsg) && ((!clptr->chat_username[0]) || (!strncasecmp(un, clptr->chat_username, 32)))) {
383 if ((!clptr->chat_room[0]) || (!strncasecmp(CC->room.QRname, clptr->chat_room, ROOMNAMELEN))) {
384 /* Output new chat data */
385 cprintf("%s\n", clptr->chat_text);
387 /* See if we've been force-quitted (kicked etc.) */
388 if (!strcmp(&clptr->chat_text[2], KICKEDMSG)) {
389 allwrite("<kicked out of this room>", 0, NULL);
391 CC->cs_flags = CC->cs_flags - CS_CHAT;
393 /* Kick user out of room */
394 CtdlInvtKick(CC->user.fullname, 0);
396 /* And return to the Lobby */
397 usergoto(config.c_baseroom, 0, 0, NULL, NULL);
403 MyLastMsg = ThisLastMsg;
411 * Delete any remaining express messages
413 void delete_express_messages(void) {
414 struct ExpressMessage *ptr;
416 begin_critical_section(S_SESSION_TABLE);
417 while (CC->FirstExpressMessage != NULL) {
418 ptr = CC->FirstExpressMessage->next;
419 if (CC->FirstExpressMessage->text != NULL)
420 free(CC->FirstExpressMessage->text);
421 free(CC->FirstExpressMessage);
422 CC->FirstExpressMessage = ptr;
424 end_critical_section(S_SESSION_TABLE);
431 * Poll for express messages (OLD METHOD -- ***DEPRECATED ***)
433 void cmd_pexp(char *argbuf)
435 struct ExpressMessage *ptr, *holdptr;
437 if (CC->FirstExpressMessage == NULL) {
438 cprintf("%d No express messages waiting.\n", ERROR + MESSAGE_NOT_FOUND);
441 begin_critical_section(S_SESSION_TABLE);
442 ptr = CC->FirstExpressMessage;
443 CC->FirstExpressMessage = NULL;
444 end_critical_section(S_SESSION_TABLE);
446 cprintf("%d Express msgs:\n", LISTING_FOLLOWS);
447 while (ptr != NULL) {
448 if (ptr->flags && EM_BROADCAST)
449 cprintf("Broadcast message ");
450 else if (ptr->flags && EM_CHAT)
451 cprintf("Chat request ");
452 else if (ptr->flags && EM_GO_AWAY)
453 cprintf("Please logoff now, as requested ");
456 cprintf("from %s:\n", ptr->sender);
457 if (ptr->text != NULL)
458 memfmout(80, ptr->text, 0, "\n");
461 if (ptr->text != NULL) free(ptr->text);
470 * Get express messages (new method)
472 void cmd_gexp(char *argbuf) {
473 struct ExpressMessage *ptr;
475 if (CC->FirstExpressMessage == NULL) {
476 cprintf("%d No express messages waiting.\n", ERROR + MESSAGE_NOT_FOUND);
480 begin_critical_section(S_SESSION_TABLE);
481 ptr = CC->FirstExpressMessage;
482 CC->FirstExpressMessage = CC->FirstExpressMessage->next;
483 end_critical_section(S_SESSION_TABLE);
485 cprintf("%d %d|%ld|%d|%s|%s\n",
487 ((ptr->next != NULL) ? 1 : 0), /* more msgs? */
488 (long)ptr->timestamp, /* time sent */
489 ptr->flags, /* flags */
490 ptr->sender, /* sender of msg */
491 config.c_nodename); /* static for now */
492 if (ptr->text != NULL) {
493 memfmout(80, ptr->text, 0, "\n");
494 if (ptr->text[strlen(ptr->text)-1] != '\n') cprintf("\n");
502 * Asynchronously deliver express messages'
504 void cmd_gexp_async(void) {
506 /* Only do this if the session can handle asynchronous protocol */
507 if (CC->is_async == 0) return;
509 /* And don't do it if there's nothing to send. */
510 if (CC->FirstExpressMessage == NULL) return;
512 cprintf("%d express msg\n", ASYNC_MSG + ASYNC_GEXP);
517 * Back end support function for send_express_message() and company
519 void add_xmsg_to_context(struct CitContext *ccptr,
520 struct ExpressMessage *newmsg)
522 struct ExpressMessage *findend;
524 if (ccptr->FirstExpressMessage == NULL) {
525 ccptr->FirstExpressMessage = newmsg;
528 findend = ccptr->FirstExpressMessage;
529 while (findend->next != NULL) {
530 findend = findend->next;
532 findend->next = newmsg;
540 * This is the back end to the express message sending function.
541 * Returns the number of users to which the message was sent.
542 * Sending a zero-length message tests for recipients without sending messages.
544 int send_express_message(char *lun, char *x_user, char *x_msg)
546 int message_sent = 0; /* number of successful sends */
548 struct CitContext *ccptr;
549 struct ExpressMessage *newmsg;
552 int do_send = 0; /* set to 1 to actually page, not
553 * just check to see if we can.
555 struct savelist *sl = NULL; /* list of rooms to save this page */
556 struct savelist *sptr;
557 struct CtdlMessage *logmsg;
560 if (strlen(x_msg) > 0) {
561 msglen = strlen(x_msg) + 4;
565 /* find the target user's context and append the message */
566 begin_critical_section(S_SESSION_TABLE);
567 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
569 if (ccptr->fake_username[0]) /* <bc> */
570 un = ccptr->fake_username;
572 un = ccptr->user.fullname;
574 if ( ((!strcasecmp(un, x_user))
575 || (!strcasecmp(x_user, "broadcast")))
576 && ((ccptr->disable_exp == 0)
577 || (CC->user.axlevel >= 6)) ) {
579 newmsg = (struct ExpressMessage *)
580 malloc(sizeof (struct ExpressMessage));
582 sizeof (struct ExpressMessage));
583 time(&(newmsg->timestamp));
584 safestrncpy(newmsg->sender, lun,
585 sizeof newmsg->sender);
586 if (!strcasecmp(x_user, "broadcast"))
587 newmsg->flags |= EM_BROADCAST;
588 newmsg->text = strdup(x_msg);
590 add_xmsg_to_context(ccptr, newmsg);
594 sptr = (struct savelist *)
595 malloc(sizeof(struct savelist));
597 MailboxName(sptr->roomname,
598 sizeof sptr->roomname,
599 &ccptr->user, PAGELOGROOM);
606 end_critical_section(S_SESSION_TABLE);
608 /* Log the page to disk if configured to do so */
609 if ( (do_send) && (message_sent) ) {
611 logmsg = malloc(sizeof(struct CtdlMessage));
612 memset(logmsg, 0, sizeof(struct CtdlMessage));
613 logmsg->cm_magic = CTDLMESSAGE_MAGIC;
614 logmsg->cm_anon_type = MES_NORMAL;
615 logmsg->cm_format_type = 0;
616 logmsg->cm_fields['A'] = strdup(lun);
617 logmsg->cm_fields['N'] = strdup(NODENAME);
618 logmsg->cm_fields['O'] = strdup(PAGELOGROOM);
619 logmsg->cm_fields['R'] = strdup(x_user);
620 logmsg->cm_fields['M'] = strdup(x_msg);
623 /* Save a copy of the message in the sender's log room,
624 * creating the room if necessary.
626 create_room(PAGELOGROOM, 4, "", 0, 1, 0);
627 msgnum = CtdlSubmitMsg(logmsg, NULL, PAGELOGROOM);
629 /* Now save a copy in the global log room, if configured */
630 if (strlen(config.c_logpages) > 0) {
631 create_room(config.c_logpages, 3, "", 0, 1, 1);
632 CtdlSaveMsgPointerInRoom(config.c_logpages, msgnum, 0);
635 /* Save a copy in each recipient's log room, creating those
636 * rooms if necessary. Note that we create as a type 5 room
637 * rather than 4, which indicates that it's a personal room
638 * but we've already supplied the namespace prefix.
641 create_room(sl->roomname, 5, "", 0, 1, 1);
642 CtdlSaveMsgPointerInRoom(sl->roomname, msgnum, 0);
648 CtdlFreeMessage(logmsg);
651 return (message_sent);
655 * send express messages <bc>
657 void cmd_sexp(char *argbuf)
659 int message_sent = 0;
662 char *lun; /* <bc> */
663 char *x_big_msgbuf = NULL;
665 if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
666 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
669 if (CC->fake_username[0])
670 lun = CC->fake_username;
672 lun = CC->user.fullname;
674 extract(x_user, argbuf, 0);
676 extract(x_msg, argbuf, 1);
679 cprintf("%d You were not previously paged.\n", ERROR + NO_SUCH_USER);
682 if ((!strcasecmp(x_user, "broadcast")) && (CC->user.axlevel < 6)) {
683 cprintf("%d Higher access required to send a broadcast.\n",
684 ERROR + HIGHER_ACCESS_REQUIRED);
687 /* This loop handles text-transfer pages */
688 if (!strcmp(x_msg, "-")) {
689 message_sent = PerformXmsgHooks(lun, x_user, "");
690 if (message_sent == 0) {
691 cprintf("%d '%s' is not logged in "
692 "or is not accepting pages.\n",
693 ERROR + NO_SUCH_USER, x_user);
696 cprintf("%d Transmit message (will deliver to %d users)\n",
697 SEND_LISTING, message_sent);
698 x_big_msgbuf = malloc(SIZ);
699 memset(x_big_msgbuf, 0, SIZ);
700 while (client_gets(x_msg), strcmp(x_msg, "000")) {
701 x_big_msgbuf = realloc(x_big_msgbuf,
702 strlen(x_big_msgbuf) + strlen(x_msg) + 4);
703 if (strlen(x_big_msgbuf) > 0)
704 if (x_big_msgbuf[strlen(x_big_msgbuf)] != '\n')
705 strcat(x_big_msgbuf, "\n");
706 strcat(x_big_msgbuf, x_msg);
708 PerformXmsgHooks(lun, x_user, x_big_msgbuf);
711 /* This loop handles inline pages */
713 message_sent = PerformXmsgHooks(lun, x_user, x_msg);
715 if (message_sent > 0) {
716 if (strlen(x_msg) > 0)
717 cprintf("%d Message sent", CIT_OK);
719 cprintf("%d Ok to send message", CIT_OK);
720 if (message_sent > 1)
721 cprintf(" to %d users", message_sent);
724 cprintf("%d '%s' is not logged in "
725 "or is not accepting pages.\n",
726 ERROR + NO_SUCH_USER, x_user);
736 * Enter or exit paging-disabled mode
738 void cmd_dexp(char *argbuf)
742 if (CtdlAccessCheck(ac_logged_in)) return;
744 new_state = extract_int(argbuf, 0);
745 if ((new_state == 0) || (new_state == 1)) {
746 CC->disable_exp = new_state;
749 cprintf("%d %d\n", CIT_OK, CC->disable_exp);
754 * Request client termination
756 void cmd_reqt(char *argbuf) {
757 struct CitContext *ccptr;
760 struct ExpressMessage *newmsg;
762 if (CtdlAccessCheck(ac_aide)) return;
763 which_session = extract_int(argbuf, 0);
765 begin_critical_section(S_SESSION_TABLE);
766 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
767 if ((ccptr->cs_pid == which_session) || (which_session == 0)) {
769 newmsg = (struct ExpressMessage *)
770 malloc(sizeof (struct ExpressMessage));
772 sizeof (struct ExpressMessage));
773 time(&(newmsg->timestamp));
774 safestrncpy(newmsg->sender, CC->user.fullname,
775 sizeof newmsg->sender);
776 newmsg->flags |= EM_GO_AWAY;
777 newmsg->text = strdup("Automatic logoff requested.");
779 add_xmsg_to_context(ccptr, newmsg);
784 end_critical_section(S_SESSION_TABLE);
785 cprintf("%d Sent termination request to %d sessions.\n", CIT_OK, sessions);
790 char *serv_chat_init(void)
792 CtdlRegisterProtoHook(cmd_chat, "CHAT", "Begin real-time chat");
793 CtdlRegisterProtoHook(cmd_pexp, "PEXP", "Poll for express messages");
794 CtdlRegisterProtoHook(cmd_gexp, "GEXP", "Get express messages");
795 CtdlRegisterProtoHook(cmd_sexp, "SEXP", "Send an express message");
796 CtdlRegisterProtoHook(cmd_dexp, "DEXP", "Disable express messages");
797 CtdlRegisterProtoHook(cmd_reqt, "REQT", "Request client termination");
798 CtdlRegisterSessionHook(cmd_gexp_async, EVT_CMD);
799 CtdlRegisterSessionHook(delete_express_messages, EVT_STOP);
800 CtdlRegisterXmsgHook(send_express_message, XMSG_PRI_LOCAL);