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,
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, "<p><b>", -1, 0);
124 StrBufAppendBufPlain(this_im->conversation, me->user.fullname, -1, 0);
125 StrBufAppendBufPlain(this_im->conversation, ":</b> ", -1, 0);
126 StrEscAppend(this_im->conversation, NULL, msgtext, 0, 0);
127 StrBufAppendBufPlain(this_im->conversation, "</p>\r\n", -1, 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)
263 && (!IsEmptyStr(ccptr->fake_roomname))) {
264 strcpy(roomname, ccptr->fake_roomname);
267 if ((ccptr->cs_flags & CS_CHAT)
268 && ((ccptr->cs_flags & CS_STEALTH) == 0)) {
269 if ((allflag == 0) || (allflag == 1)) {
270 cprintf(":| %-25s <%s>:\n",
271 (ccptr->fake_username[0]) ? ccptr->fake_username : ccptr->curr_user,
278 cprintf(":|\n:| Users not in chat:\n");
279 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
281 GenerateRoomDisplay(roomname, ccptr, CC);
282 if ((CC->user.axlevel < 6)
283 && (!IsEmptyStr(ccptr->fake_roomname))) {
284 strcpy(roomname, ccptr->fake_roomname);
287 if (((ccptr->cs_flags & CS_CHAT) == 0)
288 && ((ccptr->cs_flags & CS_STEALTH) == 0)) {
289 cprintf(":| %-25s <%s>:\n",
290 (ccptr->fake_username[0]) ? ccptr->fake_username : ccptr->curr_user,
295 end_critical_section(S_SESSION_TABLE);
299 cprintf(":|There are %d users here.\n", count);
302 cprintf(":|Note: you are the only one here.\n");
304 if (count_elsewhere > 0) {
305 cprintf(":|There are %d users chatting in other rooms.\n", count_elsewhere);
313 void cmd_chat(char *argbuf)
318 int MyLastMsg, ThisLastMsg;
319 struct ChatLine *clptr;
320 struct CitContext *t_context;
323 if (!(CC->logged_in)) {
324 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
328 CC->cs_flags = CC->cs_flags | CS_CHAT;
329 cprintf("%d Entering chat mode (type '/help' for available commands)\n",
333 MyLastMsg = ChatLastMsg;
335 if ((CC->cs_flags & CS_STEALTH) == 0) {
336 allwrite("<entering chat>", 0, NULL);
347 linelen = strlen(cmdbuf);
348 if (linelen > 100) --linelen; /* truncate too-long lines */
349 cmdbuf[linelen + 1] = 0;
351 retval = client_read_to(&cmdbuf[linelen], 1, 2);
353 if (retval < 0 || CC->kill_me) { /* socket broken? */
354 if ((CC->cs_flags & CS_STEALTH) == 0) {
355 allwrite("<disconnected>", 0, NULL);
360 /* if we have a complete line, do send processing */
361 if (!IsEmptyStr(cmdbuf))
362 if (cmdbuf[strlen(cmdbuf) - 1] == 10) {
363 cmdbuf[strlen(cmdbuf) - 1] = 0;
367 if ((!strcasecmp(cmdbuf, "exit"))
368 || (!strcasecmp(cmdbuf, "/exit"))
369 || (!strcasecmp(cmdbuf, "quit"))
370 || (!strcasecmp(cmdbuf, "logout"))
371 || (!strcasecmp(cmdbuf, "logoff"))
372 || (!strcasecmp(cmdbuf, "/q"))
373 || (!strcasecmp(cmdbuf, ".q"))
374 || (!strcasecmp(cmdbuf, "/quit"))
376 strcpy(cmdbuf, "000");
378 if (!strcmp(cmdbuf, "000")) {
379 if ((CC->cs_flags & CS_STEALTH) == 0) {
380 allwrite("<exiting chat>", 0, NULL);
384 CC->cs_flags = CC->cs_flags - CS_CHAT;
387 if ((!strcasecmp(cmdbuf, "/help"))
388 || (!strcasecmp(cmdbuf, "help"))
389 || (!strcasecmp(cmdbuf, "/?"))
390 || (!strcasecmp(cmdbuf, "?"))) {
392 cprintf(":|Available commands: \n");
393 cprintf(":|/help (prints this message) \n");
394 cprintf(":|/who (list users currently in chat) \n");
395 cprintf(":|/whobbs (list users in chat -and- elsewhere) \n");
396 cprintf(":|/me ('action' line, ala irc) \n");
397 cprintf(":|/msg (send private message, ala irc) \n");
398 if (is_room_aide()) {
399 cprintf(":|/kick (kick another user out of this room) \n");
401 cprintf(":|/quit (exit from this chat) \n");
405 if (!strcasecmp(cmdbuf, "/who")) {
409 if (!strcasecmp(cmdbuf, "/whobbs")) {
413 if (!strncasecmp(cmdbuf, "/me ", 4)) {
414 allwrite(&cmdbuf[4], 1, NULL);
417 if (!strncasecmp(cmdbuf, "/msg ", 5)) {
419 strptr1 = &cmdbuf[5];
420 if ((t_context = find_context(&strptr1))) {
421 allwrite(strptr1, 2, CC->curr_user);
422 if (strcasecmp(CC->curr_user, t_context->curr_user))
423 allwrite(strptr1, 2, t_context->curr_user);
425 cprintf(":|User not found.\n");
428 /* The /kick function is implemented by sending a specific
429 * message to the kicked-out user's context. When that message
430 * is processed by the read loop, that context will exit.
432 if ( (!strncasecmp(cmdbuf, "/kick ", 6)) && (is_room_aide()) ) {
434 strptr1 = &cmdbuf[6];
435 strcat(strptr1, " ");
436 if ((t_context = find_context(&strptr1))) {
437 if (strcasecmp(CC->curr_user, t_context->curr_user))
438 allwrite(strptr1, 3, t_context->curr_user);
440 cprintf(":|User not found.\n");
443 if ((cmdbuf[0] != '/') && (strlen(cmdbuf) > 0)) {
445 allwrite(cmdbuf, 0, NULL);
447 if ((!ok_cmd) && (cmdbuf[0]) && (cmdbuf[0] != '\n'))
448 cprintf(":|Command %s is not understood.\n", cmdbuf);
453 /* now check the queue for new incoming stuff */
455 if (CC->fake_username[0])
456 un = CC->fake_username;
459 if (ChatLastMsg > MyLastMsg) {
460 ThisLastMsg = ChatLastMsg;
461 for (clptr = ChatQueue; clptr != NULL; clptr = clptr->next) {
462 if ((clptr->chat_seq > MyLastMsg) && ((!clptr->chat_username[0]) || (!strncasecmp(un, clptr->chat_username, 32)))) {
463 if ((!clptr->chat_room[0]) || (!strncasecmp(CC->room.QRname, clptr->chat_room, ROOMNAMELEN))) {
464 /* Output new chat data */
465 cprintf("%s\n", clptr->chat_text);
467 /* See if we've been force-quitted (kicked etc.) */
468 if (!strcmp(&clptr->chat_text[2], KICKEDMSG)) {
469 allwrite("<kicked out of this room>", 0, NULL);
471 CC->cs_flags = CC->cs_flags - CS_CHAT;
473 /* Kick user out of room */
474 CtdlInvtKick(CC->user.fullname, 0);
476 /* And return to the Lobby */
477 usergoto(config.c_baseroom, 0, 0, NULL, NULL);
483 MyLastMsg = ThisLastMsg;
491 * Delete any remaining instant messages
493 void delete_instant_messages(void) {
494 struct ExpressMessage *ptr;
496 begin_critical_section(S_SESSION_TABLE);
497 while (CC->FirstExpressMessage != NULL) {
498 ptr = CC->FirstExpressMessage->next;
499 if (CC->FirstExpressMessage->text != NULL)
500 free(CC->FirstExpressMessage->text);
501 free(CC->FirstExpressMessage);
502 CC->FirstExpressMessage = ptr;
504 end_critical_section(S_SESSION_TABLE);
511 * Poll for instant messages (OLD METHOD -- ***DEPRECATED ***)
513 void cmd_pexp(char *argbuf)
515 struct ExpressMessage *ptr, *holdptr;
517 if (CC->FirstExpressMessage == NULL) {
518 cprintf("%d No instant messages waiting.\n", ERROR + MESSAGE_NOT_FOUND);
521 begin_critical_section(S_SESSION_TABLE);
522 ptr = CC->FirstExpressMessage;
523 CC->FirstExpressMessage = NULL;
524 end_critical_section(S_SESSION_TABLE);
526 cprintf("%d Express msgs:\n", LISTING_FOLLOWS);
527 while (ptr != NULL) {
528 if (ptr->flags && EM_BROADCAST)
529 cprintf("Broadcast message ");
530 else if (ptr->flags && EM_CHAT)
531 cprintf("Chat request ");
532 else if (ptr->flags && EM_GO_AWAY)
533 cprintf("Please logoff now, as requested ");
536 cprintf("from %s:\n", ptr->sender);
537 if (ptr->text != NULL)
538 memfmout(ptr->text, 0, "\n");
541 if (ptr->text != NULL) free(ptr->text);
550 * Get instant messages (new method)
552 void cmd_gexp(char *argbuf) {
553 struct ExpressMessage *ptr;
555 if (CC->FirstExpressMessage == NULL) {
556 cprintf("%d No instant messages waiting.\n", ERROR + MESSAGE_NOT_FOUND);
560 begin_critical_section(S_SESSION_TABLE);
561 ptr = CC->FirstExpressMessage;
562 CC->FirstExpressMessage = CC->FirstExpressMessage->next;
563 end_critical_section(S_SESSION_TABLE);
565 cprintf("%d %d|%ld|%d|%s|%s|%s\n",
567 ((ptr->next != NULL) ? 1 : 0), /* more msgs? */
568 (long)ptr->timestamp, /* time sent */
569 ptr->flags, /* flags */
570 ptr->sender, /* sender of msg */
571 config.c_nodename, /* static for now (and possibly deprecated) */
572 ptr->sender_email /* email or jid of sender */
575 if (ptr->text != NULL) {
576 memfmout(ptr->text, 0, "\n");
577 if (ptr->text[strlen(ptr->text)-1] != '\n') cprintf("\n");
586 * Asynchronously deliver instant messages
588 void cmd_gexp_async(void) {
590 /* Only do this if the session can handle asynchronous protocol */
591 if (CC->is_async == 0) return;
593 /* And don't do it if there's nothing to send. */
594 if (CC->FirstExpressMessage == NULL) return;
596 cprintf("%d instant msg\n", ASYNC_MSG + ASYNC_GEXP);
600 * Back end support function for send_instant_message() and company
602 void add_xmsg_to_context(struct CitContext *ccptr, struct ExpressMessage *newmsg)
604 struct ExpressMessage *findend;
606 if (ccptr->FirstExpressMessage == NULL) {
607 ccptr->FirstExpressMessage = newmsg;
610 findend = ccptr->FirstExpressMessage;
611 while (findend->next != NULL) {
612 findend = findend->next;
614 findend->next = newmsg;
617 /* If the target context is a session which can handle asynchronous
618 * messages, go ahead and set the flag for that.
620 if (ccptr->is_async) {
621 ccptr->async_waiting = 1;
622 if (ccptr->state == CON_IDLE) {
623 ccptr->state = CON_READY;
632 * This is the back end to the instant message sending function.
633 * Returns the number of users to which the message was sent.
634 * Sending a zero-length message tests for recipients without sending messages.
636 int send_instant_message(char *lun, char *lem, char *x_user, char *x_msg)
638 int message_sent = 0; /* number of successful sends */
639 struct CitContext *ccptr;
640 struct ExpressMessage *newmsg = NULL;
643 int do_send = 0; /* 1 = send message; 0 = only check for valid recipient */
645 if (strlen(x_msg) > 0) {
646 msglen = strlen(x_msg) + 4;
650 /* find the target user's context and append the message */
651 begin_critical_section(S_SESSION_TABLE);
652 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
654 if (ccptr->fake_username[0]) {
655 un = ccptr->fake_username;
658 un = ccptr->user.fullname;
661 if ( ((!strcasecmp(un, x_user))
662 || (!strcasecmp(x_user, "broadcast")))
663 && (ccptr->can_receive_im)
664 && ((ccptr->disable_exp == 0)
665 || (CC->user.axlevel >= 6)) ) {
667 newmsg = (struct ExpressMessage *)
668 malloc(sizeof (struct ExpressMessage));
670 sizeof (struct ExpressMessage));
671 time(&(newmsg->timestamp));
672 safestrncpy(newmsg->sender, lun, sizeof newmsg->sender);
673 safestrncpy(newmsg->sender_email, lem, sizeof newmsg->sender_email);
674 if (!strcasecmp(x_user, "broadcast")) {
675 newmsg->flags |= EM_BROADCAST;
677 newmsg->text = strdup(x_msg);
679 add_xmsg_to_context(ccptr, newmsg);
683 log_instant_message(CC, ccptr, newmsg->text);
689 end_critical_section(S_SESSION_TABLE);
690 return (message_sent);
694 * send instant messages
696 void cmd_sexp(char *argbuf)
698 int message_sent = 0;
699 char x_user[USERNAME_SIZE];
703 char *x_big_msgbuf = NULL;
705 if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
706 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
709 if (CC->fake_username[0])
710 lun = CC->fake_username;
712 lun = CC->user.fullname;
714 lem = CC->cs_inet_email;
716 extract_token(x_user, argbuf, 0, '|', sizeof x_user);
717 extract_token(x_msg, argbuf, 1, '|', sizeof x_msg);
720 cprintf("%d You were not previously paged.\n", ERROR + NO_SUCH_USER);
723 if ((!strcasecmp(x_user, "broadcast")) && (CC->user.axlevel < 6)) {
724 cprintf("%d Higher access required to send a broadcast.\n",
725 ERROR + HIGHER_ACCESS_REQUIRED);
728 /* This loop handles text-transfer pages */
729 if (!strcmp(x_msg, "-")) {
730 message_sent = PerformXmsgHooks(lun, lem, x_user, "");
731 if (message_sent == 0) {
732 if (getuser(NULL, x_user))
733 cprintf("%d '%s' does not exist.\n",
734 ERROR + NO_SUCH_USER, x_user);
736 cprintf("%d '%s' is not logged in "
737 "or is not accepting pages.\n",
738 ERROR + RESOURCE_NOT_OPEN, x_user);
742 cprintf("%d Transmit message (will deliver to %d users)\n",
743 SEND_LISTING, message_sent);
744 x_big_msgbuf = malloc(SIZ);
745 memset(x_big_msgbuf, 0, SIZ);
746 while (client_getln(x_msg, sizeof x_msg) >= 0 && strcmp(x_msg, "000")) {
747 x_big_msgbuf = realloc(x_big_msgbuf,
748 strlen(x_big_msgbuf) + strlen(x_msg) + 4);
749 if (!IsEmptyStr(x_big_msgbuf))
750 if (x_big_msgbuf[strlen(x_big_msgbuf)] != '\n')
751 strcat(x_big_msgbuf, "\n");
752 strcat(x_big_msgbuf, x_msg);
754 PerformXmsgHooks(lun, lem, x_user, x_big_msgbuf);
757 /* This loop handles inline pages */
759 message_sent = PerformXmsgHooks(lun, lem, x_user, x_msg);
761 if (message_sent > 0) {
762 if (!IsEmptyStr(x_msg))
763 cprintf("%d Message sent", CIT_OK);
765 cprintf("%d Ok to send message", CIT_OK);
766 if (message_sent > 1)
767 cprintf(" to %d users", message_sent);
770 if (getuser(NULL, x_user))
771 cprintf("%d '%s' does not exist.\n",
772 ERROR + NO_SUCH_USER, x_user);
774 cprintf("%d '%s' is not logged in "
775 "or is not accepting pages.\n",
776 ERROR + RESOURCE_NOT_OPEN, x_user);
786 * Enter or exit paging-disabled mode
788 void cmd_dexp(char *argbuf)
792 if (CtdlAccessCheck(ac_logged_in)) return;
794 new_state = extract_int(argbuf, 0);
795 if ((new_state == 0) || (new_state == 1)) {
796 CC->disable_exp = new_state;
799 cprintf("%d %d\n", CIT_OK, CC->disable_exp);
804 * Request client termination
806 void cmd_reqt(char *argbuf) {
807 struct CitContext *ccptr;
810 struct ExpressMessage *newmsg;
812 if (CtdlAccessCheck(ac_aide)) return;
813 which_session = extract_int(argbuf, 0);
815 begin_critical_section(S_SESSION_TABLE);
816 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
817 if ((ccptr->cs_pid == which_session) || (which_session == 0)) {
819 newmsg = (struct ExpressMessage *)
820 malloc(sizeof (struct ExpressMessage));
822 sizeof (struct ExpressMessage));
823 time(&(newmsg->timestamp));
824 safestrncpy(newmsg->sender, CC->user.fullname,
825 sizeof newmsg->sender);
826 newmsg->flags |= EM_GO_AWAY;
827 newmsg->text = strdup("Automatic logoff requested.");
829 add_xmsg_to_context(ccptr, newmsg);
834 end_critical_section(S_SESSION_TABLE);
835 cprintf("%d Sent termination request to %d sessions.\n", CIT_OK, sessions);
840 * This is the back end for flush_conversations_to_disk()
841 * At this point we've isolated a single conversation (struct imlog)
842 * and are ready to write it to disk.
844 void flush_individual_conversation(struct imlog *im) {
845 struct CtdlMessage *msg;
847 char roomname[ROOMNAMELEN];
849 StrBufAppendBufPlain(im->conversation,
855 msg = malloc(sizeof(struct CtdlMessage));
856 memset(msg, 0, sizeof(struct CtdlMessage));
857 msg->cm_magic = CTDLMESSAGE_MAGIC;
858 msg->cm_anon_type = MES_NORMAL;
859 msg->cm_format_type = FMT_RFC822;
860 if (!IsEmptyStr(im->usernames[0])) {
861 msg->cm_fields['A'] = strdup(im->usernames[0]);
863 msg->cm_fields['A'] = strdup("Citadel");
865 if (!IsEmptyStr(im->usernames[1])) {
866 msg->cm_fields['R'] = strdup(im->usernames[1]);
868 msg->cm_fields['O'] = strdup(PAGELOGROOM);
869 msg->cm_fields['N'] = strdup(NODENAME);
870 msg->cm_fields['M'] = strdup(ChrPtr(im->conversation));
872 /* Start with usernums[1] because it's guaranteed to be higher than usernums[0],
873 * so if there's only one party, usernums[0] will be zero but usernums[1] won't.
874 * Create the room if necessary. Note that we create as a type 5 room rather
875 * than 4, which indicates that it's a personal room but we've already supplied
876 * the namespace prefix.
878 * In the unlikely event that usernums[1] is zero, a room with an invalid namespace
879 * prefix will be created. That's ok because the auto-purger will clean it up later.
881 snprintf(roomname, sizeof roomname, "%010ld.%s", im->usernums[1], PAGELOGROOM);
882 create_room(roomname, 5, "", 0, 1, 1, VIEW_BBS);
883 msgnum = CtdlSubmitMsg(msg, NULL, roomname, 0);
884 CtdlFreeMessage(msg);
886 /* If there is a valid user number in usernums[0], save a copy for them too. */
887 if (im->usernums[0] > 0) {
888 snprintf(roomname, sizeof roomname, "%010ld.%s", im->usernums[0], PAGELOGROOM);
889 create_room(roomname, 5, "", 0, 1, 1, VIEW_BBS);
890 CtdlSaveMsgPointerInRoom(roomname, msgnum, 0, NULL);
893 /* Finally, if we're logging instant messages globally, do that now. */
894 if (!IsEmptyStr(config.c_logpages)) {
895 create_room(config.c_logpages, 3, "", 0, 1, 1, VIEW_BBS);
896 CtdlSaveMsgPointerInRoom(config.c_logpages, msgnum, 0, NULL);
902 * Locate instant message conversations which have gone idle
903 * (or, if the server is shutting down, locate *all* conversations)
904 * and flush them to disk (in the participants' log rooms, etc.)
906 void flush_conversations_to_disk(time_t if_older_than) {
908 struct imlog *flush_these = NULL;
909 struct imlog *dont_flush_these = NULL;
910 struct imlog *imptr = NULL;
912 begin_critical_section(S_IM_LOGS);
916 imlist = imlist->next;
917 if ((time(NULL) - imptr->lastmsg) > if_older_than)
919 /* This conversation qualifies. Move it to the list of ones to flush. */
920 imptr->next = flush_these;
924 /* Move it to the list of ones not to flush. */
925 imptr->next = dont_flush_these;
926 dont_flush_these = imptr;
929 imlist = dont_flush_these;
930 end_critical_section(S_IM_LOGS);
932 /* We are now outside of the critical section, and we are the only thread holding a
933 * pointer to a linked list of conversations to be flushed to disk.
935 while (flush_these) {
937 flush_individual_conversation(flush_these);
939 flush_these = flush_these->next;
940 FreeStrBuf(&imptr->conversation);
947 void chat_timer(void) {
948 flush_conversations_to_disk(300); /* Anything that hasn't peeped in more than 5 minutes */
951 void chat_shutdown(void) {
952 flush_conversations_to_disk(0); /* Get it ALL onto disk NOW. */
955 CTDL_MODULE_INIT(chat)
959 CtdlRegisterProtoHook(cmd_chat, "CHAT", "Begin real-time chat");
960 CtdlRegisterProtoHook(cmd_pexp, "PEXP", "Poll for instant messages");
961 CtdlRegisterProtoHook(cmd_gexp, "GEXP", "Get instant messages");
962 CtdlRegisterProtoHook(cmd_sexp, "SEXP", "Send an instant message");
963 CtdlRegisterProtoHook(cmd_dexp, "DEXP", "Disable instant messages");
964 CtdlRegisterProtoHook(cmd_reqt, "REQT", "Request client termination");
965 CtdlRegisterSessionHook(cmd_gexp_async, EVT_ASYNC);
966 CtdlRegisterSessionHook(delete_instant_messages, EVT_STOP);
967 CtdlRegisterXmsgHook(send_instant_message, XMSG_PRI_LOCAL);
968 CtdlRegisterSessionHook(chat_timer, EVT_TIMER);
969 CtdlRegisterSessionHook(chat_shutdown, EVT_SHUTDOWN);
972 /* return our Subversion id for the Log */