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, "