]> code.citadel.org Git - citadel.git/blobdiff - citadel/serv_chat.c
* Removed all of the thread cancellation cruft that is no longer necessary
[citadel.git] / citadel / serv_chat.c
index 539bdc0ec5e853e2c8be51744f59df50f9b03133..88d343e0b8520e843d78bb6deca065cef28e9212 100644 (file)
@@ -6,6 +6,7 @@
  *
  * $Id$
  */
+#include "sysdep.h"
 #include <stdlib.h>
 #include <unistd.h>
 #include <stdio.h>
 #include <sys/wait.h>
 #include <string.h>
 #include <limits.h>
-#include <pthread.h>
 #include "citadel.h"
 #include "server.h"
 #include <syslog.h>
-#ifdef HAVE_SYS_SELECT_H
-#include <sys/select.h>
-#endif
 #include "serv_chat.h"
 #include "sysdep_decls.h"
 #include "citserver.h"
@@ -39,29 +36,17 @@ int ChatLastMsg = 0;
 
 extern struct CitContext *ContextList;
 
-#define MODULE_NAME    "Chat module"
-#define MODULE_AUTHOR  "Art Cancro"
-#define MODULE_EMAIL   "ajc@uncnsrd.mt-kisco.ny.us"
-#define MAJOR_VERSION  2
-#define MINOR_VERSION  0
 
-static struct DLModule_Info info =
-{
-       MODULE_NAME,
-       MODULE_AUTHOR,
-       MODULE_EMAIL,
-       MAJOR_VERSION,
-       MINOR_VERSION
-};
-
-struct DLModule_Info *Dynamic_Module_Init(void)
+
+char *Dynamic_Module_Init(void)
 {
        CtdlRegisterProtoHook(cmd_chat, "CHAT", "Begin real-time chat");
        CtdlRegisterProtoHook(cmd_pexp, "PEXP", "Poll for express messages");
        CtdlRegisterProtoHook(cmd_gexp, "GEXP", "Get express messages");
        CtdlRegisterProtoHook(cmd_sexp, "SEXP", "Send an express message");
        CtdlRegisterSessionHook(delete_express_messages, EVT_STOP);
-       return &info;
+       CtdlRegisterXmsgHook(send_express_message, XMSG_PRI_LOCAL);
+       return "$Id$";
 }
 
 void allwrite(char *cmdbuf, int flag, char *roomname, char *username)
