From 5d47ca787f7e57fc0f7ff1bcd77190e74ca8dcc2 Mon Sep 17 00:00:00 2001 From: Andrew Pamment Date: Fri, 19 Aug 2016 12:32:04 +1000 Subject: [PATCH] Add viewing of email to WWW --- Makefile.linux.WWW | 19 +++++ bbs.h | 1 + www.c | 26 +++++- www/static/style.css | 65 +++++++++++++++ www_email.c | 192 ++++++++++++++++++++++++++++++++++++++++++- 5 files changed, 297 insertions(+), 6 deletions(-) create mode 100644 Makefile.linux.WWW diff --git a/Makefile.linux.WWW b/Makefile.linux.WWW new file mode 100644 index 0000000..b06fc84 --- /dev/null +++ b/Makefile.linux.WWW @@ -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 diff --git a/bbs.h b/bbs.h index 52e2205..1a471bf 100644 --- a/bbs.h +++ b/bbs.h @@ -206,6 +206,7 @@ extern void chomp(char *string); 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 char *www_email_summary(struct user_record *user); +extern char *www_email_display(struct user_record *user, int email); #endif #endif diff --git a/www.c b/www.c index e9ae4e1..c58af22 100644 --- a/www.c +++ b/www.c @@ -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); 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_); if (user == NULL) { @@ -401,8 +401,26 @@ int www_handler(void * cls, struct MHD_Connection * connection, const char * url return MHD_NO; } 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); } else if (strncasecmp(url, "/static/", 8) == 0) { // sanatize path if (strstr(url, "/..") != NULL) { @@ -464,12 +482,12 @@ int www_handler(void * cls, struct MHD_Connection * connection, const char * url free(footer); return MHD_NO; } - 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); MHD_destroy_response (response); - free(whole_page); free(page); free(header); free(footer); diff --git a/www/static/style.css b/www/static/style.css index 4dabd27..deab0e8 100644 --- a/www/static/style.css +++ b/www/static/style.css @@ -1,3 +1,68 @@ .footer { 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; +} diff --git a/www_email.c b/www_email.c index d47e843..782737f 100644 --- a/www_email.c +++ b/www_email.c @@ -10,6 +10,173 @@ 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, "

Your Email

\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, "
\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, "
%s
\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, "
From: %s
\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, "
Date: %.2d:%.2d %.2d-%.2d-%.2d
\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, "
\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"); + } 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, "

No Such Email

\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 *page; int max_len; @@ -47,6 +214,7 @@ char *www_email_summary(struct user_record *user) { page = (char *)realloc(page, max_len); } strcat(page, buffer); + len += strlen(buffer); sprintf(buffer, "%s/email.sq3", conf.bbs_path); rc = sqlite3_open(buffer, &db); @@ -75,6 +243,14 @@ char *www_email_summary(struct user_record *user) { return NULL; } + sprintf(buffer, "
\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) { from = strdup((char *)sqlite3_column_text(res, 0)); 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); localtime_r(&date, &msg_date); if (seen == 0) { - sprintf(buffer, "
%d
%s
%d:%2d %d-%d-%d
\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, "
%d
%s
%.2d:%.2d %.2d-%.2d-%.2d
\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) { max_len += 4096; page = (char *)realloc(page, max_len); } strcat(page, buffer); + len += strlen(buffer); } else { - sprintf(buffer, "
%d
%s
%d:%2d %d-%d-%d
\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, "
%d
%s
%.2d:%.2d %.2d-%.2d-%.2d
\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) { max_len += 4096; page = (char *)realloc(page, max_len); } strcat(page, buffer); + len += strlen(buffer); } free(from); free(subject); + msgid++; } + sprintf(buffer, "
\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;