This repository has been archived on 2024-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
magicka/src/www_msgs.c

1346 lines
37 KiB
C

#if defined(ENABLE_WWW)
#include <string.h>
#include <time.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/utsname.h>
#include <sys/stat.h>
#include <iconv.h>
#include <libgen.h>
#include "www_tree.h"
#include "jamlib/jam.h"
#include "libuuid/uuid.h"
#include "www_tree.h"
#include "bbs.h"
#include "mail_utils.h"
#define IN 0
#define OUT 1
extern struct bbs_config conf;
extern struct www_tag * aha(char *input, struct www_tag *parent);
static char *www_wordwrap(char *content, int cutoff);
/*
char *www_sanitize(char *inp) {
int i;
char *result;
int len = 0;
for (i = 0; i < strlen(inp); i++) {
if ((inp[i] == '<') || (inp[i] == '>')) {
len += 4;
} else if (inp[i] == '&') {
len += 5;
} else {
len++;
}
}
result = (char *)malloz(len + 1);
len = 0;
for (i = 0; i < strlen(inp); i++) {
if (inp[i] == '<') {
result[len++] = '&';
result[len++] = 'l';
result[len++] = 't';
result[len++] = ';';
} else if (inp[i] == '>') {
result[len++] = '&';
result[len++] = 'g';
result[len++] = 't';
result[len++] = ';';
} else if (inp[i] == '&') {
result[len++] = '&';
result[len++] = 'a';
result[len++] = 'm';
result[len++] = 'p';
result[len++] = ';';
} else {
result[len++] = inp[i];
}
}
return result;
}
*/
char *www_msgs_arealist(struct user_record *user) {
stralloc page = EMPTY_STRALLOC;
stralloc_copys(&page, "<div class=\"content-header\"><h2>Message Conferences</h2></div>\n");
for (size_t i = 0; i < ptr_vector_len(&conf.mail_conferences); i++) {
struct mail_conference *mc = get_conf(i);
if (mc->sec_level <= user->sec_level) {
stralloc_cats(&page, "<div class=\"conference-list-item\">");
stralloc_cats(&page, mc->name);
stralloc_cats(&page, "</div>\n");
for (size_t j = 0; j < ptr_vector_len(&mc->mail_areas); j++) {
struct mail_area *ma = get_area(i, j);
if (ma->read_sec_level > user->sec_level) {
continue;
}
stralloc_cats(&page, "<div class=\"area-list-");
stralloc_cats(&page, (new_messages(user, i, j) > 0) ? "new" : "item");
stralloc_cats(&page, "\"><a href=\"");
stralloc_cats(&page, conf.www_url);
stralloc_cats(&page, "msgs/");
stralloc_cat_long(&page, i);
stralloc_append1(&page, '/');
stralloc_cat_long(&page, j);
stralloc_cats(&page, "/\">");
stralloc_cats(&page, ma->name);
stralloc_cats(&page, "</a></div>\n");
}
}
}
stralloc_0(&page);
return page.s;
}
char *www_msgs_messagelist(struct user_record *user, int conference, int area, int skip) {
struct msg_headers *mhrs;
char buffer[4096];
int i;
struct tm msg_date;
time_t date;
s_JamBase *jb;
s_JamLastRead jlr;
int skip_f;
int skip_t;
char *to;
char *from;
char *subject;
char datebuf[32];
stralloc url;
struct www_tag *page;
struct www_tag *cur_tag;
struct www_tag *child_tag;
struct www_tag *child_child_tag;
struct www_tag *child_child_child_tag;
struct www_tag *child_child_child_child_tag;
if (conference < 0 || conference >= ptr_vector_len(&conf.mail_conferences))
return NULL;
struct mail_conference *mc = get_conf(conference);
if (area < 0 || area >= ptr_vector_len(&mc->mail_areas))
return NULL;
struct mail_area *ma = get_area(conference, area);
page = www_tag_new(NULL, "");
cur_tag = www_tag_new("div", NULL);
www_tag_add_attrib(cur_tag, "class", "content-header");
www_tag_add_child(page, cur_tag);
child_tag = www_tag_new("h2", NULL);
www_tag_add_child(cur_tag, child_tag);
child_child_tag = www_tag_new(NULL, mc->name);
www_tag_add_child(child_tag, child_child_tag);
child_child_tag = www_tag_new(NULL, " - ");
www_tag_add_child(child_tag, child_child_tag);
child_child_tag = www_tag_new(NULL, ma->name);
www_tag_add_child(child_tag, child_child_tag);
if (ma->type != TYPE_NETMAIL_AREA) {
cur_tag = www_tag_new("div", NULL);
www_tag_add_attrib(cur_tag, "class", "button");
www_tag_add_child(page, cur_tag);
child_tag = www_tag_new("a", NULL);
url = EMPTY_STRALLOC;
stralloc_cats(&url, conf.www_url);
stralloc_cats(&url, "msgs/new/");
stralloc_cat_long(&url, conference);
stralloc_append1(&url, '/');
stralloc_cat_long(&url, area);
stralloc_0(&url);
www_tag_add_attrib(child_tag, "href", url.s);
free(url.s);
www_tag_add_child(cur_tag, child_tag);
child_child_tag = www_tag_new(NULL, "New Message");
www_tag_add_child(child_tag, child_child_tag);
}
mhrs = read_message_headers(conference, area, user, 0);
if (mhrs == NULL) {
cur_tag = www_tag_new("h3", NULL);
www_tag_add_child(page, cur_tag);
child_tag = www_tag_new(NULL, "No Messages");
www_tag_add_child(cur_tag, child_tag);
} else {
cur_tag = www_tag_new("div", NULL);
www_tag_add_attrib(cur_tag, "class", "div-table");
www_tag_add_child(page, cur_tag);
jb = open_jam_base(ma->path);
if (!jb) {
www_tag_destroy(page);
free_message_headers(mhrs);
return NULL;
}
if (JAM_ReadLastRead(jb, user->id, &jlr) == JAM_NO_USER) {
jlr.LastReadMsg = 0;
jlr.HighReadMsg = 0;
}
JAM_CloseMB(jb);
free(jb);
skip_f = mhrs->msg_count - skip;
skip_t = mhrs->msg_count - skip - 50;
if (skip_t < 0) {
skip_t = 0;
}
for (i = skip_f - 1; i >= skip_t; i--) {
date = (time_t)mhrs->msgs[i]->msg_h->DateWritten;
gmtime_r(&date, &msg_date);
to = strdup(mhrs->msgs[i]->to);
from = strdup(mhrs->msgs[i]->from);
subject = strdup(mhrs->msgs[i]->subject);
child_tag = www_tag_new("div", NULL);
if (msgbase_is_flagged(user, conference, area, mhrs->msgs[i]->msg_h->MsgNum)) {
www_tag_add_attrib(child_tag, "class", "msg-summary-flag");
} else if (mhrs->msgs[i]->msg_h->MsgNum > jlr.HighReadMsg) {
www_tag_add_attrib(child_tag, "class", "msg-summary");
} else {
www_tag_add_attrib(child_tag, "class", "msg-summary-seen");
}
www_tag_add_child(cur_tag, child_tag);
child_child_tag = www_tag_new("div", NULL);
www_tag_add_attrib(child_child_tag, "class", "msg-summary-id");
www_tag_add_child(child_tag, child_child_tag);
url = EMPTY_STRALLOC;
stralloc_cat_long(&url, mhrs->msgs[i]->msg_no + 1);
stralloc_0(&url);
child_child_child_tag = www_tag_new(NULL, url.s);
free(url.s);
www_tag_add_child(child_child_tag, child_child_child_tag);
child_child_tag = www_tag_new("div", NULL);
www_tag_add_attrib(child_child_tag, "class", "msg-summary-subject");
www_tag_add_child(child_tag, child_child_tag);
url = EMPTY_STRALLOC;
stralloc_cats(&url, conf.www_url);
stralloc_cats(&url, "msgs/");
stralloc_cat_long(&url, conference);
stralloc_append1(&url, '/');
stralloc_cat_long(&url, area);
stralloc_append1(&url, '/');
stralloc_cat_long(&url, mhrs->msgs[i]->msg_h->MsgNum);
stralloc_0(&url);
child_child_child_tag = www_tag_new("a", NULL);
www_tag_add_attrib(child_child_child_tag, "href", url.s);
free(url.s);
www_tag_add_child(child_child_tag, child_child_child_tag);
child_child_child_child_tag = www_tag_new(NULL, subject);
www_tag_add_child(child_child_child_tag, child_child_child_child_tag);
child_child_tag = www_tag_new("div", NULL);
www_tag_add_attrib(child_child_tag, "class", "msg-summary-from");
www_tag_add_child(child_tag, child_child_tag);
child_child_child_tag = www_tag_new(NULL, from);
www_tag_add_child(child_child_tag, child_child_child_tag);
child_child_tag = www_tag_new("div", NULL);
www_tag_add_attrib(child_child_tag, "class", "msg-summary-to");
www_tag_add_child(child_tag, child_child_tag);
child_child_child_tag = www_tag_new(NULL, to);
www_tag_add_child(child_child_tag, child_child_child_tag);
child_child_tag = www_tag_new("div", NULL);
www_tag_add_attrib(child_child_tag, "class", "msg-summary-date");
www_tag_add_child(child_tag, child_child_tag);
if (conf.date_style == 1)
strftime(datebuf, sizeof datebuf, "%H:%M %m-%d-%y", &msg_date);
else
strftime(datebuf, sizeof datebuf, "%H:%M %d-%m-%y", &msg_date);
child_child_child_tag = www_tag_new(NULL, datebuf);
www_tag_add_child(child_child_tag, child_child_child_tag);
free(to);
free(from);
free(subject);
}
if (skip + 50 <= mhrs->msg_count) {
cur_tag = www_tag_new("div", NULL);
www_tag_add_attrib(cur_tag, "class", "msg-summary-next");
www_tag_add_child(page, cur_tag);
child_tag = www_tag_new("a", NULL);
url = EMPTY_STRALLOC;
stralloc_cats(&url, conf.www_url);
stralloc_cats(&url, "msgs/");
stralloc_cat_long(&url, conference);
stralloc_append1(&url, '/');
stralloc_cat_long(&url, area);
stralloc_cats(&url, "/?skip=");
stralloc_cat_long(&url, skip + 50);
stralloc_0(&url);
www_tag_add_attrib(child_tag, "href", url.s);
free(url.s);
www_tag_add_child(cur_tag, child_tag);
child_child_tag = www_tag_new(NULL, "Next");
www_tag_add_child(child_tag, child_child_tag);
}
if (skip > 0) {
cur_tag = www_tag_new("div", NULL);
www_tag_add_attrib(cur_tag, "class", "msg-summary-prev");
www_tag_add_child(page, cur_tag);
child_tag = www_tag_new("a", NULL);
url = EMPTY_STRALLOC;
if (skip - 50 < 0) {
stralloc_cats(&url, conf.www_url);
stralloc_cats(&url, "msgs/");
stralloc_cat_long(&url, conference);
stralloc_append1(&url, '/');
stralloc_cat_long(&url, area);
stralloc_append1(&url, '/');
} else {
stralloc_cats(&url, conf.www_url);
stralloc_cats(&url, "msgs/");
stralloc_cat_long(&url, conference);
stralloc_append1(&url, '/');
stralloc_cat_long(&url, area);
stralloc_cats(&url, "/?skip=");
stralloc_cat_long(&url, skip - 50);
}
stralloc_0(&url);
www_tag_add_attrib(child_tag, "href", url.s);
free(url.s);
www_tag_add_child(cur_tag, child_tag);
child_child_tag = www_tag_new(NULL, "Prev");
www_tag_add_child(child_tag, child_child_tag);
}
free_message_headers(mhrs);
}
return www_tag_unwravel(page);
}
char *www_msgs_messageview(struct user_record *user, int conference, int area, int msg) {
s_JamBase *jb;
s_JamMsgHeader jmh;
s_JamSubPacket *jsp;
s_JamSubfield jsf;
s_JamLastRead jlr;
s_JamBaseHeader jbh;
char *subject = NULL;
char *from = NULL;
char *to = NULL;
char *daddress = NULL;
char *oaddress = NULL;
char *msgid = NULL;
char *replyid = NULL;
char *body = NULL;
char *body2 = NULL;
char *replybody = NULL;
int z;
struct tm msg_date;
time_t date;
char buffer[4096];
int chars;
int i;
char *from_s;
char *subject_s;
char *to_s;
int l1, l2;
char *aha_text;
char *nodename;
struct fido_addr *nodeno;
struct www_tag *page;
struct www_tag *cur_tag;
struct www_tag *child_tag;
struct www_tag *child_child_tag;
struct www_tag *child_child_child_tag;
if (conference < 0 || conference >= ptr_vector_len(&conf.mail_conferences))
return NULL;
struct mail_conference *mc = get_conf(conference);
if (area < 0 || area >= ptr_vector_len(&mc->mail_areas))
return NULL;
struct mail_area *ma = get_area(conference, area);
jb = open_jam_base(ma->path);
if (!jb) {
return NULL;
}
JAM_ReadMBHeader(jb, &jbh);
memset(&jmh, 0, sizeof(s_JamMsgHeader));
z = JAM_ReadMsgHeader(jb, msg - 1, &jmh, &jsp);
if (z != 0) {
JAM_CloseMB(jb);
free(jb);
return NULL;
}
if (jmh.Attribute & JAM_MSG_DELETED) {
JAM_DelSubPacket(jsp);
JAM_CloseMB(jb);
free(jb);
return NULL;
}
for (z = 0; z < jsp->NumFields; z++) {
if (jsp->Fields[z]->LoID == JAMSFLD_SUBJECT) {
subject = strndup(jsp->Fields[z]->Buffer, jsp->Fields[z]->DatLen);
} else if (jsp->Fields[z]->LoID == JAMSFLD_SENDERNAME) {
from = strndup(jsp->Fields[z]->Buffer, jsp->Fields[z]->DatLen);
} else if (jsp->Fields[z]->LoID == JAMSFLD_RECVRNAME) {
to = strndup(jsp->Fields[z]->Buffer, jsp->Fields[z]->DatLen);
} else if (jsp->Fields[z]->LoID == JAMSFLD_DADDRESS) {
daddress = strndup(jsp->Fields[z]->Buffer, jsp->Fields[z]->DatLen);
} else if (jsp->Fields[z]->LoID == JAMSFLD_OADDRESS) {
oaddress = strndup(jsp->Fields[z]->Buffer, jsp->Fields[z]->DatLen);
} else if (jsp->Fields[z]->LoID == JAMSFLD_MSGID) {
msgid = strndup(jsp->Fields[z]->Buffer, jsp->Fields[z]->DatLen);
} else if (jsp->Fields[z]->LoID == JAMSFLD_REPLYID) {
replyid = strndup(jsp->Fields[z]->Buffer, jsp->Fields[z]->DatLen);
}
}
JAM_DelSubPacket(jsp);
if (subject == NULL) {
subject = strdup("(No Subject)");
}
if (from == NULL) {
from = strdup("(No Sender)");
}
if (to == NULL) {
to = strdup("(No Recipient)");
}
if (jmh.Attribute & JAM_MSG_PRIVATE) {
if (!msg_is_to(user, to, daddress, mc->nettype, mc->realnames, conference) &&
!msg_is_from(user, from, oaddress, mc->nettype, mc->realnames, conference) &&
!msg_is_to(user, to, daddress, mc->nettype, !mc->realnames, conference) &&
!msg_is_from(user, from, oaddress, mc->nettype, !mc->realnames, conference)) {
free(subject);
free(from);
free(to);
free(oaddress);
free(daddress);
free(msgid);
free(replyid);
JAM_CloseMB(jb);
free(jb);
return NULL;
}
}
body = (char *)malloz(jmh.TxtLen + 1);
JAM_ReadMsgText(jb, jmh.TxtOffset, jmh.TxtLen, (char *)body);
if (JAM_ReadLastRead(jb, user->id, &jlr) == JAM_NO_USER) {
jlr.UserCRC = JAM_Crc32(user->loginname, strlen(user->loginname));
jlr.UserID = user->id;
jlr.HighReadMsg = msg;
}
jlr.LastReadMsg = msg;
if (jlr.HighReadMsg < msg) {
jlr.HighReadMsg = msg;
}
JAM_WriteLastRead(jb, user->id, &jlr);
JAM_CloseMB(jb);
free(jb);
page = www_tag_new(NULL, "");
cur_tag = www_tag_new("div", NULL);
www_tag_add_attrib(cur_tag, "class", "content-header");
www_tag_add_child(page, cur_tag);
child_tag = www_tag_new("a", NULL);
stralloc url = EMPTY_STRALLOC;
stralloc_cats(&url, conf.www_url);
stralloc_cats(&url, "msgs/");
stralloc_cat_long(&url, conference);
stralloc_append1(&url, '/');
stralloc_cat_long(&url, area);
stralloc_0(&url);
www_tag_add_attrib(child_tag, "href", url.s);
free(url.s);
www_tag_add_child(cur_tag, child_tag);
child_child_tag = www_tag_new("h2", NULL);
www_tag_add_child(child_tag, child_child_tag);
child_child_child_tag = www_tag_new(NULL, mc->name);
www_tag_add_child(child_child_tag, child_child_child_tag);
child_child_child_tag = www_tag_new(NULL, " - ");
www_tag_add_child(child_child_tag, child_child_child_tag);
child_child_child_tag = www_tag_new(NULL, ma->name);
www_tag_add_child(child_child_tag, child_child_child_tag);
cur_tag = www_tag_new("div", NULL);
if (msgbase_is_flagged(user, conference, area, msg)) {
www_tag_add_attrib(cur_tag, "class", "msg-view-header-flagged");
} else {
www_tag_add_attrib(cur_tag, "class", "msg-view-header");
}
www_tag_add_child(page, cur_tag);
child_tag = www_tag_new("div", NULL);
www_tag_add_attrib(child_tag, "class", "msg-view-subject");
www_tag_add_child(cur_tag, child_tag);
child_child_tag = www_tag_new(NULL, subject);
www_tag_add_child(child_tag, child_child_tag);
child_tag = www_tag_new("div", NULL);
www_tag_add_attrib(child_tag, "class", "msg-view-from");
www_tag_add_child(cur_tag, child_tag);
if (ma->type != TYPE_LOCAL_AREA && oaddress != NULL) {
if (mc->nettype == NETWORK_MAGI) {
snprintf(buffer, sizeof buffer, "From: %s (@%s)", from, oaddress);
child_child_tag = www_tag_new(NULL, buffer);
www_tag_add_child(child_tag, child_child_tag);
} else if (mc->nettype == NETWORK_FIDO) {
nodeno = parse_fido_addr(oaddress);
if (nodeno != NULL) {
nodename = nl_get_bbsname(nodeno, mc->domain);
if (strcmp(nodename, "Unknown") == 0) {
snprintf(buffer, sizeof buffer, "From: %s (%s)", from, oaddress);
child_child_tag = www_tag_new(NULL, buffer);
www_tag_add_child(child_tag, child_child_tag);
} else {
snprintf(buffer, sizeof buffer, "From: %s (", from);
child_child_tag = www_tag_new(NULL, buffer);
www_tag_add_child(child_tag, child_child_tag);
child_child_tag = www_tag_new("span", NULL);
www_tag_add_attrib(child_child_tag, "class", "bbsname");
www_tag_add_child(child_tag, child_child_tag);
child_child_child_tag = www_tag_new(NULL, nodename);
www_tag_add_child(child_child_tag, child_child_child_tag);
snprintf(buffer, sizeof buffer, " - %s)", oaddress);
child_child_tag = www_tag_new(NULL, buffer);
www_tag_add_child(child_tag, child_child_tag);
}
free(nodename);
free(nodeno);
} else {
snprintf(buffer, sizeof buffer, "From: %s (%s)", from, oaddress);
child_child_tag = www_tag_new(NULL, buffer);
www_tag_add_child(child_tag, child_child_tag);
}
} else {
snprintf(buffer, sizeof buffer, "From: %s (%s)", from, oaddress);
child_child_tag = www_tag_new(NULL, buffer);
www_tag_add_child(child_tag, child_child_tag);
}
} else {
snprintf(buffer, sizeof buffer, "From: %s", from);
child_child_tag = www_tag_new(NULL, buffer);
www_tag_add_child(child_tag, child_child_tag);
}
child_tag = www_tag_new("div", NULL);
www_tag_add_attrib(child_tag, "class", "msg-view-to");
www_tag_add_child(cur_tag, child_tag);
child_child_tag = www_tag_new(NULL, "To : ");
www_tag_add_child(child_tag, child_child_tag);
child_child_tag = www_tag_new(NULL, to);
www_tag_add_child(child_tag, child_child_tag);
child_tag = www_tag_new("div", NULL);
www_tag_add_attrib(child_tag, "class", "msg-view-date");
www_tag_add_child(cur_tag, child_tag);
date = (time_t)jmh.DateWritten;
gmtime_r(&date, &msg_date);
if (conf.date_style == 1) {
snprintf(buffer, sizeof buffer, "Date: %.2d:%.2d %.2d-%.2d-%.2d",
msg_date.tm_hour, msg_date.tm_min, msg_date.tm_mon + 1, msg_date.tm_mday, msg_date.tm_year - 100);
} else {
snprintf(buffer, sizeof buffer, "Date: %.2d:%.2d %.2d-%.2d-%.2d",
msg_date.tm_hour, msg_date.tm_min, msg_date.tm_mday, msg_date.tm_mon + 1, msg_date.tm_year - 100);
}
child_child_tag = www_tag_new(NULL, buffer);
www_tag_add_child(child_tag, child_child_tag);
child_tag = www_tag_new("div", NULL);
www_tag_add_attrib(child_tag, "class", "msg-view-options");
www_tag_add_child(cur_tag, child_tag);
child_child_tag = www_tag_new("a", NULL);
url = EMPTY_STRALLOC;
stralloc_cats(&url, conf.www_url);
stralloc_cats(&url, "msgs/flag/");
stralloc_cat_long(&url, conference);
stralloc_append1(&url, '/');
stralloc_cat_long(&url, area);
stralloc_append1(&url, '/');
stralloc_cat_long(&url, msg);
stralloc_0(&url);
www_tag_add_attrib(child_child_tag, "href", url.s);
free(url.s);
www_tag_add_child(child_tag, child_child_tag);
child_child_child_tag = www_tag_new("img", NULL);
url = EMPTY_STRALLOC;
stralloc_cats(&url, conf.www_url);
stralloc_cats(&url, "static/flag.png");
stralloc_0(&url);
www_tag_add_attrib(child_child_child_tag, "src", url.s);
free(url.s);
www_tag_add_child(child_child_tag, child_child_child_tag);
cur_tag = www_tag_new("div", NULL);
www_tag_add_attrib(cur_tag, "id", "msgbody");
www_tag_add_child(page, cur_tag);
aha_text = strndup(body, jmh.TxtLen);
aha(aha_text, cur_tag);
free(aha_text);
if (ma->write_sec_level <= user->sec_level && ma->type != TYPE_NETMAIL_AREA) {
cur_tag = www_tag_new("div", NULL);
www_tag_add_attrib(cur_tag, "class", "msg-reply-form");
www_tag_add_child(page, cur_tag);
child_tag = www_tag_new("h3", NULL);
www_tag_add_child(cur_tag, child_tag);
child_child_tag = www_tag_new(NULL, "Reply");
www_tag_add_child(child_tag, child_child_tag);
child_tag = www_tag_new("form", NULL);
url = EMPTY_STRALLOC;
stralloc_cats(&url, conf.www_url);
stralloc_cats(&url, "msgs/");
stralloc_0(&url);
www_tag_add_attrib(child_tag, "action", url.s);
free(url.s);
www_tag_add_attrib(child_tag, "method", "POST");
www_tag_add_attrib(child_tag, "enctype", "application/x-www-form-urlencoded;charset=UTF-8");
www_tag_add_child(cur_tag, child_tag);
child_child_tag = www_tag_new("input", NULL);
www_tag_add_attrib(child_child_tag, "type", "hidden");
www_tag_add_attrib(child_child_tag, "name", "conference");
snprintf(buffer, sizeof buffer, "%d", conference);
www_tag_add_attrib(child_child_tag, "value", buffer);
www_tag_add_child(child_tag, child_child_tag);
child_child_tag = www_tag_new("input", NULL);
www_tag_add_attrib(child_child_tag, "type", "hidden");
www_tag_add_attrib(child_child_tag, "name", "area");
snprintf(buffer, sizeof buffer, "%d", area);
www_tag_add_attrib(child_child_tag, "value", buffer);
www_tag_add_child(child_tag, child_child_tag);
child_child_tag = www_tag_new("input", NULL);
www_tag_add_attrib(child_child_tag, "type", "hidden");
www_tag_add_attrib(child_child_tag, "name", "replyid");
if (msgid == NULL) {
www_tag_add_attrib(child_child_tag, "value", "NULL");
} else {
www_tag_add_attrib(child_child_tag, "value", msgid);
}
www_tag_add_child(child_tag, child_child_tag);
child_child_tag = www_tag_new(NULL, "To : ");
www_tag_add_child(child_tag, child_child_tag);
child_child_tag = www_tag_new("input", NULL);
www_tag_add_attrib(child_child_tag, "type", "text");
www_tag_add_attrib(child_child_tag, "name", "recipient");
www_tag_add_attrib(child_child_tag, "value", from);
www_tag_add_child(child_tag, child_child_tag);
child_child_tag = www_tag_new("br", NULL);
www_tag_add_child(child_tag, child_child_tag);
child_child_tag = www_tag_new(NULL, "Subject : ");
www_tag_add_child(child_tag, child_child_tag);
child_child_tag = www_tag_new("input", NULL);
www_tag_add_attrib(child_child_tag, "type", "text");
www_tag_add_attrib(child_child_tag, "name", "subject");
if (strncasecmp(subject, "re:", 3) != 0) {
snprintf(buffer, sizeof buffer, "RE: %s", subject);
www_tag_add_attrib(child_child_tag, "value", buffer);
} else {
www_tag_add_attrib(child_child_tag, "value", subject);
}
www_tag_add_child(child_tag, child_child_tag);
child_child_tag = www_tag_new("br", NULL);
www_tag_add_child(child_tag, child_child_tag);
child_child_tag = www_tag_new("textarea", NULL);
www_tag_add_attrib(child_child_tag, "name", "body");
www_tag_add_attrib(child_child_tag, "rows", "25");
www_tag_add_attrib(child_child_tag, "cols", "79");
www_tag_add_attrib(child_child_tag, "wrap", "soft");
www_tag_add_attrib(child_child_tag, "id", "replybody");
www_tag_add_child(child_tag, child_child_tag);
stralloc text = EMPTY_STRALLOC;
stralloc_append1(&text, ' ');
stralloc_append1(&text, from[0]);
stralloc_cats(&text, "> ");
replybody = (char *)malloz(strlen(body) + 1);
l2 = 0;
for (l1 = 0; l1 < strlen(body); l1++) {
if (body[l1] == '\e' && body[l1 + 1] == '[') {
while (strchr("ABCDEFGHIGJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", body[l1]) == NULL && l1 < strlen(body))
l1++;
} else {
replybody[l2++] = body[l1];
replybody[l2] = '\0';
}
}
free(body);
chars = 0;
body2 = www_wordwrap(replybody, 69);
if (body2 == NULL) {
body2 = replybody;
} else {
free(replybody);
}
for (i = 0; i < strlen(body2); i++) {
if (body2[i] == '\r') {
snprintf(buffer, sizeof buffer, "\n %c> ", from[0]);
chars = 0;
} else if (chars == 73) {
snprintf(buffer, sizeof buffer, "\n %c> %c", from[0], body2[i]);
chars = 1;
} else {
if (isalnum(body2[i]) || isspace(body2[i]) || ispunct(body2[i])) {
snprintf(buffer, sizeof buffer, "%c", body2[i]);
} else {
snprintf(buffer, sizeof buffer, "?");
}
chars++;
}
stralloc_cats(&text, buffer);
}
free(body2);
stralloc_0(&text);
child_child_child_tag = www_tag_new(NULL, text.s);
www_tag_add_child(child_child_tag, child_child_child_tag);
child_child_tag = www_tag_new("br", NULL);
www_tag_add_child(child_tag, child_child_tag);
child_child_tag = www_tag_new("input", NULL);
www_tag_add_attrib(child_child_tag, "type", "submit");
www_tag_add_attrib(child_child_tag, "name", "submit");
www_tag_add_attrib(child_child_tag, "value", "Reply");
www_tag_add_child(child_tag, child_child_tag);
child_child_tag = www_tag_new("br", NULL);
www_tag_add_child(child_tag, child_child_tag);
}
free(subject);
free(from);
free(to);
free(oaddress);
free(daddress);
free(msgid);
free(replyid);
return www_tag_unwravel(page);
}
static char *www_wordwrap(char *content, int cutoff) {
int len = strlen(content);
int i;
int line_count = 0;
char *last_space = NULL;
char *ret;
int at = 0;
int extra = 0;
int quote_line = 0;
int z;
for (i = 0; i < len; i++) {
if (content[i] == '\n') {
continue;
}
content[at++] = content[i];
}
content[at] = '\0';
at = 0;
len = strlen(content);
for (i = 0; i < len - 1; i++) {
if (content[i] == '>' && line_count < 4) {
quote_line = 1;
}
if (content[i] == '\r' && content[i + 1] != '\r') {
if (content[i + 1] == ' ' && quote_line != 1) {
content[at++] = '\r';
line_count = 0;
quote_line = 0;
} else if (quote_line != 1) {
for (z = i + 1; content[z] != ' ' && z < len; z++)
;
if (at > 0 && content[at - 1] != '\r' && content[at - 1] != ' ' && cutoff - line_count < z - i) {
content[at++] = ' ';
line_count++;
} else {
content[at++] = '\r';
line_count = 0;
quote_line = 0;
}
} else if (quote_line == 1) {
content[at++] = '\r';
line_count = 0;
quote_line = 0;
}
} else if (i < len - 2 && content[i] == '\r' && content[i + 1] == '\r') {
content[at++] = '\r';
content[at++] = '\r';
line_count = 0;
quote_line = 0;
i++;
} else {
content[at++] = content[i];
line_count++;
}
}
content[at++] = content[i];
content[at] = '\0';
at = 0;
len = strlen(content);
ret = (char *)malloz(len + 1);
line_count = 0;
quote_line = 0;
for (i = 0; i < len; i++) {
if (content[i] != '\r') {
ret[at] = content[i];
if (content[i] == ' ') {
last_space = &ret[at];
}
at++;
if (content[i] == '>' && line_count < 4) {
quote_line = 1;
}
} else {
ret[at++] = content[i];
}
ret[at] = '\0';
if (content[i] == '\r') {
line_count = 0;
last_space = NULL;
quote_line = 0;
} else if (line_count == cutoff) {
// wrap
if (quote_line == 1) {
while (i < len - 1 && content[i + 1] != '\r') {
i++;
}
last_space = NULL;
line_count = 0;
quote_line = 0;
} else if (last_space != NULL) {
*last_space = '\r';
line_count = strlen(&last_space[1]);
last_space = NULL;
quote_line = 0;
} else {
extra++;
ret = (char *)realloc(ret, strlen(content) + extra + 1);
if (ret == NULL) {
return NULL;
}
ret[at++] = '\r';
ret[at] = '\0';
last_space = NULL;
line_count = 0;
quote_line = 0;
}
} else {
line_count++;
}
}
return ret;
}
int www_send_msg(struct user_record *user, char *to, char *subj, int conference, int area, char *replyid, char *body) {
s_JamBase *jb;
s_JamMsgHeader jmh;
s_JamSubPacket *jsp;
s_JamSubfield jsf;
s_JamLastRead jlr;
s_JamBaseHeader jbh;
int z;
int sem_fd;
char *page;
int max_len;
int len;
char buffer[256];
char qwkuuid[38];
char *body2;
char *tagline;
struct utsname name;
char *body3;
iconv_t ic;
size_t inc;
size_t ouc;
size_t sz;
char *inbuf, *oubuf;
uuid_t magi_msgid, qwk_msgid;
if (subj == NULL || to == NULL || body == NULL) {
return 0;
}
if (conference < 0 || conference >= ptr_vector_len(&conf.mail_conferences))
return 0;
struct mail_conference *mc = get_conf(conference);
if (area < 0 || area >= ptr_vector_len(&mc->mail_areas))
return 0;
struct mail_area *ma = get_area(conference, area);
if (ma->type == TYPE_LOCAL_AREA && (strcasecmp(to, "all") != 0 && check_user(to) && check_fullname_j(to))) {
return 0;
}
if (ma->write_sec_level <= user->sec_level && ma->type != TYPE_NETMAIL_AREA) {
jb = open_jam_base(ma->path);
if (!jb) {
return 0;
}
JAM_ClearMsgHeader(&jmh);
jmh.DateWritten = (uint32_t)utc_to_local(time(NULL));
jmh.Attribute |= JAM_MSG_LOCAL;
if (mc->realnames == 0) {
strlcpy(buffer, user->loginname, sizeof buffer);
} else {
snprintf(buffer, sizeof buffer, "%s %s", user->firstname, user->lastname);
}
jsp = JAM_NewSubPacket();
jsf.LoID = JAMSFLD_SENDERNAME;
jsf.HiID = 0;
jsf.DatLen = strlen(buffer);
jsf.Buffer = (char *)buffer;
JAM_PutSubfield(jsp, &jsf);
jsf.LoID = JAMSFLD_RECVRNAME;
jsf.HiID = 0;
jsf.DatLen = strlen(to);
jsf.Buffer = (char *)to;
JAM_PutSubfield(jsp, &jsf);
jsf.LoID = JAMSFLD_SUBJECT;
jsf.HiID = 0;
jsf.DatLen = strlen(subj);
jsf.Buffer = (char *)subj;
JAM_PutSubfield(jsp, &jsf);
if (ma->type == TYPE_ECHOMAIL_AREA) {
jmh.Attribute |= JAM_MSG_TYPEECHO;
if (mc->nettype == NETWORK_FIDO) {
if (mc->fidoaddr->point) {
snprintf(buffer, sizeof buffer, "%d:%d/%d.%d",
mc->fidoaddr->zone,
mc->fidoaddr->net,
mc->fidoaddr->node,
mc->fidoaddr->point);
} else {
snprintf(buffer, sizeof buffer, "%d:%d/%d",
mc->fidoaddr->zone,
mc->fidoaddr->net,
mc->fidoaddr->node);
}
jsf.LoID = JAMSFLD_OADDRESS;
jsf.HiID = 0;
jsf.DatLen = strlen(buffer);
jsf.Buffer = (char *)buffer;
JAM_PutSubfield(jsp, &jsf);
snprintf(buffer, sizeof buffer, "%d:%d/%d.%d %08lx",
mc->fidoaddr->zone,
mc->fidoaddr->net,
mc->fidoaddr->node,
mc->fidoaddr->point,
generate_msgid());
jsf.LoID = JAMSFLD_MSGID;
jsf.HiID = 0;
jsf.DatLen = strlen(buffer);
jsf.Buffer = (char *)buffer;
JAM_PutSubfield(jsp, &jsf);
jmh.MsgIdCRC = JAM_Crc32(buffer, strlen(buffer));
if (strcasecmp(replyid, "NULL") != 0) {
jsf.LoID = JAMSFLD_REPLYID;
jsf.HiID = 0;
jsf.DatLen = strlen(replyid);
jsf.Buffer = (char *)replyid;
JAM_PutSubfield(jsp, &jsf);
jmh.ReplyCRC = JAM_Crc32(replyid, strlen(replyid));
}
} else if (mc->nettype == NETWORK_MAGI) {
snprintf(buffer, sizeof buffer, "%d", mc->maginode);
jsf.LoID = JAMSFLD_OADDRESS;
jsf.HiID = 0;
jsf.DatLen = strlen(buffer);
jsf.Buffer = (char *)buffer;
JAM_PutSubfield(jsp, &jsf);
uuid_generate(magi_msgid);
uuid_unparse_lower(magi_msgid, buffer);
jsf.LoID = JAMSFLD_MSGID;
jsf.HiID = 0;
jsf.DatLen = strlen(buffer);
jsf.Buffer = (char *)buffer;
JAM_PutSubfield(jsp, &jsf);
jmh.MsgIdCRC = JAM_Crc32(buffer, strlen(buffer));
if (strcasecmp(replyid, "NULL") != 0) {
jsf.LoID = JAMSFLD_REPLYID;
jsf.HiID = 0;
jsf.DatLen = strlen(replyid);
jsf.Buffer = (char *)replyid;
JAM_PutSubfield(jsp, &jsf);
jmh.ReplyCRC = JAM_Crc32(replyid, strlen(replyid));
}
} else if (mc->nettype == NETWORK_QWK) {
jsf.LoID = JAMSFLD_OADDRESS;
jsf.HiID = 0;
jsf.DatLen = strlen(conf.bwave_name);
jsf.Buffer = (char *)conf.bwave_name;
JAM_PutSubfield(jsp, &jsf);
if (conf.external_address != NULL) {
uuid_generate(qwk_msgid);
uuid_unparse_lower(qwk_msgid, qwkuuid);
snprintf(buffer, sizeof buffer, "<%s@%s>", qwkuuid, conf.external_address);
jsf.LoID = JAMSFLD_MSGID;
jsf.HiID = 0;
jsf.DatLen = strlen(buffer);
jsf.Buffer = (char *)buffer;
JAM_PutSubfield(jsp, &jsf);
if (strcasecmp(replyid, "NULL") != 0) {
jsf.LoID = JAMSFLD_REPLYID;
jsf.HiID = 0;
jsf.DatLen = strlen(replyid);
jsf.Buffer = (char *)replyid;
JAM_PutSubfield(jsp, &jsf);
jmh.ReplyCRC = JAM_Crc32(replyid, strlen(replyid));
}
}
}
}
while (1) {
z = JAM_LockMB(jb, 100);
if (z == 0) {
break;
} else if (z == JAM_LOCK_FAILED) {
sleep(1);
} else {
JAM_CloseMB(jb);
free(jb);
return 0;
}
}
if (z != 0) {
JAM_CloseMB(jb);
free(jb);
return 0;
}
tagline = conf.default_tagline;
if (mc->tagline != NULL) {
tagline = mc->tagline;
}
uname(&name);
if (mc->nettype == NETWORK_FIDO) {
if (mc->fidoaddr->point == 0) {
snprintf(buffer, sizeof buffer, "\r\r--- MagickaBBS v%d.%d%s (%s/%s)\r * Origin: %s (%d:%d/%d)\r",
VERSION_MAJOR, VERSION_MINOR, VERSION_STR, name.sysname, name.machine, tagline,
mc->fidoaddr->zone, mc->fidoaddr->net, mc->fidoaddr->node);
} else {
snprintf(buffer, sizeof buffer, "\r\r--- MagickaBBS v%d.%d%s (%s/%s)\r * Origin: %s (%d:%d/%d.%d)\r",
VERSION_MAJOR, VERSION_MINOR, VERSION_STR, name.sysname, name.machine, tagline,
mc->fidoaddr->zone, mc->fidoaddr->net, mc->fidoaddr->node, mc->fidoaddr->point);
}
} else if (mc->nettype == NETWORK_MAGI) {
snprintf(buffer, sizeof buffer, "\r\r--- MagickaBBS v%d.%d%s (%s/%s)\r * Origin: %s (@%d)\r",
VERSION_MAJOR, VERSION_MINOR, VERSION_STR, name.sysname, name.machine, tagline, mc->maginode);
} else if (mc->nettype == NETWORK_QWK) {
snprintf(buffer, sizeof buffer, "\r\r---\r * MagickaBBS * %s\r",
tagline);
} else {
snprintf(buffer, sizeof buffer, "\r");
}
char *p = body;
stralloc unhtmlized = EMPTY_STRALLOC;
// remove nbsp
while (*p != '\0') {
if ((*p & 0xff) == 0xc2 && (*(p + 1) & 0xff) == 0xa0) {
stralloc_append1(&unhtmlized, ' ');
p++;
} else {
stralloc_append1(&unhtmlized, *p);
}
p++;
}
stralloc_0(&unhtmlized);
body2 = www_wordwrap(unhtmlized.s, 73);
free(unhtmlized.s);
if (body2 == NULL) {
JAM_UnlockMB(jb);
JAM_DelSubPacket(jsp);
JAM_CloseMB(jb);
free(jb);
return 0;
}
body3 = str2dup(body2, buffer);
if (body3 == NULL) {
free(body2);
JAM_UnlockMB(jb);
JAM_DelSubPacket(jsp);
JAM_CloseMB(jb);
free(jb);
return 0;
}
free(body2);
body2 = (char *)malloz(strlen(body3) + 1);
ic = iconv_open("CP437", "UTF-8");
inc = strlen(body3);
ouc = strlen(body3);
inbuf = body3;
oubuf = body2;
sz = iconv(ic, &inbuf, &inc, &oubuf, &ouc);
free(body3);
if (JAM_AddMessage(jb, &jmh, jsp, (char *)body2, oubuf - body2)) {
free(body2);
JAM_UnlockMB(jb);
JAM_DelSubPacket(jsp);
JAM_CloseMB(jb);
free(jb);
iconv_close(ic);
return 0;
} else {
if (ma->type == TYPE_ECHOMAIL_AREA) {
if (conf.echomail_sem != NULL) {
sem_fd = open(conf.echomail_sem, O_RDWR | O_CREAT, S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH);
close(sem_fd);
}
}
}
free(body2);
JAM_UnlockMB(jb);
JAM_DelSubPacket(jsp);
JAM_CloseMB(jb);
free(jb);
iconv_close(ic);
return 1;
}
return 0;
}
char *www_new_msg(struct user_record *user, int conference, int area) {
struct www_tag *page = www_tag_new(NULL, "");
struct www_tag *cur_tag;
struct www_tag *child_tag;
struct www_tag *child_child_tag;
char buffer[10];
cur_tag = www_tag_new("div", NULL);
www_tag_add_attrib(cur_tag, "class", "content-header");
www_tag_add_child(page, cur_tag);
child_tag = www_tag_new("h2", NULL);
www_tag_add_child(cur_tag, child_tag);
child_child_tag = www_tag_new(NULL, "New Message");
www_tag_add_child(child_tag, child_child_tag);
cur_tag = www_tag_new("form", NULL);
stralloc url = EMPTY_STRALLOC;
stralloc_cats(&url, conf.www_url);
stralloc_cats(&url, "msgs/");
stralloc_0(&url);
www_tag_add_attrib(cur_tag, "action", url.s);
free(url.s);
www_tag_add_attrib(cur_tag, "method", "POST");
www_tag_add_attrib(cur_tag, "onsubmit", "return validate()");
www_tag_add_attrib(cur_tag, "enctype", "application/x-www-form-urlencoded;charset=UTF-8");
www_tag_add_child(page, cur_tag);
child_tag = www_tag_new("input", NULL);
www_tag_add_attrib(child_tag, "type", "hidden");
www_tag_add_attrib(child_tag, "name", "conference");
snprintf(buffer, sizeof buffer, "%d", conference);
www_tag_add_attrib(child_tag, "value", buffer);
www_tag_add_child(cur_tag, child_tag);
child_tag = www_tag_new("input", NULL);
www_tag_add_attrib(child_tag, "type", "hidden");
www_tag_add_attrib(child_tag, "name", "area");
snprintf(buffer, sizeof buffer, "%d", area);
www_tag_add_attrib(child_tag, "value", buffer);
www_tag_add_child(cur_tag, child_tag);
child_tag = www_tag_new("input", NULL);
www_tag_add_attrib(child_tag, "type", "hidden");
www_tag_add_attrib(child_tag, "name", "replyid");
www_tag_add_attrib(child_tag, "value", "NULL");
www_tag_add_child(cur_tag, child_tag);
child_tag = www_tag_new(NULL, "To : ");
www_tag_add_child(cur_tag, child_tag);
child_tag = www_tag_new("input", NULL);
www_tag_add_attrib(child_tag, "type", "text");
www_tag_add_attrib(child_tag, "name", "recipient");
www_tag_add_attrib(child_tag, "value", "All");
www_tag_add_attrib(child_tag, "id", "recipient");
www_tag_add_child(cur_tag, child_tag);
child_tag = www_tag_new("br", NULL);
www_tag_add_child(cur_tag, child_tag);
child_tag = www_tag_new(NULL, "Subject : ");
www_tag_add_child(cur_tag, child_tag);
child_tag = www_tag_new("input", NULL);
www_tag_add_attrib(child_tag, "type", "text");
www_tag_add_attrib(child_tag, "name", "subject");
www_tag_add_attrib(child_tag, "id", "subject");
www_tag_add_child(cur_tag, child_tag);
child_tag = www_tag_new("br", NULL);
www_tag_add_child(cur_tag, child_tag);
child_tag = www_tag_new("textarea", NULL);
www_tag_add_attrib(child_tag, "name", "body");
www_tag_add_attrib(child_tag, "id", "body");
www_tag_add_attrib(child_tag, "rows", "25");
www_tag_add_attrib(child_tag, "cols", "79");
www_tag_add_attrib(child_tag, "wrap", "soft");
www_tag_add_child(cur_tag, child_tag);
child_child_tag = www_tag_new(NULL, "");
www_tag_add_child(child_tag, child_child_tag);
child_tag = www_tag_new("br", NULL);
www_tag_add_child(cur_tag, child_tag);
child_tag = www_tag_new("input", NULL);
www_tag_add_attrib(child_tag, "type", "submit");
www_tag_add_attrib(child_tag, "name", "submit");
www_tag_add_attrib(child_tag, "value", "Send");
www_tag_add_child(cur_tag, child_tag);
child_tag = www_tag_new("br", NULL);
www_tag_add_child(cur_tag, child_tag);
return www_tag_unwravel(page);
}
#endif