@@ -77,11 +62,11 @@ void allwrite(char *cmdbuf, int flag, char *roomname, char *username)
        else
                un = CC->usersupp.fullname;
        if (flag == 1) {
-               sprintf(bcast, ":|<%s %s>", un, cmdbuf);
+               snprintf(bcast, sizeof bcast, ":|<%s %s>", un, cmdbuf);
        } else if (flag == 0) {
-               sprintf(bcast, "%s|%s", un, cmdbuf);
+               snprintf(bcast, sizeof bcast, "%s|%s", un, cmdbuf);
        } else if (flag == 2) {
-               sprintf(bcast, ":|<%s whispers %s>", un, cmdbuf);
+               snprintf(bcast, sizeof bcast, ":|<%s whispers %s>", un, cmdbuf);
        }
        if ((strcasecmp(cmdbuf, "NOOP")) && (flag != 2)) {
                fp = fopen(CHATLOG, "a");
@@ -98,15 +83,15 @@ void allwrite(char *cmdbuf, int flag, char *roomname, char *username)
        time(&now);
        clnew->next = NULL;
        clnew->chat_time = now;
-       strncpy(clnew->chat_room, roomname, sizeof clnew->chat_room);
+       safestrncpy(clnew->chat_room, roomname, sizeof clnew->chat_room);
        clnew->chat_room[sizeof clnew->chat_room - 1] = 0;
        if (username) {
-               strncpy(clnew->chat_username, username,
+               safestrncpy(clnew->chat_username, username,
                        sizeof clnew->chat_username);
                clnew->chat_username[sizeof clnew->chat_username - 1] = 0;
        } else
                clnew->chat_username[0] = '\0';
-       strcpy(clnew->chat_text, bcast);
+       safestrncpy(clnew->chat_text, bcast, sizeof clnew->chat_text);
 
        /* Here's the critical section.
         * First, add the new message to the queue...
@@ -161,32 +146,51 @@ t_context *find_context(char **unstr)
 }
 
 /*
- * List users in chat.  Setting allflag to 1 also lists users elsewhere.
+ * List users in chat.
+ * allflag ==  0 = list users in chat
+ *             1 = list users in chat, followed by users not in chat
+ *             2 = display count only
  */
 
 void do_chat_listing(int allflag)
 {
        struct CitContext *ccptr;
+       int count = 0;
+       char roomname[ROOMNAMELEN];
 
-       cprintf(":|\n:| Users currently in chat:\n");
+       if ((allflag == 0) || (allflag == 1))
+               cprintf(":|\n:| Users currently in chat:\n");
        begin_critical_section(S_SESSION_TABLE);
        for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
-               if ((!strcasecmp(ccptr->cs_room, "<chat>"))
+               if (ccptr->cs_flags & CS_CHAT) ++count;
+               if ((ccptr->cs_flags & CS_CHAT)
                    && ((ccptr->cs_flags & CS_STEALTH) == 0)) {
-                       cprintf(":| %-25s <%s>\n", (ccptr->fake_username[0]) ? ccptr->fake_username : ccptr->curr_user, ccptr->chat_room);
+                       if ((allflag == 0) || (allflag == 1))
+                               cprintf(":| %-25s <%s>\n", (ccptr->fake_username[0]) ? ccptr->fake_username : ccptr->curr_user, ccptr->chat_room);
                }
        }
 
        if (allflag == 1) {
                cprintf(":|\n:| Users not in chat:\n");
                for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
-                       if ((strcasecmp(ccptr->cs_room, "<chat>"))
+                       GenerateRoomDisplay(roomname, ccptr, CC);
+                       if (((ccptr->cs_flags & CS_CHAT) == 0)
                            && ((ccptr->cs_flags & CS_STEALTH) == 0)) {
-                               cprintf(":| %-25s <%s>:\n", (ccptr->fake_username[0]) ? ccptr->fake_username : ccptr->curr_user, (ccptr->fake_roomname[0]) ? ccptr->fake_roomname : ccptr->cs_room);
+                               cprintf(":| %-25s <%s>:\n",
+                                       (ccptr->fake_username[0]) ? ccptr->fake_username : ccptr->curr_user,
+                                       roomname);
                        }
                }
        }
        end_critical_section(S_SESSION_TABLE);
+
+       if (allflag == 2) {
+               if (count > 1) 
+                       cprintf(":|There are %d users here.\n", count);
+               else
+                       cprintf(":|Note: you are the only one here.\n");
+       }
+
        cprintf(":|\n");
 }
 
@@ -196,7 +200,6 @@ void cmd_chat(char *argbuf)
        char cmdbuf[256];
        char *un;
        char *strptr1;
-       char hold_cs_room[ROOMNAMELEN];
        int MyLastMsg, ThisLastMsg;
        struct ChatLine *clptr;
        struct CitContext *t_context;
@@ -208,9 +211,7 @@ void cmd_chat(char *argbuf)
        }
        strcpy(CC->chat_room, "Main room");
 
-       strcpy(hold_cs_room, CC->cs_room);
        CC->cs_flags = CC->cs_flags | CS_CHAT;
-       set_wtmpsupp("<chat>");
        cprintf("%d Entering chat mode (type '/help' for available commands)\n",
                START_CHAT_MODE);
 
@@ -221,6 +222,8 @@ void cmd_chat(char *argbuf)
        }
        strcpy(cmdbuf, "");
 
