#define desired_section ((char *)CtdlGetUserData(SYM_DESIRED_SECTION))
#define ma ((struct ma_info *)CtdlGetUserData(SYM_MA_INFO))
+#define msg_repl ((struct repl *)CtdlGetUserData(SYM_REPL))
extern struct config config;
-/* FIX dumb stub function */
-void save_message(char *junk, char *more_junk, char *foo, int wahw, int ee) {
-}
+char *msgkeys[] = {
+ "", "", "", "", "", "", "", "",
+ "", "", "", "", "", "", "", "",
+ "", "", "", "", "", "", "", "",
+ "", "", "", "", "", "", "", "",
+ "", "", "", "", "", "", "", "",
+ "", "", "", "", "", "", "", "",
+ "", "", "", "", "", "", "", "",
+ "", "", "", "", "", "", "", "",
+ "",
+ "from",
+ "", "", "", "", "", "",
+ "hnod",
+ "msgn",
+ "", "", "",
+ "text",
+ "node",
+ "room",
+ "path",
+ "",
+ "rcpt",
+ "",
+ "time",
+ "subj",
+ "",
+ "",
+ "",
+ "",
+ ""
+};
/*
* This function is self explanatory.
}
+
+/* Determine if a given message matches the fields in a message template.
+ * Return 0 for a successful match.
+ */
+int CtdlMsgCmp(struct CtdlMessage *msg, struct CtdlMessage *template) {
+ int i;
+
+ /* If there aren't any fields in the template, all messages will
+ * match.
+ */
+ if (template == NULL) return(0);
+
+ /* Null messages are bogus. */
+ if (msg == NULL) return(1);
+
+ for (i='A'; i<='Z'; ++i) {
+ if (template->cm_fields[i] != NULL) {
+ if (msg->cm_fields[i] == NULL) {
+ return 1;
+ }
+ if (strcasecmp(msg->cm_fields[i],
+ template->cm_fields[i])) return 1;
+ }
+ }
+
+ /* All compares succeeded: we have a match! */
+ return 0;
+}
+
+
+
+
/*
* API function to perform an operation for each qualifying message in the
* current room.
*/
void CtdlForEachMessage(int mode, long ref,
char *content_type,
+ struct CtdlMessage *compare,
void (*CallBack) (long msgnum))
{
int num_msgs = 0;
long thismsg;
struct SuppMsgInfo smi;
+ struct CtdlMessage *msg;
/* Learn about the user and room in question */
get_mm();
}
num_msgs = sort_msglist(msglist, num_msgs);
+
+ /* If a template was supplied, filter out the messages which
+ * don't match. (This could induce some delays!)
+ */
+ if (num_msgs > 0) {
+ if (compare != NULL) {
+ for (a = 0; a < num_msgs; ++a) {
+ msg = CtdlFetchMessage(msglist[a]);
+ if (msg != NULL) {
+ if (CtdlMsgCmp(msg, compare)) {
+ msglist[a] = 0L;
+ }
+ CtdlFreeMessage(msg);
+ }
+ }
+ }
+ }
+
/*
* Now iterate through the message list, according to the
{
int mode = 0;
char which[256];
+ char buf[256];
+ char tfield[256];
+ char tvalue[256];
int cm_ref = 0;
+ int i;
+ int with_template = 0;
+ struct CtdlMessage *template = NULL;
extract(which, cmdbuf, 0);
cm_ref = extract_int(cmdbuf, 1);
+ with_template = extract_int(cmdbuf, 2);
mode = MSGS_ALL;
strcat(which, " ");
cprintf("%d not logged in\n", ERROR + NOT_LOGGED_IN);
return;
}
- cprintf("%d Message list...\n", LISTING_FOLLOWS);
- CtdlForEachMessage(mode, cm_ref, NULL, simple_listing);
+
+ if (with_template) {
+ cprintf("%d Send template then receive message list\n",
+ START_CHAT_MODE);
+ template = (struct CtdlMessage *)
+ mallok(sizeof(struct CtdlMessage));
+ memset(template, 0, sizeof(struct CtdlMessage));
+ while(client_gets(buf), strcmp(buf,"000")) {
+ extract(tfield, buf, 0);
+ extract(tvalue, buf, 1);
+ for (i='A'; i<='Z'; ++i) if (msgkeys[i]!=NULL) {
+ if (!strcasecmp(tfield, msgkeys[i])) {
+ template->cm_fields[i] =
+ strdoop(tvalue);
+ }
+ }
+ }
+ }
+ else {
+ cprintf("%d Message list...\n", LISTING_FOLLOWS);
+ }
+
+ CtdlForEachMessage(mode, cm_ref, NULL, template, simple_listing);
+ if (template != NULL) CtdlFreeMessage(template);
cprintf("000\n");
}
} while ((field_length > 0) && (field_header != 'M'));
cdb_free(dmsgtext);
+
+ /* Perform "before read" hooks (aborting if any return nonzero) */
+ if (PerformMessageHooks(ret, EVT_BEFOREREAD) > 0) {
+ CtdlFreeMessage(ret);
+ return NULL;
+ }
+
return (ret);
}
void output_message(char *msgid, int mode, int headers_only)
{
long msg_num;
- int a, i;
+ int a, i, k;
char buf[1024];
time_t xtime;
CIT_UBYTE ch;
+ char allkeys[256];
+ char display_name[256];
struct CtdlMessage *TheMessage = NULL;
if ((mode == MT_CITADEL) || (mode == MT_MIME)) {
- if (TheMessage->cm_fields['P']) {
- cprintf("path=%s\n", TheMessage->cm_fields['P']);
- }
- if (TheMessage->cm_fields['I']) {
- cprintf("msgn=%s\n", TheMessage->cm_fields['I']);
- }
- if (TheMessage->cm_fields['T']) {
- cprintf("time=%s\n", TheMessage->cm_fields['T']);
- }
+ strcpy(display_name, "<unknown>");
if (TheMessage->cm_fields['A']) {
strcpy(buf, TheMessage->cm_fields['A']);
PerformUserHooks(buf, (-1L), EVT_OUTPUTMSG);
if (TheMessage->cm_anon_type == MES_ANON)
- cprintf("from=****");
+ strcpy(display_name, "****");
else if (TheMessage->cm_anon_type == MES_AN2)
- cprintf("from=anonymous");
+ strcpy(display_name, "anonymous");
else
- cprintf("from=%s", buf);
+ strcpy(display_name, buf);
if ((is_room_aide())
&& ((TheMessage->cm_anon_type == MES_ANON)
|| (TheMessage->cm_anon_type == MES_AN2))) {
- cprintf(" [%s]", buf);
+ sprintf(&display_name[strlen(display_name)],
+ " [%s]", buf);
}
- cprintf("\n");
- }
- if (TheMessage->cm_fields['O']) {
- cprintf("room=%s\n", TheMessage->cm_fields['O']);
- }
- if (TheMessage->cm_fields['N']) {
- cprintf("node=%s\n", TheMessage->cm_fields['N']);
}
- if (TheMessage->cm_fields['H']) {
- cprintf("hnod=%s\n", TheMessage->cm_fields['H']);
- }
- if (TheMessage->cm_fields['R']) {
- cprintf("rcpt=%s\n", TheMessage->cm_fields['R']);
- }
- if (TheMessage->cm_fields['U']) {
- cprintf("subj=%s\n", TheMessage->cm_fields['U']);
+
+ strcpy(allkeys, FORDER);
+ for (i=0; i<strlen(allkeys); ++i) {
+ k = (int) allkeys[i];
+ if (k != 'M') {
+ if (TheMessage->cm_fields[k] != NULL) {
+ if (k == 'A') {
+ cprintf("%s=%s\n",
+ msgkeys[k],
+ display_name);
+ }
+ else {
+ cprintf("%s=%s\n",
+ msgkeys[k],
+ TheMessage->cm_fields[k]
+ );
+ }
+ }
+ }
}
+
}
/* begin header processing loop for RFC822 transfer format */
{
long msgnum;
struct CtdlMessage *msg;
- struct sermsgret smr;
+ struct ser_ret smr;
if (CC->internal_pgm == 0) {
cprintf("%d This command is for internal programs only.\n",
long newmsgid;
long retval;
char msgidbuf[32];
- struct sermsgret smr;
+ struct ser_ret smr;
/* Get a new message number */
newmsgid = get_new_message_number();
/*
* Serialize a struct CtdlMessage into the format used on disk and network.
*
- * This function returns a "struct sermsgret" (defined in msgbase.h) which
+ * This function loads up a "struct ser_ret" (defined in server.h) which
* contains the length of the serialized message and a pointer to the
* serialized message in memory. THE LATTER MUST BE FREED BY THE CALLER.
*/
-void serialize_message(struct sermsgret *ret, /* return values */
+void serialize_message(struct ser_ret *ret, /* return values */
struct CtdlMessage *msg) /* unserialized msg */
{
size_t wlen;
ret->ser[1] = msg->cm_anon_type;
ret->ser[2] = msg->cm_format_type;
wlen = 3;
- lprintf(9, "stuff\n");
+ lprintf(9, "stuff\n");
for (i=0; i<26; ++i) if (msg->cm_fields[(int)forder[i]] != NULL) {
ret->ser[wlen++] = (char)forder[i];
strcpy(&ret->ser[wlen], msg->cm_fields[(int)forder[i]]);
}
if (ret->len != wlen) lprintf(3, "ERROR: len=%d wlen=%d\n",
ret->len, wlen);
+ lprintf(9, "done serializing\n");
return;
}
+/*
+ * Back end for the ReplicationChecks() function
+ */
+void check_repl(long msgnum) {
+ struct CtdlMessage *msg;
+ time_t timestamp;
+
+ msg = NULL; /* FIX change to get */
+
+
+ if (msg->cm_fields['T'] != NULL) {
+ timestamp = atol(msg->cm_fields['T']);
+ if (timestamp > msg_repl->highest) {
+ msg_repl->highest = timestamp; /* newer! */
+ return;
+ }
+ }
+
+ /* Existing isn't newer? Then delete the old one(s). */
+ CtdlDeleteMessages(&CC->quickroom.QRname, msgnum, NULL);
+}
+
+
+/*
+ * Check to see if any messages already exist which carry the same Extended ID
+ * as this one.
+ *
+ * If any are found:
+ * -> With older timestamps: delete them and return 0. Message will be saved.
+ * -> With newer timestamps: return 1. Message save will be aborted.
+ */
+int ReplicationChecks(struct CtdlMessage *msg) {
+ struct CtdlMessage *template;
+ int abort_this = 0;
+
+ /* No extended id? Don't do anything. */
+ if (msg->cm_fields['E'] == NULL) return 0;
+ if (strlen(msg->cm_fields['E']) == 0) return 0;
+
+ CtdlAllocUserData(SYM_REPL, sizeof(struct repl));
+ strcpy(msg_repl->extended_id, msg->cm_fields['E']);
+ msg_repl->highest = (-1L);
+
+ template = (struct CtdlMessage *) malloc(sizeof(struct CtdlMessage));
+ memset(template, 0, sizeof(struct CtdlMessage));
+ template->cm_fields['E'] = strdoop(msg->cm_fields['E']);
+
+ CtdlForEachMessage(MSGS_ALL, 0L, NULL, template, check_repl);
+
+ /* If a newer message exists with the same Extended ID, abort
+ * this save.
+ */
+ if (msg_repl->highest > atol(msg->cm_fields['T']) ) {
+ abort_this = 1;
+ }
+
+ CtdlFreeMessage(template);
+ return(abort_this);
+}
+
+
+
+
+
+
+
+
/*
* Save a message to disk
*/
}
}
+ /* Perform "before save" hooks (aborting if any return nonzero) */
+ if (PerformMessageHooks(msg, EVT_BEFORESAVE) > 0) return;
+
+ /* If this message has an Extended ID, perform replication checks */
+ if (ReplicationChecks(msg) > 0) return;
+
/* Network mail - send a copy to the network program. */
if ((strlen(recipient) > 0) && (mailtype != MES_LOCAL)) {
sprintf(aaa, "./network/spoolin/netmail.%04lx.%04x.%04x",
smi.smi_refcount = successful_local_recipients;
safestrncpy(smi.smi_content_type, content_type, 64);
PutSuppMsgInfo(&smi);
+
+ /* Perform "after save" hooks */
+ PerformMessageHooks(msg, EVT_AFTERSAVE);
}
/*
* Convenience function for generating small administrative messages.
*/
-void quickie_message(char *from, char *room, char *text)
+void quickie_message(char *from, char *to, char *room, char *text)
{
struct CtdlMessage *msg;
msg->cm_fields['A'] = strdoop(from);
msg->cm_fields['O'] = strdoop(room);
msg->cm_fields['N'] = strdoop(NODENAME);
+ if (to != NULL)
+ msg->cm_fields['R'] = strdoop(to);
msg->cm_fields['M'] = strdoop(text);
CtdlSaveMsg(msg, "", room, MES_LOCAL, 1);
void cmd_ent3(char *entargs)
{
char recp[256];
- char buf[256];
int a;
int e = 0;
+ unsigned char ch, which_field;
struct usersupp tempUS;
long msglen;
- long bloklen;
- FILE *fp;
+ struct CtdlMessage *msg;
+ char *tempbuf;
if (CC->internal_pgm == 0) {
cprintf("%d This command is for internal programs only.\n",
}
}
}
+
/* At this point, message has been approved. */
if (extract_int(entargs, 0) == 0) {
cprintf("%d OK to send\n", OK);
return;
}
- /* open a temp file to hold the message */
- fp = fopen(CC->temp, "wb");
- if (fp == NULL) {
- cprintf("%d Cannot open %s: %s\n",
- ERROR + INTERNAL_ERROR,
- CC->temp, strerror(errno));
+
+ msglen = extract_long(entargs, 2);
+ msg = mallok(sizeof(struct CtdlMessage));
+ if (msg == NULL) {
+ cprintf("%d Out of memory\n", ERROR+INTERNAL_ERROR);
+ return;
+ }
+
+ memset(msg, 0, sizeof(struct CtdlMessage));
+ tempbuf = mallok(msglen);
+ if (tempbuf == NULL) {
+ cprintf("%d Out of memory\n", ERROR+INTERNAL_ERROR);
+ phree(msg);
return;
}
- msglen = extract_long(entargs, 2);
+
cprintf("%d %ld\n", SEND_BINARY, msglen);
- while (msglen > 0L) {
- bloklen = ((msglen >= 255L) ? 255 : msglen);
- client_read(buf, (int) bloklen);
- fwrite(buf, (int) bloklen, 1, fp);
- msglen = msglen - bloklen;
- }
- fclose(fp);
- save_message(CC->temp, recp, "", e, 0);
+ client_read(&ch, 1); /* 0xFF magic number */
+ msg->cm_magic = CTDLMESSAGE_MAGIC;
+ client_read(&ch, 1); /* anon type */
+ msg->cm_anon_type = ch;
+ client_read(&ch, 1); /* format type */
+ msg->cm_format_type = ch;
+ msglen = msglen - 3;
+
+ while (msglen > 0) {
+ client_read(&which_field, 1);
+ --msglen;
+ tempbuf[0] = 0;
+ do {
+ client_read(&ch, 1);
+ --msglen;
+ a = strlen(tempbuf);
+ tempbuf[a+1] = 0;
+ tempbuf[a] = ch;
+ } while ( (ch != 0) && (msglen > 0) );
+ msg->cm_fields[which_field] = strdoop(tempbuf);
+ }
+
+ msg->cm_flags = CM_SKIP_HOOKS;
+ CtdlSaveMsg(msg, recp, "", e, 0);
+ CtdlFreeMessage(msg);
+ phree(tempbuf);
}
/*
* API function to delete messages which match a set of criteria
* (returns the actual number of messages deleted)
- * FIX ... still need to implement delete by content type
*/
int CtdlDeleteMessages(char *room_name, /* which room */
long dmsgnum, /* or "0" for any */
getuser(&CC->usersupp, CC->curr_user);
if ((CC->usersupp.axlevel < 6)
&& (CC->usersupp.usernum != CC->quickroom.QRroomaide)
- && ((CC->quickroom.QRflags & QR_MAILBOX) == 0)) {
+ && ((CC->quickroom.QRflags & QR_MAILBOX) == 0)
+ && (!(CC->internal_pgm))) {
cprintf("%d Higher access required.\n",
ERROR + HIGHER_ACCESS_REQUIRED);
return;
/*
* Write a generic object to this room
+ *
+ * Note: this could be much more efficient. Right now we use two temporary
+ * files, and still pull the message into memory as with all others.
*/
void CtdlWriteObject(char *req_room, /* Room to stuff it in */
char *content_type, /* MIME type of this object */
char *tempfilename, /* Where to fetch it from */
- int is_mailbox, /* Private mailbox room? */
+ struct usersupp *is_mailbox, /* Mailbox room? */
int is_binary, /* Is encoding necessary? */
- int is_unique /* Del others of this type? */
+ int is_unique, /* Del others of this type? */
+ unsigned int flags /* Internal save flags */
)
{
struct CtdlMessage *msg;
size_t len;
- lprintf(9, "CtdlWriteObject() called\n");
-
- if (is_mailbox)
- MailboxName(roomname, &CC->usersupp, req_room);
+ if (is_mailbox != NULL)
+ MailboxName(roomname, is_mailbox, req_room);
else
safestrncpy(roomname, req_room, sizeof(roomname));
+ lprintf(9, "CtdlWriteObject() to <%s> (flags=%d)\n", roomname, flags);
strcpy(filename, tmpnam(NULL));
fp = fopen(filename, "w");
msg->cm_anon_type = MES_NORMAL;
msg->cm_format_type = 4;
msg->cm_fields['A'] = strdoop(CC->usersupp.fullname);
- msg->cm_fields['O'] = strdoop(roomname);
+ msg->cm_fields['O'] = strdoop(req_room);
msg->cm_fields['N'] = strdoop(config.c_nodename);
msg->cm_fields['H'] = strdoop(config.c_humannode);
+ msg->cm_flags = flags;
lprintf(9, "Loading\n");
fp = fopen(filename, "rb");
unlink(filename);
/* Create the requested room if we have to. */
- lprintf(9, "Checking room\n");
if (getroom(&qrbuf, roomname) != 0) {
create_room(roomname, 4, "", 0);
}
CtdlDeleteMessages(roomname, 0L, content_type));
}
/* Now write the data */
- lprintf(9, "Calling CtdlSaveMsg()\n");
CtdlSaveMsg(msg, "", roomname, MES_LOCAL, 1);
- lprintf(9, "is valid? %d\n",
- is_valid_message(msg) );
- lprintf(9, "Calling CtdlFreeMsg()\n");
CtdlFreeMessage(msg);
- lprintf(9, "Done.\n");
}