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;
55 void allwrite(char *cmdbuf, int flag, char *username)
60 struct ChatLine *clptr, *clnew;
63 if (CC->fake_username[0])
64 un = CC->fake_username;
66 un = CC->user.fullname;
68 snprintf(bcast, sizeof bcast, ":|<%s %s>", un, cmdbuf);
69 } else if (flag == 0) {
70 snprintf(bcast, sizeof bcast, "%s|%s", un, cmdbuf);
71 } else if (flag == 2) {
72 snprintf(bcast, sizeof bcast, ":|<%s whispers %s>", un, cmdbuf);
74 if ((strcasecmp(cmdbuf, "NOOP")) && (flag != 2)) {
75 fp = fopen(CHATLOG, "a");
77 fprintf(fp, "%s\n", bcast);
80 clnew = (struct ChatLine *) mallok(sizeof(struct ChatLine));
81 memset(clnew, 0, sizeof(struct ChatLine));
83 fprintf(stderr, "citserver: cannot alloc chat line: %s\n",
89 clnew->chat_time = now;
90 safestrncpy(clnew->chat_room, CC->room.QRname,
91 sizeof clnew->chat_room);
92 clnew->chat_room[sizeof clnew->chat_room - 1] = 0;
94 safestrncpy(clnew->chat_username, username,
95 sizeof clnew->chat_username);
96 clnew->chat_username[sizeof clnew->chat_username - 1] = 0;
98 clnew->chat_username[0] = '\0';
99 safestrncpy(clnew->chat_text, bcast, sizeof clnew->chat_text);
101 /* Here's the critical section.
102 * First, add the new message to the queue...
104 begin_critical_section(S_CHATQUEUE);
106 clnew->chat_seq = ChatLastMsg;
107 if (ChatQueue == NULL) {
110 for (clptr = ChatQueue; clptr->next != NULL; clptr = clptr->next);;
114 /* Then, before releasing the lock, free the expired messages */
115 while ((ChatQueue != NULL) && (now - ChatQueue->chat_time >= 120L)) {
117 ChatQueue = ChatQueue->next;
120 end_critical_section(S_CHATQUEUE);
124 t_context *find_context(char **unstr)
126 t_context *t_cc, *found_cc = NULL;
129 if ((!*unstr) || (!unstr))
132 begin_critical_section(S_SESSION_TABLE);
133 for (t_cc = ContextList; ((t_cc) && (!found_cc)); t_cc = t_cc->next) {
134 if (t_cc->fake_username[0])
135 name = t_cc->fake_username;
137 name = t_cc->curr_user;
139 if ((!strncasecmp(name, tptr, strlen(name))) && (tptr[strlen(name)] == ' ')) {
141 *unstr = &(tptr[strlen(name) + 1]);
144 end_critical_section(S_SESSION_TABLE);
150 * List users in chat.
151 * allflag == 0 = list users in chat
152 * 1 = list users in chat, followed by users not in chat
153 * 2 = display count only
156 void do_chat_listing(int allflag)
158 struct CitContext *ccptr;
160 int count_elsewhere = 0;
161 char roomname[ROOMNAMELEN];
163 if ((allflag == 0) || (allflag == 1))
164 cprintf(":|\n:| Users currently in chat:\n");
165 begin_critical_section(S_SESSION_TABLE);
166 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
167 if (ccptr->cs_flags & CS_CHAT) {
168 if (!strcasecmp(ccptr->room.QRname,
177 GenerateRoomDisplay(roomname, ccptr, CC);
178 if ((CC->user.axlevel < 6)
179 && (strlen(ccptr->fake_roomname)>0)) {
180 strcpy(roomname, ccptr->fake_roomname);
183 if ((ccptr->cs_flags & CS_CHAT)
184 && ((ccptr->cs_flags & CS_STEALTH) == 0)) {
185 if ((allflag == 0) || (allflag == 1)) {
186 cprintf(":| %-25s <%s>:\n",
187 (ccptr->fake_username[0]) ? ccptr->fake_username : ccptr->curr_user,
194 cprintf(":|\n:| Users not in chat:\n");
195 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
197 GenerateRoomDisplay(roomname, ccptr, CC);
198 if ((CC->user.axlevel < 6)
199 && (strlen(ccptr->fake_roomname)>0)) {
200 strcpy(roomname, ccptr->fake_roomname);
203 if (((ccptr->cs_flags & CS_CHAT) == 0)
204 && ((ccptr->cs_flags & CS_STEALTH) == 0)) {
205 cprintf(":| %-25s <%s>:\n",
206 (ccptr->fake_username[0]) ? ccptr->fake_username : ccptr->curr_user,
211 end_critical_section(S_SESSION_TABLE);
215 cprintf(":|There are %d users here.\n", count);
218 cprintf(":|Note: you are the only one here.\n");
220 if (count_elsewhere > 0) {
221 cprintf(":|There are %d users chatting in other rooms.\n", count_elsewhere);
229 void cmd_chat(char *argbuf)
234 int MyLastMsg, ThisLastMsg;
235 struct ChatLine *clptr;
236 struct CitContext *t_context;
239 if (!(CC->logged_in)) {
240 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
244 CC->cs_flags = CC->cs_flags | CS_CHAT;
245 cprintf("%d Entering chat mode (type '/help' for available commands)\n",
248 MyLastMsg = ChatLastMsg;
250 if ((CC->cs_flags & CS_STEALTH) == 0) {
251 allwrite("<entering chat>", 0, NULL);
262 linelen = strlen(cmdbuf);
263 if (linelen > 100) --linelen; /* truncate too-long lines */
264 cmdbuf[linelen + 1] = 0;
266 retval = client_read_to(&cmdbuf[linelen], 1, 2);
268 if (retval < 0 || CC->kill_me) { /* socket broken? */
269 if ((CC->cs_flags & CS_STEALTH) == 0) {
270 allwrite("<disconnected>", 0, NULL);
275 /* if we have a complete line, do send processing */
276 if (strlen(cmdbuf) > 0)
277 if (cmdbuf[strlen(cmdbuf) - 1] == 10) {
278 cmdbuf[strlen(cmdbuf) - 1] = 0;
282 if ((!strcasecmp(cmdbuf, "exit"))
283 || (!strcasecmp(cmdbuf, "/exit"))
284 || (!strcasecmp(cmdbuf, "quit"))
285 || (!strcasecmp(cmdbuf, "logout"))
286 || (!strcasecmp(cmdbuf, "logoff"))
287 || (!strcasecmp(cmdbuf, "/q"))
288 || (!strcasecmp(cmdbuf, ".q"))
289 || (!strcasecmp(cmdbuf, "/quit"))
291 strcpy(cmdbuf, "000");
293 if (!strcmp(cmdbuf, "000")) {
294 if ((CC->cs_flags & CS_STEALTH) == 0) {
295 allwrite("<exiting chat>", 0, NULL);
299 CC->cs_flags = CC->cs_flags - CS_CHAT;
302 if ((!strcasecmp(cmdbuf, "/help"))
303 || (!strcasecmp(cmdbuf, "help"))
304 || (!strcasecmp(cmdbuf, "/?"))
305 || (!strcasecmp(cmdbuf, "?"))) {
307 cprintf(":|Available commands: \n");
308 cprintf(":|/help (prints this message) \n");
309 cprintf(":|/who (list users currently in chat) \n");
310 cprintf(":|/whobbs (list users in chat -and- elsewhere) \n");
311 cprintf(":|/me ('action' line, ala irc) \n");
312 cprintf(":|/msg (send private message, ala irc) \n");
313 cprintf(":|/quit (return to the BBS) \n");
317 if (!strcasecmp(cmdbuf, "/who")) {
321 if (!strcasecmp(cmdbuf, "/whobbs")) {
325 if (!strncasecmp(cmdbuf, "/me ", 4)) {
326 allwrite(&cmdbuf[4], 1, NULL);
329 if (!strncasecmp(cmdbuf, "/msg ", 5)) {
331 strptr1 = &cmdbuf[5];
332 if ((t_context = find_context(&strptr1))) {
333 allwrite(strptr1, 2, CC->curr_user);
334 if (strcasecmp(CC->curr_user, t_context->curr_user))
335 allwrite(strptr1, 2, t_context->curr_user);
337 cprintf(":|User not found.\n");
340 if ((cmdbuf[0] != '/') && (strlen(cmdbuf) > 0)) {
342 allwrite(cmdbuf, 0, NULL);
344 if ((!ok_cmd) && (cmdbuf[0]) && (cmdbuf[0] != '\n'))
345 cprintf(":|Command %s is not understood.\n", cmdbuf);
350 /* now check the queue for new incoming stuff */
352 if (CC->fake_username[0])
353 un = CC->fake_username;
356 if (ChatLastMsg > MyLastMsg) {
357 ThisLastMsg = ChatLastMsg;
358 for (clptr = ChatQueue; clptr != NULL; clptr = clptr->next) {
359 if ((clptr->chat_seq > MyLastMsg) && ((!clptr->chat_username[0]) || (!strncasecmp(un, clptr->chat_username, 32)))) {
360 if ((!clptr->chat_room[0]) || (!strncasecmp(CC->room.QRname, clptr->chat_room, ROOMNAMELEN))) {
361 cprintf("%s\n", clptr->chat_text);
365 MyLastMsg = ThisLastMsg;
373 * Delete any remaining express messages
375 void delete_express_messages(void) {
376 struct ExpressMessage *ptr;
378 begin_critical_section(S_SESSION_TABLE);
379 while (CC->FirstExpressMessage != NULL) {
380 ptr = CC->FirstExpressMessage->next;
381 if (CC->FirstExpressMessage->text != NULL)
382 phree(CC->FirstExpressMessage->text);
383 phree(CC->FirstExpressMessage);
384 CC->FirstExpressMessage = ptr;
386 end_critical_section(S_SESSION_TABLE);
393 * Poll for express messages (OLD METHOD -- ***DEPRECATED ***)
395 void cmd_pexp(char *argbuf)
397 struct ExpressMessage *ptr, *holdptr;
399 if (CC->FirstExpressMessage == NULL) {
400 cprintf("%d No express messages waiting.\n", ERROR);
403 begin_critical_section(S_SESSION_TABLE);
404 ptr = CC->FirstExpressMessage;
405 CC->FirstExpressMessage = NULL;
406 end_critical_section(S_SESSION_TABLE);
408 cprintf("%d Express msgs:\n", LISTING_FOLLOWS);
409 while (ptr != NULL) {
410 if (ptr->flags && EM_BROADCAST)
411 cprintf("Broadcast message ");
412 else if (ptr->flags && EM_CHAT)
413 cprintf("Chat request ");
414 else if (ptr->flags && EM_GO_AWAY)
415 cprintf("Please logoff now, as requested ");
418 cprintf("from %s:\n", ptr->sender);
419 if (ptr->text != NULL)
420 memfmout(80, ptr->text, 0, "\n");
423 if (ptr->text != NULL) phree(ptr->text);
432 * Get express messages (new method)
434 void cmd_gexp(char *argbuf) {
435 struct ExpressMessage *ptr;
437 if (CC->FirstExpressMessage == NULL) {
438 cprintf("%d No express messages waiting.\n", ERROR);
442 begin_critical_section(S_SESSION_TABLE);
443 ptr = CC->FirstExpressMessage;
444 CC->FirstExpressMessage = CC->FirstExpressMessage->next;
445 end_critical_section(S_SESSION_TABLE);
447 cprintf("%d %d|%ld|%d|%s|%s\n",
449 ((ptr->next != NULL) ? 1 : 0), /* more msgs? */
450 (long)ptr->timestamp, /* time sent */
451 ptr->flags, /* flags */
452 ptr->sender, /* sender of msg */
453 config.c_nodename); /* static for now */
454 if (ptr->text != NULL) {
455 memfmout(80, ptr->text, 0, "\n");
456 if (ptr->text[strlen(ptr->text)-1] != '\n') cprintf("\n");
464 * Asynchronously deliver express messages'
466 void cmd_gexp_async(void) {
468 /* Only do this if the session can handle asynchronous protocol */
469 if (CC->is_async == 0) return;
471 /* And don't do it if there's nothing to send. */
472 if (CC->FirstExpressMessage == NULL) return;
474 cprintf("%d express msg\n", ASYNC_MSG + ASYNC_GEXP);
479 * Back end support function for send_express_message() and company
481 void add_xmsg_to_context(struct CitContext *ccptr,
482 struct ExpressMessage *newmsg)
484 struct ExpressMessage *findend;
486 if (ccptr->FirstExpressMessage == NULL) {
487 ccptr->FirstExpressMessage = newmsg;
490 findend = ccptr->FirstExpressMessage;
491 while (findend->next != NULL) {
492 findend = findend->next;
494 findend->next = newmsg;
502 * This is the back end to the express message sending function.
503 * Returns the number of users to which the message was sent.
504 * Sending a zero-length message tests for recipients without sending messages.
506 int send_express_message(char *lun, char *x_user, char *x_msg)
508 int message_sent = 0; /* number of successful sends */
510 struct CitContext *ccptr;
511 struct ExpressMessage *newmsg;
514 int do_send = 0; /* set to 1 to actually page, not
515 * just check to see if we can.
517 struct savelist *sl = NULL; /* list of rooms to save this page */
518 struct savelist *sptr;
519 struct CtdlMessage *logmsg;
522 if (strlen(x_msg) > 0) {
523 msglen = strlen(x_msg) + 4;
527 /* find the target user's context and append the message */
528 begin_critical_section(S_SESSION_TABLE);
529 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
531 if (ccptr->fake_username[0]) /* <bc> */
532 un = ccptr->fake_username;
534 un = ccptr->user.fullname;
536 if ( ((!strcasecmp(un, x_user))
537 || (!strcasecmp(x_user, "broadcast")))
538 && ((ccptr->disable_exp == 0)
539 || (CC->user.axlevel >= 6)) ) {
541 newmsg = (struct ExpressMessage *)
542 mallok(sizeof (struct ExpressMessage));
544 sizeof (struct ExpressMessage));
545 time(&(newmsg->timestamp));
546 safestrncpy(newmsg->sender, lun,
547 sizeof newmsg->sender);
548 if (!strcasecmp(x_user, "broadcast"))
549 newmsg->flags |= EM_BROADCAST;
550 newmsg->text = strdoop(x_msg);
552 add_xmsg_to_context(ccptr, newmsg);
556 sptr = (struct savelist *)
557 malloc(sizeof(struct savelist));
559 MailboxName(sptr->roomname,
560 sizeof sptr->roomname,
561 &ccptr->user, PAGELOGROOM);
568 end_critical_section(S_SESSION_TABLE);
570 /* Log the page to disk if configured to do so */
571 if ( (do_send) && (message_sent) ) {
573 logmsg = mallok(sizeof(struct CtdlMessage));
574 memset(logmsg, 0, sizeof(struct CtdlMessage));
575 logmsg->cm_magic = CTDLMESSAGE_MAGIC;
576 logmsg->cm_anon_type = MES_NORMAL;
577 logmsg->cm_format_type = 0;
578 logmsg->cm_fields['A'] = strdoop(lun);
579 logmsg->cm_fields['N'] = strdoop(NODENAME);
580 logmsg->cm_fields['O'] = strdoop(PAGELOGROOM);
581 logmsg->cm_fields['R'] = strdoop(x_user);
582 logmsg->cm_fields['M'] = strdoop(x_msg);
585 /* Save a copy of the message in the sender's log room,
586 * creating the room if necessary.
588 create_room(PAGELOGROOM, 4, "", 0, 1, 0);
589 msgnum = CtdlSubmitMsg(logmsg, NULL, PAGELOGROOM);
591 /* Now save a copy in the global log room, if configured */
592 if (strlen(config.c_logpages) > 0) {
593 create_room(config.c_logpages, 3, "", 0, 1, 1);
594 CtdlSaveMsgPointerInRoom(config.c_logpages, msgnum, 0);
597 /* Save a copy in each recipient's log room, creating those
598 * rooms if necessary. Note that we create as a type 5 room
599 * rather than 4, which indicates that it's a personal room
600 * but we've already supplied the namespace prefix.
603 create_room(sl->roomname, 5, "", 0, 1, 1);
604 CtdlSaveMsgPointerInRoom(sl->roomname, msgnum, 0);
610 CtdlFreeMessage(logmsg);
613 return (message_sent);
617 * send express messages <bc>
619 void cmd_sexp(char *argbuf)
621 int message_sent = 0;
624 char *lun; /* <bc> */
625 char *x_big_msgbuf = NULL;
627 if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
628 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
631 if (CC->fake_username[0])
632 lun = CC->fake_username;
634 lun = CC->user.fullname;
636 extract(x_user, argbuf, 0);
638 extract(x_msg, argbuf, 1);
641 cprintf("%d You were not previously paged.\n", ERROR);
644 if ((!strcasecmp(x_user, "broadcast")) && (CC->user.axlevel < 6)) {
645 cprintf("%d Higher access required to send a broadcast.\n",
646 ERROR + HIGHER_ACCESS_REQUIRED);
649 /* This loop handles text-transfer pages */
650 if (!strcmp(x_msg, "-")) {
651 message_sent = PerformXmsgHooks(lun, x_user, "");
652 if (message_sent == 0) {
653 cprintf("%d '%s' is not logged in "
654 "or is not accepting pages.\n",
658 cprintf("%d Transmit message (will deliver to %d users)\n",
659 SEND_LISTING, message_sent);
660 x_big_msgbuf = mallok(SIZ);
661 memset(x_big_msgbuf, 0, SIZ);
662 while (client_gets(x_msg), strcmp(x_msg, "000")) {
663 x_big_msgbuf = reallok(x_big_msgbuf,
664 strlen(x_big_msgbuf) + strlen(x_msg) + 4);
665 if (strlen(x_big_msgbuf) > 0)
666 if (x_big_msgbuf[strlen(x_big_msgbuf)] != '\n')
667 strcat(x_big_msgbuf, "\n");
668 strcat(x_big_msgbuf, x_msg);
670 PerformXmsgHooks(lun, x_user, x_big_msgbuf);
673 /* This loop handles inline pages */
675 message_sent = PerformXmsgHooks(lun, x_user, x_msg);
677 if (message_sent > 0) {
678 if (strlen(x_msg) > 0)
679 cprintf("%d Message sent", CIT_OK);
681 cprintf("%d Ok to send message", CIT_OK);
682 if (message_sent > 1)
683 cprintf(" to %d users", message_sent);
686 cprintf("%d '%s' is not logged in "
687 "or is not accepting pages.\n",
698 * Enter or exit paging-disabled mode
700 void cmd_dexp(char *argbuf)
704 if (CtdlAccessCheck(ac_logged_in)) return;
706 new_state = extract_int(argbuf, 0);
707 if ((new_state == 0) || (new_state == 1)) {
708 CC->disable_exp = new_state;
711 cprintf("%d %d\n", CIT_OK, CC->disable_exp);
716 * Request client termination
718 void cmd_reqt(char *argbuf) {
719 struct CitContext *ccptr;
722 struct ExpressMessage *newmsg;
724 if (CtdlAccessCheck(ac_aide)) return;
725 which_session = extract_int(argbuf, 0);
727 begin_critical_section(S_SESSION_TABLE);
728 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
729 if ((ccptr->cs_pid == which_session) || (which_session == 0)) {
731 newmsg = (struct ExpressMessage *)
732 mallok(sizeof (struct ExpressMessage));
734 sizeof (struct ExpressMessage));
735 time(&(newmsg->timestamp));
736 safestrncpy(newmsg->sender, CC->user.fullname,
737 sizeof newmsg->sender);
738 newmsg->flags |= EM_GO_AWAY;
739 newmsg->text = strdoop("Automatic logoff requested.");
741 add_xmsg_to_context(ccptr, newmsg);
746 end_critical_section(S_SESSION_TABLE);
747 cprintf("%d Sent termination request to %d sessions.\n", CIT_OK, sessions);
752 char *serv_chat_init(void)
754 CtdlRegisterProtoHook(cmd_chat, "CHAT", "Begin real-time chat");
755 CtdlRegisterProtoHook(cmd_pexp, "PEXP", "Poll for express messages");
756 CtdlRegisterProtoHook(cmd_gexp, "GEXP", "Get express messages");
757 CtdlRegisterProtoHook(cmd_sexp, "SEXP", "Send an express message");
758 CtdlRegisterProtoHook(cmd_dexp, "DEXP", "Disable express messages");
759 CtdlRegisterProtoHook(cmd_reqt, "REQT", "Request client termination");
760 CtdlRegisterSessionHook(cmd_gexp_async, EVT_CMD);
761 CtdlRegisterSessionHook(delete_express_messages, EVT_STOP);
762 CtdlRegisterXmsgHook(send_express_message, XMSG_PRI_LOCAL);