Add viewing of email to WWW

This commit is contained in:
Andrew Pamment 2016-08-19 12:32:04 +10:00
parent a42963a77d
commit 5d47ca787f
5 changed files with 297 additions and 6 deletions

19
Makefile.linux.WWW Normal file
View File

@ -0,0 +1,19 @@
CC=cc
CFLAGS=-I/usr/local/include -DENABLE_WWW=1
DEPS = bbs.h
JAMLIB = jamlib/jamlib.a
ZMODEM = Xmodem/libzmodem.a
LUA = lua/liblua.a
MICROHTTPD=-lmicrohttpd -lb64
OBJ = inih/ini.o bbs.o main.o users.o main_menu.o mail_menu.o doors.o bbs_list.o chat_system.o email.o files.o settings.o lua_glue.o strings.o www.o www_email.o
%.o: %.c $(DEPS)
$(CC) -c -o $@ $< $(CFLAGS)
magicka: $(OBJ)
$(CC) -o magicka -o $@ $^ $(CFLAGS) -L/usr/local/lib -lsqlite3 $(JAMLIB) $(ZMODEM) $(LUA) -lutil -lm -ldl -lssl -lcrypto -lssh $(MICROHTTPD)
.PHONY: clean
clean:
rm -f $(OBJ) magicka

1
bbs.h
View File

@ -206,6 +206,7 @@ extern void chomp(char *string);
extern void www_init(); extern void www_init();
extern int www_handler(void * cls, struct MHD_Connection * connection, const char * url, const char * method, const char * version, const char * upload_data, size_t * upload_data_size, void ** ptr); extern int www_handler(void * cls, struct MHD_Connection * connection, const char * url, const char * method, const char * version, const char * upload_data, size_t * upload_data_size, void ** ptr);
extern char *www_email_summary(struct user_record *user); extern char *www_email_summary(struct user_record *user);
extern char *www_email_display(struct user_record *user, int email);
#endif #endif
#endif #endif

24
www.c
View File

