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>
32 #include <libcitadel.h>
35 #include "serv_chat.h"
36 #include "citserver.h"
47 #include "ctdl_module.h"
49 struct ChatLine *ChatQueue = NULL;
55 char usernames[2][128];
60 struct imlog *imlist = NULL;
63 * This function handles the logging of instant messages to disk.
65 void log_instant_message(struct CitContext *me, struct CitContext *them, char *msgtext)
69 struct imlog *iptr = NULL;
70 struct imlog *this_im = NULL;
72 memset(usernums, 0, sizeof usernums);
73 usernums[0] = me->user.usernum;
74 usernums[1] = them->user.usernum;
76 /* Always put the lower user number first, so we can use the array as a hash value which
77 * represents a pair of users. For a broadcast message one of the users will be 0.
79 if (usernums[0] > usernums[1]) {
81 usernums[0] = usernums[1];
85 begin_critical_section(S_IM_LOGS);
87 /* Look for an existing conversation in the hash table.
88 * If not found, create a new one.
92 for (iptr = imlist; iptr != NULL; iptr = iptr->next) {
93 if ((iptr->usernums[0] == usernums[0]) && (iptr->usernums[1] == usernums[1])) {
94 /* Existing conversation */
98 if (this_im == NULL) {
99 /* New conversation */
100 this_im = malloc(sizeof(struct imlog));
101 memset(this_im, 0, sizeof (struct imlog));
102 this_im->usernums[0] = usernums[0];
103 this_im->usernums[1] = usernums[1];
104 /* usernames[] and usernums[] might not be in the same order. This is not an error. */
106 safestrncpy(this_im->usernames[0], me->user.fullname, sizeof this_im->usernames[0]);
109 safestrncpy(this_im->usernames[1], them->user.fullname, sizeof this_im->usernames[1]);
111 this_im->conversation = NewStrBuf();
112 this_im->next = imlist;
114 StrBufAppendBufPlain(this_im->conversation, HKEY(
115 "Content-type: text/html\r\n"
116 "Content-transfer-encoding: 7bit\r\n"
122 this_im->lastmsg = time(NULL); /* Touch the timestamp so we know when to flush */
123 StrBufAppendBufPlain(this_im->conversation, HKEY("<p><b>"), 0);
124 StrBufAppendBufPlain(this_im->conversation, me->user.fullname, -1, 0);
125 StrBufAppendBufPlain(this_im->conversation, HKEY(":</b> "), 0);
126 StrEscAppend(this_im->conversation, NULL, msgtext, 0, 0);
127 StrBufAppendBufPlain(this_im->conversation, HKEY("</p>\r\n"), 0);
128 end_critical_section(S_IM_LOGS);
132 * This message can be set to anything you want, but it is
133 * checked for consistency so don't move it away from here.
135 #define KICKEDMSG "You have been kicked out of this room."
137 void allwrite(char *cmdbuf, int flag, char *username)
142 struct ChatLine *clptr, *clnew;
145 if (CC->fake_username[0])
146 un = CC->fake_username;
148 un = CC->user.fullname;
150 snprintf(bcast, sizeof bcast, ":|<%s %s>", un, cmdbuf);
151 } else if (flag == 0) {
152 snprintf(bcast, sizeof bcast, "%s|%s", un, cmdbuf);
153 } else if (flag == 2) {
154 snprintf(bcast, sizeof bcast, ":|<%s whispers %s>", un, cmdbuf);
155 } else if (flag == 3) {
156 snprintf(bcast, sizeof bcast, ":|%s", KICKEDMSG);
158 if ((strcasecmp(cmdbuf, "NOOP")) && (flag != 2)) {
159 fp = fopen(CHATLOG, "a");
161 fprintf(fp, "%s\n", bcast);
164 clnew = (struct ChatLine *) malloc(sizeof(struct ChatLine));
165 memset(clnew, 0, sizeof(struct ChatLine));
167 fprintf(stderr, "citserver: cannot alloc chat line: %s\n",
173 clnew->chat_time = now;
174 safestrncpy(clnew->chat_room, CC->room.QRname,
175 sizeof clnew->chat_room);
176 clnew->chat_room[sizeof clnew->chat_room - 1] = 0;
178 safestrncpy(clnew->chat_username, username,
179 sizeof clnew->chat_username);
180 clnew->chat_username[sizeof clnew->chat_username - 1] = 0;
182 clnew->chat_username[0] = '\0';
183 safestrncpy(clnew->chat_text, bcast, sizeof clnew->chat_text);
185 /* Here's the critical section.
186 * First, add the new message to the queue...
188 begin_critical_section(S_CHATQUEUE);
190 clnew->chat_seq = ChatLastMsg;
191 if (ChatQueue == NULL) {
194 for (clptr = ChatQueue; clptr->next != NULL; clptr = clptr->next);;
198 /* Then, before releasing the lock, free the expired messages */
199 while ((ChatQueue != NULL) && (now - ChatQueue->chat_time >= 120L)) {
201 ChatQueue = ChatQueue->next;
204 end_critical_section(S_CHATQUEUE);
208 t_context *find_context(char **unstr)
210 t_context *t_cc, *found_cc = NULL;
213 if ((!*unstr) || (!unstr))
216 begin_critical_section(S_SESSION_TABLE);
217 for (t_cc = ContextList; ((t_cc) && (!found_cc)); t_cc = t_cc->next) {
218 if (t_cc->fake_username[0])
219 name = t_cc->fake_username;
221 name = t_cc->curr_user;
223 if ((!strncasecmp(name, tptr, strlen(name))) && (tptr[strlen(name)] == ' ')) {
225 *unstr = &(tptr[strlen(name) + 1]);
228 end_critical_section(S_SESSION_TABLE);
234 * List users in chat.
235 * allflag == 0 = list users in chat
236 * 1 = list users in chat, followed by users not in chat
237 * 2 = display count only
240 void do_chat_listing(int allflag)
242 struct CitContext *ccptr;
244 int count_elsewhere = 0;
245 char roomname[ROOMNAMELEN];
247 if ((allflag == 0) || (allflag == 1))
248 cprintf(":|\n:| Users currently in chat:\n");
249 begin_critical_section(S_SESSION_TABLE);
250 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
251 if (ccptr->cs_flags & CS_CHAT) {
252 if (!strcasecmp(ccptr->room.QRname,
261 GenerateRoomDisplay(roomname, ccptr, CC);
262 if ((CC->user.axlevel < 6) && (!IsEmptyStr(ccptr->fake_roomname))) {
263 strcpy(roomname, ccptr->fake_roomname);
266 if ((ccptr->cs_flags & CS_CHAT) && ((ccptr->cs_flags & CS_STEALTH) == 0)) {
267 if ((allflag == 0) || (allflag == 1)) {
268 cprintf(":| %-25s <%s>:\n",
269 (ccptr->fake_username[0]) ? ccptr->fake_username : ccptr->curr_user,
276 cprintf(":|\n:| Users not in chat:\n");
277 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
279 GenerateRoomDisplay(roomname, ccptr, CC);
280 if ((CC->user.axlevel < 6)
281 && (!IsEmptyStr(ccptr->fake_roomname))) {
282 strcpy(roomname, ccptr->fake_roomname);
285 if (((ccptr->cs_flags & CS_CHAT) == 0)
286 && ((ccptr->cs_flags & CS_STEALTH) == 0)) {
287 cprintf(":| %-25s <%s>:\n",
288 (ccptr->fake_username[0]) ? ccptr->fake_username : ccptr->curr_user,
293 end_critical_section(S_SESSION_TABLE);
297 cprintf(":|There are %d users here.\n", count);
300 cprintf(":|Note: you are the only one here.\n");
302 if (count_elsewhere > 0) {
303 cprintf(":|There are %d users chatting in other rooms.\n", count_elsewhere);
311 void cmd_chat(char *argbuf)
316 int MyLastMsg, ThisLastMsg;
317 struct ChatLine *clptr;
318 struct CitContext *t_context;
321 if (!(CC->logged_in)) {
322 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
326 CC->cs_flags = CC->cs_flags | CS_CHAT;
327 cprintf("%d Entering chat mode (type '/help' for available commands)\n",
331 MyLastMsg = ChatLastMsg;
333 if ((CC->cs_flags & CS_STEALTH) == 0) {
334 allwrite("<entering chat>", 0, NULL);
345 linelen = strlen(cmdbuf);
346 if (linelen > 100) --linelen; /* truncate too-long lines */
347 cmdbuf[linelen + 1] = 0;
349 retval = client_read_to(&cmdbuf[linelen], 1, 2);
351 if (retval < 0 || CC->kill_me) { /* socket broken? */
352 if ((CC->cs_flags & CS_STEALTH) == 0) {
353 allwrite("<disconnected>", 0, NULL);
358 /* if we have a complete line, do send processing */
359 if (!IsEmptyStr(cmdbuf))
360 if (cmdbuf[strlen(cmdbuf) - 1] == 10) {
361 cmdbuf[strlen(cmdbuf) - 1] = 0;
365 if ((!strcasecmp(cmdbuf, "exit"))
366 || (!strcasecmp(cmdbuf, "/exit"))
367 || (!strcasecmp(cmdbuf, "quit"))
368 || (!strcasecmp(cmdbuf, "logout"))
369 || (!strcasecmp(cmdbuf, "logoff"))
370 || (!strcasecmp(cmdbuf, "/q"))
371 || (!strcasecmp(cmdbuf, ".q"))
372 || (!strcasecmp(cmdbuf, "/quit"))
374 strcpy(cmdbuf, "000");
376 if (!strcmp(cmdbuf, "000")) {
377 if ((CC->cs_flags & CS_STEALTH) == 0) {
378 allwrite("<exiting chat>", 0, NULL);
382 CC->cs_flags = CC->cs_flags - CS_CHAT;
385 if ((!strcasecmp(cmdbuf, "/help"))
386 || (!strcasecmp(cmdbuf, "help"))
387 || (!strcasecmp(cmdbuf, "/?"))
388 || (!strcasecmp(cmdbuf, "?"))) {
390 cprintf(":|Available commands: \n");
391 cprintf(":|/help (prints this message) \n");
392 cprintf(":|/who (list users currently in chat) \n");
393 cprintf(":|/whobbs (list users in chat -and- elsewhere) \n");
394 cprintf(":|/me ('action' line, ala irc) \n");
395 cprintf(":|/msg (send private message, ala irc) \n");
396 if (is_room_aide()) {
397 cprintf(":|/kick (kick another user out of this room) \n");
399 cprintf(":|/quit (exit from this chat) \n");
403 if (!strcasecmp(cmdbuf, "/who")) {
407 if (!strcasecmp(cmdbuf, "/whobbs")) {
411 if (!strncasecmp(cmdbuf, "/me ", 4)) {
412 allwrite(&cmdbuf[4], 1, NULL);
415 if (!strncasecmp(cmdbuf, "/msg ", 5)) {
417 strptr1 = &cmdbuf[5];
418 if ((t_context = find_context(&strptr1))) {
419 allwrite(strptr1, 2, CC->curr_user);
420 if (strcasecmp(CC->curr_user, t_context->curr_user))
421 allwrite(strptr1, 2, t_context->curr_user);
423 cprintf(":|User not found.\n");
426 /* The /kick function is implemented by sending a specific
427 * message to the kicked-out user's context. When that message
428 * is processed by the read loop, that context will exit.
430 if ( (!strncasecmp(cmdbuf, "/kick ", 6)) && (is_room_aide()) ) {
432 strptr1 = &cmdbuf[6];
433 strcat(strptr1, " ");
434 if ((t_context = find_context(&strptr1))) {
435 if (strcasecmp(CC->curr_user, t_context->curr_user))
436 allwrite(strptr1, 3, t_context->curr_user);
438 cprintf(":|User not found.\n");
441 if ((cmdbuf[0] != '/') && (strlen(cmdbuf) > 0)) {
443 allwrite(cmdbuf, 0, NULL);
445 if ((!ok_cmd) && (cmdbuf[0]) && (cmdbuf[0] != '\n'))
446 cprintf(":|Command %s is not understood.\n", cmdbuf);
451 /* now check the queue for new incoming stuff */
453 if (CC->fake_username[0])
454 un = CC->fake_username;
457 if (ChatLastMsg > MyLastMsg) {
458 ThisLastMsg = ChatLastMsg;
459 for (clptr = ChatQueue; clptr != NULL; clptr = clptr->next) {
460 if ((clptr->chat_seq > MyLastMsg) && ((!clptr->chat_username[0]) || (!strncasecmp(un, clptr->chat_username, 32)))) {
461 if ((!clptr->chat_room[0]) || (!strncasecmp(CC->room.QRname, clptr->chat_room, ROOMNAMELEN))) {
462 /* Output new chat data */
463 cprintf("%s\n", clptr->chat_text);
465 /* See if we've been force-quitted (kicked etc.) */
466 if (!strcmp(&clptr->chat_text[2], KICKEDMSG)) {
467 allwrite("<kicked out of this room>", 0, NULL);
469 CC->cs_flags = CC->cs_flags - CS_CHAT;
471 /* Kick user out of room */
472 CtdlInvtKick(CC->user.fullname, 0);
474 /* And return to the Lobby */
475 usergoto(config.c_baseroom, 0, 0, NULL, NULL);
481 MyLastMsg = ThisLastMsg;
489 * Delete any remaining instant messages
491 void delete_instant_messages(void) {
492 struct ExpressMessage *ptr;
494 begin_critical_section(S_SESSION_TABLE);
495 while (CC->FirstExpressMessage != NULL) {
496 ptr = CC->FirstExpressMessage->next;
497 if (CC->FirstExpressMessage->text != NULL)
498 free(CC->FirstExpressMessage->text);
499 free(CC->FirstExpressMessage);
500 CC->FirstExpressMessage = ptr;
502 end_critical_section(S_SESSION_TABLE);
509 * Poll for instant messages (OLD METHOD -- ***DEPRECATED ***)
511 void cmd_pexp(char *argbuf)
513 struct ExpressMessage *ptr, *holdptr;
515 if (CC->FirstExpressMessage == NULL) {
516 cprintf("%d No instant messages waiting.\n", ERROR + MESSAGE_NOT_FOUND);
519 begin_critical_section(S_SESSION_TABLE);
520 ptr = CC->FirstExpressMessage;
521 CC->FirstExpressMessage = NULL;
522 end_critical_section(S_SESSION_TABLE);
524 cprintf("%d Express msgs:\n", LISTING_FOLLOWS);
525 while (ptr != NULL) {
526 if (ptr->flags && EM_BROADCAST)
527 cprintf("Broadcast message ");
528 else if (ptr->flags && EM_CHAT)
529 cprintf("Chat request ");
530 else if (ptr->flags && EM_GO_AWAY)
531 cprintf("Please logoff now, as requested ");
534 cprintf("from %s:\n", ptr->sender);
535 if (ptr->text != NULL)
536 memfmout(ptr->text, 0, "\n");
539 if (ptr->text != NULL) free(ptr->text);
548 * Get instant messages (new method)
550 void cmd_gexp(char *argbuf) {
551 struct ExpressMessage *ptr;
553 if (CC->FirstExpressMessage == NULL) {
554 cprintf("%d No instant messages waiting.\n", ERROR + MESSAGE_NOT_FOUND);
558 begin_critical_section(S_SESSION_TABLE);
559 ptr = CC->FirstExpressMessage;
560 CC->FirstExpressMessage = CC->FirstExpressMessage->next;
561 end_critical_section(S_SESSION_TABLE);
563 cprintf("%d %d|%ld|%d|%s|%s|%s\n",
565 ((ptr->next != NULL) ? 1 : 0), /* more msgs? */
566 (long)ptr->timestamp, /* time sent */
567 ptr->flags, /* flags */
568 ptr->sender, /* sender of msg */
569 config.c_nodename, /* static for now (and possibly deprecated) */
570 ptr->sender_email /* email or jid of sender */
573 if (ptr->text != NULL) {
574 memfmout(ptr->text, 0, "\n");
575 if (ptr->text[strlen(ptr->text)-1] != '\n') cprintf("\n");
584 * Asynchronously deliver instant messages
586 void cmd_gexp_async(void) {
588 /* Only do this if the session can handle asynchronous protocol */
589 if (CC->is_async == 0) return;
591 /* And don't do it if there's nothing to send. */
592 if (CC->FirstExpressMessage == NULL) return;
594 cprintf("%d instant msg\n", ASYNC_MSG + ASYNC_GEXP);
598 * Back end support function for send_instant_message() and company
600 void add_xmsg_to_context(struct CitContext *ccptr, struct ExpressMessage *newmsg)
602 struct ExpressMessage *findend;
604 if (ccptr->FirstExpressMessage == NULL) {
605 ccptr->FirstExpressMessage = newmsg;
608 findend = ccptr->FirstExpressMessage;
609 while (findend->next != NULL) {
610 findend = findend->next;
612 findend->next = newmsg;
615 /* If the target context is a session which can handle asynchronous
616 * messages, go ahead and set the flag for that.
618 if (ccptr->is_async) {
619 ccptr->async_waiting = 1;
620 if (ccptr->state == CON_IDLE) {
621 ccptr->state = CON_READY;
630 * This is the back end to the instant message sending function.
631 * Returns the number of users to which the message was sent.
632 * Sending a zero-length message tests for recipients without sending messages.
634 int send_instant_message(char *lun, char *lem, char *x_user, char *x_msg)
636 int message_sent = 0; /* number of successful sends */
637 struct CitContext *ccptr;
638 struct ExpressMessage *newmsg = NULL;
641 int do_send = 0; /* 1 = send message; 0 = only check for valid recipient */
643 if (strlen(x_msg) > 0) {
644 msglen = strlen(x_msg) + 4;
648 /* find the target user's context and append the message */
649 begin_critical_section(S_SESSION_TABLE);
650 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
652 if (ccptr->fake_username[0]) {
653 un = ccptr->fake_username;
656 un = ccptr->user.fullname;
659 if ( ((!strcasecmp(un, x_user))
660 || (!strcasecmp(x_user, "broadcast")))
661 && (ccptr->can_receive_im)
662 && ((ccptr->disable_exp == 0)
663 || (CC->user.axlevel >= 6)) ) {
665 newmsg = (struct ExpressMessage *)
666 malloc(sizeof (struct ExpressMessage));
668 sizeof (struct ExpressMessage));
669 time(&(newmsg->timestamp));
670 safestrncpy(newmsg->sender, lun, sizeof newmsg->sender);
671 safestrncpy(newmsg->sender_email, lem, sizeof newmsg->sender_email);
672 if (!strcasecmp(x_user, "broadcast")) {
673 newmsg->flags |= EM_BROADCAST;
675 newmsg->text = strdup(x_msg);
677 add_xmsg_to_context(ccptr, newmsg);
681 log_instant_message(CC, ccptr, newmsg->text);
687 end_critical_section(S_SESSION_TABLE);
688 return (message_sent);
692 * send instant messages
694 void cmd_sexp(char *argbuf)
696 int message_sent = 0;
697 char x_user[USERNAME_SIZE];
701 char *x_big_msgbuf = NULL;
703 if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
704 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
707 if (CC->fake_username[0])
708 lun = CC->fake_username;
710 lun = CC->user.fullname;
712 lem = CC->cs_inet_email;
714 extract_token(x_user, argbuf, 0, '|', sizeof x_user);
715 extract_token(x_msg, argbuf, 1, '|', sizeof x_msg);
718 cprintf("%d You were not previously paged.\n", ERROR + NO_SUCH_USER);
721 if ((!strcasecmp(x_user, "broadcast")) && (CC->user.axlevel < 6)) {
722 cprintf("%d Higher access required to send a broadcast.\n",
723 ERROR + HIGHER_ACCESS_REQUIRED);
726 /* This loop handles text-transfer pages */
727 if (!strcmp(x_msg, "-")) {
728 message_sent = PerformXmsgHooks(lun, lem, x_user, "");
729 if (message_sent == 0) {
730 if (getuser(NULL, x_user))
731 cprintf("%d '%s' does not exist.\n",
732 ERROR + NO_SUCH_USER, x_user);
734 cprintf("%d '%s' is not logged in "
735 "or is not accepting pages.\n",
736 ERROR + RESOURCE_NOT_OPEN, x_user);
740 cprintf("%d Transmit message (will deliver to %d users)\n",
741 SEND_LISTING, message_sent);
742 x_big_msgbuf = malloc(SIZ);
743 memset(x_big_msgbuf, 0, SIZ);
744 while (client_getln(x_msg, sizeof x_msg) >= 0 && strcmp(x_msg, "000")) {
745 x_big_msgbuf = realloc(x_big_msgbuf,
746 strlen(x_big_msgbuf) + strlen(x_msg) + 4);
747 if (!IsEmptyStr(x_big_msgbuf))
748 if (x_big_msgbuf[strlen(x_big_msgbuf)] != '\n')
749 strcat(x_big_msgbuf, "\n");
750 strcat(x_big_msgbuf, x_msg);
752 PerformXmsgHooks(lun, lem, x_user, x_big_msgbuf);
755 /* This loop handles inline pages */
757 message_sent = PerformXmsgHooks(lun, lem, x_user, x_msg);
759 if (message_sent > 0) {
760 if (!IsEmptyStr(x_msg))
761 cprintf("%d Message sent", CIT_OK);
763 cprintf("%d Ok to send message", CIT_OK);
764 if (message_sent > 1)
765 cprintf(" to %d users", message_sent);
768 if (getuser(NULL, x_user))
769 cprintf("%d '%s' does not exist.\n",
770 ERROR + NO_SUCH_USER, x_user);
772 cprintf("%d '%s' is not logged in "
773 "or is not accepting pages.\n",
774 ERROR + RESOURCE_NOT_OPEN, x_user);
784 * Enter or exit paging-disabled mode
786 void cmd_dexp(char *argbuf)
790 if (CtdlAccessCheck(ac_logged_in)) return;
792 new_state = extract_int(argbuf, 0);
793 if ((new_state == 0) || (new_state == 1)) {
794 CC->disable_exp = new_state;
797 cprintf("%d %d\n", CIT_OK, CC->disable_exp);
802 * Request client termination
804 void cmd_reqt(char *argbuf) {
805 struct CitContext *ccptr;
808 struct ExpressMessage *newmsg;
810 if (CtdlAccessCheck(ac_aide)) return;
811 which_session = extract_int(argbuf, 0);
813 begin_critical_section(S_SESSION_TABLE);
814 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
815 if ((ccptr->cs_pid == which_session) || (which_session == 0)) {
817 newmsg = (struct ExpressMessage *)
818 malloc(sizeof (struct ExpressMessage));
820 sizeof (struct ExpressMessage));
821 time(&(newmsg->timestamp));
822 safestrncpy(newmsg->sender, CC->user.fullname,
823 sizeof newmsg->sender);
824 newmsg->flags |= EM_GO_AWAY;
825 newmsg->text = strdup("Automatic logoff requested.");
827 add_xmsg_to_context(ccptr, newmsg);
832 end_critical_section(S_SESSION_TABLE);
833 cprintf("%d Sent termination request to %d sessions.\n", CIT_OK, sessions);
838 * This is the back end for flush_conversations_to_disk()
839 * At this point we've isolated a single conversation (struct imlog)
840 * and are ready to write it to disk.
842 void flush_individual_conversation(struct imlog *im) {
843 struct CtdlMessage *msg;
845 char roomname[ROOMNAMELEN];
847 StrBufAppendBufPlain(im->conversation, HKEY(
853 msg = malloc(sizeof(struct CtdlMessage));
854 memset(msg, 0, sizeof(struct CtdlMessage));
855 msg->cm_magic = CTDLMESSAGE_MAGIC;
856 msg->cm_anon_type = MES_NORMAL;
857 msg->cm_format_type = FMT_RFC822;
858 if (!IsEmptyStr(im->usernames[0])) {
859 msg->cm_fields['A'] = strdup(im->usernames[0]);
861 msg->cm_fields['A'] = strdup("Citadel");
863 if (!IsEmptyStr(im->usernames[1])) {
864 msg->cm_fields['R'] = strdup(im->usernames[1]);
866 msg->cm_fields['O'] = strdup(PAGELOGROOM);
867 msg->cm_fields['N'] = strdup(NODENAME);
868 msg->cm_fields['M'] = strdup(ChrPtr(im->conversation));
870 /* Start with usernums[1] because it's guaranteed to be higher than usernums[0],
871 * so if there's only one party, usernums[0] will be zero but usernums[1] won't.
872 * Create the room if necessary. Note that we create as a type 5 room rather
873 * than 4, which indicates that it's a personal room but we've already supplied
874 * the namespace prefix.
876 * In the unlikely event that usernums[1] is zero, a room with an invalid namespace
877 * prefix will be created. That's ok because the auto-purger will clean it up later.
879 snprintf(roomname, sizeof roomname, "%010ld.%s", im->usernums[1], PAGELOGROOM);
880 create_room(roomname, 5, "", 0, 1, 1, VIEW_BBS);
881 msgnum = CtdlSubmitMsg(msg, NULL, roomname, 0);
882 CtdlFreeMessage(msg);
884 /* If there is a valid user number in usernums[0], save a copy for them too. */
885 if (im->usernums[0] > 0) {
886 snprintf(roomname, sizeof roomname, "%010ld.%s", im->usernums[0], PAGELOGROOM);
887 create_room(roomname, 5, "", 0, 1, 1, VIEW_BBS);
888 CtdlSaveMsgPointerInRoom(roomname, msgnum, 0, NULL);
891 /* Finally, if we're logging instant messages globally, do that now. */
892 if (!IsEmptyStr(config.c_logpages)) {
893 create_room(config.c_logpages, 3, "", 0, 1, 1, VIEW_BBS);
894 CtdlSaveMsgPointerInRoom(config.c_logpages, msgnum, 0, NULL);
900 * Locate instant message conversations which have gone idle
901 * (or, if the server is shutting down, locate *all* conversations)
902 * and flush them to disk (in the participants' log rooms, etc.)
904 void flush_conversations_to_disk(time_t if_older_than) {
906 struct imlog *flush_these = NULL;
907 struct imlog *dont_flush_these = NULL;
908 struct imlog *imptr = NULL;
910 begin_critical_section(S_IM_LOGS);
914 imlist = imlist->next;
915 if ((time(NULL) - imptr->lastmsg) > if_older_than)
917 /* This conversation qualifies. Move it to the list of ones to flush. */
918 imptr->next = flush_these;
922 /* Move it to the list of ones not to flush. */
923 imptr->next = dont_flush_these;
924 dont_flush_these = imptr;
927 imlist = dont_flush_these;
928 end_critical_section(S_IM_LOGS);
930 /* We are now outside of the critical section, and we are the only thread holding a
931 * pointer to a linked list of conversations to be flushed to disk.
933 while (flush_these) {
935 flush_individual_conversation(flush_these);
937 flush_these = flush_these->next;
938 FreeStrBuf(&imptr->conversation);
945 void chat_timer(void) {
946 flush_conversations_to_disk(300); /* Anything that hasn't peeped in more than 5 minutes */
949 void chat_shutdown(void) {
950 flush_conversations_to_disk(0); /* Get it ALL onto disk NOW. */
953 CTDL_MODULE_INIT(chat)
957 CtdlRegisterProtoHook(cmd_chat, "CHAT", "Begin real-time chat");
958 CtdlRegisterProtoHook(cmd_pexp, "PEXP", "Poll for instant messages");
959 CtdlRegisterProtoHook(cmd_gexp, "GEXP", "Get instant messages");
960 CtdlRegisterProtoHook(cmd_sexp, "SEXP", "Send an instant message");
961 CtdlRegisterProtoHook(cmd_dexp, "DEXP", "Disable instant messages");
962 CtdlRegisterProtoHook(cmd_reqt, "REQT", "Request client termination");
963 CtdlRegisterSessionHook(cmd_gexp_async, EVT_ASYNC);
964 CtdlRegisterSessionHook(delete_instant_messages, EVT_STOP);
965 CtdlRegisterXmsgHook(send_instant_message, XMSG_PRI_LOCAL);
966 CtdlRegisterSessionHook(chat_timer, EVT_TIMER);
967 CtdlRegisterSessionHook(chat_shutdown, EVT_SHUTDOWN);
970 /* return our Subversion id for the Log */