+       do_chat_listing(2);
+
        while (1) {
                int ok_cmd;
 
@@ -253,7 +256,6 @@ void cmd_chat(char *argbuf)
                                        sleep(1);
                                        cprintf("000\n");
                                        CC->cs_flags = CC->cs_flags - CS_CHAT;
-                                       set_wtmpsupp(hold_cs_room);
                                        return;
                                }
                                if ((!strcasecmp(cmdbuf, "/help"))
@@ -388,7 +390,7 @@ void cmd_pexp(char *argbuf)
                        cprintf("Message ");
                cprintf("from %s:\n", ptr->sender);
                if (ptr->text != NULL)
-                       cprintf("%s\n\n", ptr->text);
+                       memfmout(80, ptr->text, 0);
 
                holdptr = ptr->next;
                if (ptr->text != NULL) phree(ptr->text);
@@ -423,7 +425,7 @@ void cmd_gexp(char *argbuf) {
                ptr->sender,                            /* sender of msg */
                config.c_nodename);                     /* static for now */
        if (ptr->text != NULL) {
-               cprintf("%s", ptr->text);
+               memfmout(80, ptr->text, 0);
                if (ptr->text[strlen(ptr->text)-1] != '\n') cprintf("\n");
                phree(ptr->text);
                }
@@ -444,7 +446,13 @@ int send_express_message(char *lun, char *x_user, char *x_msg)
        struct CitContext *ccptr;
        struct ExpressMessage *newmsg, *findend;
        char *un;
-       FILE *fp;
+       size_t msglen = 0;
+       int do_send = 0;
+
+       if (strlen(x_msg) > 0) {
+               msglen = strlen(x_msg) + 4;
+               do_send = 1;
+               }
 
        /* find the target user's context and append the message */
        begin_critical_section(S_SESSION_TABLE);
@@ -457,16 +465,17 @@ int send_express_message(char *lun, char *x_user, char *x_msg)
 
                if ((!strcasecmp(un, x_user))
                    || (!strcasecmp(x_user, "broadcast"))) {
-                       if (strlen(x_msg) > 0) {
+                       if (do_send) {
                                newmsg = (struct ExpressMessage *)
                                        mallok(sizeof (struct ExpressMessage));
                                memset(newmsg, 0,
                                        sizeof (struct ExpressMessage));
-                               strcpy(newmsg->sender, un);
+                               safestrncpy(newmsg->sender, lun,
+                                           sizeof newmsg->sender);
                                if (!strcasecmp(x_user, "broadcast"))
                                        newmsg->flags |= EM_BROADCAST;
-                               newmsg->text = mallok(strlen(x_msg)+2);
-                               strcpy(newmsg->text, x_msg);
+                               newmsg->text = mallok(msglen);
+                               safestrncpy(newmsg->text, x_msg, msglen);
 
                                if (ccptr->FirstExpressMessage == NULL)
                                        ccptr->FirstExpressMessage = newmsg;
@@ -482,21 +491,11 @@ int send_express_message(char *lun, char *x_user, char *x_msg)
        }
        end_critical_section(S_SESSION_TABLE);
 
-       /* Log the page to disk if configured to do so */
-       if ((strlen(config.c_logpages) > 0) && (strlen(x_msg) > 0)) {
-               fp = fopen(CC->temp, "wb");
-               fprintf(fp, "%c%c%c", 255, MES_NORMAL, 0);
-               fprintf(fp, "Psysop%c", 0);
-               fprintf(fp, "T%ld%c", time(NULL), 0);
-               fprintf(fp, "A%s%c", lun, 0);
-               fprintf(fp, "R%s%c", x_user, 0);
-               fprintf(fp, "O%s%c", config.c_logpages, 0);
-               fprintf(fp, "N%s%c", NODENAME, 0);
-               fprintf(fp, "M%s\n%c", x_msg, 0);
-               fclose(fp);
-               save_message(CC->temp, "", config.c_logpages, M_LOCAL, 1);
-               unlink(CC->temp);
+       /* Log the page to disk if configured to do so  */
+       if ((strlen(config.c_logpages) > 0) && (do_send) ) {
+               quickie_message(lun, x_user, config.c_logpages, x_msg);
        }
+
        return (message_sent);
 }
 
@@ -535,7 +534,7 @@ void cmd_sexp(char *argbuf)
        }
        /* This loop handles text-transfer pages */
        if (!strcmp(x_msg, "-")) {
-               message_sent = send_express_message(lun, x_user, "");
+               message_sent = PerformXmsgHooks(lun, x_user, "");
                if (message_sent == 0) {
                        cprintf("%d No user '%s' logged in.\n", ERROR, x_user);
                        return;
@@ -552,12 +551,12 @@ void cmd_sexp(char *argbuf)
                                strcat(x_big_msgbuf, "\n");
                        strcat(x_big_msgbuf, x_msg);
                }
-               send_express_message(lun, x_user, x_big_msgbuf);
+               PerformXmsgHooks(lun, x_user, x_big_msgbuf);
                phree(x_big_msgbuf);
 
                /* This loop handles inline pages */
        } else {
-               message_sent = send_express_message(lun, x_user, x_msg);
+               message_sent = PerformXmsgHooks(lun, x_user, x_msg);
 
                if (message_sent > 0) {
                        if (strlen(x_msg) > 0)