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"
62 #include "ctdl_module.h"
64 struct ChatLine *ChatQueue = NULL;
70 char usernames[2][128];
76 struct imlog *imlist = NULL;
79 * This function handles the logging of instant messages to disk.
81 void log_instant_message(struct CitContext *me, struct CitContext *them, char *msgtext, int serial_number)
85 struct imlog *iptr = NULL;
86 struct imlog *this_im = NULL;
88 memset(usernums, 0, sizeof usernums);
89 usernums[0] = me->user.usernum;
90 usernums[1] = them->user.usernum;
92 /* Always put the lower user number first, so we can use the array as a hash value which
93 * represents a pair of users. For a broadcast message one of the users will be 0.
95 if (usernums[0] > usernums[1]) {
97 usernums[0] = usernums[1];
101 begin_critical_section(S_IM_LOGS);
103 /* Look for an existing conversation in the hash table.
104 * If not found, create a new one.
108 for (iptr = imlist; iptr != NULL; iptr = iptr->next) {
109 if ((iptr->usernums[0] == usernums[0]) && (iptr->usernums[1] == usernums[1])) {
110 /* Existing conversation */
114 if (this_im == NULL) {
115 /* New conversation */
116 this_im = malloc(sizeof(struct imlog));
117 memset(this_im, 0, sizeof (struct imlog));
118 this_im->usernums[0] = usernums[0];
119 this_im->usernums[1] = usernums[1];
120 /* usernames[] and usernums[] might not be in the same order. This is not an error. */
122 safestrncpy(this_im->usernames[0], me->user.fullname, sizeof this_im->usernames[0]);
125 safestrncpy(this_im->usernames[1], them->user.fullname, sizeof this_im->usernames[1]);
127 this_im->conversation = NewStrBuf();
128 this_im->next = imlist;
130 StrBufAppendBufPlain(this_im->conversation, HKEY(
131 "Content-type: text/html\r\n"
132 "Content-transfer-encoding: 7bit\r\n"
139 /* Since it's possible for this function to get called more than once if a user is logged
140 * in on multiple sessions, we use the message's serial number to keep track of whether
141 * we've already logged it.
143 if (this_im->last_serial != serial_number)
145 this_im->lastmsg = time(NULL); /* Touch the timestamp so we know when to flush */
146 this_im->last_serial = serial_number;
147 StrBufAppendBufPlain(this_im->conversation, HKEY("<p><b>"), 0);
148 StrBufAppendBufPlain(this_im->conversation, me->user.fullname, -1, 0);
149 StrBufAppendBufPlain(this_im->conversation, HKEY(":</b> "), 0);
150 StrEscAppend(this_im->conversation, NULL, msgtext, 0, 0);
151 StrBufAppendBufPlain(this_im->conversation, HKEY("</p>\r\n"), 0);
153 end_critical_section(S_IM_LOGS);
157 * This message can be set to anything you want, but it is
158 * checked for consistency so don't move it away from here.
160 #define KICKEDMSG "You have been kicked out of this room."
162 void allwrite(char *cmdbuf, int flag, char *username)
167 struct ChatLine *clptr, *clnew;
170 if (CC->fake_username[0])
171 un = CC->fake_username;
173 un = CC->user.fullname;
175 snprintf(bcast, sizeof bcast, ":|<%s %s>", un, cmdbuf);
176 } else if (flag == 0) {
177 snprintf(bcast, sizeof bcast, "%s|%s", un, cmdbuf);
178 } else if (flag == 2) {
179 snprintf(bcast, sizeof bcast, ":|<%s whispers %s>", un, cmdbuf);
180 } else if (flag == 3) {
181 snprintf(bcast, sizeof bcast, ":|%s", KICKEDMSG);
183 if ((strcasecmp(cmdbuf, "NOOP")) && (flag != 2)) {
184 fp = fopen(CHATLOG, "a");
186 fprintf(fp, "%s\n", bcast);
189 clnew = (struct ChatLine *) malloc(sizeof(struct ChatLine));
190 memset(clnew, 0, sizeof(struct ChatLine));
192 fprintf(stderr, "citserver: cannot alloc chat line: %s\n",
198 clnew->chat_time = now;
199 safestrncpy(clnew->chat_room, CC->room.QRname,
200 sizeof clnew->chat_room);
201 clnew->chat_room[sizeof clnew->chat_room - 1] = 0;
203 safestrncpy(clnew->chat_username, username,
204 sizeof clnew->chat_username);
205 clnew->chat_username[sizeof clnew->chat_username - 1] = 0;
207 clnew->chat_username[0] = '\0';
208 safestrncpy(clnew->chat_text, bcast, sizeof clnew->chat_text);
210 /* Here's the critical section.
211 * First, add the new message to the queue...
213 begin_critical_section(S_CHATQUEUE);
215 clnew->chat_seq = ChatLastMsg;
216 if (ChatQueue == NULL) {
219 for (clptr = ChatQueue; clptr->next != NULL; clptr = clptr->next);;
223 /* Then, before releasing the lock, free the expired messages */
224 while ((ChatQueue != NULL) && (now - ChatQueue->chat_time >= 120L)) {
226 ChatQueue = ChatQueue->next;
229 end_critical_section(S_CHATQUEUE);
233 t_context *find_context(char **unstr)
235 t_context *t_cc, *found_cc = NULL;
238 if ((!*unstr) || (!unstr))
241 begin_critical_section(S_SESSION_TABLE);
242 for (t_cc = ContextList; ((t_cc) && (!found_cc)); t_cc = t_cc->next) {
243 if (t_cc->fake_username[0])
244 name = t_cc->fake_username;
246 name = t_cc->curr_user;
248 if ((!strncasecmp(name, tptr, strlen(name))) && (tptr[strlen(name)] == ' ')) {
250 *unstr = &(tptr[strlen(name) + 1]);
253 end_critical_section(S_SESSION_TABLE);
259 * List users in chat.
260 * allflag == 0 = list users in chat
261 * 1 = list users in chat, followed by users not in chat
262 * 2 = display count only
265 void do_chat_listing(int allflag)
267 struct CitContext *ccptr;
269 int count_elsewhere = 0;
270 char roomname[ROOMNAMELEN];
272 if ((allflag == 0) || (allflag == 1))
273 cprintf(":|\n:| Users currently in chat:\n");
274 begin_critical_section(S_SESSION_TABLE);
275 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
276 if (ccptr->cs_flags & CS_CHAT) {
277 if (!strcasecmp(ccptr->room.QRname,
286 GenerateRoomDisplay(roomname, ccptr, CC);
287 if ((CC->user.axlevel < 6) && (!IsEmptyStr(ccptr->fake_roomname))) {
288 strcpy(roomname, ccptr->fake_roomname);
291 if ((ccptr->cs_flags & CS_CHAT) && ((ccptr->cs_flags & CS_STEALTH) == 0)) {
292 if ((allflag == 0) || (allflag == 1)) {
293 cprintf(":| %-25s <%s>:\n",
294 (ccptr->fake_username[0]) ? ccptr->fake_username : ccptr->curr_user,
301 cprintf(":|\n:| Users not in chat:\n");
302 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
304 GenerateRoomDisplay(roomname, ccptr, CC);
305 if ((CC->user.axlevel < 6)
306 && (!IsEmptyStr(ccptr->fake_roomname))) {
307 strcpy(roomname, ccptr->fake_roomname);
310 if (((ccptr->cs_flags & CS_CHAT) == 0)
311 && ((ccptr->cs_flags & CS_STEALTH) == 0)) {
312 cprintf(":| %-25s <%s>:\n",
313 (ccptr->fake_username[0]) ? ccptr->fake_username : ccptr->curr_user,
318 end_critical_section(S_SESSION_TABLE);
322 cprintf(":|There are %d users here.\n", count);
325 cprintf(":|Note: you are the only one here.\n");
327 if (count_elsewhere > 0) {
328 cprintf(":|There are %d users chatting in other rooms.\n", count_elsewhere);
336 void cmd_chat(char *argbuf)
341 int MyLastMsg, ThisLastMsg;
342 struct ChatLine *clptr;
343 struct CitContext *t_context;
346 if (!(CC->logged_in)) {
347 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
351 CC->cs_flags = CC->cs_flags | CS_CHAT;
352 cprintf("%d Entering chat mode (type '/help' for available commands)\n",
356 MyLastMsg = ChatLastMsg;
358 if ((CC->cs_flags & CS_STEALTH) == 0) {
359 allwrite("<entering chat>", 0, NULL);
370 linelen = strlen(cmdbuf);
371 if (linelen > 100) --linelen; /* truncate too-long lines */
372 cmdbuf[linelen + 1] = 0;
374 retval = client_read_to(&cmdbuf[linelen], 1, 2);
376 if (retval < 0 || CC->kill_me) { /* socket broken? */
377 if ((CC->cs_flags & CS_STEALTH) == 0) {
378 allwrite("<disconnected>", 0, NULL);
383 /* if we have a complete line, do send processing */
384 if (!IsEmptyStr(cmdbuf))
385 if (cmdbuf[strlen(cmdbuf) - 1] == 10) {
386 cmdbuf[strlen(cmdbuf) - 1] = 0;
390 if ((!strcasecmp(cmdbuf, "exit"))
391 || (!strcasecmp(cmdbuf, "/exit"))
392 || (!strcasecmp(cmdbuf, "quit"))
393 || (!strcasecmp(cmdbuf, "logout"))
394 || (!strcasecmp(cmdbuf, "logoff"))
395 || (!strcasecmp(cmdbuf, "/q"))
396 || (!strcasecmp(cmdbuf, ".q"))
397 || (!strcasecmp(cmdbuf, "/quit"))
399 strcpy(cmdbuf, "000");
401 if (!strcmp(cmdbuf, "000")) {
402 if ((CC->cs_flags & CS_STEALTH) == 0) {
403 allwrite("<exiting chat>", 0, NULL);
407 CC->cs_flags = CC->cs_flags - CS_CHAT;
410 if ((!strcasecmp(cmdbuf, "/help"))
411 || (!strcasecmp(cmdbuf, "help"))
412 || (!strcasecmp(cmdbuf, "/?"))
413 || (!strcasecmp(cmdbuf, "?"))) {
415 cprintf(":|Available commands: \n");
416 cprintf(":|/help (prints this message) \n");
417 cprintf(":|/who (list users currently in chat) \n");
418 cprintf(":|/whobbs (list users in chat -and- elsewhere) \n");
419 cprintf(":|/me ('action' line, ala irc) \n");
420 cprintf(":|/msg (send private message, ala irc) \n");
421 if (is_room_aide()) {
422 cprintf(":|/kick (kick another user out of this room) \n");
424 cprintf(":|/quit (exit from this chat) \n");
428 if (!strcasecmp(cmdbuf, "/who")) {
432 if (!strcasecmp(cmdbuf, "/whobbs")) {
436 if (!strncasecmp(cmdbuf, "/me ", 4)) {
437 allwrite(&cmdbuf[4], 1, NULL);
440 if (!strncasecmp(cmdbuf, "/msg ", 5)) {
442 strptr1 = &cmdbuf[5];
443 if ((t_context = find_context(&strptr1))) {
444 allwrite(strptr1, 2, CC->curr_user);
445 if (strcasecmp(CC->curr_user, t_context->curr_user))
446 allwrite(strptr1, 2, t_context->curr_user);
448 cprintf(":|User not found.\n");
451 /* The /kick function is implemented by sending a specific
452 * message to the kicked-out user's context. When that message
453 * is processed by the read loop, that context will exit.
455 if ( (!strncasecmp(cmdbuf, "/kick ", 6)) && (is_room_aide()) ) {
457 strptr1 = &cmdbuf[6];
458 strcat(strptr1, " ");
459 if ((t_context = find_context(&strptr1))) {
460 if (strcasecmp(CC->curr_user, t_context->curr_user))
461 allwrite(strptr1, 3, t_context->curr_user);
463 cprintf(":|User not found.\n");
466 if ((cmdbuf[0] != '/') && (strlen(cmdbuf) > 0)) {
468 allwrite(cmdbuf, 0, NULL);
470 if ((!ok_cmd) && (cmdbuf[0]) && (cmdbuf[0] != '\n'))
471 cprintf(":|Command %s is not understood.\n", cmdbuf);
476 /* now check the queue for new incoming stuff */
478 if (CC->fake_username[0])
479 un = CC->fake_username;
482 if (ChatLastMsg > MyLastMsg) {
483 ThisLastMsg = ChatLastMsg;
484 for (clptr = ChatQueue; clptr != NULL; clptr = clptr->next) {
485 if ((clptr->chat_seq > MyLastMsg) && ((!clptr->chat_username[0]) || (!strncasecmp(un, clptr->chat_username, 32)))) {
486 if ((!clptr->chat_room[0]) || (!strncasecmp(CC->room.QRname, clptr->chat_room, ROOMNAMELEN))) {
487 /* Output new chat data */
488 cprintf("%s\n", clptr->chat_text);
490 /* See if we've been force-quitted (kicked etc.) */
491 if (!strcmp(&clptr->chat_text[2], KICKEDMSG)) {
492 allwrite("<kicked out of this room>", 0, NULL);
494 CC->cs_flags = CC->cs_flags - CS_CHAT;
496 /* Kick user out of room */
497 CtdlInvtKick(CC->user.fullname, 0);
499 /* And return to the Lobby */
500 CtdlUserGoto(config.c_baseroom, 0, 0, NULL, NULL);
506 MyLastMsg = ThisLastMsg;
514 * Delete any remaining instant messages
516 void delete_instant_messages(void) {
517 struct ExpressMessage *ptr;
519 begin_critical_section(S_SESSION_TABLE);
520 while (CC->FirstExpressMessage != NULL) {
521 ptr = CC->FirstExpressMessage->next;
522 if (CC->FirstExpressMessage->text != NULL)
523 free(CC->FirstExpressMessage->text);
524 free(CC->FirstExpressMessage);
525 CC->FirstExpressMessage = ptr;
527 end_critical_section(S_SESSION_TABLE);
534 * Poll for instant messages (OLD METHOD -- ***DEPRECATED ***)
536 void cmd_pexp(char *argbuf)
538 struct ExpressMessage *ptr, *holdptr;
540 if (CC->FirstExpressMessage == NULL) {
541 cprintf("%d No instant messages waiting.\n", ERROR + MESSAGE_NOT_FOUND);
544 begin_critical_section(S_SESSION_TABLE);
545 ptr = CC->FirstExpressMessage;
546 CC->FirstExpressMessage = NULL;
547 end_critical_section(S_SESSION_TABLE);
549 cprintf("%d Express msgs:\n", LISTING_FOLLOWS);
550 while (ptr != NULL) {
551 if (ptr->flags && EM_BROADCAST)
552 cprintf("Broadcast message ");
553 else if (ptr->flags && EM_CHAT)
554 cprintf("Chat request ");
555 else if (ptr->flags && EM_GO_AWAY)
556 cprintf("Please logoff now, as requested ");
559 cprintf("from %s:\n", ptr->sender);
560 if (ptr->text != NULL)
561 memfmout(ptr->text, 0, "\n");
564 if (ptr->text != NULL) free(ptr->text);
573 * Get instant messages (new method)
575 void cmd_gexp(char *argbuf) {
576 struct ExpressMessage *ptr;
578 if (CC->FirstExpressMessage == NULL) {
579 cprintf("%d No instant messages waiting.\n", ERROR + MESSAGE_NOT_FOUND);
583 begin_critical_section(S_SESSION_TABLE);
584 ptr = CC->FirstExpressMessage;
585 CC->FirstExpressMessage = CC->FirstExpressMessage->next;
586 end_critical_section(S_SESSION_TABLE);
588 cprintf("%d %d|%ld|%d|%s|%s|%s\n",
590 ((ptr->next != NULL) ? 1 : 0), /* more msgs? */
591 (long)ptr->timestamp, /* time sent */
592 ptr->flags, /* flags */
593 ptr->sender, /* sender of msg */
594 config.c_nodename, /* static for now (and possibly deprecated) */
595 ptr->sender_email /* email or jid of sender */
598 if (ptr->text != NULL) {
599 memfmout(ptr->text, 0, "\n");
600 if (ptr->text[strlen(ptr->text)-1] != '\n') cprintf("\n");
609 * Asynchronously deliver instant messages
611 void cmd_gexp_async(void) {
613 /* Only do this if the session can handle asynchronous protocol */
614 if (CC->is_async == 0) return;
616 /* And don't do it if there's nothing to send. */
617 if (CC->FirstExpressMessage == NULL) return;
619 cprintf("%d instant msg\n", ASYNC_MSG + ASYNC_GEXP);
623 * Back end support function for send_instant_message() and company
625 void add_xmsg_to_context(struct CitContext *ccptr, struct ExpressMessage *newmsg)
627 struct ExpressMessage *findend;
629 if (ccptr->FirstExpressMessage == NULL) {
630 ccptr->FirstExpressMessage = newmsg;
633 findend = ccptr->FirstExpressMessage;
634 while (findend->next != NULL) {
635 findend = findend->next;
637 findend->next = newmsg;
640 /* If the target context is a session which can handle asynchronous
641 * messages, go ahead and set the flag for that.
643 if (ccptr->is_async) {
644 ccptr->async_waiting = 1;
645 if (ccptr->state == CON_IDLE) {
646 ccptr->state = CON_READY;
655 * This is the back end to the instant message sending function.
656 * Returns the number of users to which the message was sent.
657 * Sending a zero-length message tests for recipients without sending messages.
659 int send_instant_message(char *lun, char *lem, char *x_user, char *x_msg)
661 int message_sent = 0; /* number of successful sends */
662 struct CitContext *ccptr;
663 struct ExpressMessage *newmsg = NULL;
666 int do_send = 0; /* 1 = send message; 0 = only check for valid recipient */
667 static int serial_number = 0; /* this keeps messages from getting logged twice */
669 if (strlen(x_msg) > 0) {
670 msglen = strlen(x_msg) + 4;
674 /* find the target user's context and append the message */
675 begin_critical_section(S_SESSION_TABLE);
677 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
679 if (ccptr->fake_username[0]) {
680 un = ccptr->fake_username;
683 un = ccptr->user.fullname;
686 if ( ((!strcasecmp(un, x_user))
687 || (!strcasecmp(x_user, "broadcast")))
688 && (ccptr->can_receive_im)
689 && ((ccptr->disable_exp == 0)
690 || (CC->user.axlevel >= 6)) ) {
692 newmsg = (struct ExpressMessage *) malloc(sizeof (struct ExpressMessage));
693 memset(newmsg, 0, sizeof (struct ExpressMessage));
694 time(&(newmsg->timestamp));
695 safestrncpy(newmsg->sender, lun, sizeof newmsg->sender);
696 safestrncpy(newmsg->sender_email, lem, sizeof newmsg->sender_email);
697 if (!strcasecmp(x_user, "broadcast")) {
698 newmsg->flags |= EM_BROADCAST;
700 newmsg->text = strdup(x_msg);
702 add_xmsg_to_context(ccptr, newmsg);
706 log_instant_message(CC, ccptr, newmsg->text, serial_number);
712 end_critical_section(S_SESSION_TABLE);
713 return (message_sent);
717 * send instant messages
719 void cmd_sexp(char *argbuf)
721 int message_sent = 0;
722 char x_user[USERNAME_SIZE];
726 char *x_big_msgbuf = NULL;
728 if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
729 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
732 if (CC->fake_username[0])
733 lun = CC->fake_username;
735 lun = CC->user.fullname;
737 lem = CC->cs_inet_email;
739 extract_token(x_user, argbuf, 0, '|', sizeof x_user);
740 extract_token(x_msg, argbuf, 1, '|', sizeof x_msg);
743 cprintf("%d You were not previously paged.\n", ERROR + NO_SUCH_USER);
746 if ((!strcasecmp(x_user, "broadcast")) && (CC->user.axlevel < 6)) {
747 cprintf("%d Higher access required to send a broadcast.\n",
748 ERROR + HIGHER_ACCESS_REQUIRED);
751 /* This loop handles text-transfer pages */
752 if (!strcmp(x_msg, "-")) {
753 message_sent = PerformXmsgHooks(lun, lem, x_user, "");
754 if (message_sent == 0) {
755 if (CtdlGetUser(NULL, x_user))
756 cprintf("%d '%s' does not exist.\n",
757 ERROR + NO_SUCH_USER, x_user);
759 cprintf("%d '%s' is not logged in "
760 "or is not accepting pages.\n",
761 ERROR + RESOURCE_NOT_OPEN, x_user);
765 cprintf("%d Transmit message (will deliver to %d users)\n",
766 SEND_LISTING, message_sent);
767 x_big_msgbuf = malloc(SIZ);
768 memset(x_big_msgbuf, 0, SIZ);
769 while (client_getln(x_msg, sizeof x_msg) >= 0 && strcmp(x_msg, "000")) {
770 x_big_msgbuf = realloc(x_big_msgbuf,
771 strlen(x_big_msgbuf) + strlen(x_msg) + 4);
772 if (!IsEmptyStr(x_big_msgbuf))
773 if (x_big_msgbuf[strlen(x_big_msgbuf)] != '\n')
774 strcat(x_big_msgbuf, "\n");
775 strcat(x_big_msgbuf, x_msg);
777 PerformXmsgHooks(lun, lem, x_user, x_big_msgbuf);
780 /* This loop handles inline pages */
782 message_sent = PerformXmsgHooks(lun, lem, x_user, x_msg);
784 if (message_sent > 0) {
785 if (!IsEmptyStr(x_msg))
786 cprintf("%d Message sent", CIT_OK);
788 cprintf("%d Ok to send message", CIT_OK);
789 if (message_sent > 1)
790 cprintf(" to %d users", message_sent);
793 if (CtdlGetUser(NULL, x_user))
794 cprintf("%d '%s' does not exist.\n",
795 ERROR + NO_SUCH_USER, x_user);
797 cprintf("%d '%s' is not logged in "
798 "or is not accepting pages.\n",
799 ERROR + RESOURCE_NOT_OPEN, x_user);
809 * Enter or exit paging-disabled mode
811 void cmd_dexp(char *argbuf)
815 if (CtdlAccessCheck(ac_logged_in)) return;
817 new_state = extract_int(argbuf, 0);
818 if ((new_state == 0) || (new_state == 1)) {
819 CC->disable_exp = new_state;
822 cprintf("%d %d\n", CIT_OK, CC->disable_exp);
827 * Request client termination
829 void cmd_reqt(char *argbuf) {
830 struct CitContext *ccptr;
833 struct ExpressMessage *newmsg;
835 if (CtdlAccessCheck(ac_aide)) return;
836 which_session = extract_int(argbuf, 0);
838 begin_critical_section(S_SESSION_TABLE);
839 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
840 if ((ccptr->cs_pid == which_session) || (which_session == 0)) {
842 newmsg = (struct ExpressMessage *)
843 malloc(sizeof (struct ExpressMessage));
845 sizeof (struct ExpressMessage));
846 time(&(newmsg->timestamp));
847 safestrncpy(newmsg->sender, CC->user.fullname,
848 sizeof newmsg->sender);
849 newmsg->flags |= EM_GO_AWAY;
850 newmsg->text = strdup("Automatic logoff requested.");
852 add_xmsg_to_context(ccptr, newmsg);
857 end_critical_section(S_SESSION_TABLE);
858 cprintf("%d Sent termination request to %d sessions.\n", CIT_OK, sessions);
863 * This is the back end for flush_conversations_to_disk()
864 * At this point we've isolated a single conversation (struct imlog)
865 * and are ready to write it to disk.
867 void flush_individual_conversation(struct imlog *im) {
868 struct CtdlMessage *msg;
870 char roomname[ROOMNAMELEN];
872 StrBufAppendBufPlain(im->conversation, HKEY(
878 msg = malloc(sizeof(struct CtdlMessage));
879 memset(msg, 0, sizeof(struct CtdlMessage));
880 msg->cm_magic = CTDLMESSAGE_MAGIC;
881 msg->cm_anon_type = MES_NORMAL;
882 msg->cm_format_type = FMT_RFC822;
883 if (!IsEmptyStr(im->usernames[0])) {
884 msg->cm_fields['A'] = strdup(im->usernames[0]);
886 msg->cm_fields['A'] = strdup("Citadel");
888 if (!IsEmptyStr(im->usernames[1])) {
889 msg->cm_fields['R'] = strdup(im->usernames[1]);
891 msg->cm_fields['O'] = strdup(PAGELOGROOM);
892 msg->cm_fields['N'] = strdup(NODENAME);
893 msg->cm_fields['M'] = SmashStrBuf(&im->conversation); /* we own this memory now */
895 /* Start with usernums[1] because it's guaranteed to be higher than usernums[0],
896 * so if there's only one party, usernums[0] will be zero but usernums[1] won't.
897 * Create the room if necessary. Note that we create as a type 5 room rather
898 * than 4, which indicates that it's a personal room but we've already supplied
899 * the namespace prefix.
901 * In the unlikely event that usernums[1] is zero, a room with an invalid namespace
902 * prefix will be created. That's ok because the auto-purger will clean it up later.
904 snprintf(roomname, sizeof roomname, "%010ld.%s", im->usernums[1], PAGELOGROOM);
905 CtdlCreateRoom(roomname, 5, "", 0, 1, 1, VIEW_BBS);
906 msgnum = CtdlSubmitMsg(msg, NULL, roomname, 0);
907 CtdlFreeMessage(msg);
909 /* If there is a valid user number in usernums[0], save a copy for them too. */
910 if (im->usernums[0] > 0) {
911 snprintf(roomname, sizeof roomname, "%010ld.%s", im->usernums[0], PAGELOGROOM);
912 CtdlCreateRoom(roomname, 5, "", 0, 1, 1, VIEW_BBS);
913 CtdlSaveMsgPointerInRoom(roomname, msgnum, 0, NULL);
916 /* Finally, if we're logging instant messages globally, do that now. */
917 if (!IsEmptyStr(config.c_logpages)) {
918 CtdlCreateRoom(config.c_logpages, 3, "", 0, 1, 1, VIEW_BBS);
919 CtdlSaveMsgPointerInRoom(config.c_logpages, msgnum, 0, NULL);
925 * Locate instant message conversations which have gone idle
926 * (or, if the server is shutting down, locate *all* conversations)
927 * and flush them to disk (in the participants' log rooms, etc.)
929 void flush_conversations_to_disk(time_t if_older_than) {
931 struct imlog *flush_these = NULL;
932 struct imlog *dont_flush_these = NULL;
933 struct imlog *imptr = NULL;
935 begin_critical_section(S_IM_LOGS);
939 imlist = imlist->next;
940 if ((time(NULL) - imptr->lastmsg) > if_older_than)
942 /* This conversation qualifies. Move it to the list of ones to flush. */
943 imptr->next = flush_these;
947 /* Move it to the list of ones not to flush. */
948 imptr->next = dont_flush_these;
949 dont_flush_these = imptr;
952 imlist = dont_flush_these;
953 end_critical_section(S_IM_LOGS);
955 /* We are now outside of the critical section, and we are the only thread holding a
956 * pointer to a linked list of conversations to be flushed to disk.
958 while (flush_these) {
960 flush_individual_conversation(flush_these); /* This will free the string buffer */
962 flush_these = flush_these->next;
969 void chat_timer(void) {
970 flush_conversations_to_disk(300); /* Anything that hasn't peeped in more than 5 minutes */
973 void chat_shutdown(void) {
974 flush_conversations_to_disk(0); /* Get it ALL onto disk NOW. */
977 CTDL_MODULE_INIT(chat)
981 CtdlRegisterProtoHook(cmd_chat, "CHAT", "Begin real-time chat");
982 CtdlRegisterProtoHook(cmd_pexp, "PEXP", "Poll for instant messages");
983 CtdlRegisterProtoHook(cmd_gexp, "GEXP", "Get instant messages");
984 CtdlRegisterProtoHook(cmd_sexp, "SEXP", "Send an instant message");
985 CtdlRegisterProtoHook(cmd_dexp, "DEXP", "Disable instant messages");
986 CtdlRegisterProtoHook(cmd_reqt, "REQT", "Request client termination");
987 CtdlRegisterSessionHook(cmd_gexp_async, EVT_ASYNC);
988 CtdlRegisterSessionHook(delete_instant_messages, EVT_STOP);
989 CtdlRegisterXmsgHook(send_instant_message, XMSG_PRI_LOCAL);
990 CtdlRegisterSessionHook(chat_timer, EVT_TIMER);
991 CtdlRegisterSessionHook(chat_shutdown, EVT_SHUTDOWN);
994 /* return our Subversion id for the Log */