@ -385,7 +385,7 @@ int www_handler(void * cls, struct MHD_Connection * connection, const char * url
whole_page = (char *)malloc(strlen(header) + strlen(page) + strlen(footer) + 1); whole_page = (char *)malloc(strlen(header) + strlen(page) + strlen(footer) + 1);
sprintf(whole_page, "%s%s%s", header, page, footer); sprintf(whole_page, "%s%s%s", header, page, footer);
} else if (strcasecmp(url, "/email/") == 0 || strncasecmp(url, "/email", 6) == 0) { } else if (strcasecmp(url, "/email/") == 0 || strcasecmp(url, "/email") == 0) {
user = www_auth_ok(connection, url_); user = www_auth_ok(connection, url_);
if (user == NULL) { if (user == NULL) {
@ -402,6 +402,24 @@ int www_handler(void * cls, struct MHD_Connection * connection, const char * url
} }
whole_page = (char *)malloc(strlen(header) + strlen(page) + strlen(footer) + 1); whole_page = (char *)malloc(strlen(header) + strlen(page) + strlen(footer) + 1);
sprintf(whole_page, "%s%s%s", header, page, footer);
} else if (strncasecmp(url, "/email/", 7) == 0) {
user = www_auth_ok(connection, url_);
if (user == NULL) {
www_401(header, footer, connection);
free(header);
free(footer);
return MHD_YES;
}
page = www_email_display(user, atoi(&url[7]));
if (page == NULL) {
free(header);
free(footer);
return MHD_NO;
}
whole_page = (char *)malloc(strlen(header) + strlen(page) + strlen(footer) + 1);
sprintf(whole_page, "%s%s%s", header, page, footer); sprintf(whole_page, "%s%s%s", header, page, footer);
} else if (strncasecmp(url, "/static/", 8) == 0) { } else if (strncasecmp(url, "/static/", 8) == 0) {
// sanatize path // sanatize path
@ -464,12 +482,12 @@ int www_handler(void * cls, struct MHD_Connection * connection, const char * url
free(footer); free(footer);
return MHD_NO; return MHD_NO;
} }
response = MHD_create_response_from_buffer (strlen (whole_page), (void*) whole_page, MHD_RESPMEM_PERSISTENT); response = MHD_create_response_from_buffer (strlen (whole_page), (void*) whole_page, MHD_RESPMEM_PERSISTENT);
MHD_add_response_header(response, MHD_HTTP_HEADER_CONTENT_TYPE, "text/html");
ret = MHD_queue_response (connection, MHD_HTTP_OK, response); ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
MHD_destroy_response (response); MHD_destroy_response (response);
free(whole_page);
free(page); free(page);
free(header); free(header);
free(footer); free(footer);

View File

@ -1,3 +1,68 @@
.footer { .footer {
font-size: small; font-size: small;
} }
.div-table {
display: table;
width: auto;
background-color: #eee;
border: 1px solid #666666;
border-spacing: 5px;
}
.email-summary-seen {
display: table-row;
width: auto;
clear: both;
background-color: #eee;
}
.email-summary {
display: table-row;
width: auto;
clear: both;
background-color: #b2ffb0;
}
.email-id {
float: left;
display: table-column;
width: 100px;
}
.email-subject {
float: left;
display: table-column;
width: 400px;
}
.email-from {
float: left;
display: table-column;
width: 200px;
}
.email-date {
float: left;
display: table-column;
width: 200px;
}
.email-view-header {
background-color: #eee;
border: 1px solid #666666;
padding: 5px;
margin-bottom: 10px;
}
.email-view-subject {
font-size: xx-large;
}
.email-view-from {
color: #222;
}
.email-view-date {
color: #222;
}

View File

@ -10,6 +10,173 @@
extern struct bbs_config conf; extern struct bbs_config conf;
char *www_email_display(struct user_record *user, int email) {
char *page;
int max_len;
int len;
char buffer[4096];
sqlite3 *db;
sqlite3_stmt *res;
int rc;
struct tm msg_date;
time_t date;
char *from;
char *subject;
char *body;
int id;
int i;
char *err_msg = 0;
char *email_create_sql = "CREATE TABLE IF NOT EXISTS email ("
"id INTEGER PRIMARY KEY,"
"sender TEXT COLLATE NOCASE,"
"recipient TEXT COLLATE NOCASE,"
"subject TEXT,"
"body TEXT,"
"date INTEGER,"
"seen INTEGER);";
char *email_show_sql = "SELECT id,sender,subject,body,date FROM email WHERE recipient LIKE ? LIMIT ?, 1";
char *update_seen_sql = "UPDATE email SET seen=1 WHERE id=?";
page = (char *)malloc(4096);
max_len = 4096;
len = 0;
memset(page, 0, 4096);
sprintf(buffer, "%s/email.sq3", conf.bbs_path);
rc = sqlite3_open(buffer, &db);
if (rc != SQLITE_OK) {
sqlite3_close(db);
free(page);
return NULL;
}
rc = sqlite3_exec(db, email_create_sql, 0, 0, &err_msg);
if (rc != SQLITE_OK ) {
sqlite3_free(err_msg);
sqlite3_close(db);
return NULL;
}
rc = sqlite3_prepare_v2(db, email_show_sql, -1, &res, 0);
if (rc == SQLITE_OK) {
sqlite3_bind_text(res, 1, user->loginname, -1, 0);
sqlite3_bind_int(res, 2, email - 1);
} else {
sqlite3_finalize(res);
sqlite3_close(db);
free(page);
return NULL;
}
if (sqlite3_step(res) == SQLITE_ROW) {
id = sqlite3_column_int(res, 0);
from = strdup((char *)sqlite3_column_text(res, 1));
subject = strdup((char *)sqlite3_column_text(res, 2));
body = strdup((char *)sqlite3_column_text(res, 3));
date = (time_t)sqlite3_column_int(res, 4);
localtime_r(&date, &msg_date);
sprintf(buffer, "<div class=\"content-header\"><h2>Your Email</h2></div>\n");
if (len + strlen(buffer) > max_len - 1) {
max_len += 4096;
page = (char *)realloc(page, max_len);
}
strcat(page, buffer);
len += strlen(buffer);
sprintf(buffer, "<div class=\"email-view-header\">\n");
if (len + strlen(buffer) > max_len - 1) {
max_len += 4096;
page = (char *)realloc(page, max_len);
}
strcat(page, buffer);
len += strlen(buffer);
sprintf(buffer, "<div class=\"email-view-subject\">%s</div>\n", subject);
if (len + strlen(buffer) > max_len - 1) {
max_len += 4096;
page = (char *)realloc(page, max_len);
}
strcat(page, buffer);
len += strlen(buffer);
sprintf(buffer, "<div class=\"email-view-from\">From: %s</div>\n", from);
if (len + strlen(buffer) > max_len - 1) {
max_len += 4096;
page = (char *)realloc(page, max_len);
}
strcat(page, buffer);
len += strlen(buffer);
sprintf(buffer, "<div class=\"email-view-date\">Date: %.2d:%.2d %.2d-%.2d-%.2d</div>\n", msg_date.tm_hour, msg_date.tm_min, msg_date.tm_mday, msg_date.tm_mon + 1, msg_date.tm_year - 100);
if (len + strlen(buffer) > max_len - 1) {
max_len += 4096;
page = (char *)realloc(page, max_len);
}
strcat(page, buffer);
len += strlen(buffer);
sprintf(buffer, "</div>\n");
if (len + strlen(buffer) > max_len - 1) {
max_len += 4096;
page = (char *)realloc(page, max_len);
}
strcat(page, buffer);
len += strlen(buffer);
for (i=0;i<strlen(body);i++) {
if (body[i] == '\r') {
sprintf(buffer, "<br />");
} else {
sprintf(buffer, "%c", body[i]);
}
if (len + strlen(buffer) > max_len - 1) {
max_len += 4096;
page = (char *)realloc(page, max_len);
}
strcat(page, buffer);
len += strlen(buffer);
}
free(from);
free(body);
free(subject);
sqlite3_finalize(res);
rc = sqlite3_prepare_v2(db, update_seen_sql, -1, &res, 0);
if (rc == SQLITE_OK) {
sqlite3_bind_int(res, 1, id);
} else {
sqlite3_finalize(res);
sqlite3_close(db);
free(page);
return NULL;
}
sqlite3_step(res);
} else {
sprintf(buffer, "<div class=\"content-header\"><h2>No Such Email</h2></div>\n");
if (len + strlen(buffer) > max_len - 1) {
max_len += 4096;
page = (char *)realloc(page, max_len);
}
strcat(page, buffer);
len += strlen(buffer);
}
sqlite3_finalize(res);
sqlite3_close(db);
return page;
}
char *www_email_summary(struct user_record *user) { char *www_email_summary(struct user_record *user) {
char *page; char *page;
int max_len; int max_len;
@ -47,6 +214,7 @@ char *www_email_summary(struct user_record *user) {
page = (char *)realloc(page, max_len); page = (char *)realloc(page, max_len);
} }
strcat(page, buffer); strcat(page, buffer);
len += strlen(buffer);
sprintf(buffer, "%s/email.sq3", conf.bbs_path); sprintf(buffer, "%s/email.sq3", conf.bbs_path);
rc = sqlite3_open(buffer, &db); rc = sqlite3_open(buffer, &db);
@ -75,6 +243,14 @@ char *www_email_summary(struct user_record *user) {
return NULL; return NULL;
} }
sprintf(buffer, "<div class=\"div-table\">\n");
if (len + strlen(buffer) > max_len - 1) {
max_len += 4096;
page = (char *)realloc(page, max_len);
}
strcat(page, buffer);
len += strlen(buffer);
while (sqlite3_step(res) == SQLITE_ROW) { while (sqlite3_step(res) == SQLITE_ROW) {
from = strdup((char *)sqlite3_column_text(res, 0)); from = strdup((char *)sqlite3_column_text(res, 0));
subject = strdup((char *)sqlite3_column_text(res, 1)); subject = strdup((char *)sqlite3_column_text(res, 1));
@ -82,23 +258,35 @@ char *www_email_summary(struct user_record *user) {
date = (time_t)sqlite3_column_int(res, 3); date = (time_t)sqlite3_column_int(res, 3);
localtime_r(&date, &msg_date); localtime_r(&date, &msg_date);
if (seen == 0) { if (seen == 0) {
sprintf(buffer, "<div class=\"email-summary\"><div class=\"email-id\">%d</div><div class=\"email-subject\"><a href=\"/email/%d\">%s</a></div><div class=\"email-from\">%s</div><div class=\"email-date\">%d:%2d %d-%d-%d</div></div>\n", msgid + 1, msgid + 1, subject, from, msg_date.tm_hour, msg_date.tm_min, msg_date.tm_mday, msg_date.tm_mon + 1, msg_date.tm_year - 100); sprintf(buffer, "<div class=\"email-summary\"><div class=\"email-id\">%d</div><div class=\"email-subject\"><a href=\"/email/%d\">%s</a></div><div class=\"email-from\">%s</div><div class=\"email-date\">%.2d:%.2d %.2d-%.2d-%.2d</div></div>\n", msgid + 1, msgid + 1, subject, from, msg_date.tm_hour, msg_date.tm_min, msg_date.tm_mday, msg_date.tm_mon + 1, msg_date.tm_year - 100);
if (len + strlen(buffer) > max_len - 1) { if (len + strlen(buffer) > max_len - 1) {
max_len += 4096; max_len += 4096;
page = (char *)realloc(page, max_len); page = (char *)realloc(page, max_len);
} }
strcat(page, buffer); strcat(page, buffer);
len += strlen(buffer);
} else { } else {
sprintf(buffer, "<div class=\"email-summary-seen\"><div class=\"email-id\">%d</div><div class=\"email-subject\"><a href=\"/email/%d\">%s</a></div><div class=\"email-from\">%s</div><div class=\"email-date\">%d:%2d %d-%d-%d</div></div>\n", msgid + 1, msgid + 1, subject, from, msg_date.tm_hour, msg_date.tm_min, msg_date.tm_mday, msg_date.tm_mon + 1, msg_date.tm_year - 100); sprintf(buffer, "<div class=\"email-summary-seen\"><div class=\"email-id\">%d</div><div class=\"email-subject\"><a href=\"/email/%d\">%s</a></div><div class=\"email-from\">%s</div><div class=\"email-date\">%.2d:%.2d %.2d-%.2d-%.2d</div></div>\n", msgid + 1, msgid + 1, subject, from, msg_date.tm_hour, msg_date.tm_min, msg_date.tm_mday, msg_date.tm_mon + 1, msg_date.tm_year - 100);
if (len + strlen(buffer) > max_len - 1) { if (len + strlen(buffer) > max_len - 1) {
max_len += 4096; max_len += 4096;
page = (char *)realloc(page, max_len); page = (char *)realloc(page, max_len);
} }
strcat(page, buffer); strcat(page, buffer);
len += strlen(buffer);
} }
free(from); free(from);
free(subject); free(subject);
msgid++;
} }
sprintf(buffer, "</div>\n");
if (len + strlen(buffer) > max_len - 1) {
max_len += 4096;
page = (char *)realloc(page, max_len);
}
strcat(page, buffer);
len += strlen(buffer);
sqlite3_finalize(res); sqlite3_finalize(res);
sqlite3_close(db); sqlite3_close(db);
return page; return page;