diff --git a/bbs.h b/bbs.h index adb9eea..7850afc 100644 --- a/bbs.h +++ b/bbs.h @@ -210,6 +210,7 @@ extern char *www_email_summary(struct user_record *user); extern char *www_email_display(struct user_record *user, int email); extern int www_send_email(struct user_record *user, char *recipient, char *subject, char *body); extern char *www_new_email(); +extern int www_email_delete(struct user_record *user, int id); #endif #endif diff --git a/www.c b/www.c index 48d8fb5..8787df2 100644 --- a/www.c +++ b/www.c @@ -50,20 +50,20 @@ void www_request_completed(void *cls, struct MHD_Connection *connection, void ** free(con_info->values); free(con_info->keys); } - - if (con_info->user != NULL) { - free(con_info->user->loginname); - free(con_info->user->password); - free(con_info->user->firstname); - free(con_info->user->lastname); - free(con_info->user->email); - free(con_info->user->location); - free(con_info->user->sec_info); - free(con_info->user); - } - + MHD_destroy_post_processor(con_info->pp); } + if (con_info->user != NULL) { + free(con_info->user->loginname); + free(con_info->user->password); + free(con_info->user->firstname); + free(con_info->user->lastname); + free(con_info->user->email); + free(con_info->user->location); + free(con_info->user->sec_info); + free(con_info->user); + } + free(con_info->url); free(con_info); } @@ -367,7 +367,6 @@ int www_handler(void * cls, struct MHD_Connection * connection, const char * url int i; int fno; const char *url_ = url; - struct user_record *user; char *subj, *to, *body; struct connection_info_s *con_inf; @@ -499,15 +498,15 @@ int www_handler(void * cls, struct MHD_Connection * connection, const char * url sprintf(whole_page, "%s%s%s", header, page, footer); } else if (strcasecmp(url, "/email/") == 0 || strcasecmp(url, "/email") == 0) { - user = www_auth_ok(connection, url_); + con_inf->user = www_auth_ok(connection, url_); - if (user == NULL) { + if (con_inf->user == NULL) { www_401(header, footer, connection); free(header); free(footer); return MHD_YES; } - page = www_email_summary(user); + page = www_email_summary(con_inf->user); if (page == NULL) { free(header); free(footer); @@ -517,9 +516,9 @@ int www_handler(void * cls, struct MHD_Connection * connection, const char * url sprintf(whole_page, "%s%s%s", header, page, footer); } else if(strcasecmp(url, "/email/new") == 0) { - user = www_auth_ok(connection, url_); + con_inf->user = www_auth_ok(connection, url_); - if (user == NULL) { + if (con_inf->user == NULL) { www_401(header, footer, connection); free(header); free(footer); @@ -534,16 +533,51 @@ 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 (strncasecmp(url, "/email/", 7) == 0) { - user = www_auth_ok(connection, url_); + } else if (strncasecmp(url, "/email/delete/", 14) == 0) { + con_inf->user = www_auth_ok(connection, url_); - if (user == NULL) { + if (con_inf->user == NULL) { www_401(header, footer, connection); free(header); free(footer); return MHD_YES; } - page = www_email_display(user, atoi(&url[7])); + + if (!www_email_delete(con_inf->user, atoi(&url[14]))) { + page = (char *)malloc(31); + if (page == NULL) { + free(header); + free(footer); + return MHD_NO; + } + sprintf(page, "

Error Deleting Email.

"); + } else { + page = (char *)malloc(24); + if (page == NULL) { + free(header); + free(footer); + return MHD_NO; + } + sprintf(page, "

Email Deleted!

