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;
246 if (!(CC->logged_in)) {
247 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
251 CC->cs_flags = CC->cs_flags | CS_CHAT;
252 cprintf("%d Entering chat mode (type '/help' for available commands)\n",
255 MyLastMsg = ChatLastMsg;
257 if ((CC->cs_flags & CS_STEALTH) == 0) {
258 allwrite("<entering chat>", 0, NULL);
269 linelen = strlen(cmdbuf);
270 if (linelen > 100) --linelen; /* truncate too-long lines */
271 cmdbuf[linelen + 1] = 0;
273 retval = client_read_to(&cmdbuf[linelen], 1, 2);
275 if (retval < 0 || CC->kill_me) { /* socket broken? */
276 if ((CC->cs_flags & CS_STEALTH) == 0) {
277 allwrite("<disconnected>", 0, NULL);
282 /* if we have a complete line, do send processing */
283 if (strlen(cmdbuf) > 0)
284 if (cmdbuf[strlen(cmdbuf) - 1] == 10) {
285 cmdbuf[strlen(cmdbuf) - 1] = 0;
289 if ((!strcasecmp(cmdbuf, "exit"))
290 || (!strcasecmp(cmdbuf, "/exit"))
291 || (!strcasecmp(cmdbuf, "quit"))
292 || (!strcasecmp(cmdbuf, "logout"))
293 || (!strcasecmp(cmdbuf, "logoff"))
294 || (!strcasecmp(cmdbuf, "/q"))
295 || (!strcasecmp(cmdbuf, ".q"))
296 || (!strcasecmp(cmdbuf, "/quit"))
298 strcpy(cmdbuf, "000");
300 if (!strcmp(cmdbuf, "000")) {
301 if ((CC->cs_flags & CS_STEALTH) == 0) {
302 allwrite("<exiting chat>", 0, NULL);
306 CC->cs_flags = CC->cs_flags - CS_CHAT;
309 if ((!strcasecmp(cmdbuf, "/help"))
310 || (!strcasecmp(cmdbuf, "help"))
311 || (!strcasecmp(cmdbuf, "/?"))
312 || (!strcasecmp(cmdbuf, "?"))) {
314 cprintf(":|Available commands: \n");
315 cprintf(":|/help (prints this message) \n");
316 cprintf(":|/who (list users currently in chat) \n");
317 cprintf(":|/whobbs (list users in chat -and- elsewhere) \n");
318 cprintf(":|/me ('action' line, ala irc) \n");
319 cprintf(":|/msg (send private message, ala irc) \n");
320 if (is_room_aide()) {
321 cprintf(":|/kick (kick another user out of this room) \n");
323 cprintf(":|/quit (return to the BBS) \n");
327 if (!strcasecmp(cmdbuf, "/who")) {
331 if (!strcasecmp(cmdbuf, "/whobbs")) {
335 if (!strncasecmp(cmdbuf, "/me ", 4)) {
336 allwrite(&cmdbuf[4], 1, NULL);
339 if (!strncasecmp(cmdbuf, "/msg ", 5)) {
341 strptr1 = &cmdbuf[5];
342 if ((t_context = find_context(&strptr1))) {
343 allwrite(strptr1, 2, CC->curr_user);
344 if (strcasecmp(CC->curr_user, t_context->curr_user))
345 allwrite(strptr1, 2, t_context->curr_user);
347 cprintf(":|User not found.\n");
350 /* The /kick function is implemented by sending a specific
351 * message to the kicked-out user's context. When that message
352 * is processed by the read loop, that context will exit.
354 if ( (!strncasecmp(cmdbuf, "/kick ", 6)) && (is_room_aide()) ) {
356 strptr1 = &cmdbuf[6];
357 strcat(strptr1, " ");
358 if ((t_context = find_context(&strptr1))) {
359 if (strcasecmp(CC->curr_user, t_context->curr_user))
360 allwrite(strptr1, 3, t_context->curr_user);
362 cprintf(":|User not found.\n");
365 if ((cmdbuf[0] != '/') && (strlen(cmdbuf) > 0)) {
367 allwrite(cmdbuf, 0, NULL);
369 if ((!ok_cmd) && (cmdbuf[0]) && (cmdbuf[0] != '\n'))
370 cprintf(":|Command %s is not understood.\n", cmdbuf);
375 /* now check the queue for new incoming stuff */
377 if (CC->fake_username[0])
378 un = CC->fake_username;
381 if (ChatLastMsg > MyLastMsg) {
382 ThisLastMsg = ChatLastMsg;
383 for (clptr = ChatQueue; clptr != NULL; clptr = clptr->next) {
384 if ((clptr->chat_seq > MyLastMsg) && ((!clptr->chat_username[0]) || (!strncasecmp(un, clptr->chat_username, 32)))) {
385 if ((!clptr->chat_room[0]) || (!strncasecmp(CC->room.QRname, clptr->chat_room, ROOMNAMELEN))) {
386 /* Output new chat data */
387 cprintf("%s\n", clptr->chat_text);
389 /* See if we've been force-quitted (kicked etc.) */
390 if (!strcmp(&clptr->chat_text[2], KICKEDMSG)) {
391 allwrite("<kicked out of this room>", 0, NULL);
393 CC->cs_flags = CC->cs_flags - CS_CHAT;
395 /* Kick user out of room */
396 CtdlInvtKick(CC->user.fullname, 0);
398 /* And return to the Lobby */
399 usergoto(config.c_baseroom, 0, 0, NULL, NULL);
405 MyLastMsg = ThisLastMsg;
413 * Delete any remaining instant messages
415 void delete_instant_messages(void) {
416 struct ExpressMessage *ptr;
418 begin_critical_section(S_SESSION_TABLE);
419 while (CC->FirstExpressMessage != NULL) {
420 ptr = CC->FirstExpressMessage->next;
421 if (CC->FirstExpressMessage->text != NULL)
422 free(CC->FirstExpressMessage->text);
423 free(CC->FirstExpressMessage);
424 CC->FirstExpressMessage = ptr;
426 end_critical_section(S_SESSION_TABLE);
433 * Poll for instant messages (OLD METHOD -- ***DEPRECATED ***)
435 void cmd_pexp(char *argbuf)
437 struct ExpressMessage *ptr, *holdptr;
439 if (CC->FirstExpressMessage == NULL) {
440 cprintf("%d No instant messages waiting.\n", ERROR + MESSAGE_NOT_FOUND);
443 begin_critical_section(S_SESSION_TABLE);
444 ptr = CC->FirstExpressMessage;
445 CC->FirstExpressMessage = NULL;
446 end_critical_section(S_SESSION_TABLE);
448 cprintf("%d Express msgs:\n", LISTING_FOLLOWS);
449 while (ptr != NULL) {
450 if (ptr->flags && EM_BROADCAST)
451 cprintf("Broadcast message ");
452 else if (ptr->flags && EM_CHAT)
453 cprintf("Chat request ");
454 else if (ptr->flags && EM_GO_AWAY)
455 cprintf("Please logoff now, as requested ");
458 cprintf("from %s:\n", ptr->sender);
459 if (ptr->text != NULL)
460 memfmout(80, ptr->text, 0, "\n");
463 if (ptr->text != NULL) free(ptr->text);
472 * Get instant messages (new method)
474 void cmd_gexp(char *argbuf) {
475 struct ExpressMessage *ptr;
477 if (CC->FirstExpressMessage == NULL) {
478 cprintf("%d No instant messages waiting.\n", ERROR + MESSAGE_NOT_FOUND);
482 begin_critical_section(S_SESSION_TABLE);
483 ptr = CC->FirstExpressMessage;
484 CC->FirstExpressMessage = CC->FirstExpressMessage->next;
485 end_critical_section(S_SESSION_TABLE);
487 cprintf("%d %d|%ld|%d|%s|%s\n",
489 ((ptr->next != NULL) ? 1 : 0), /* more msgs? */
490 (long)ptr->timestamp, /* time sent */
491 ptr->flags, /* flags */
492 ptr->sender, /* sender of msg */
493 config.c_nodename); /* static for now */
494 if (ptr->text != NULL) {
495 memfmout(80, ptr->text, 0, "\n");
496 if (ptr->text[strlen(ptr->text)-1] != '\n') cprintf("\n");
504 * Asynchronously deliver instant messages
506 void cmd_gexp_async(void) {
508 /* Only do this if the session can handle asynchronous protocol */
509 if (CC->is_async == 0) return;
511 /* And don't do it if there's nothing to send. */
512 if (CC->FirstExpressMessage == NULL) return;
514 cprintf("%d instant msg\n", ASYNC_MSG + ASYNC_GEXP);
518 * Back end support function for send_instant_message() and company
520 void add_xmsg_to_context(struct CitContext *ccptr,
521 struct ExpressMessage *newmsg)
523 struct ExpressMessage *findend;
525 if (ccptr->FirstExpressMessage == NULL) {
526 ccptr->FirstExpressMessage = newmsg;
529 findend = ccptr->FirstExpressMessage;
530 while (findend->next != NULL) {
531 findend = findend->next;
533 findend->next = newmsg;
536 /* If the target context is a session which can handle asynchronous
537 * messages, go ahead and set the flag for that.
539 if (ccptr->is_async) {
540 ccptr->async_waiting = 1;
541 if (ccptr->state == CON_IDLE) {
542 ccptr->state = CON_READY;
551 * This is the back end to the instant message sending function.
552 * Returns the number of users to which the message was sent.
553 * Sending a zero-length message tests for recipients without sending messages.
555 int send_instant_message(char *lun, char *x_user, char *x_msg)
557 int message_sent = 0; /* number of successful sends */
559 struct CitContext *ccptr;
560 struct ExpressMessage *newmsg;
563 int do_send = 0; /* set to 1 to actually page, not
564 * just check to see if we can.
566 struct savelist *sl = NULL; /* list of rooms to save this page */
567 struct savelist *sptr;
568 struct CtdlMessage *logmsg;
571 if (strlen(x_msg) > 0) {
572 msglen = strlen(x_msg) + 4;
576 /* find the target user's context and append the message */
577 begin_critical_section(S_SESSION_TABLE);
578 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
580 if (ccptr->fake_username[0]) /* <bc> */
581 un = ccptr->fake_username;
583 un = ccptr->user.fullname;
585 if ( ((!strcasecmp(un, x_user))
586 || (!strcasecmp(x_user, "broadcast")))
587 && ((ccptr->disable_exp == 0)
588 || (CC->user.axlevel >= 6)) ) {
590 newmsg = (struct ExpressMessage *)
591 malloc(sizeof (struct ExpressMessage));
593 sizeof (struct ExpressMessage));
594 time(&(newmsg->timestamp));
595 safestrncpy(newmsg->sender, lun,
596 sizeof newmsg->sender);
597 if (!strcasecmp(x_user, "broadcast"))
598 newmsg->flags |= EM_BROADCAST;
599 newmsg->text = strdup(x_msg);
601 add_xmsg_to_context(ccptr, newmsg);
605 sptr = (struct savelist *)
606 malloc(sizeof(struct savelist));
608 MailboxName(sptr->roomname,
609 sizeof sptr->roomname,
610 &ccptr->user, PAGELOGROOM);
617 end_critical_section(S_SESSION_TABLE);
619 /* Log the page to disk if configured to do so */
620 if ( (do_send) && (message_sent) ) {
622 logmsg = malloc(sizeof(struct CtdlMessage));
623 memset(logmsg, 0, sizeof(struct CtdlMessage));
624 logmsg->cm_magic = CTDLMESSAGE_MAGIC;
625 logmsg->cm_anon_type = MES_NORMAL;
626 logmsg->cm_format_type = 0;
627 logmsg->cm_fields['A'] = strdup(lun);
628 logmsg->cm_fields['N'] = strdup(NODENAME);
629 logmsg->cm_fields['O'] = strdup(PAGELOGROOM);
630 logmsg->cm_fields['R'] = strdup(x_user);
631 logmsg->cm_fields['M'] = strdup(x_msg);
634 /* Save a copy of the message in the sender's log room,
635 * creating the room if necessary.
637 create_room(PAGELOGROOM, 4, "", 0, 1, 0);
638 msgnum = CtdlSubmitMsg(logmsg, NULL, PAGELOGROOM);
640 /* Now save a copy in the global log room, if configured */
641 if (strlen(config.c_logpages) > 0) {
642 create_room(config.c_logpages, 3, "", 0, 1, 1);
643 CtdlSaveMsgPointerInRoom(config.c_logpages, msgnum, 0);
646 /* Save a copy in each recipient's log room, creating those
647 * rooms if necessary. Note that we create as a type 5 room
648 * rather than 4, which indicates that it's a personal room
649 * but we've already supplied the namespace prefix.
652 create_room(sl->roomname, 5, "", 0, 1, 1);
653 CtdlSaveMsgPointerInRoom(sl->roomname, msgnum, 0);
659 CtdlFreeMessage(logmsg);
662 return (message_sent);
666 * send instant messages <bc>
668 void cmd_sexp(char *argbuf)
670 int message_sent = 0;
673 char *lun; /* <bc> */
674 char *x_big_msgbuf = NULL;
676 if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
677 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
680 if (CC->fake_username[0])
681 lun = CC->fake_username;
683 lun = CC->user.fullname;
685 extract(x_user, argbuf, 0);
687 extract(x_msg, argbuf, 1);
690 cprintf("%d You were not previously paged.\n", ERROR + NO_SUCH_USER);
693 if ((!strcasecmp(x_user, "broadcast")) && (CC->user.axlevel < 6)) {
694 cprintf("%d Higher access required to send a broadcast.\n",
695 ERROR + HIGHER_ACCESS_REQUIRED);
698 /* This loop handles text-transfer pages */
699 if (!strcmp(x_msg, "-")) {
700 message_sent = PerformXmsgHooks(lun, x_user, "");
701 if (message_sent == 0) {
702 if (getuser(NULL, x_user))
703 cprintf("%d '%s' does not exist.\n",
704 ERROR + NO_SUCH_USER, x_user);
706 cprintf("%d '%s' is not logged in "
707 "or is not accepting pages.\n",
708 ERROR + RESOURCE_NOT_OPEN, x_user);
711 cprintf("%d Transmit message (will deliver to %d users)\n",
712 SEND_LISTING, message_sent);
713 x_big_msgbuf = malloc(SIZ);
714 memset(x_big_msgbuf, 0, SIZ);
715 while (client_gets(x_msg), strcmp(x_msg, "000")) {
716 x_big_msgbuf = realloc(x_big_msgbuf,
717 strlen(x_big_msgbuf) + strlen(x_msg) + 4);
718 if (strlen(x_big_msgbuf) > 0)
719 if (x_big_msgbuf[strlen(x_big_msgbuf)] != '\n')
720 strcat(x_big_msgbuf, "\n");
721 strcat(x_big_msgbuf, x_msg);
723 PerformXmsgHooks(lun, x_user, x_big_msgbuf);
726 /* This loop handles inline pages */
728 message_sent = PerformXmsgHooks(lun, x_user, x_msg);
730 if (message_sent > 0) {
731 if (strlen(x_msg) > 0)
732 cprintf("%d Message sent", CIT_OK);
734 cprintf("%d Ok to send message", CIT_OK);
735 if (message_sent > 1)
736 cprintf(" to %d users", message_sent);
739 if (getuser(NULL, x_user))
740 cprintf("%d '%s' does not exist.\n",
741 ERROR + NO_SUCH_USER, x_user);
743 cprintf("%d '%s' is not logged in "
744 "or is not accepting pages.\n",
745 ERROR + RESOURCE_NOT_OPEN, x_user);
755 * Enter or exit paging-disabled mode
757 void cmd_dexp(char *argbuf)
761 if (CtdlAccessCheck(ac_logged_in)) return;
763 new_state = extract_int(argbuf, 0);
764 if ((new_state == 0) || (new_state == 1)) {
765 CC->disable_exp = new_state;
768 cprintf("%d %d\n", CIT_OK, CC->disable_exp);
773 * Request client termination
775 void cmd_reqt(char *argbuf) {
776 struct CitContext *ccptr;
779 struct ExpressMessage *newmsg;
781 if (CtdlAccessCheck(ac_aide)) return;
782 which_session = extract_int(argbuf, 0);
784 begin_critical_section(S_SESSION_TABLE);
785 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
786 if ((ccptr->cs_pid == which_session) || (which_session == 0)) {
788 newmsg = (struct ExpressMessage *)
789 malloc(sizeof (struct ExpressMessage));
791 sizeof (struct ExpressMessage));
792 time(&(newmsg->timestamp));
793 safestrncpy(newmsg->sender, CC->user.fullname,
794 sizeof newmsg->sender);
795 newmsg->flags |= EM_GO_AWAY;
796 newmsg->text = strdup("Automatic logoff requested.");
798 add_xmsg_to_context(ccptr, newmsg);
803 end_critical_section(S_SESSION_TABLE);
804 cprintf("%d Sent termination request to %d sessions.\n", CIT_OK, sessions);
809 char *serv_chat_init(void)
811 CtdlRegisterProtoHook(cmd_chat, "CHAT", "Begin real-time chat");
812 CtdlRegisterProtoHook(cmd_pexp, "PEXP", "Poll for instant messages");
813 CtdlRegisterProtoHook(cmd_gexp, "GEXP", "Get instant messages");
814 CtdlRegisterProtoHook(cmd_sexp, "SEXP", "Send an instant message");
815 CtdlRegisterProtoHook(cmd_dexp, "DEXP", "Disable instant messages");
816 CtdlRegisterProtoHook(cmd_reqt, "REQT", "Request client termination");
817 CtdlRegisterSessionHook(cmd_gexp_async, EVT_ASYNC);
818 CtdlRegisterSessionHook(delete_instant_messages, EVT_STOP);
819 CtdlRegisterXmsgHook(send_instant_message, XMSG_PRI_LOCAL);