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 GenerateRoomDisplay(roomname, ccptr, CC);
187 if ((CC->usersupp.axlevel < 6)
188 && (strlen(ccptr->fake_roomname)>0)) {
189 strcpy(roomname, ccptr->fake_roomname);
192 if (((ccptr->cs_flags & CS_CHAT) == 0)
193 && ((ccptr->cs_flags & CS_STEALTH) == 0)) {
194 cprintf(":| %-25s <%s>:\n",
195 (ccptr->fake_username[0]) ? ccptr->fake_username : ccptr->curr_user,
200 end_critical_section(S_SESSION_TABLE);
204 cprintf(":|There are %d users here.\n", count);
207 cprintf(":|Note: you are the only one here.\n");
209 if (count_elsewhere > 0) {
210 cprintf(":|There are %d users chatting in other rooms.\n", count_elsewhere);
218 void cmd_chat(char *argbuf)
223 int MyLastMsg, ThisLastMsg;
224 struct ChatLine *clptr;
225 struct CitContext *t_context;
228 if (!(CC->logged_in)) {
229 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
233 CC->cs_flags = CC->cs_flags | CS_CHAT;
234 cprintf("%d Entering chat mode (type '/help' for available commands)\n",
237 MyLastMsg = ChatLastMsg;
239 if ((CC->cs_flags & CS_STEALTH) == 0) {
240 allwrite("<entering chat>", 0, NULL);
251 linelen = strlen(cmdbuf);
252 if (linelen > 100) --linelen; /* truncate too-long lines */
253 cmdbuf[linelen + 1] = 0;
255 retval = client_read_to(&cmdbuf[linelen], 1, 2);
257 if (retval < 0) { /* socket broken? */
258 if ((CC->cs_flags & CS_STEALTH) == 0) {
259 allwrite("<disconnected>", 0, NULL);
264 /* if we have a complete line, do send processing */
265 if (strlen(cmdbuf) > 0)
266 if (cmdbuf[strlen(cmdbuf) - 1] == 10) {
267 cmdbuf[strlen(cmdbuf) - 1] = 0;
271 if ((!strcasecmp(cmdbuf, "exit"))
272 || (!strcasecmp(cmdbuf, "/exit"))
273 || (!strcasecmp(cmdbuf, "quit"))
274 || (!strcasecmp(cmdbuf, "logout"))
275 || (!strcasecmp(cmdbuf, "logoff"))
276 || (!strcasecmp(cmdbuf, "/q"))
277 || (!strcasecmp(cmdbuf, ".q"))
278 || (!strcasecmp(cmdbuf, "/quit"))
280 strcpy(cmdbuf, "000");
282 if (!strcmp(cmdbuf, "000")) {
283 if ((CC->cs_flags & CS_STEALTH) == 0) {
284 allwrite("<exiting chat>", 0, NULL);
288 CC->cs_flags = CC->cs_flags - CS_CHAT;
291 if ((!strcasecmp(cmdbuf, "/help"))
292 || (!strcasecmp(cmdbuf, "help"))
293 || (!strcasecmp(cmdbuf, "/?"))
294 || (!strcasecmp(cmdbuf, "?"))) {
296 cprintf(":|Available commands: \n");
297 cprintf(":|/help (prints this message) \n");
298 cprintf(":|/who (list users currently in chat) \n");
299 cprintf(":|/whobbs (list users in chat -and- elsewhere) \n");
300 cprintf(":|/me ('action' line, ala irc) \n");
301 cprintf(":|/msg (send private message, ala irc) \n");
302 cprintf(":|/quit (return to the BBS) \n");
306 if (!strcasecmp(cmdbuf, "/who")) {
310 if (!strcasecmp(cmdbuf, "/whobbs")) {
314 if (!strncasecmp(cmdbuf, "/me ", 4)) {
315 allwrite(&cmdbuf[4], 1, NULL);
318 if (!strncasecmp(cmdbuf, "/msg ", 5)) {
320 strptr1 = &cmdbuf[5];
321 if ((t_context = find_context(&strptr1))) {
322 allwrite(strptr1, 2, CC->curr_user);
323 if (strcasecmp(CC->curr_user, t_context->curr_user))
324 allwrite(strptr1, 2, t_context->curr_user);
326 cprintf(":|User not found.\n", cmdbuf);
329 if ((cmdbuf[0] != '/') && (strlen(cmdbuf) > 0)) {
331 allwrite(cmdbuf, 0, NULL);
333 if ((!ok_cmd) && (cmdbuf[0]) && (cmdbuf[0] != '\n'))
334 cprintf(":|Command %s is not understood.\n", cmdbuf);
339 /* now check the queue for new incoming stuff */
341 if (CC->fake_username[0])
342 un = CC->fake_username;
345 if (ChatLastMsg > MyLastMsg) {
346 ThisLastMsg = ChatLastMsg;
347 for (clptr = ChatQueue; clptr != NULL; clptr = clptr->next) {
348 if ((clptr->chat_seq > MyLastMsg) && ((!clptr->chat_username[0]) || (!strncasecmp(un, clptr->chat_username, 32)))) {
349 if ((!clptr->chat_room[0]) || (!strncasecmp(CC->quickroom.QRname, clptr->chat_room, ROOMNAMELEN))) {
350 cprintf("%s\n", clptr->chat_text);
354 MyLastMsg = ThisLastMsg;
362 * Delete any remaining express messages
364 void delete_express_messages(void) {
365 struct ExpressMessage *ptr;
367 begin_critical_section(S_SESSION_TABLE);
368 while (CC->FirstExpressMessage != NULL) {
369 ptr = CC->FirstExpressMessage->next;
370 if (CC->FirstExpressMessage->text != NULL)
371 phree(CC->FirstExpressMessage->text);
372 phree(CC->FirstExpressMessage);
373 CC->FirstExpressMessage = ptr;
375 end_critical_section(S_SESSION_TABLE);
382 * Poll for express messages (OLD METHOD -- ***DEPRECATED ***)
384 void cmd_pexp(char *argbuf)
386 struct ExpressMessage *ptr, *holdptr;
388 if (CC->FirstExpressMessage == NULL) {
389 cprintf("%d No express messages waiting.\n", ERROR);
392 begin_critical_section(S_SESSION_TABLE);
393 ptr = CC->FirstExpressMessage;
394 CC->FirstExpressMessage = NULL;
395 end_critical_section(S_SESSION_TABLE);
397 cprintf("%d Express msgs:\n", LISTING_FOLLOWS);
398 while (ptr != NULL) {
399 if (ptr->flags && EM_BROADCAST)
400 cprintf("Broadcast message ");
401 else if (ptr->flags && EM_CHAT)
402 cprintf("Chat request ");
403 else if (ptr->flags && EM_GO_AWAY)
404 cprintf("Please logoff now, as requested ");
407 cprintf("from %s:\n", ptr->sender);
408 if (ptr->text != NULL)
409 memfmout(80, ptr->text, 0, "\n");
412 if (ptr->text != NULL) phree(ptr->text);
421 * Get express messages (new method)
423 void cmd_gexp(char *argbuf) {
424 struct ExpressMessage *ptr;
426 if (CC->FirstExpressMessage == NULL) {
427 cprintf("%d No express messages waiting.\n", ERROR);
431 begin_critical_section(S_SESSION_TABLE);
432 ptr = CC->FirstExpressMessage;
433 CC->FirstExpressMessage = CC->FirstExpressMessage->next;
434 end_critical_section(S_SESSION_TABLE);
436 cprintf("%d %d|%ld|%d|%s|%s\n",
438 ((ptr->next != NULL) ? 1 : 0), /* more msgs? */
439 ptr->timestamp, /* time sent */
440 ptr->flags, /* flags */
441 ptr->sender, /* sender of msg */
442 config.c_nodename); /* static for now */
443 if (ptr->text != NULL) {
444 memfmout(80, ptr->text, 0, "\n");
445 if (ptr->text[strlen(ptr->text)-1] != '\n') cprintf("\n");
454 * Back end support function for send_express_message() and company
456 void add_xmsg_to_context(struct CitContext *ccptr,
457 struct ExpressMessage *newmsg)
459 struct ExpressMessage *findend;
461 if (ccptr->FirstExpressMessage == NULL) {
462 ccptr->FirstExpressMessage = newmsg;
465 findend = ccptr->FirstExpressMessage;
466 while (findend->next != NULL) {
467 findend = findend->next;
469 findend->next = newmsg;
477 * This is the back end to the express message sending function.
478 * Returns the number of users to which the message was sent.
479 * Sending a zero-length message tests for recipients without sending messages.
481 int send_express_message(char *lun, char *x_user, char *x_msg)
483 int message_sent = 0; /* number of successful sends */
485 struct CitContext *ccptr;
486 struct ExpressMessage *newmsg;
489 int do_send = 0; /* set to 1 to actually page, not
490 * just check to see if we can.
492 struct savelist *sl = NULL; /* list of rooms to save this page */
493 struct savelist *sptr;
494 struct CtdlMessage *logmsg;
495 char roomname[ROOMNAMELEN];
498 if (strlen(x_msg) > 0) {
499 msglen = strlen(x_msg) + 4;
503 /* find the target user's context and append the message */
504 begin_critical_section(S_SESSION_TABLE);
505 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
507 if (ccptr->fake_username[0]) /* <bc> */
508 un = ccptr->fake_username;
510 un = ccptr->usersupp.fullname;
512 if ( ((!strcasecmp(un, x_user))
513 || (!strcasecmp(x_user, "broadcast")))
514 && ((ccptr->disable_exp == 0)
515 || (CC->usersupp.axlevel >= 6)) ) {
517 newmsg = (struct ExpressMessage *)
518 mallok(sizeof (struct ExpressMessage));
520 sizeof (struct ExpressMessage));
521 time(&(newmsg->timestamp));
522 safestrncpy(newmsg->sender, lun,
523 sizeof newmsg->sender);
524 if (!strcasecmp(x_user, "broadcast"))
525 newmsg->flags |= EM_BROADCAST;
526 newmsg->text = strdoop(x_msg);
528 add_xmsg_to_context(ccptr, newmsg);
532 sptr = (struct savelist *)
533 malloc(sizeof(struct savelist));
535 MailboxName(sptr->roomname,
536 &ccptr->usersupp, PAGELOGROOM);
543 end_critical_section(S_SESSION_TABLE);
545 /* Log the page to disk if configured to do so */
546 if ( (do_send) && (message_sent) ) {
548 logmsg = mallok(sizeof(struct CtdlMessage));
549 memset(logmsg, 0, sizeof(struct CtdlMessage));
550 logmsg->cm_magic = CTDLMESSAGE_MAGIC;
551 logmsg->cm_anon_type = MES_NORMAL;
552 logmsg->cm_format_type = 0;
553 logmsg->cm_fields['A'] = strdoop(lun);
554 logmsg->cm_fields['N'] = strdoop(NODENAME);
555 logmsg->cm_fields['O'] = strdoop(PAGELOGROOM);
556 logmsg->cm_fields['R'] = strdoop(x_user);
557 logmsg->cm_fields['M'] = strdoop(x_msg);
559 MailboxName(roomname, &CC->usersupp, PAGELOGROOM);
560 create_room(roomname, 4, "", 0);
561 msgnum = CtdlSaveMsg(logmsg, "", roomname, MES_LOCAL);
562 if (strlen(config.c_logpages) > 0) {
563 create_room(config.c_logpages, 3, "", 0);
564 CtdlSaveMsgPointerInRoom(config.c_logpages, msgnum, 0);
567 create_room(sl->roomname, 4, "", 0);
568 CtdlSaveMsgPointerInRoom(sl->roomname, msgnum, 0);
574 CtdlFreeMessage(logmsg);
577 return (message_sent);
581 * send express messages <bc>
583 void cmd_sexp(char *argbuf)
585 int message_sent = 0;
588 char *lun; /* <bc> */
589 char *x_big_msgbuf = NULL;
591 if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
592 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
595 if (CC->fake_username[0])
596 lun = CC->fake_username;
598 lun = CC->usersupp.fullname;
600 extract(x_user, argbuf, 0);
602 extract(x_msg, argbuf, 1);
605 cprintf("%d You were not previously paged.\n", ERROR);
608 if ((!strcasecmp(x_user, "broadcast")) && (CC->usersupp.axlevel < 6)) {
609 cprintf("%d Higher access required to send a broadcast.\n",
610 ERROR + HIGHER_ACCESS_REQUIRED);
613 /* This loop handles text-transfer pages */
614 if (!strcmp(x_msg, "-")) {
615 message_sent = PerformXmsgHooks(lun, x_user, "");
616 if (message_sent == 0) {
617 cprintf("%d '%s' is not logged in "
618 "or is not accepting pages.\n",
622 cprintf("%d Transmit message (will deliver to %d users)\n",
623 SEND_LISTING, message_sent);
624 x_big_msgbuf = mallok(256);
625 memset(x_big_msgbuf, 0, 256);
626 while (client_gets(x_msg), strcmp(x_msg, "000")) {
627 x_big_msgbuf = reallok(x_big_msgbuf,
628 strlen(x_big_msgbuf) + strlen(x_msg) + 4);
629 if (strlen(x_big_msgbuf) > 0)
630 if (x_big_msgbuf[strlen(x_big_msgbuf)] != '\n')
631 strcat(x_big_msgbuf, "\n");
632 strcat(x_big_msgbuf, x_msg);
634 PerformXmsgHooks(lun, x_user, x_big_msgbuf);
637 /* This loop handles inline pages */
639 message_sent = PerformXmsgHooks(lun, x_user, x_msg);
641 if (message_sent > 0) {
642 if (strlen(x_msg) > 0)
643 cprintf("%d Message sent", OK);
645 cprintf("%d Ok to send message", OK);
646 if (message_sent > 1)
647 cprintf(" to %d users", message_sent);
650 cprintf("%d '%s' is not logged in "
651 "or is not accepting pages.\n",
662 * Enter or exit paging-disabled mode
664 void cmd_dexp(char *argbuf)
668 if (!CC->logged_in) {
669 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
673 new_state = extract_int(argbuf, 0);
674 if ((new_state == 0) || (new_state == 1)) {
675 CC->disable_exp = new_state;
677 cprintf("%d %d\n", OK, CC->disable_exp);
682 * Request client termination
684 void cmd_reqt(char *argbuf) {
685 struct CitContext *ccptr;
688 struct ExpressMessage *newmsg;
690 if (CtdlAccessCheck(ac_aide)) return;
691 which_session = extract_int(argbuf, 0);
693 begin_critical_section(S_SESSION_TABLE);
694 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
695 if ((ccptr->cs_pid == which_session) || (which_session == 0)) {
697 newmsg = (struct ExpressMessage *)
698 mallok(sizeof (struct ExpressMessage));
700 sizeof (struct ExpressMessage));
701 time(&(newmsg->timestamp));
702 safestrncpy(newmsg->sender, CC->usersupp.fullname,
703 sizeof newmsg->sender);
704 newmsg->flags |= EM_GO_AWAY;
705 newmsg->text = strdoop("Automatic logoff requested.");
707 add_xmsg_to_context(ccptr, newmsg);
712 end_critical_section(S_SESSION_TABLE);
713 cprintf("%d Sent termination request to %d sessions.\n", OK, sessions);
718 char *Dynamic_Module_Init(void)
720 CtdlRegisterProtoHook(cmd_chat, "CHAT", "Begin real-time chat");
721 CtdlRegisterProtoHook(cmd_pexp, "PEXP", "Poll for express messages");
722 CtdlRegisterProtoHook(cmd_gexp, "GEXP", "Get express messages");
723 CtdlRegisterProtoHook(cmd_sexp, "SEXP", "Send an express message");
724 CtdlRegisterProtoHook(cmd_dexp, "DEXP", "Disable express messages");
725 CtdlRegisterProtoHook(cmd_reqt, "REQT", "Request client termination");
726 CtdlRegisterSessionHook(delete_express_messages, EVT_STOP);
727 CtdlRegisterXmsgHook(send_express_message, XMSG_PRI_LOCAL);