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/www_email.c

296 lines
7.5 KiB
C
Raw Normal View History

2016-08-17 09:26:01 +00:00
#if defined(ENABLE_WWW)
#include <string.h>
#include <sqlite3.h>
#include <time.h>
#include <stdlib.h>
#include "bbs.h"
extern struct bbs_config conf;
2016-08-19 02:32:04 +00:00
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;
}
2016-08-17 09:26:01 +00:00
char *www_email_summary(struct user_record *user) {
char *page;
int max_len;
int len;
char buffer[4096];
sqlite3 *db;
sqlite3_stmt *res;
int rc;
char *email_summary_sql = "SELECT sender,subject,seen,date FROM email WHERE recipient LIKE ?";
struct tm msg_date;
time_t date;
char *from;
char *subject;
int seen;
int msgid = 0;
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);";
page = (char *)malloc(4096);
max_len = 4096;
len = 0;
memset(page, 0, 4096);
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);
2016-08-19 02:32:04 +00:00
len += strlen(buffer);
2016-08-17 09:26:01 +00:00
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_summary_sql, -1, &res, 0);
if (rc == SQLITE_OK) {
sqlite3_bind_text(res, 1, user->loginname, -1, 0);
} else {
sqlite3_finalize(res);
sqlite3_close(db);
free(page);
return NULL;
}
2016-08-19 02:32:04 +00:00
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);
2016-08-17 09:26:01 +00:00
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);
localtime_r(&date, &msg_date);
if (seen == 0) {
2016-08-19 02:32:04 +00:00
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);
2016-08-17 09:26:01 +00:00
if (len + strlen(buffer) > max_len - 1) {
max_len += 4096;
page = (char *)realloc(page, max_len);
}
strcat(page, buffer);
2016-08-19 02:32:04 +00:00
len += strlen(buffer);
2016-08-17 09:26:01 +00:00
} else {
2016-08-19 02:32:04 +00:00
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);
2016-08-17 09:26:01 +00:00
if (len + strlen(buffer) > max_len - 1) {
max_len += 4096;
page = (char *)realloc(page, max_len);
}
strcat(page, buffer);
2016-08-19 02:32:04 +00:00
len += strlen(buffer);
2016-08-17 09:26:01 +00:00
}
free(from);
free(subject);
2016-08-19 02:32:04 +00:00
msgid++;
2016-08-17 09:26:01 +00:00
}
2016-08-19 02:32:04 +00:00
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);
2016-08-17 09:26:01 +00:00
sqlite3_finalize(res);
sqlite3_close(db);
return page;
}
#endif