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 #ifdef TIME_WITH_SYS_TIME
27 #include "serv_chat.h"
28 #include "sysdep_decls.h"
29 #include "citserver.h"
32 #include "dynloader.h"
38 struct ChatLine *ChatQueue = NULL;
41 extern struct CitContext *ContextList;
45 void allwrite(char *cmdbuf, int flag, char *username)
50 struct ChatLine *clptr, *clnew;
53 if (CC->fake_username[0])
54 un = CC->fake_username;
56 un = CC->usersupp.fullname;
58 snprintf(bcast, sizeof bcast, ":|<%s %s>", un, cmdbuf);
59 } else if (flag == 0) {
60 snprintf(bcast, sizeof bcast, "%s|%s", un, cmdbuf);
61 } else if (flag == 2) {
62 snprintf(bcast, sizeof bcast, ":|<%s whispers %s>", un, cmdbuf);
64 if ((strcasecmp(cmdbuf, "NOOP")) && (flag != 2)) {
65 fp = fopen(CHATLOG, "a");
66 fprintf(fp, "%s\n", bcast);
69 clnew = (struct ChatLine *) mallok(sizeof(struct ChatLine));
70 memset(clnew, 0, sizeof(struct ChatLine));
72 fprintf(stderr, "citserver: cannot alloc chat line: %s\n",
78 clnew->chat_time = now;
79 safestrncpy(clnew->chat_room, CC->quickroom.QRname,
80 sizeof clnew->chat_room);
81 clnew->chat_room[sizeof clnew->chat_room - 1] = 0;
83 safestrncpy(clnew->chat_username, username,
84 sizeof clnew->chat_username);
85 clnew->chat_username[sizeof clnew->chat_username - 1] = 0;
87 clnew->chat_username[0] = '\0';
88 safestrncpy(clnew->chat_text, bcast, sizeof clnew->chat_text);
90 /* Here's the critical section.
91 * First, add the new message to the queue...
93 begin_critical_section(S_CHATQUEUE);
95 clnew->chat_seq = ChatLastMsg;
96 if (ChatQueue == NULL) {
99 for (clptr = ChatQueue; clptr->next != NULL; clptr = clptr->next);;
103 /* Then, before releasing the lock, free the expired messages */
104 while ((ChatQueue != NULL) && (now - ChatQueue->chat_time >= 120L)) {
106 ChatQueue = ChatQueue->next;
109 end_critical_section(S_CHATQUEUE);
113 t_context *find_context(char **unstr)
115 t_context *t_cc, *found_cc = NULL;
118 if ((!*unstr) || (!unstr))
121 begin_critical_section(S_SESSION_TABLE);
122 for (t_cc = ContextList; ((t_cc) && (!found_cc)); t_cc = t_cc->next) {
123 if (t_cc->fake_username[0])
124 name = t_cc->fake_username;
126 name = t_cc->curr_user;
128 if ((!strncasecmp(name, tptr, strlen(name))) && (tptr[strlen(name)] == ' ')) {
130 *unstr = &(tptr[strlen(name) + 1]);
133 end_critical_section(S_SESSION_TABLE);
139 * List users in chat.
140 * allflag == 0 = list users in chat
141 * 1 = list users in chat, followed by users not in chat
142 * 2 = display count only
145 void do_chat_listing(int allflag)
147 struct CitContext *ccptr;
149 int count_elsewhere = 0;
150 char roomname[ROOMNAMELEN];
152 if ((allflag == 0) || (allflag == 1))
153 cprintf(":|\n:| Users currently in chat:\n");
154 begin_critical_section(S_SESSION_TABLE);
155 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
156 if (ccptr->cs_flags & CS_CHAT) {
157 if (!strcasecmp(ccptr->quickroom.QRname,
158 CC->quickroom.QRname)) {
166 GenerateRoomDisplay(roomname, ccptr, CC);
167 if ((CC->usersupp.axlevel < 6)
168 && (strlen(ccptr->fake_roomname)>0)) {
169 strcpy(roomname, ccptr->fake_roomname);
172 if ((ccptr->cs_flags & CS_CHAT)
173 && ((ccptr->cs_flags & CS_STEALTH) == 0)) {
174 if ((allflag == 0) || (allflag == 1)) {
175 cprintf(":| %-25s <%s>:\n",
176 (ccptr->fake_username[0]) ? ccptr->fake_username : ccptr->curr_user,
183 cprintf(":|\n:| Users not in chat:\n");
184 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
186 if (((ccptr->cs_flags & CS_CHAT) == 0)
187 && ((ccptr->cs_flags & CS_STEALTH) == 0)) {
188 cprintf(":| %-25s <%s>:\n",
189 (ccptr->fake_username[0]) ? ccptr->fake_username : ccptr->curr_user,
194 end_critical_section(S_SESSION_TABLE);
198 cprintf(":|There are %d users here.\n", count);
201 cprintf(":|Note: you are the only one here.\n");
203 if (count_elsewhere > 0) {
204 cprintf(":|There are %d users chatting in other rooms.\n", count_elsewhere);
212 void cmd_chat(char *argbuf)
217 int MyLastMsg, ThisLastMsg;
218 struct ChatLine *clptr;
219 struct CitContext *t_context;
222 if (!(CC->logged_in)) {
223 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
227 CC->cs_flags = CC->cs_flags | CS_CHAT;
228 cprintf("%d Entering chat mode (type '/help' for available commands)\n",
231 MyLastMsg = ChatLastMsg;
233 if ((CC->cs_flags & CS_STEALTH) == 0) {
234 allwrite("<entering chat>", 0, NULL);
245 linelen = strlen(cmdbuf);
246 if (linelen > 100) --linelen; /* truncate too-long lines */
247 cmdbuf[linelen + 1] = 0;
249 retval = client_read_to(&cmdbuf[linelen], 1, 2);
251 if (retval < 0) { /* socket broken? */
252 if ((CC->cs_flags & CS_STEALTH) == 0) {
253 allwrite("<disconnected>", 0, NULL);
258 /* if we have a complete line, do send processing */
259 if (strlen(cmdbuf) > 0)
260 if (cmdbuf[strlen(cmdbuf) - 1] == 10) {
261 cmdbuf[strlen(cmdbuf) - 1] = 0;
265 if ((!strcasecmp(cmdbuf, "exit"))
266 || (!strcasecmp(cmdbuf, "/exit"))
267 || (!strcasecmp(cmdbuf, "quit"))
268 || (!strcasecmp(cmdbuf, "logout"))
269 || (!strcasecmp(cmdbuf, "logoff"))
270 || (!strcasecmp(cmdbuf, "/q"))
271 || (!strcasecmp(cmdbuf, ".q"))
272 || (!strcasecmp(cmdbuf, "/quit"))
274 strcpy(cmdbuf, "000");
276 if (!strcmp(cmdbuf, "000")) {
277 if ((CC->cs_flags & CS_STEALTH) == 0) {
278 allwrite("<exiting chat>", 0, NULL);
282 CC->cs_flags = CC->cs_flags - CS_CHAT;
285 if ((!strcasecmp(cmdbuf, "/help"))
286 || (!strcasecmp(cmdbuf, "help"))
287 || (!strcasecmp(cmdbuf, "/?"))
288 || (!strcasecmp(cmdbuf, "?"))) {
290 cprintf(":|Available commands: \n");
291 cprintf(":|/help (prints this message) \n");
292 cprintf(":|/who (list users currently in chat) \n");
293 cprintf(":|/whobbs (list users in chat -and- elsewhere) \n");
294 cprintf(":|/me ('action' line, ala irc) \n");
295 cprintf(":|/msg (send private message, ala irc) \n");
296 cprintf(":|/quit (return to the BBS) \n");
300 if (!strcasecmp(cmdbuf, "/who")) {
304 if (!strcasecmp(cmdbuf, "/whobbs")) {
308 if (!strncasecmp(cmdbuf, "/me ", 4)) {
309 allwrite(&cmdbuf[4], 1, NULL);
312 if (!strncasecmp(cmdbuf, "/msg ", 5)) {
314 strptr1 = &cmdbuf[5];
315 if ((t_context = find_context(&strptr1))) {
316 allwrite(strptr1, 2, CC->curr_user);
317 if (strcasecmp(CC->curr_user, t_context->curr_user))
318 allwrite(strptr1, 2, t_context->curr_user);
320 cprintf(":|User not found.\n", cmdbuf);
323 if ((cmdbuf[0] != '/') && (strlen(cmdbuf) > 0)) {
325 allwrite(cmdbuf, 0, NULL);
327 if ((!ok_cmd) && (cmdbuf[0]) && (cmdbuf[0] != '\n'))
328 cprintf(":|Command %s is not understood.\n", cmdbuf);
333 /* now check the queue for new incoming stuff */
335 if (CC->fake_username[0])
336 un = CC->fake_username;
339 if (ChatLastMsg > MyLastMsg) {
340 ThisLastMsg = ChatLastMsg;
341 for (clptr = ChatQueue; clptr != NULL; clptr = clptr->next) {
342 if ((clptr->chat_seq > MyLastMsg) && ((!clptr->chat_username[0]) || (!strncasecmp(un, clptr->chat_username, 32)))) {
343 if ((!clptr->chat_room[0]) || (!strncasecmp(CC->quickroom.QRname, clptr->chat_room, ROOMNAMELEN))) {
344 cprintf("%s\n", clptr->chat_text);
348 MyLastMsg = ThisLastMsg;
356 * Delete any remaining express messages
358 void delete_express_messages(void) {
359 struct ExpressMessage *ptr;
361 begin_critical_section(S_SESSION_TABLE);
362 while (CC->FirstExpressMessage != NULL) {
363 ptr = CC->FirstExpressMessage->next;
364 if (CC->FirstExpressMessage->text != NULL)
365 phree(CC->FirstExpressMessage->text);
366 phree(CC->FirstExpressMessage);
367 CC->FirstExpressMessage = ptr;
369 end_critical_section(S_SESSION_TABLE);
376 * Poll for express messages (OLD METHOD -- ***DEPRECATED ***)
378 void cmd_pexp(char *argbuf)
380 struct ExpressMessage *ptr, *holdptr;
382 if (CC->FirstExpressMessage == NULL) {
383 cprintf("%d No express messages waiting.\n", ERROR);
386 begin_critical_section(S_SESSION_TABLE);
387 ptr = CC->FirstExpressMessage;
388 CC->FirstExpressMessage = NULL;
389 end_critical_section(S_SESSION_TABLE);
391 cprintf("%d Express msgs:\n", LISTING_FOLLOWS);
392 while (ptr != NULL) {
393 if (ptr->flags && EM_BROADCAST)
394 cprintf("Broadcast message ");
395 else if (ptr->flags && EM_CHAT)
396 cprintf("Chat request ");
397 else if (ptr->flags && EM_GO_AWAY)
398 cprintf("Please logoff now, as requested ");
401 cprintf("from %s:\n", ptr->sender);
402 if (ptr->text != NULL)
403 memfmout(80, ptr->text, 0, "\n");
406 if (ptr->text != NULL) phree(ptr->text);
415 * Get express messages (new method)
417 void cmd_gexp(char *argbuf) {
418 struct ExpressMessage *ptr;
420 if (CC->FirstExpressMessage == NULL) {
421 cprintf("%d No express messages waiting.\n", ERROR);
425 begin_critical_section(S_SESSION_TABLE);
426 ptr = CC->FirstExpressMessage;
427 CC->FirstExpressMessage = CC->FirstExpressMessage->next;
428 end_critical_section(S_SESSION_TABLE);
430 cprintf("%d %d|%ld|%d|%s|%s\n",
432 ((ptr->next != NULL) ? 1 : 0), /* more msgs? */
433 ptr->timestamp, /* time sent */
434 ptr->flags, /* flags */
435 ptr->sender, /* sender of msg */
436 config.c_nodename); /* static for now */
437 if (ptr->text != NULL) {
438 memfmout(80, ptr->text, 0, "\n");
439 if (ptr->text[strlen(ptr->text)-1] != '\n') cprintf("\n");
448 * Back end support function for send_express_message() and company
450 void add_xmsg_to_context(struct CitContext *ccptr,
451 struct ExpressMessage *newmsg)
453 struct ExpressMessage *findend;
455 if (ccptr->FirstExpressMessage == NULL) {
456 ccptr->FirstExpressMessage = newmsg;
459 findend = ccptr->FirstExpressMessage;
460 while (findend->next != NULL) {
461 findend = findend->next;
463 findend->next = newmsg;
471 * This is the back end to the express message sending function.
472 * Returns the number of users to which the message was sent.
473 * Sending a zero-length message tests for recipients without sending messages.
475 int send_express_message(char *lun, char *x_user, char *x_msg)
477 int message_sent = 0; /* number of successful sends */
479 struct CitContext *ccptr;
480 struct ExpressMessage *newmsg;
483 int do_send = 0; /* set to 1 to actually page, not
484 * just check to see if we can.
486 struct savelist *sl = NULL; /* list of rooms to save this page */
487 struct savelist *sptr;
488 struct CtdlMessage *logmsg;
489 char roomname[ROOMNAMELEN];
492 if (strlen(x_msg) > 0) {
493 msglen = strlen(x_msg) + 4;
497 /* find the target user's context and append the message */
498 begin_critical_section(S_SESSION_TABLE);
499 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
501 if (ccptr->fake_username[0]) /* <bc> */
502 un = ccptr->fake_username;
504 un = ccptr->usersupp.fullname;
506 if ( ((!strcasecmp(un, x_user))
507 || (!strcasecmp(x_user, "broadcast")))
508 && ((ccptr->disable_exp == 0)
509 || (CC->usersupp.axlevel >= 6)) ) {
511 newmsg = (struct ExpressMessage *)
512 mallok(sizeof (struct ExpressMessage));
514 sizeof (struct ExpressMessage));
515 time(&(newmsg->timestamp));
516 safestrncpy(newmsg->sender, lun,
517 sizeof newmsg->sender);
518 if (!strcasecmp(x_user, "broadcast"))
519 newmsg->flags |= EM_BROADCAST;
520 newmsg->text = strdoop(x_msg);
522 add_xmsg_to_context(ccptr, newmsg);
526 sptr = (struct savelist *)
527 malloc(sizeof(struct savelist));
529 MailboxName(sptr->roomname,
530 &ccptr->usersupp, PAGELOGROOM);
537 end_critical_section(S_SESSION_TABLE);
539 /* Log the page to disk if configured to do so */
540 if ( (do_send) && (message_sent) ) {
542 logmsg = mallok(sizeof(struct CtdlMessage));
543 memset(logmsg, 0, sizeof(struct CtdlMessage));
544 logmsg->cm_magic = CTDLMESSAGE_MAGIC;
545 logmsg->cm_anon_type = MES_NORMAL;
546 logmsg->cm_format_type = 0;
547 logmsg->cm_fields['A'] = strdoop(lun);
548 logmsg->cm_fields['N'] = strdoop(NODENAME);
549 logmsg->cm_fields['O'] = strdoop(PAGELOGROOM);
550 logmsg->cm_fields['R'] = strdoop(x_user);
551 logmsg->cm_fields['M'] = strdoop(x_msg);
553 MailboxName(roomname, &CC->usersupp, PAGELOGROOM);
554 create_room(roomname, 4, "", 0);
555 msgnum = CtdlSaveMsg(logmsg, "", roomname, MES_LOCAL);
556 if (strlen(config.c_logpages) > 0) {
557 create_room(config.c_logpages, 3, "", 0);
558 CtdlSaveMsgPointerInRoom(config.c_logpages, msgnum, 0);
561 create_room(sl->roomname, 4, "", 0);
562 CtdlSaveMsgPointerInRoom(sl->roomname, msgnum, 0);
568 CtdlFreeMessage(logmsg);
571 return (message_sent);
575 * send express messages <bc>
577 void cmd_sexp(char *argbuf)
579 int message_sent = 0;
582 char *lun; /* <bc> */
583 char *x_big_msgbuf = NULL;
585 if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
586 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
589 if (CC->fake_username[0])
590 lun = CC->fake_username;
592 lun = CC->usersupp.fullname;
594 extract(x_user, argbuf, 0);
596 extract(x_msg, argbuf, 1);
599 cprintf("%d You were not previously paged.\n", ERROR);
602 if ((!strcasecmp(x_user, "broadcast")) && (CC->usersupp.axlevel < 6)) {
603 cprintf("%d Higher access required to send a broadcast.\n",
604 ERROR + HIGHER_ACCESS_REQUIRED);
607 /* This loop handles text-transfer pages */
608 if (!strcmp(x_msg, "-")) {
609 message_sent = PerformXmsgHooks(lun, x_user, "");
610 if (message_sent == 0) {
611 cprintf("%d '%s' is not logged in "
612 "or is not accepting pages.\n",
616 cprintf("%d Transmit message (will deliver to %d users)\n",
617 SEND_LISTING, message_sent);
618 x_big_msgbuf = mallok(256);
619 memset(x_big_msgbuf, 0, 256);
620 while (client_gets(x_msg), strcmp(x_msg, "000")) {
621 x_big_msgbuf = reallok(x_big_msgbuf,
622 strlen(x_big_msgbuf) + strlen(x_msg) + 4);
623 if (strlen(x_big_msgbuf) > 0)
624 if (x_big_msgbuf[strlen(x_big_msgbuf)] != '\n')
625 strcat(x_big_msgbuf, "\n");
626 strcat(x_big_msgbuf, x_msg);
628 PerformXmsgHooks(lun, x_user, x_big_msgbuf);
631 /* This loop handles inline pages */
633 message_sent = PerformXmsgHooks(lun, x_user, x_msg);
635 if (message_sent > 0) {
636 if (strlen(x_msg) > 0)
637 cprintf("%d Message sent", OK);
639 cprintf("%d Ok to send message", OK);
640 if (message_sent > 1)
641 cprintf(" to %d users", message_sent);
644 cprintf("%d '%s' is not logged in "
645 "or is not accepting pages.\n",
656 * Enter or exit paging-disabled mode
658 void cmd_dexp(char *argbuf)
662 if (!CC->logged_in) {
663 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
667 new_state = extract_int(argbuf, 0);
668 if ((new_state == 0) || (new_state == 1)) {
669 CC->disable_exp = new_state;
671 cprintf("%d %d\n", OK, CC->disable_exp);
676 * Request client termination
678 void cmd_reqt(char *argbuf) {
679 struct CitContext *ccptr;
682 struct ExpressMessage *newmsg;
684 if (CtdlAccessCheck(ac_aide)) return;
685 which_session = extract_int(argbuf, 0);
687 begin_critical_section(S_SESSION_TABLE);
688 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
689 if ((ccptr->cs_pid == which_session) || (which_session == 0)) {
691 newmsg = (struct ExpressMessage *)
692 mallok(sizeof (struct ExpressMessage));
694 sizeof (struct ExpressMessage));
695 time(&(newmsg->timestamp));
696 safestrncpy(newmsg->sender, CC->usersupp.fullname,
697 sizeof newmsg->sender);
698 newmsg->flags |= EM_GO_AWAY;
699 newmsg->text = strdoop("Automatic logoff requested.");
701 add_xmsg_to_context(ccptr, newmsg);
706 end_critical_section(S_SESSION_TABLE);
707 cprintf("%d Sent termination request to %d sessions.\n", OK, sessions);
712 char *Dynamic_Module_Init(void)
714 CtdlRegisterProtoHook(cmd_chat, "CHAT", "Begin real-time chat");
715 CtdlRegisterProtoHook(cmd_pexp, "PEXP", "Poll for express messages");
716 CtdlRegisterProtoHook(cmd_gexp, "GEXP", "Get express messages");
717 CtdlRegisterProtoHook(cmd_sexp, "SEXP", "Send an express message");
718 CtdlRegisterProtoHook(cmd_dexp, "DEXP", "Disable express messages");
719 CtdlRegisterProtoHook(cmd_reqt, "REQT", "Request client termination");
720 CtdlRegisterSessionHook(delete_express_messages, EVT_STOP);
721 CtdlRegisterXmsgHook(send_express_message, XMSG_PRI_LOCAL);