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 "dynloader.h"
36 #include "serv_chat.h"
37 #include "sysdep_decls.h"
38 #include "citserver.h"
46 struct ChatLine *ChatQueue = NULL;
52 void allwrite(char *cmdbuf, int flag, char *username)
57 struct ChatLine *clptr, *clnew;
60 if (CC->fake_username[0])
61 un = CC->fake_username;
63 un = CC->usersupp.fullname;
65 snprintf(bcast, sizeof bcast, ":|<%s %s>", un, cmdbuf);
66 } else if (flag == 0) {
67 snprintf(bcast, sizeof bcast, "%s|%s", un, cmdbuf);
68 } else if (flag == 2) {
69 snprintf(bcast, sizeof bcast, ":|<%s whispers %s>", un, cmdbuf);
71 if ((strcasecmp(cmdbuf, "NOOP")) && (flag != 2)) {
72 fp = fopen(CHATLOG, "a");
73 fprintf(fp, "%s\n", bcast);
76 clnew = (struct ChatLine *) mallok(sizeof(struct ChatLine));
77 memset(clnew, 0, sizeof(struct ChatLine));
79 fprintf(stderr, "citserver: cannot alloc chat line: %s\n",
85 clnew->chat_time = now;
86 safestrncpy(clnew->chat_room, CC->quickroom.QRname,
87 sizeof clnew->chat_room);
88 clnew->chat_room[sizeof clnew->chat_room - 1] = 0;
90 safestrncpy(clnew->chat_username, username,
91 sizeof clnew->chat_username);
92 clnew->chat_username[sizeof clnew->chat_username - 1] = 0;
94 clnew->chat_username[0] = '\0';
95 safestrncpy(clnew->chat_text, bcast, sizeof clnew->chat_text);
97 /* Here's the critical section.
98 * First, add the new message to the queue...
100 begin_critical_section(S_CHATQUEUE);
102 clnew->chat_seq = ChatLastMsg;
103 if (ChatQueue == NULL) {
106 for (clptr = ChatQueue; clptr->next != NULL; clptr = clptr->next);;
110 /* Then, before releasing the lock, free the expired messages */
111 while ((ChatQueue != NULL) && (now - ChatQueue->chat_time >= 120L)) {
113 ChatQueue = ChatQueue->next;
116 end_critical_section(S_CHATQUEUE);
120 t_context *find_context(char **unstr)
122 t_context *t_cc, *found_cc = NULL;
125 if ((!*unstr) || (!unstr))
128 begin_critical_section(S_SESSION_TABLE);
129 for (t_cc = ContextList; ((t_cc) && (!found_cc)); t_cc = t_cc->next) {
130 if (t_cc->fake_username[0])
131 name = t_cc->fake_username;
133 name = t_cc->curr_user;
135 if ((!strncasecmp(name, tptr, strlen(name))) && (tptr[strlen(name)] == ' ')) {
137 *unstr = &(tptr[strlen(name) + 1]);
140 end_critical_section(S_SESSION_TABLE);
146 * List users in chat.
147 * allflag == 0 = list users in chat
148 * 1 = list users in chat, followed by users not in chat
149 * 2 = display count only
152 void do_chat_listing(int allflag)
154 struct CitContext *ccptr;
156 int count_elsewhere = 0;
157 char roomname[ROOMNAMELEN];
159 if ((allflag == 0) || (allflag == 1))
160 cprintf(":|\n:| Users currently in chat:\n");
161 begin_critical_section(S_SESSION_TABLE);
162 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
163 if (ccptr->cs_flags & CS_CHAT) {
164 if (!strcasecmp(ccptr->quickroom.QRname,
165 CC->quickroom.QRname)) {
173 GenerateRoomDisplay(roomname, ccptr, CC);
174 if ((CC->usersupp.axlevel < 6)
175 && (strlen(ccptr->fake_roomname)>0)) {
176 strcpy(roomname, ccptr->fake_roomname);
179 if ((ccptr->cs_flags & CS_CHAT)
180 && ((ccptr->cs_flags & CS_STEALTH) == 0)) {
181 if ((allflag == 0) || (allflag == 1)) {
182 cprintf(":| %-25s <%s>:\n",
183 (ccptr->fake_username[0]) ? ccptr->fake_username : ccptr->curr_user,
190 cprintf(":|\n:| Users not in chat:\n");
191 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
193 GenerateRoomDisplay(roomname, ccptr, CC);
194 if ((CC->usersupp.axlevel < 6)
195 && (strlen(ccptr->fake_roomname)>0)) {
196 strcpy(roomname, ccptr->fake_roomname);
199 if (((ccptr->cs_flags & CS_CHAT) == 0)
200 && ((ccptr->cs_flags & CS_STEALTH) == 0)) {
201 cprintf(":| %-25s <%s>:\n",
202 (ccptr->fake_username[0]) ? ccptr->fake_username : ccptr->curr_user,
207 end_critical_section(S_SESSION_TABLE);
211 cprintf(":|There are %d users here.\n", count);
214 cprintf(":|Note: you are the only one here.\n");
216 if (count_elsewhere > 0) {
217 cprintf(":|There are %d users chatting in other rooms.\n", count_elsewhere);
225 void cmd_chat(char *argbuf)
230 int MyLastMsg, ThisLastMsg;
231 struct ChatLine *clptr;
232 struct CitContext *t_context;
235 if (!(CC->logged_in)) {
236 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
240 CC->cs_flags = CC->cs_flags | CS_CHAT;
241 cprintf("%d Entering chat mode (type '/help' for available commands)\n",
244 MyLastMsg = ChatLastMsg;
246 if ((CC->cs_flags & CS_STEALTH) == 0) {
247 allwrite("<entering chat>", 0, NULL);
258 linelen = strlen(cmdbuf);
259 if (linelen > 100) --linelen; /* truncate too-long lines */
260 cmdbuf[linelen + 1] = 0;
262 retval = client_read_to(&cmdbuf[linelen], 1, 2);
264 if (retval < 0) { /* socket broken? */
265 if ((CC->cs_flags & CS_STEALTH) == 0) {
266 allwrite("<disconnected>", 0, NULL);
271 /* if we have a complete line, do send processing */
272 if (strlen(cmdbuf) > 0)
273 if (cmdbuf[strlen(cmdbuf) - 1] == 10) {
274 cmdbuf[strlen(cmdbuf) - 1] = 0;
278 if ((!strcasecmp(cmdbuf, "exit"))
279 || (!strcasecmp(cmdbuf, "/exit"))
280 || (!strcasecmp(cmdbuf, "quit"))
281 || (!strcasecmp(cmdbuf, "logout"))
282 || (!strcasecmp(cmdbuf, "logoff"))
283 || (!strcasecmp(cmdbuf, "/q"))
284 || (!strcasecmp(cmdbuf, ".q"))
285 || (!strcasecmp(cmdbuf, "/quit"))
287 strcpy(cmdbuf, "000");
289 if (!strcmp(cmdbuf, "000")) {
290 if ((CC->cs_flags & CS_STEALTH) == 0) {
291 allwrite("<exiting chat>", 0, NULL);
295 CC->cs_flags = CC->cs_flags - CS_CHAT;
298 if ((!strcasecmp(cmdbuf, "/help"))
299 || (!strcasecmp(cmdbuf, "help"))
300 || (!strcasecmp(cmdbuf, "/?"))
301 || (!strcasecmp(cmdbuf, "?"))) {
303 cprintf(":|Available commands: \n");
304 cprintf(":|/help (prints this message) \n");
305 cprintf(":|/who (list users currently in chat) \n");
306 cprintf(":|/whobbs (list users in chat -and- elsewhere) \n");
307 cprintf(":|/me ('action' line, ala irc) \n");
308 cprintf(":|/msg (send private message, ala irc) \n");
309 cprintf(":|/quit (return to the BBS) \n");
313 if (!strcasecmp(cmdbuf, "/who")) {
317 if (!strcasecmp(cmdbuf, "/whobbs")) {
321 if (!strncasecmp(cmdbuf, "/me ", 4)) {
322 allwrite(&cmdbuf[4], 1, NULL);
325 if (!strncasecmp(cmdbuf, "/msg ", 5)) {
327 strptr1 = &cmdbuf[5];
328 if ((t_context = find_context(&strptr1))) {
329 allwrite(strptr1, 2, CC->curr_user);
330 if (strcasecmp(CC->curr_user, t_context->curr_user))
331 allwrite(strptr1, 2, t_context->curr_user);
333 cprintf(":|User not found.\n", cmdbuf);
336 if ((cmdbuf[0] != '/') && (strlen(cmdbuf) > 0)) {
338 allwrite(cmdbuf, 0, NULL);
340 if ((!ok_cmd) && (cmdbuf[0]) && (cmdbuf[0] != '\n'))
341 cprintf(":|Command %s is not understood.\n", cmdbuf);
346 /* now check the queue for new incoming stuff */
348 if (CC->fake_username[0])
349 un = CC->fake_username;
352 if (ChatLastMsg > MyLastMsg) {
353 ThisLastMsg = ChatLastMsg;
354 for (clptr = ChatQueue; clptr != NULL; clptr = clptr->next) {
355 if ((clptr->chat_seq > MyLastMsg) && ((!clptr->chat_username[0]) || (!strncasecmp(un, clptr->chat_username, 32)))) {
356 if ((!clptr->chat_room[0]) || (!strncasecmp(CC->quickroom.QRname, clptr->chat_room, ROOMNAMELEN))) {
357 cprintf("%s\n", clptr->chat_text);
361 MyLastMsg = ThisLastMsg;
369 * Delete any remaining express messages
371 void delete_express_messages(void) {
372 struct ExpressMessage *ptr;
374 begin_critical_section(S_SESSION_TABLE);
375 while (CC->FirstExpressMessage != NULL) {
376 ptr = CC->FirstExpressMessage->next;
377 if (CC->FirstExpressMessage->text != NULL)
378 phree(CC->FirstExpressMessage->text);
379 phree(CC->FirstExpressMessage);
380 CC->FirstExpressMessage = ptr;
382 end_critical_section(S_SESSION_TABLE);
389 * Poll for express messages (OLD METHOD -- ***DEPRECATED ***)
391 void cmd_pexp(char *argbuf)
393 struct ExpressMessage *ptr, *holdptr;
395 if (CC->FirstExpressMessage == NULL) {
396 cprintf("%d No express messages waiting.\n", ERROR);
399 begin_critical_section(S_SESSION_TABLE);
400 ptr = CC->FirstExpressMessage;
401 CC->FirstExpressMessage = NULL;
402 end_critical_section(S_SESSION_TABLE);
404 cprintf("%d Express msgs:\n", LISTING_FOLLOWS);
405 while (ptr != NULL) {
406 if (ptr->flags && EM_BROADCAST)
407 cprintf("Broadcast message ");
408 else if (ptr->flags && EM_CHAT)
409 cprintf("Chat request ");
410 else if (ptr->flags && EM_GO_AWAY)
411 cprintf("Please logoff now, as requested ");
414 cprintf("from %s:\n", ptr->sender);
415 if (ptr->text != NULL)
416 memfmout(80, ptr->text, 0, "\n");
419 if (ptr->text != NULL) phree(ptr->text);
428 * Get express messages (new method)
430 void cmd_gexp(char *argbuf) {
431 struct ExpressMessage *ptr;
433 if (CC->FirstExpressMessage == NULL) {
434 cprintf("%d No express messages waiting.\n", ERROR);
438 begin_critical_section(S_SESSION_TABLE);
439 ptr = CC->FirstExpressMessage;
440 CC->FirstExpressMessage = CC->FirstExpressMessage->next;
441 end_critical_section(S_SESSION_TABLE);
443 cprintf("%d %d|%ld|%d|%s|%s\n",
445 ((ptr->next != NULL) ? 1 : 0), /* more msgs? */
446 ptr->timestamp, /* time sent */
447 ptr->flags, /* flags */
448 ptr->sender, /* sender of msg */
449 config.c_nodename); /* static for now */
450 if (ptr->text != NULL) {
451 memfmout(80, ptr->text, 0, "\n");
452 if (ptr->text[strlen(ptr->text)-1] != '\n') cprintf("\n");
460 * Asynchronously deliver express messages'
462 void cmd_gexp_async(void) {
464 /* Only do this if the session can handle asynchronous protocol */
465 if (CC->is_async == 0) return;
467 /* And don't do it if there's nothing to send. */
468 if (CC->FirstExpressMessage == NULL) return;
470 cprintf("%d express msg\n", ASYNC_MSG + ASYNC_GEXP);
475 * Back end support function for send_express_message() and company
477 void add_xmsg_to_context(struct CitContext *ccptr,
478 struct ExpressMessage *newmsg)
480 struct ExpressMessage *findend;
482 if (ccptr->FirstExpressMessage == NULL) {
483 ccptr->FirstExpressMessage = newmsg;
486 findend = ccptr->FirstExpressMessage;
487 while (findend->next != NULL) {
488 findend = findend->next;
490 findend->next = newmsg;
498 * This is the back end to the express message sending function.
499 * Returns the number of users to which the message was sent.
500 * Sending a zero-length message tests for recipients without sending messages.
502 int send_express_message(char *lun, char *x_user, char *x_msg)
504 int message_sent = 0; /* number of successful sends */
506 struct CitContext *ccptr;
507 struct ExpressMessage *newmsg;
510 int do_send = 0; /* set to 1 to actually page, not
511 * just check to see if we can.
513 struct savelist *sl = NULL; /* list of rooms to save this page */
514 struct savelist *sptr;
515 struct CtdlMessage *logmsg;
518 if (strlen(x_msg) > 0) {
519 msglen = strlen(x_msg) + 4;
523 /* find the target user's context and append the message */
524 begin_critical_section(S_SESSION_TABLE);
525 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
527 if (ccptr->fake_username[0]) /* <bc> */
528 un = ccptr->fake_username;
530 un = ccptr->usersupp.fullname;
532 if ( ((!strcasecmp(un, x_user))
533 || (!strcasecmp(x_user, "broadcast")))
534 && ((ccptr->disable_exp == 0)
535 || (CC->usersupp.axlevel >= 6)) ) {
537 newmsg = (struct ExpressMessage *)
538 mallok(sizeof (struct ExpressMessage));
540 sizeof (struct ExpressMessage));
541 time(&(newmsg->timestamp));
542 safestrncpy(newmsg->sender, lun,
543 sizeof newmsg->sender);
544 if (!strcasecmp(x_user, "broadcast"))
545 newmsg->flags |= EM_BROADCAST;
546 newmsg->text = strdoop(x_msg);
548 add_xmsg_to_context(ccptr, newmsg);
552 sptr = (struct savelist *)
553 malloc(sizeof(struct savelist));
555 MailboxName(sptr->roomname,
556 &ccptr->usersupp, PAGELOGROOM);
563 end_critical_section(S_SESSION_TABLE);
565 /* Log the page to disk if configured to do so */
566 if ( (do_send) && (message_sent) ) {
568 logmsg = mallok(sizeof(struct CtdlMessage));
569 memset(logmsg, 0, sizeof(struct CtdlMessage));
570 logmsg->cm_magic = CTDLMESSAGE_MAGIC;
571 logmsg->cm_anon_type = MES_NORMAL;
572 logmsg->cm_format_type = 0;
573 logmsg->cm_fields['A'] = strdoop(lun);
574 logmsg->cm_fields['N'] = strdoop(NODENAME);
575 logmsg->cm_fields['O'] = strdoop(PAGELOGROOM);
576 logmsg->cm_fields['R'] = strdoop(x_user);
577 logmsg->cm_fields['M'] = strdoop(x_msg);
580 /* Save a copy of the message in the sender's log room,
581 * creating the room if necessary.
583 create_room(PAGELOGROOM, 4, "", 0, 1);
584 msgnum = CtdlSaveMsg(logmsg, "", PAGELOGROOM, MES_LOCAL);
586 /* Now save a copy in the global log room, if configured */
587 if (strlen(config.c_logpages) > 0) {
588 create_room(config.c_logpages, 3, "", 0, 1);
589 CtdlSaveMsgPointerInRoom(config.c_logpages, msgnum, 0);
592 /* Save a copy in each recipient's log room, creating those
593 * rooms if necessary. Note that we create as a type 5 room
594 * rather than 4, which indicates that it's a personal room
595 * but we've already supplied the namespace prefix.
598 create_room(sl->roomname, 5, "", 0, 1);
599 CtdlSaveMsgPointerInRoom(sl->roomname, msgnum, 0);
605 CtdlFreeMessage(logmsg);
608 return (message_sent);
612 * send express messages <bc>
614 void cmd_sexp(char *argbuf)
616 int message_sent = 0;
619 char *lun; /* <bc> */
620 char *x_big_msgbuf = NULL;
622 if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
623 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
626 if (CC->fake_username[0])
627 lun = CC->fake_username;
629 lun = CC->usersupp.fullname;
631 extract(x_user, argbuf, 0);
633 extract(x_msg, argbuf, 1);
636 cprintf("%d You were not previously paged.\n", ERROR);
639 if ((!strcasecmp(x_user, "broadcast")) && (CC->usersupp.axlevel < 6)) {
640 cprintf("%d Higher access required to send a broadcast.\n",
641 ERROR + HIGHER_ACCESS_REQUIRED);
644 /* This loop handles text-transfer pages */
645 if (!strcmp(x_msg, "-")) {
646 message_sent = PerformXmsgHooks(lun, x_user, "");
647 if (message_sent == 0) {
648 cprintf("%d '%s' is not logged in "
649 "or is not accepting pages.\n",
653 cprintf("%d Transmit message (will deliver to %d users)\n",
654 SEND_LISTING, message_sent);
655 x_big_msgbuf = mallok(SIZ);
656 memset(x_big_msgbuf, 0, SIZ);
657 while (client_gets(x_msg), strcmp(x_msg, "000")) {
658 x_big_msgbuf = reallok(x_big_msgbuf,
659 strlen(x_big_msgbuf) + strlen(x_msg) + 4);
660 if (strlen(x_big_msgbuf) > 0)
661 if (x_big_msgbuf[strlen(x_big_msgbuf)] != '\n')
662 strcat(x_big_msgbuf, "\n");
663 strcat(x_big_msgbuf, x_msg);
665 PerformXmsgHooks(lun, x_user, x_big_msgbuf);
668 /* This loop handles inline pages */
670 message_sent = PerformXmsgHooks(lun, x_user, x_msg);
672 if (message_sent > 0) {
673 if (strlen(x_msg) > 0)
674 cprintf("%d Message sent", OK);
676 cprintf("%d Ok to send message", OK);
677 if (message_sent > 1)
678 cprintf(" to %d users", message_sent);
681 cprintf("%d '%s' is not logged in "
682 "or is not accepting pages.\n",
693 * Enter or exit paging-disabled mode
695 void cmd_dexp(char *argbuf)
699 if (!CC->logged_in) {
700 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
704 new_state = extract_int(argbuf, 0);
705 if ((new_state == 0) || (new_state == 1)) {
706 CC->disable_exp = new_state;
708 cprintf("%d %d\n", OK, CC->disable_exp);
713 * Request client termination
715 void cmd_reqt(char *argbuf) {
716 struct CitContext *ccptr;
719 struct ExpressMessage *newmsg;
721 if (CtdlAccessCheck(ac_aide)) return;
722 which_session = extract_int(argbuf, 0);
724 begin_critical_section(S_SESSION_TABLE);
725 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
726 if ((ccptr->cs_pid == which_session) || (which_session == 0)) {
728 newmsg = (struct ExpressMessage *)
729 mallok(sizeof (struct ExpressMessage));
731 sizeof (struct ExpressMessage));
732 time(&(newmsg->timestamp));
733 safestrncpy(newmsg->sender, CC->usersupp.fullname,
734 sizeof newmsg->sender);
735 newmsg->flags |= EM_GO_AWAY;
736 newmsg->text = strdoop("Automatic logoff requested.");
738 add_xmsg_to_context(ccptr, newmsg);
743 end_critical_section(S_SESSION_TABLE);
744 cprintf("%d Sent termination request to %d sessions.\n", OK, sessions);
749 char *Dynamic_Module_Init(void)
751 CtdlRegisterProtoHook(cmd_chat, "CHAT", "Begin real-time chat");
752 CtdlRegisterProtoHook(cmd_pexp, "PEXP", "Poll for express messages");
753 CtdlRegisterProtoHook(cmd_gexp, "GEXP", "Get express messages");
754 CtdlRegisterProtoHook(cmd_sexp, "SEXP", "Send an express message");
755 CtdlRegisterProtoHook(cmd_dexp, "DEXP", "Disable express messages");
756 CtdlRegisterProtoHook(cmd_reqt, "REQT", "Request client termination");
757 CtdlRegisterSessionHook(cmd_gexp_async, EVT_CMD);
758 CtdlRegisterSessionHook(delete_express_messages, EVT_STOP);
759 CtdlRegisterXmsgHook(send_express_message, XMSG_PRI_LOCAL);