4 * This module handles all "real time" communication between users. The
5 * modes of communication currently supported are Chat and Paging.
7 * Copyright (c) 1987-2009 by the citadel.org team
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32 #include <sys/types.h>
34 #if TIME_WITH_SYS_TIME
35 # include <sys/time.h>
39 # include <sys/time.h>
48 #include <libcitadel.h>
51 #include "serv_chat.h"
52 #include "citserver.h"
63 #include "ctdl_module.h"
65 struct ChatLine *ChatQueue = NULL;
71 char usernames[2][128];
77 struct imlog *imlist = NULL;
80 * This function handles the logging of instant messages to disk.
82 void log_instant_message(struct CitContext *me, struct CitContext *them, char *msgtext, int serial_number)
86 struct imlog *iptr = NULL;
87 struct imlog *this_im = NULL;
89 memset(usernums, 0, sizeof usernums);
90 usernums[0] = me->user.usernum;
91 usernums[1] = them->user.usernum;
93 /* Always put the lower user number first, so we can use the array as a hash value which
94 * represents a pair of users. For a broadcast message one of the users will be 0.
96 if (usernums[0] > usernums[1]) {
98 usernums[0] = usernums[1];
102 begin_critical_section(S_IM_LOGS);
104 /* Look for an existing conversation in the hash table.
105 * If not found, create a new one.
109 for (iptr = imlist; iptr != NULL; iptr = iptr->next) {
110 if ((iptr->usernums[0] == usernums[0]) && (iptr->usernums[1] == usernums[1])) {
111 /* Existing conversation */
115 if (this_im == NULL) {
116 /* New conversation */
117 this_im = malloc(sizeof(struct imlog));
118 memset(this_im, 0, sizeof (struct imlog));
119 this_im->usernums[0] = usernums[0];
120 this_im->usernums[1] = usernums[1];
121 /* usernames[] and usernums[] might not be in the same order. This is not an error. */
123 safestrncpy(this_im->usernames[0], me->user.fullname, sizeof this_im->usernames[0]);
126 safestrncpy(this_im->usernames[1], them->user.fullname, sizeof this_im->usernames[1]);
128 this_im->conversation = NewStrBuf();
129 this_im->next = imlist;
131 StrBufAppendBufPlain(this_im->conversation, HKEY(
132 "Content-type: text/html\r\n"
133 "Content-transfer-encoding: 7bit\r\n"
140 /* Since it's possible for this function to get called more than once if a user is logged
141 * in on multiple sessions, we use the message's serial number to keep track of whether
142 * we've already logged it.
144 if (this_im->last_serial != serial_number)
146 this_im->lastmsg = time(NULL); /* Touch the timestamp so we know when to flush */
147 this_im->last_serial = serial_number;
148 StrBufAppendBufPlain(this_im->conversation, HKEY("<p><b>"), 0);
149 StrBufAppendBufPlain(this_im->conversation, me->user.fullname, -1, 0);
150 StrBufAppendBufPlain(this_im->conversation, HKEY(":</b> "), 0);
151 StrEscAppend(this_im->conversation, NULL, msgtext, 0, 0);
152 StrBufAppendBufPlain(this_im->conversation, HKEY("</p>\r\n"), 0);
154 end_critical_section(S_IM_LOGS);
158 * This message can be set to anything you want, but it is
159 * checked for consistency so don't move it away from here.
161 #define KICKEDMSG "You have been kicked out of this room."
163 void allwrite(char *cmdbuf, int flag, char *username)
168 struct ChatLine *clptr, *clnew;
171 if (CC->fake_username[0])
172 un = CC->fake_username;
174 un = CC->user.fullname;
176 snprintf(bcast, sizeof bcast, ":|<%s %s>", un, cmdbuf);
177 } else if (flag == 0) {
178 snprintf(bcast, sizeof bcast, "%s|%s", un, cmdbuf);
179 } else if (flag == 2) {
180 snprintf(bcast, sizeof bcast, ":|<%s whispers %s>", un, cmdbuf);
181 } else if (flag == 3) {
182 snprintf(bcast, sizeof bcast, ":|%s", KICKEDMSG);
184 if ((strcasecmp(cmdbuf, "NOOP")) && (flag != 2)) {
185 fp = fopen(CHATLOG, "a");
187 fprintf(fp, "%s\n", bcast);
190 clnew = (struct ChatLine *) malloc(sizeof(struct ChatLine));
191 memset(clnew, 0, sizeof(struct ChatLine));
193 fprintf(stderr, "citserver: cannot alloc chat line: %s\n",
199 clnew->chat_time = now;
200 safestrncpy(clnew->chat_room, CC->room.QRname,
201 sizeof clnew->chat_room);
202 clnew->chat_room[sizeof clnew->chat_room - 1] = 0;
204 safestrncpy(clnew->chat_username, username,
205 sizeof clnew->chat_username);
206 clnew->chat_username[sizeof clnew->chat_username - 1] = 0;
208 clnew->chat_username[0] = '\0';
209 safestrncpy(clnew->chat_text, bcast, sizeof clnew->chat_text);
211 /* Here's the critical section.
212 * First, add the new message to the queue...
214 begin_critical_section(S_CHATQUEUE);
216 clnew->chat_seq = ChatLastMsg;
217 if (ChatQueue == NULL) {
220 for (clptr = ChatQueue; clptr->next != NULL; clptr = clptr->next);;
224 /* Then, before releasing the lock, free the expired messages */
225 while ((ChatQueue != NULL) && (now - ChatQueue->chat_time >= 120L)) {
227 ChatQueue = ChatQueue->next;
230 end_critical_section(S_CHATQUEUE);
234 t_context *find_context(char **unstr)
236 t_context *t_cc, *found_cc = NULL;
239 if ((!*unstr) || (!unstr))
242 begin_critical_section(S_SESSION_TABLE);
243 for (t_cc = ContextList; ((t_cc) && (!found_cc)); t_cc = t_cc->next) {
244 if (t_cc->fake_username[0])
245 name = t_cc->fake_username;
247 name = t_cc->curr_user;
249 if ((!strncasecmp(name, tptr, strlen(name))) && (tptr[strlen(name)] == ' ')) {
251 *unstr = &(tptr[strlen(name) + 1]);
254 end_critical_section(S_SESSION_TABLE);
260 * List users in chat.
261 * allflag == 0 = list users in chat
262 * 1 = list users in chat, followed by users not in chat
263 * 2 = display count only
266 void do_chat_listing(int allflag)
268 struct CitContext *ccptr;
270 int count_elsewhere = 0;
271 char roomname[ROOMNAMELEN];
273 if ((allflag == 0) || (allflag == 1))
274 cprintf(":|\n:| Users currently in chat:\n");
275 begin_critical_section(S_SESSION_TABLE);
276 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
277 if (ccptr->cs_flags & CS_CHAT) {
278 if (!strcasecmp(ccptr->room.QRname,
287 GenerateRoomDisplay(roomname, ccptr, CC);
288 if ((CC->user.axlevel < 6) && (!IsEmptyStr(ccptr->fake_roomname))) {
289 strcpy(roomname, ccptr->fake_roomname);
292 if ((ccptr->cs_flags & CS_CHAT) && ((ccptr->cs_flags & CS_STEALTH) == 0)) {
293 if ((allflag == 0) || (allflag == 1)) {
294 cprintf(":| %-25s <%s>:\n",
295 (ccptr->fake_username[0]) ? ccptr->fake_username : ccptr->curr_user,
302 cprintf(":|\n:| Users not in chat:\n");
303 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
305 GenerateRoomDisplay(roomname, ccptr, CC);
306 if ((CC->user.axlevel < 6)
307 && (!IsEmptyStr(ccptr->fake_roomname))) {
308 strcpy(roomname, ccptr->fake_roomname);
311 if (((ccptr->cs_flags & CS_CHAT) == 0)
312 && ((ccptr->cs_flags & CS_STEALTH) == 0)) {
313 cprintf(":| %-25s <%s>:\n",
314 (ccptr->fake_username[0]) ? ccptr->fake_username : ccptr->curr_user,
319 end_critical_section(S_SESSION_TABLE);
323 cprintf(":|There are %d users here.\n", count);
326 cprintf(":|Note: you are the only one here.\n");
328 if (count_elsewhere > 0) {
329 cprintf(":|There are %d users chatting in other rooms.\n", count_elsewhere);
337 void cmd_chat(char *argbuf)
342 int MyLastMsg, ThisLastMsg;
343 struct ChatLine *clptr;
344 struct CitContext *t_context;
347 if (!(CC->logged_in)) {
348 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
352 CC->cs_flags = CC->cs_flags | CS_CHAT;
353 cprintf("%d Entering chat mode (type '/help' for available commands)\n",
357 MyLastMsg = ChatLastMsg;
359 if ((CC->cs_flags & CS_STEALTH) == 0) {
360 allwrite("<entering chat>", 0, NULL);
371 linelen = strlen(cmdbuf);
372 if (linelen > 100) --linelen; /* truncate too-long lines */
373 cmdbuf[linelen + 1] = 0;
375 retval = client_read_to(&cmdbuf[linelen], 1, 2);
377 if (retval < 0 || CC->kill_me) { /* socket broken? */
378 if ((CC->cs_flags & CS_STEALTH) == 0) {
379 allwrite("<disconnected>", 0, NULL);
384 /* if we have a complete line, do send processing */
385 if (!IsEmptyStr(cmdbuf))
386 if (cmdbuf[strlen(cmdbuf) - 1] == 10) {
387 cmdbuf[strlen(cmdbuf) - 1] = 0;
391 if ((!strcasecmp(cmdbuf, "exit"))
392 || (!strcasecmp(cmdbuf, "/exit"))
393 || (!strcasecmp(cmdbuf, "quit"))
394 || (!strcasecmp(cmdbuf, "logout"))
395 || (!strcasecmp(cmdbuf, "logoff"))
396 || (!strcasecmp(cmdbuf, "/q"))
397 || (!strcasecmp(cmdbuf, ".q"))
398 || (!strcasecmp(cmdbuf, "/quit"))
400 strcpy(cmdbuf, "000");
402 if (!strcmp(cmdbuf, "000")) {
403 if ((CC->cs_flags & CS_STEALTH) == 0) {
404 allwrite("<exiting chat>", 0, NULL);
408 CC->cs_flags = CC->cs_flags - CS_CHAT;
411 if ((!strcasecmp(cmdbuf, "/help"))
412 || (!strcasecmp(cmdbuf, "help"))
413 || (!strcasecmp(cmdbuf, "/?"))
414 || (!strcasecmp(cmdbuf, "?"))) {
416 cprintf(":|Available commands: \n");
417 cprintf(":|/help (prints this message) \n");
418 cprintf(":|/who (list users currently in chat) \n");
419 cprintf(":|/whobbs (list users in chat -and- elsewhere) \n");
420 cprintf(":|/me ('action' line, ala irc) \n");
421 cprintf(":|/msg (send private message, ala irc) \n");
422 if (is_room_aide()) {
423 cprintf(":|/kick (kick another user out of this room) \n");
425 cprintf(":|/quit (exit from this chat) \n");
429 if (!strcasecmp(cmdbuf, "/who")) {
433 if (!strcasecmp(cmdbuf, "/whobbs")) {
437 if (!strncasecmp(cmdbuf, "/me ", 4)) {
438 allwrite(&cmdbuf[4], 1, NULL);
441 if (!strncasecmp(cmdbuf, "/msg ", 5)) {
443 strptr1 = &cmdbuf[5];
444 if ((t_context = find_context(&strptr1))) {
445 allwrite(strptr1, 2, CC->curr_user);
446 if (strcasecmp(CC->curr_user, t_context->curr_user))
447 allwrite(strptr1, 2, t_context->curr_user);
449 cprintf(":|User not found.\n");
452 /* The /kick function is implemented by sending a specific
453 * message to the kicked-out user's context. When that message
454 * is processed by the read loop, that context will exit.
456 if ( (!strncasecmp(cmdbuf, "/kick ", 6)) && (is_room_aide()) ) {
458 strptr1 = &cmdbuf[6];
459 strcat(strptr1, " ");
460 if ((t_context = find_context(&strptr1))) {
461 if (strcasecmp(CC->curr_user, t_context->curr_user))
462 allwrite(strptr1, 3, t_context->curr_user);
464 cprintf(":|User not found.\n");
467 if ((cmdbuf[0] != '/') && (strlen(cmdbuf) > 0)) {
469 allwrite(cmdbuf, 0, NULL);
471 if ((!ok_cmd) && (cmdbuf[0]) && (cmdbuf[0] != '\n'))
472 cprintf(":|Command %s is not understood.\n", cmdbuf);
477 /* now check the queue for new incoming stuff */
479 if (CC->fake_username[0])
480 un = CC->fake_username;
483 if (ChatLastMsg > MyLastMsg) {
484 ThisLastMsg = ChatLastMsg;
485 for (clptr = ChatQueue; clptr != NULL; clptr = clptr->next) {
486 if ((clptr->chat_seq > MyLastMsg) && ((!clptr->chat_username[0]) || (!strncasecmp(un, clptr->chat_username, 32)))) {
487 if ((!clptr->chat_room[0]) || (!strncasecmp(CC->room.QRname, clptr->chat_room, ROOMNAMELEN))) {
488 /* Output new chat data */
489 cprintf("%s\n", clptr->chat_text);
491 /* See if we've been force-quitted (kicked etc.) */
492 if (!strcmp(&clptr->chat_text[2], KICKEDMSG)) {
493 allwrite("<kicked out of this room>", 0, NULL);
495 CC->cs_flags = CC->cs_flags - CS_CHAT;
497 /* Kick user out of room */
498 CtdlInvtKick(CC->user.fullname, 0);
500 /* And return to the Lobby */
501 usergoto(config.c_baseroom, 0, 0, NULL, NULL);
507 MyLastMsg = ThisLastMsg;
515 * Delete any remaining instant messages
517 void delete_instant_messages(void) {
518 struct ExpressMessage *ptr;
520 begin_critical_section(S_SESSION_TABLE);
521 while (CC->FirstExpressMessage != NULL) {
522 ptr = CC->FirstExpressMessage->next;
523 if (CC->FirstExpressMessage->text != NULL)
524 free(CC->FirstExpressMessage->text);
525 free(CC->FirstExpressMessage);
526 CC->FirstExpressMessage = ptr;
528 end_critical_section(S_SESSION_TABLE);
535 * Poll for instant messages (OLD METHOD -- ***DEPRECATED ***)
537 void cmd_pexp(char *argbuf)
539 struct ExpressMessage *ptr, *holdptr;
541 if (CC->FirstExpressMessage == NULL) {
542 cprintf("%d No instant messages waiting.\n", ERROR + MESSAGE_NOT_FOUND);
545 begin_critical_section(S_SESSION_TABLE);
546 ptr = CC->FirstExpressMessage;
547 CC->FirstExpressMessage = NULL;
548 end_critical_section(S_SESSION_TABLE);
550 cprintf("%d Express msgs:\n", LISTING_FOLLOWS);
551 while (ptr != NULL) {
552 if (ptr->flags && EM_BROADCAST)
553 cprintf("Broadcast message ");
554 else if (ptr->flags && EM_CHAT)
555 cprintf("Chat request ");
556 else if (ptr->flags && EM_GO_AWAY)
557 cprintf("Please logoff now, as requested ");
560 cprintf("from %s:\n", ptr->sender);
561 if (ptr->text != NULL)
562 memfmout(ptr->text, 0, "\n");
565 if (ptr->text != NULL) free(ptr->text);
574 * Get instant messages (new method)
576 void cmd_gexp(char *argbuf) {
577 struct ExpressMessage *ptr;
579 if (CC->FirstExpressMessage == NULL) {
580 cprintf("%d No instant messages waiting.\n", ERROR + MESSAGE_NOT_FOUND);
584 begin_critical_section(S_SESSION_TABLE);
585 ptr = CC->FirstExpressMessage;
586 CC->FirstExpressMessage = CC->FirstExpressMessage->next;
587 end_critical_section(S_SESSION_TABLE);
589 cprintf("%d %d|%ld|%d|%s|%s|%s\n",
591 ((ptr->next != NULL) ? 1 : 0), /* more msgs? */
592 (long)ptr->timestamp, /* time sent */
593 ptr->flags, /* flags */
594 ptr->sender, /* sender of msg */
595 config.c_nodename, /* static for now (and possibly deprecated) */
596 ptr->sender_email /* email or jid of sender */
599 if (ptr->text != NULL) {
600 memfmout(ptr->text, 0, "\n");
601 if (ptr->text[strlen(ptr->text)-1] != '\n') cprintf("\n");
610 * Asynchronously deliver instant messages
612 void cmd_gexp_async(void) {
614 /* Only do this if the session can handle asynchronous protocol */
615 if (CC->is_async == 0) return;
617 /* And don't do it if there's nothing to send. */
618 if (CC->FirstExpressMessage == NULL) return;
620 cprintf("%d instant msg\n", ASYNC_MSG + ASYNC_GEXP);
624 * Back end support function for send_instant_message() and company
626 void add_xmsg_to_context(struct CitContext *ccptr, struct ExpressMessage *newmsg)
628 struct ExpressMessage *findend;
630 if (ccptr->FirstExpressMessage == NULL) {
631 ccptr->FirstExpressMessage = newmsg;
634 findend = ccptr->FirstExpressMessage;
635 while (findend->next != NULL) {
636 findend = findend->next;
638 findend->next = newmsg;
641 /* If the target context is a session which can handle asynchronous
642 * messages, go ahead and set the flag for that.
644 if (ccptr->is_async) {
645 ccptr->async_waiting = 1;
646 if (ccptr->state == CON_IDLE) {
647 ccptr->state = CON_READY;
656 * This is the back end to the instant message sending function.
657 * Returns the number of users to which the message was sent.
658 * Sending a zero-length message tests for recipients without sending messages.
660 int send_instant_message(char *lun, char *lem, char *x_user, char *x_msg)
662 int message_sent = 0; /* number of successful sends */
663 struct CitContext *ccptr;
664 struct ExpressMessage *newmsg = NULL;
667 int do_send = 0; /* 1 = send message; 0 = only check for valid recipient */
668 static int serial_number = 0; /* this keeps messages from getting logged twice */
670 if (strlen(x_msg) > 0) {
671 msglen = strlen(x_msg) + 4;
675 /* find the target user's context and append the message */
676 begin_critical_section(S_SESSION_TABLE);
678 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
680 if (ccptr->fake_username[0]) {
681 un = ccptr->fake_username;
684 un = ccptr->user.fullname;
687 if ( ((!strcasecmp(un, x_user))
688 || (!strcasecmp(x_user, "broadcast")))
689 && (ccptr->can_receive_im)
690 && ((ccptr->disable_exp == 0)
691 || (CC->user.axlevel >= 6)) ) {
693 newmsg = (struct ExpressMessage *) malloc(sizeof (struct ExpressMessage));
694 memset(newmsg, 0, sizeof (struct ExpressMessage));
695 time(&(newmsg->timestamp));
696 safestrncpy(newmsg->sender, lun, sizeof newmsg->sender);
697 safestrncpy(newmsg->sender_email, lem, sizeof newmsg->sender_email);
698 if (!strcasecmp(x_user, "broadcast")) {
699 newmsg->flags |= EM_BROADCAST;
701 newmsg->text = strdup(x_msg);
703 add_xmsg_to_context(ccptr, newmsg);
707 log_instant_message(CC, ccptr, newmsg->text, serial_number);
713 end_critical_section(S_SESSION_TABLE);
714 return (message_sent);
718 * send instant messages
720 void cmd_sexp(char *argbuf)
722 int message_sent = 0;
723 char x_user[USERNAME_SIZE];
727 char *x_big_msgbuf = NULL;
729 if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
730 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
733 if (CC->fake_username[0])
734 lun = CC->fake_username;
736 lun = CC->user.fullname;
738 lem = CC->cs_inet_email;
740 extract_token(x_user, argbuf, 0, '|', sizeof x_user);
741 extract_token(x_msg, argbuf, 1, '|', sizeof x_msg);
744 cprintf("%d You were not previously paged.\n", ERROR + NO_SUCH_USER);
747 if ((!strcasecmp(x_user, "broadcast")) && (CC->user.axlevel < 6)) {
748 cprintf("%d Higher access required to send a broadcast.\n",
749 ERROR + HIGHER_ACCESS_REQUIRED);
752 /* This loop handles text-transfer pages */
753 if (!strcmp(x_msg, "-")) {
754 message_sent = PerformXmsgHooks(lun, lem, x_user, "");
755 if (message_sent == 0) {
756 if (getuser(NULL, x_user))
757 cprintf("%d '%s' does not exist.\n",
758 ERROR + NO_SUCH_USER, x_user);
760 cprintf("%d '%s' is not logged in "
761 "or is not accepting pages.\n",
762 ERROR + RESOURCE_NOT_OPEN, x_user);
766 cprintf("%d Transmit message (will deliver to %d users)\n",
767 SEND_LISTING, message_sent);
768 x_big_msgbuf = malloc(SIZ);
769 memset(x_big_msgbuf, 0, SIZ);
770 while (client_getln(x_msg, sizeof x_msg) >= 0 && strcmp(x_msg, "000")) {
771 x_big_msgbuf = realloc(x_big_msgbuf,
772 strlen(x_big_msgbuf) + strlen(x_msg) + 4);
773 if (!IsEmptyStr(x_big_msgbuf))
774 if (x_big_msgbuf[strlen(x_big_msgbuf)] != '\n')
775 strcat(x_big_msgbuf, "\n");
776 strcat(x_big_msgbuf, x_msg);
778 PerformXmsgHooks(lun, lem, x_user, x_big_msgbuf);
781 /* This loop handles inline pages */
783 message_sent = PerformXmsgHooks(lun, lem, x_user, x_msg);
785 if (message_sent > 0) {
786 if (!IsEmptyStr(x_msg))
787 cprintf("%d Message sent", CIT_OK);
789 cprintf("%d Ok to send message", CIT_OK);
790 if (message_sent > 1)
791 cprintf(" to %d users", message_sent);
794 if (getuser(NULL, x_user))
795 cprintf("%d '%s' does not exist.\n",
796 ERROR + NO_SUCH_USER, x_user);
798 cprintf("%d '%s' is not logged in "
799 "or is not accepting pages.\n",
800 ERROR + RESOURCE_NOT_OPEN, x_user);
810 * Enter or exit paging-disabled mode
812 void cmd_dexp(char *argbuf)
816 if (CtdlAccessCheck(ac_logged_in)) return;
818 new_state = extract_int(argbuf, 0);
819 if ((new_state == 0) || (new_state == 1)) {
820 CC->disable_exp = new_state;
823 cprintf("%d %d\n", CIT_OK, CC->disable_exp);
828 * Request client termination
830 void cmd_reqt(char *argbuf) {
831 struct CitContext *ccptr;
834 struct ExpressMessage *newmsg;
836 if (CtdlAccessCheck(ac_aide)) return;
837 which_session = extract_int(argbuf, 0);
839 begin_critical_section(S_SESSION_TABLE);
840 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
841 if ((ccptr->cs_pid == which_session) || (which_session == 0)) {
843 newmsg = (struct ExpressMessage *)
844 malloc(sizeof (struct ExpressMessage));
846 sizeof (struct ExpressMessage));
847 time(&(newmsg->timestamp));
848 safestrncpy(newmsg->sender, CC->user.fullname,
849 sizeof newmsg->sender);
850 newmsg->flags |= EM_GO_AWAY;
851 newmsg->text = strdup("Automatic logoff requested.");
853 add_xmsg_to_context(ccptr, newmsg);
858 end_critical_section(S_SESSION_TABLE);
859 cprintf("%d Sent termination request to %d sessions.\n", CIT_OK, sessions);
864 * This is the back end for flush_conversations_to_disk()
865 * At this point we've isolated a single conversation (struct imlog)
866 * and are ready to write it to disk.
868 void flush_individual_conversation(struct imlog *im) {
869 struct CtdlMessage *msg;
871 char roomname[ROOMNAMELEN];
873 StrBufAppendBufPlain(im->conversation, HKEY(
879 msg = malloc(sizeof(struct CtdlMessage));
880 memset(msg, 0, sizeof(struct CtdlMessage));
881 msg->cm_magic = CTDLMESSAGE_MAGIC;
882 msg->cm_anon_type = MES_NORMAL;
883 msg->cm_format_type = FMT_RFC822;
884 if (!IsEmptyStr(im->usernames[0])) {
885 msg->cm_fields['A'] = strdup(im->usernames[0]);
887 msg->cm_fields['A'] = strdup("Citadel");
889 if (!IsEmptyStr(im->usernames[1])) {
890 msg->cm_fields['R'] = strdup(im->usernames[1]);
892 msg->cm_fields['O'] = strdup(PAGELOGROOM);
893 msg->cm_fields['N'] = strdup(NODENAME);
894 msg->cm_fields['M'] = SmashStrBuf(&im->conversation); /* we own this memory now */
896 /* Start with usernums[1] because it's guaranteed to be higher than usernums[0],
897 * so if there's only one party, usernums[0] will be zero but usernums[1] won't.
898 * Create the room if necessary. Note that we create as a type 5 room rather
899 * than 4, which indicates that it's a personal room but we've already supplied
900 * the namespace prefix.
902 * In the unlikely event that usernums[1] is zero, a room with an invalid namespace
903 * prefix will be created. That's ok because the auto-purger will clean it up later.
905 snprintf(roomname, sizeof roomname, "%010ld.%s", im->usernums[1], PAGELOGROOM);
906 create_room(roomname, 5, "", 0, 1, 1, VIEW_BBS);
907 msgnum = CtdlSubmitMsg(msg, NULL, roomname, 0);
908 CtdlFreeMessage(msg);
910 /* If there is a valid user number in usernums[0], save a copy for them too. */
911 if (im->usernums[0] > 0) {
912 snprintf(roomname, sizeof roomname, "%010ld.%s", im->usernums[0], PAGELOGROOM);
913 create_room(roomname, 5, "", 0, 1, 1, VIEW_BBS);
914 CtdlSaveMsgPointerInRoom(roomname, msgnum, 0, NULL);
917 /* Finally, if we're logging instant messages globally, do that now. */
918 if (!IsEmptyStr(config.c_logpages)) {
919 create_room(config.c_logpages, 3, "", 0, 1, 1, VIEW_BBS);
920 CtdlSaveMsgPointerInRoom(config.c_logpages, msgnum, 0, NULL);
926 * Locate instant message conversations which have gone idle
927 * (or, if the server is shutting down, locate *all* conversations)
928 * and flush them to disk (in the participants' log rooms, etc.)
930 void flush_conversations_to_disk(time_t if_older_than) {
932 struct imlog *flush_these = NULL;
933 struct imlog *dont_flush_these = NULL;
934 struct imlog *imptr = NULL;
936 begin_critical_section(S_IM_LOGS);
940 imlist = imlist->next;
941 if ((time(NULL) - imptr->lastmsg) > if_older_than)
943 /* This conversation qualifies. Move it to the list of ones to flush. */
944 imptr->next = flush_these;
948 /* Move it to the list of ones not to flush. */
949 imptr->next = dont_flush_these;
950 dont_flush_these = imptr;
953 imlist = dont_flush_these;
954 end_critical_section(S_IM_LOGS);
956 /* We are now outside of the critical section, and we are the only thread holding a
957 * pointer to a linked list of conversations to be flushed to disk.
959 while (flush_these) {
961 flush_individual_conversation(flush_these); /* This will free the string buffer */
963 flush_these = flush_these->next;
970 void chat_timer(void) {
971 flush_conversations_to_disk(300); /* Anything that hasn't peeped in more than 5 minutes */
974 void chat_shutdown(void) {
975 flush_conversations_to_disk(0); /* Get it ALL onto disk NOW. */
978 CTDL_MODULE_INIT(chat)
982 CtdlRegisterProtoHook(cmd_chat, "CHAT", "Begin real-time chat");
983 CtdlRegisterProtoHook(cmd_pexp, "PEXP", "Poll for instant messages");
984 CtdlRegisterProtoHook(cmd_gexp, "GEXP", "Get instant messages");
985 CtdlRegisterProtoHook(cmd_sexp, "SEXP", "Send an instant message");
986 CtdlRegisterProtoHook(cmd_dexp, "DEXP", "Disable instant messages");
987 CtdlRegisterProtoHook(cmd_reqt, "REQT", "Request client termination");
988 CtdlRegisterSessionHook(cmd_gexp_async, EVT_ASYNC);
989 CtdlRegisterSessionHook(delete_instant_messages, EVT_STOP);
990 CtdlRegisterXmsgHook(send_instant_message, XMSG_PRI_LOCAL);
991 CtdlRegisterSessionHook(chat_timer, EVT_TIMER);
992 CtdlRegisterSessionHook(chat_shutdown, EVT_SHUTDOWN);
995 /* return our Subversion id for the Log */