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"
50 struct ChatLine *ChatQueue = NULL;
56 void allwrite(char *cmdbuf, int flag, char *username)
61 struct ChatLine *clptr, *clnew;
64 if (CC->fake_username[0])
65 un = CC->fake_username;
67 un = CC->usersupp.fullname;
69 snprintf(bcast, sizeof bcast, ":|<%s %s>", un, cmdbuf);
70 } else if (flag == 0) {
71 snprintf(bcast, sizeof bcast, "%s|%s", un, cmdbuf);
72 } else if (flag == 2) {
73 snprintf(bcast, sizeof bcast, ":|<%s whispers %s>", un, cmdbuf);
75 if ((strcasecmp(cmdbuf, "NOOP")) && (flag != 2)) {
76 fp = fopen(CHATLOG, "a");
78 fprintf(fp, "%s\n", bcast);
81 clnew = (struct ChatLine *) mallok(sizeof(struct ChatLine));
82 memset(clnew, 0, sizeof(struct ChatLine));
84 fprintf(stderr, "citserver: cannot alloc chat line: %s\n",
90 clnew->chat_time = now;
91 safestrncpy(clnew->chat_room, CC->quickroom.QRname,
92 sizeof clnew->chat_room);
93 clnew->chat_room[sizeof clnew->chat_room - 1] = 0;
95 safestrncpy(clnew->chat_username, username,
96 sizeof clnew->chat_username);
97 clnew->chat_username[sizeof clnew->chat_username - 1] = 0;
99 clnew->chat_username[0] = '\0';
100 safestrncpy(clnew->chat_text, bcast, sizeof clnew->chat_text);
102 /* Here's the critical section.
103 * First, add the new message to the queue...
105 begin_critical_section(S_CHATQUEUE);
107 clnew->chat_seq = ChatLastMsg;
108 if (ChatQueue == NULL) {
111 for (clptr = ChatQueue; clptr->next != NULL; clptr = clptr->next);;
115 /* Then, before releasing the lock, free the expired messages */
116 while ((ChatQueue != NULL) && (now - ChatQueue->chat_time >= 120L)) {
118 ChatQueue = ChatQueue->next;
121 end_critical_section(S_CHATQUEUE);
125 t_context *find_context(char **unstr)
127 t_context *t_cc, *found_cc = NULL;
130 if ((!*unstr) || (!unstr))
133 begin_critical_section(S_SESSION_TABLE);
134 for (t_cc = ContextList; ((t_cc) && (!found_cc)); t_cc = t_cc->next) {
135 if (t_cc->fake_username[0])
136 name = t_cc->fake_username;
138 name = t_cc->curr_user;
140 if ((!strncasecmp(name, tptr, strlen(name))) && (tptr[strlen(name)] == ' ')) {
142 *unstr = &(tptr[strlen(name) + 1]);
145 end_critical_section(S_SESSION_TABLE);
151 * List users in chat.
152 * allflag == 0 = list users in chat
153 * 1 = list users in chat, followed by users not in chat
154 * 2 = display count only
157 void do_chat_listing(int allflag)
159 struct CitContext *ccptr;
161 int count_elsewhere = 0;
162 char roomname[ROOMNAMELEN];
164 if ((allflag == 0) || (allflag == 1))
165 cprintf(":|\n:| Users currently in chat:\n");
166 begin_critical_section(S_SESSION_TABLE);
167 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
168 if (ccptr->cs_flags & CS_CHAT) {
169 if (!strcasecmp(ccptr->quickroom.QRname,
170 CC->quickroom.QRname)) {
178 GenerateRoomDisplay(roomname, ccptr, CC);
179 if ((CC->usersupp.axlevel < 6)
180 && (strlen(ccptr->fake_roomname)>0)) {
181 strcpy(roomname, ccptr->fake_roomname);
184 if ((ccptr->cs_flags & CS_CHAT)
185 && ((ccptr->cs_flags & CS_STEALTH) == 0)) {
186 if ((allflag == 0) || (allflag == 1)) {
187 cprintf(":| %-25s <%s>:\n",
188 (ccptr->fake_username[0]) ? ccptr->fake_username : ccptr->curr_user,
195 cprintf(":|\n:| Users not in chat:\n");
196 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
198 GenerateRoomDisplay(roomname, ccptr, CC);
199 if ((CC->usersupp.axlevel < 6)
200 && (strlen(ccptr->fake_roomname)>0)) {
201 strcpy(roomname, ccptr->fake_roomname);
204 if (((ccptr->cs_flags & CS_CHAT) == 0)
205 && ((ccptr->cs_flags & CS_STEALTH) == 0)) {
206 cprintf(":| %-25s <%s>:\n",
207 (ccptr->fake_username[0]) ? ccptr->fake_username : ccptr->curr_user,
212 end_critical_section(S_SESSION_TABLE);
216 cprintf(":|There are %d users here.\n", count);
219 cprintf(":|Note: you are the only one here.\n");
221 if (count_elsewhere > 0) {
222 cprintf(":|There are %d users chatting in other rooms.\n", count_elsewhere);
230 void cmd_chat(char *argbuf)
235 int MyLastMsg, ThisLastMsg;
236 struct ChatLine *clptr;
237 struct CitContext *t_context;
240 if (!(CC->logged_in)) {
241 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
245 CC->cs_flags = CC->cs_flags | CS_CHAT;
246 cprintf("%d Entering chat mode (type '/help' for available commands)\n",
249 MyLastMsg = ChatLastMsg;
251 if ((CC->cs_flags & CS_STEALTH) == 0) {
252 allwrite("<entering chat>", 0, NULL);
263 linelen = strlen(cmdbuf);
264 if (linelen > 100) --linelen; /* truncate too-long lines */
265 cmdbuf[linelen + 1] = 0;
267 retval = client_read_to(&cmdbuf[linelen], 1, 2);
269 if (retval < 0 || CC->kill_me) { /* socket broken? */
270 if ((CC->cs_flags & CS_STEALTH) == 0) {
271 allwrite("<disconnected>", 0, NULL);
276 /* if we have a complete line, do send processing */
277 if (strlen(cmdbuf) > 0)
278 if (cmdbuf[strlen(cmdbuf) - 1] == 10) {
279 cmdbuf[strlen(cmdbuf) - 1] = 0;
283 if ((!strcasecmp(cmdbuf, "exit"))
284 || (!strcasecmp(cmdbuf, "/exit"))
285 || (!strcasecmp(cmdbuf, "quit"))
286 || (!strcasecmp(cmdbuf, "logout"))
287 || (!strcasecmp(cmdbuf, "logoff"))
288 || (!strcasecmp(cmdbuf, "/q"))
289 || (!strcasecmp(cmdbuf, ".q"))
290 || (!strcasecmp(cmdbuf, "/quit"))
292 strcpy(cmdbuf, "000");
294 if (!strcmp(cmdbuf, "000")) {
295 if ((CC->cs_flags & CS_STEALTH) == 0) {
296 allwrite("<exiting chat>", 0, NULL);
300 CC->cs_flags = CC->cs_flags - CS_CHAT;
303 if ((!strcasecmp(cmdbuf, "/help"))
304 || (!strcasecmp(cmdbuf, "help"))
305 || (!strcasecmp(cmdbuf, "/?"))
306 || (!strcasecmp(cmdbuf, "?"))) {
308 cprintf(":|Available commands: \n");
309 cprintf(":|/help (prints this message) \n");
310 cprintf(":|/who (list users currently in chat) \n");
311 cprintf(":|/whobbs (list users in chat -and- elsewhere) \n");
312 cprintf(":|/me ('action' line, ala irc) \n");
313 cprintf(":|/msg (send private message, ala irc) \n");
314 cprintf(":|/quit (return to the BBS) \n");
318 if (!strcasecmp(cmdbuf, "/who")) {
322 if (!strcasecmp(cmdbuf, "/whobbs")) {
326 if (!strncasecmp(cmdbuf, "/me ", 4)) {
327 allwrite(&cmdbuf[4], 1, NULL);
330 if (!strncasecmp(cmdbuf, "/msg ", 5)) {
332 strptr1 = &cmdbuf[5];
333 if ((t_context = find_context(&strptr1))) {
334 allwrite(strptr1, 2, CC->curr_user);
335 if (strcasecmp(CC->curr_user, t_context->curr_user))
336 allwrite(strptr1, 2, t_context->curr_user);
338 cprintf(":|User not found.\n");
341 if ((cmdbuf[0] != '/') && (strlen(cmdbuf) > 0)) {
343 allwrite(cmdbuf, 0, NULL);
345 if ((!ok_cmd) && (cmdbuf[0]) && (cmdbuf[0] != '\n'))
346 cprintf(":|Command %s is not understood.\n", cmdbuf);
351 /* now check the queue for new incoming stuff */
353 if (CC->fake_username[0])
354 un = CC->fake_username;
357 if (ChatLastMsg > MyLastMsg) {
358 ThisLastMsg = ChatLastMsg;
359 for (clptr = ChatQueue; clptr != NULL; clptr = clptr->next) {
360 if ((clptr->chat_seq > MyLastMsg) && ((!clptr->chat_username[0]) || (!strncasecmp(un, clptr->chat_username, 32)))) {
361 if ((!clptr->chat_room[0]) || (!strncasecmp(CC->quickroom.QRname, clptr->chat_room, ROOMNAMELEN))) {
362 cprintf("%s\n", clptr->chat_text);
366 MyLastMsg = ThisLastMsg;
374 * Delete any remaining express messages
376 void delete_express_messages(void) {
377 struct ExpressMessage *ptr;
379 begin_critical_section(S_SESSION_TABLE);
380 while (CC->FirstExpressMessage != NULL) {
381 ptr = CC->FirstExpressMessage->next;
382 if (CC->FirstExpressMessage->text != NULL)
383 phree(CC->FirstExpressMessage->text);
384 phree(CC->FirstExpressMessage);
385 CC->FirstExpressMessage = ptr;
387 end_critical_section(S_SESSION_TABLE);
394 * Poll for express messages (OLD METHOD -- ***DEPRECATED ***)
396 void cmd_pexp(char *argbuf)
398 struct ExpressMessage *ptr, *holdptr;
400 if (CC->FirstExpressMessage == NULL) {
401 cprintf("%d No express messages waiting.\n", ERROR);
404 begin_critical_section(S_SESSION_TABLE);
405 ptr = CC->FirstExpressMessage;
406 CC->FirstExpressMessage = NULL;
407 end_critical_section(S_SESSION_TABLE);
409 cprintf("%d Express msgs:\n", LISTING_FOLLOWS);
410 while (ptr != NULL) {
411 if (ptr->flags && EM_BROADCAST)
412 cprintf("Broadcast message ");
413 else if (ptr->flags && EM_CHAT)
414 cprintf("Chat request ");
415 else if (ptr->flags && EM_GO_AWAY)
416 cprintf("Please logoff now, as requested ");
419 cprintf("from %s:\n", ptr->sender);
420 if (ptr->text != NULL)
421 memfmout(80, ptr->text, 0, "\n");
424 if (ptr->text != NULL) phree(ptr->text);
433 * Get express messages (new method)
435 void cmd_gexp(char *argbuf) {
436 struct ExpressMessage *ptr;
438 if (CC->FirstExpressMessage == NULL) {
439 cprintf("%d No express messages waiting.\n", ERROR);
443 begin_critical_section(S_SESSION_TABLE);
444 ptr = CC->FirstExpressMessage;
445 CC->FirstExpressMessage = CC->FirstExpressMessage->next;
446 end_critical_section(S_SESSION_TABLE);
448 cprintf("%d %d|%ld|%d|%s|%s\n",
450 ((ptr->next != NULL) ? 1 : 0), /* more msgs? */
451 (long)ptr->timestamp, /* time sent */
452 ptr->flags, /* flags */
453 ptr->sender, /* sender of msg */
454 config.c_nodename); /* static for now */
455 if (ptr->text != NULL) {
456 memfmout(80, ptr->text, 0, "\n");
457 if (ptr->text[strlen(ptr->text)-1] != '\n') cprintf("\n");
465 * Asynchronously deliver express messages'
467 void cmd_gexp_async(void) {
469 /* Only do this if the session can handle asynchronous protocol */
470 if (CC->is_async == 0) return;
472 /* And don't do it if there's nothing to send. */
473 if (CC->FirstExpressMessage == NULL) return;
475 cprintf("%d express msg\n", ASYNC_MSG + ASYNC_GEXP);
480 * Back end support function for send_express_message() and company
482 void add_xmsg_to_context(struct CitContext *ccptr,
483 struct ExpressMessage *newmsg)
485 struct ExpressMessage *findend;
487 if (ccptr->FirstExpressMessage == NULL) {
488 ccptr->FirstExpressMessage = newmsg;
491 findend = ccptr->FirstExpressMessage;
492 while (findend->next != NULL) {
493 findend = findend->next;
495 findend->next = newmsg;
503 * This is the back end to the express message sending function.
504 * Returns the number of users to which the message was sent.
505 * Sending a zero-length message tests for recipients without sending messages.
507 int send_express_message(char *lun, char *x_user, char *x_msg)
509 int message_sent = 0; /* number of successful sends */
511 struct CitContext *ccptr;
512 struct ExpressMessage *newmsg;
515 int do_send = 0; /* set to 1 to actually page, not
516 * just check to see if we can.
518 struct savelist *sl = NULL; /* list of rooms to save this page */
519 struct savelist *sptr;
520 struct CtdlMessage *logmsg;
523 if (strlen(x_msg) > 0) {
524 msglen = strlen(x_msg) + 4;
528 /* find the target user's context and append the message */
529 begin_critical_section(S_SESSION_TABLE);
530 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
532 if (ccptr->fake_username[0]) /* <bc> */
533 un = ccptr->fake_username;
535 un = ccptr->usersupp.fullname;
537 if ( ((!strcasecmp(un, x_user))
538 || (!strcasecmp(x_user, "broadcast")))
539 && ((ccptr->disable_exp == 0)
540 || (CC->usersupp.axlevel >= 6)) ) {
542 newmsg = (struct ExpressMessage *)
543 mallok(sizeof (struct ExpressMessage));
545 sizeof (struct ExpressMessage));
546 time(&(newmsg->timestamp));
547 safestrncpy(newmsg->sender, lun,
548 sizeof newmsg->sender);
549 if (!strcasecmp(x_user, "broadcast"))
550 newmsg->flags |= EM_BROADCAST;
551 newmsg->text = strdoop(x_msg);
553 add_xmsg_to_context(ccptr, newmsg);
557 sptr = (struct savelist *)
558 malloc(sizeof(struct savelist));
560 MailboxName(sptr->roomname,
561 sizeof sptr->roomname,
562 &ccptr->usersupp, PAGELOGROOM);
569 end_critical_section(S_SESSION_TABLE);
571 /* Log the page to disk if configured to do so */
572 if ( (do_send) && (message_sent) ) {
574 logmsg = mallok(sizeof(struct CtdlMessage));
575 memset(logmsg, 0, sizeof(struct CtdlMessage));
576 logmsg->cm_magic = CTDLMESSAGE_MAGIC;
577 logmsg->cm_anon_type = MES_NORMAL;
578 logmsg->cm_format_type = 0;
579 logmsg->cm_fields['A'] = strdoop(lun);
580 logmsg->cm_fields['N'] = strdoop(NODENAME);
581 logmsg->cm_fields['O'] = strdoop(PAGELOGROOM);
582 logmsg->cm_fields['R'] = strdoop(x_user);
583 logmsg->cm_fields['M'] = strdoop(x_msg);
586 /* Save a copy of the message in the sender's log room,
587 * creating the room if necessary.
589 create_room(PAGELOGROOM, 4, "", 0, 1);
590 msgnum = CtdlSubmitMsg(logmsg, NULL, PAGELOGROOM);
592 /* Now save a copy in the global log room, if configured */
593 if (strlen(config.c_logpages) > 0) {
594 create_room(config.c_logpages, 3, "", 0, 1);
595 CtdlSaveMsgPointerInRoom(config.c_logpages, msgnum, 0);
598 /* Save a copy in each recipient's log room, creating those
599 * rooms if necessary. Note that we create as a type 5 room
600 * rather than 4, which indicates that it's a personal room
601 * but we've already supplied the namespace prefix.
604 create_room(sl->roomname, 5, "", 0, 1);
605 CtdlSaveMsgPointerInRoom(sl->roomname, msgnum, 0);
611 CtdlFreeMessage(logmsg);
614 return (message_sent);
618 * send express messages <bc>
620 void cmd_sexp(char *argbuf)
622 int message_sent = 0;
625 char *lun; /* <bc> */
626 char *x_big_msgbuf = NULL;
628 if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
629 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
632 if (CC->fake_username[0])
633 lun = CC->fake_username;
635 lun = CC->usersupp.fullname;
637 extract(x_user, argbuf, 0);
639 extract(x_msg, argbuf, 1);
642 cprintf("%d You were not previously paged.\n", ERROR);
645 if ((!strcasecmp(x_user, "broadcast")) && (CC->usersupp.axlevel < 6)) {
646 cprintf("%d Higher access required to send a broadcast.\n",
647 ERROR + HIGHER_ACCESS_REQUIRED);
650 /* This loop handles text-transfer pages */
651 if (!strcmp(x_msg, "-")) {
652 message_sent = PerformXmsgHooks(lun, x_user, "");
653 if (message_sent == 0) {
654 cprintf("%d '%s' is not logged in "
655 "or is not accepting pages.\n",
659 cprintf("%d Transmit message (will deliver to %d users)\n",
660 SEND_LISTING, message_sent);
661 x_big_msgbuf = mallok(SIZ);
662 memset(x_big_msgbuf, 0, SIZ);
663 while (client_gets(x_msg), strcmp(x_msg, "000")) {
664 x_big_msgbuf = reallok(x_big_msgbuf,
665 strlen(x_big_msgbuf) + strlen(x_msg) + 4);
666 if (strlen(x_big_msgbuf) > 0)
667 if (x_big_msgbuf[strlen(x_big_msgbuf)] != '\n')
668 strcat(x_big_msgbuf, "\n");
669 strcat(x_big_msgbuf, x_msg);
671 PerformXmsgHooks(lun, x_user, x_big_msgbuf);
674 /* This loop handles inline pages */
676 message_sent = PerformXmsgHooks(lun, x_user, x_msg);
678 if (message_sent > 0) {
679 if (strlen(x_msg) > 0)
680 cprintf("%d Message sent", CIT_OK);
682 cprintf("%d Ok to send message", CIT_OK);
683 if (message_sent > 1)
684 cprintf(" to %d users", message_sent);
687 cprintf("%d '%s' is not logged in "
688 "or is not accepting pages.\n",
699 * Enter or exit paging-disabled mode
701 void cmd_dexp(char *argbuf)
705 if (!CC->logged_in) {
706 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
710 new_state = extract_int(argbuf, 0);
711 if ((new_state == 0) || (new_state == 1)) {
712 CC->disable_exp = new_state;
714 cprintf("%d %d\n", CIT_OK, CC->disable_exp);
719 * Request client termination
721 void cmd_reqt(char *argbuf) {
722 struct CitContext *ccptr;
725 struct ExpressMessage *newmsg;
727 if (CtdlAccessCheck(ac_aide)) return;
728 which_session = extract_int(argbuf, 0);
730 begin_critical_section(S_SESSION_TABLE);
731 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
732 if ((ccptr->cs_pid == which_session) || (which_session == 0)) {
734 newmsg = (struct ExpressMessage *)
735 mallok(sizeof (struct ExpressMessage));
737 sizeof (struct ExpressMessage));
738 time(&(newmsg->timestamp));
739 safestrncpy(newmsg->sender, CC->usersupp.fullname,
740 sizeof newmsg->sender);
741 newmsg->flags |= EM_GO_AWAY;
742 newmsg->text = strdoop("Automatic logoff requested.");
744 add_xmsg_to_context(ccptr, newmsg);
749 end_critical_section(S_SESSION_TABLE);
750 cprintf("%d Sent termination request to %d sessions.\n", CIT_OK, sessions);
755 char *Dynamic_Module_Init(void)
757 CtdlRegisterProtoHook(cmd_chat, "CHAT", "Begin real-time chat");
758 CtdlRegisterProtoHook(cmd_pexp, "PEXP", "Poll for express messages");
759 CtdlRegisterProtoHook(cmd_gexp, "GEXP", "Get express messages");
760 CtdlRegisterProtoHook(cmd_sexp, "SEXP", "Send an express message");
761 CtdlRegisterProtoHook(cmd_dexp, "DEXP", "Disable express messages");
762 CtdlRegisterProtoHook(cmd_reqt, "REQT", "Request client termination");
763 CtdlRegisterSessionHook(cmd_gexp_async, EVT_CMD);
764 CtdlRegisterSessionHook(delete_express_messages, EVT_STOP);
765 CtdlRegisterXmsgHook(send_express_message, XMSG_PRI_LOCAL);