*
* $Id$
*/
+#include "sysdep.h"
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/wait.h>
#include <string.h>
#include <limits.h>
+#ifdef HAVE_PTHREAD_H
#include <pthread.h>
+#endif
#include "citadel.h"
#include "server.h"
#include <syslog.h>
#define MODULE_NAME "Chat module"
#define MODULE_AUTHOR "Art Cancro"
#define MODULE_EMAIL "ajc@uncnsrd.mt-kisco.ny.us"
-#define MAJOR_VERSION 1
+#define MAJOR_VERSION 2
#define MINOR_VERSION 0
static struct DLModule_Info info =
struct DLModule_Info *Dynamic_Module_Init(void)
{
+ CtdlSendExpressMessageFunc = send_express_message;
+
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;
}
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");
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...
}
/*
- * 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;
- 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>")) ++count;
if ((!strcasecmp(ccptr->cs_room, "<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);
}
}
}
}
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");
}
}
strcpy(CC->chat_room, "Main room");
- strcpy(hold_cs_room, CC->cs_room);
+ safestrncpy(hold_cs_room, CC->cs_room, sizeof hold_cs_room);
CC->cs_flags = CC->cs_flags | CS_CHAT;
set_wtmpsupp("<chat>");
cprintf("%d Entering chat mode (type '/help' for available commands)\n",
}
strcpy(cmdbuf, "");
+ do_chat_listing(2);
+
while (1) {
int ok_cmd;
}
+
/*
- * poll for express messages
+ * Delete any remaining express messages
+ */
+void delete_express_messages(void) {
+ struct ExpressMessage *ptr;
+
+ begin_critical_section(S_SESSION_TABLE);
+ while (CC->FirstExpressMessage != NULL) {
+ ptr = CC->FirstExpressMessage->next;
+ if (CC->FirstExpressMessage->text != NULL)
+ phree(CC->FirstExpressMessage->text);
+ phree(CC->FirstExpressMessage);
+ CC->FirstExpressMessage = ptr;
+ }
+ end_critical_section(S_SESSION_TABLE);
+ }
+
+
+
+
+/*
+ * Poll for express messages (OLD METHOD -- ***DEPRECATED ***)
*/
void cmd_pexp(char *argbuf)
-{ /* arg unused */
- char *ptr;
+{
+ struct ExpressMessage *ptr, *holdptr;
- if (CC->ExpressMessages == NULL) {
+ if (CC->FirstExpressMessage == NULL) {
cprintf("%d No express messages waiting.\n", ERROR);
return;
}
begin_critical_section(S_SESSION_TABLE);
- ptr = CC->ExpressMessages;
- CC->ExpressMessages = NULL;
+ ptr = CC->FirstExpressMessage;
+ CC->FirstExpressMessage = NULL;
end_critical_section(S_SESSION_TABLE);
- cprintf("%d Express msgs:\n%s", LISTING_FOLLOWS, ptr);
- if (ptr[strlen(ptr)] != '\n')
- cprintf("\n");
+ cprintf("%d Express msgs:\n", LISTING_FOLLOWS);
+ while (ptr != NULL) {
+ if (ptr->flags && EM_BROADCAST)
+ cprintf("Broadcast message ");
+ else if (ptr->flags && EM_CHAT)
+ cprintf("Chat request ");
+ else if (ptr->flags && EM_GO_AWAY)
+ cprintf("Please logoff now, as requested ");
+ else
+ cprintf("Message ");
+ cprintf("from %s:\n", ptr->sender);
+ if (ptr->text != NULL)
+ memfmout(80, ptr->text, 0);
+
+ holdptr = ptr->next;
+ if (ptr->text != NULL) phree(ptr->text);
+ phree(ptr);
+ ptr = holdptr;
+ }
cprintf("000\n");
+}
+
+
+/*
+ * Get express messages (new method)
+ */
+void cmd_gexp(char *argbuf) {
+ struct ExpressMessage *ptr;
+
+ if (CC->FirstExpressMessage == NULL) {
+ cprintf("%d No express messages waiting.\n", ERROR);
+ return;
+ }
+ begin_critical_section(S_SESSION_TABLE);
+ ptr = CC->FirstExpressMessage;
+ CC->FirstExpressMessage = CC->FirstExpressMessage->next;
+ end_critical_section(S_SESSION_TABLE);
+
+ cprintf("%d %d|%ld|%d|%s|%s\n",
+ LISTING_FOLLOWS,
+ ((ptr->next != NULL) ? 1 : 0), /* more msgs? */
+ ptr->timestamp, /* time sent */
+ ptr->flags, /* flags */
+ ptr->sender, /* sender of msg */
+ config.c_nodename); /* static for now */
+ if (ptr->text != NULL) {
+ memfmout(80, ptr->text, 0);
+ if (ptr->text[strlen(ptr->text)-1] != '\n') cprintf("\n");
+ phree(ptr->text);
+ }
+ cprintf("000\n");
phree(ptr);
}
+
/*
* This is the back end to the express message sending function.
* Returns the number of users to which the message was sent.
{
int message_sent = 0;
struct CitContext *ccptr;
- char *msgptr;
+ struct ExpressMessage *newmsg, *findend;
char *un;
- int newlen;
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);
if ((!strcasecmp(un, x_user))
|| (!strcasecmp(x_user, "broadcast"))) {
- if (strlen(x_msg) > 0) {
- strcpy(ccptr->last_pager, CC->curr_user);
- if (ccptr->ExpressMessages == NULL) {
- newlen = strlen(x_msg) + 80;
- msgptr = mallok(newlen);
- bzero(msgptr, newlen);
- } else {
- newlen = (strlen(ccptr->ExpressMessages)
- + strlen(x_msg) + 80);
- msgptr = reallok(
- ccptr->ExpressMessages, newlen);
- }
- if (msgptr != NULL) {
- sprintf(&msgptr[strlen(msgptr)],
- "%s from %s:\n %s\n",
- ((!strcasecmp(x_user, "broadcast")) ? "Broadcast message" : "Message"),
- lun, x_msg);
- ccptr->ExpressMessages = msgptr;
+ if (do_send) {
+ newmsg = (struct ExpressMessage *)
+ mallok(sizeof (struct ExpressMessage));
+ memset(newmsg, 0,
+ sizeof (struct ExpressMessage));
+ safestrncpy(newmsg->sender, lun,
+ sizeof newmsg->sender);
+ if (!strcasecmp(x_user, "broadcast"))
+ newmsg->flags |= EM_BROADCAST;
+ newmsg->text = mallok(msglen);
+ safestrncpy(newmsg->text, x_msg, msglen);
+
+ if (ccptr->FirstExpressMessage == NULL)
+ ccptr->FirstExpressMessage = newmsg;
+ else {
+ findend = ccptr->FirstExpressMessage;
+ while (findend->next != NULL)
+ findend = findend->next;
+ findend->next = newmsg;
}
}
++message_sent;
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)) {
+ if ((strlen(config.c_logpages) > 0) && (do_send) ) {
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, "T%ld%c", (long)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);
+ save_message(CC->temp, "", config.c_logpages, MES_LOCAL, 1);
unlink(CC->temp);
}
return (message_sent);
extract(x_user, argbuf, 0);
- if (!strcmp(x_user, ".")) {
- strcpy(x_user, CC->last_pager);
- }
extract(x_msg, argbuf, 1);
if (!x_user[0]) {
cprintf("%d Transmit message (will deliver to %d users)\n",
SEND_LISTING, message_sent);
x_big_msgbuf = mallok(256);
- bzero(x_big_msgbuf, 256);
+ memset(x_big_msgbuf, 0, 256);
while (client_gets(x_msg), strcmp(x_msg, "000")) {
x_big_msgbuf = reallok(x_big_msgbuf,
strlen(x_big_msgbuf) + strlen(x_msg) + 4);