"); + } + 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, "/email/", 7) == 0) { + con_inf->user = www_auth_ok(connection, url_); + + if (con_inf->user == NULL) { + www_401(header, footer, connection); + free(header); + free(footer); + return MHD_YES; + } + page = www_email_display(con_inf->user, atoi(&url[7])); if (page == NULL) { free(header); free(footer); @@ -611,9 +645,8 @@ int www_handler(void * cls, struct MHD_Connection * connection, const char * url } else if (strcmp(method, "POST") == 0) { if (strcasecmp(url, "/email/") == 0 || strcasecmp(url, "/email") == 0) { con_inf->user = www_auth_ok(connection, url_); - user = www_auth_ok(connection, url_); - - if (user == NULL) { + + if (con_inf->user == NULL) { www_401(header, footer, connection); free(header); free(footer); diff --git a/www/static/delete.png b/www/static/delete.png new file mode 100644 index 0000000..f9643da Binary files /dev/null and b/www/static/delete.png differ diff --git a/www/static/style.css b/www/static/style.css index 25ff4ae..6cd60f3 100644 --- a/www/static/style.css +++ b/www/static/style.css @@ -48,6 +48,12 @@ width: 200px; } +.email-delete { + float: left; + display: table-column; + width: 30px; +} + .email-view-header { background-color: #eee; border: 1px solid #666666; diff --git a/www_email.c b/www_email.c index f5dc0aa..9e324ac 100644 --- a/www_email.c +++ b/www_email.c @@ -10,6 +10,57 @@ extern struct bbs_config conf; +int www_email_delete(struct user_record *user, int id) { + char buffer[256]; + sqlite3 *db; + sqlite3_stmt *res; + int rc; + char *csql = "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 *dsql = "DELETE FROM email WHERE id=? AND recipient LIKE ?"; + char *err_msg = 0; + + sprintf(buffer, "%s/email.sq3", conf.bbs_path); + + rc = sqlite3_open(buffer, &db); + if (rc != SQLITE_OK) { + sqlite3_close(db); + + return 0; + } + + + rc = sqlite3_exec(db, csql, 0, 0, &err_msg); + if (rc != SQLITE_OK ) { + sqlite3_free(err_msg); + sqlite3_close(db); + + return 0; + } + + rc = sqlite3_prepare_v2(db, dsql, -1, &res, 0); + + if (rc == SQLITE_OK) { + sqlite3_bind_int(res, 1, id); + sqlite3_bind_text(res, 2, user->loginname, -1, 0); + } else { + sqlite3_finalize(res); + sqlite3_close(db); + return 0; + } + sqlite3_step(res); + + sqlite3_finalize(res); + sqlite3_close(db); + return 1; +} + int www_send_email(struct user_record *user, char *recipient, char *subject, char *ibody) { char buffer[256]; sqlite3 *db; @@ -406,12 +457,13 @@ char *www_email_summary(struct user_record *user) { sqlite3 *db; sqlite3_stmt *res; int rc; - char *email_summary_sql = "SELECT sender,subject,seen,date FROM email WHERE recipient LIKE ?"; + char *email_summary_sql = "SELECT id,sender,subject,seen,date FROM email WHERE recipient LIKE ?"; struct tm msg_date; time_t date; char *from; char *subject; int seen; + int id; int msgid = 0; char *err_msg = 0; char *email_create_sql = "CREATE TABLE IF NOT EXISTS email (" @@ -482,13 +534,14 @@ char *www_email_summary(struct user_record *user) { 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)); - seen = sqlite3_column_int(res, 2); - date = (time_t)sqlite3_column_int(res, 3); + id = sqlite3_column_int(res, 0); + from = strdup((char *)sqlite3_column_text(res, 1)); + subject = strdup((char *)sqlite3_column_text(res, 2)); + seen = sqlite3_column_int(res, 3); + date = (time_t)sqlite3_column_int(res, 4); localtime_r(&date, &msg_date); if (seen == 0) { - sprintf(buffer, "
%d
%s
%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); + sprintf(buffer, "
%d
%s
%s
%.2d:%.2d %.2d-%.2d-%.2d
\"delete\"
\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, id); if (len + strlen(buffer) > max_len - 1) { max_len += 4096; page = (char *)realloc(page, max_len); @@ -496,7 +549,7 @@ char *www_email_summary(struct user_record *user) { strcat(page, buffer); len += strlen(buffer); } else { - sprintf(buffer, "
%d
%s
%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); + sprintf(buffer, "
%d
%s
%s
%.2d:%.2d %.2d-%.2d-%.2d
\"delete\"
\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, id); if (len + strlen(buffer) > max_len - 1) { max_len += 4096; page = (char *)realloc(page, max_len);