#if defined(ENABLE_WWW) #include #include #include #include #include #include "bbs.h" extern struct bbs_config conf; int www_email_delete(struct user_record *user, int id) { char buffer[PATH_MAX]; 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; } sqlite3_busy_timeout(db, 5000); 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 pathbuf[PATH_MAX]; 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 *isql = "INSERT INTO email (sender, recipient, subject, body, date, seen) VALUES(?, ?, ?, ?, ?, 0)"; char *err_msg = 0; stralloc sa = EMPTY_STRALLOC; char *body = NULL; struct utsname name; if (recipient == NULL || subject == NULL || ibody == NULL) { return 0; } if (check_user(recipient)) { return 0; } uname(&name); for (char *p = ibody; *p != '\0'; ++p) if (*p != '\n') stralloc_append1(&sa, *p); stralloc_cats(&sa, "\r--- MagickaBBS v"); stralloc_cat_long(&sa, VERSION_MAJOR); stralloc_append1(&sa, '.'); stralloc_cat_long(&sa, VERSION_MINOR); stralloc_cats(&sa, VERSION_STR); stralloc_cats(&sa, " ("); stralloc_cats(&sa, name.sysname); stralloc_append1(&sa, '/'); stralloc_cats(&sa, name.machine); stralloc_cats(&sa, ")\r"); stralloc_cats(&sa, " * Origin: "); stralloc_cats(&sa, conf.default_tagline); stralloc_cats(&sa, " \r"); stralloc_0(&sa); body = sa.s; snprintf(pathbuf, sizeof pathbuf, "%s/email.sq3", conf.bbs_path); rc = sqlite3_open(pathbuf, &db); if (rc != SQLITE_OK) { sqlite3_close(db); free(body); return 0; } sqlite3_busy_timeout(db, 5000); rc = sqlite3_exec(db, csql, 0, 0, &err_msg); if (rc != SQLITE_OK) { sqlite3_free(err_msg); sqlite3_close(db); free(body); return 0; } rc = sqlite3_prepare_v2(db, isql, -1, &res, 0); if (rc != SQLITE_OK) { sqlite3_finalize(res); sqlite3_close(db); free(body); return 0; } sqlite3_bind_text(res, 1, user->loginname, -1, 0); sqlite3_bind_text(res, 2, recipient, -1, 0); sqlite3_bind_text(res, 3, subject, -1, 0); sqlite3_bind_text(res, 4, body, -1, 0); sqlite3_bind_int(res, 5, time(NULL)); sqlite3_step(res); sqlite3_finalize(res); sqlite3_close(db); free(body); return 1; } char *www_new_email() { stralloc page = EMPTY_STRALLOC; stralloc_copys(&page, "

New Email

\n"); stralloc_cats(&page, "
\n"); stralloc_cats(&page, "To :
\n"); stralloc_cats(&page, "Subject :
\n"); stralloc_cats(&page, "\n
"); stralloc_cats(&page, "\n
"); stralloc_cats(&page, "
\n"); stralloc_0(&page); return page.s; } char *www_email_display(struct user_record *user, int email) { stralloc page = EMPTY_STRALLOC; char pathbuf[PATH_MAX]; char datebuf[32]; sqlite3 *db; sqlite3_stmt *res; int rc; struct tm msg_date; time_t date; char *from; char *subject; char *body; int id; int i; int chars; 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=?"; snprintf(pathbuf, sizeof pathbuf, "%s/email.sq3", conf.bbs_path); rc = sqlite3_open(pathbuf, &db); if (rc != SQLITE_OK) { sqlite3_close(db); return NULL; } sqlite3_busy_timeout(db, 5000); 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_finalize(res); sqlite3_close(db); return NULL; } sqlite3_bind_text(res, 1, user->loginname, -1, 0); sqlite3_bind_int(res, 2, email - 1); if (sqlite3_step(res) != SQLITE_ROW) { return strdup("

No Such Email

\n"); } id = sqlite3_column_int(res, 0); from = (char *)sqlite3_column_text(res, 1); subject = (char *)sqlite3_column_text(res, 2); body = (char *)sqlite3_column_text(res, 3); date = (time_t)sqlite3_column_int(res, 4); localtime_r(&date, &msg_date); stralloc_copys(&page, "

Your Email

\n"); stralloc_cats(&page, "
\n"); stralloc_cats(&page, "
"); stralloc_cats(&page, subject); stralloc_cats(&page, "
\n"); stralloc_cats(&page, "
From: "); stralloc_cats(&page, from); stralloc_cats(&page, "
\n"); stralloc_cats(&page, "
Date: "); if (conf.date_style == 1) strftime(datebuf, sizeof datebuf, "%H:%M %m-%d-%y", &msg_date); else strftime(datebuf, sizeof datebuf, "%H:%M %d-%m-%y", &msg_date); stralloc_cats(&page, datebuf); stralloc_cats(&page, "
\n"); stralloc_cats(&page, "
\n"); stralloc_cats(&page, "
\n"); for (char *p = body; *p != '\0'; ++p) { switch (*p) { case '\r': stralloc_cats(&page, "
"); break; case '<': stralloc_cats(&page, "<"); break; case '>': stralloc_cats(&page, ">"); break; default: stralloc_append1(&page, *p); break; } } stralloc_cats(&page, "
\n"); stralloc_cats(&page, "
\n"); stralloc_cats(&page, "

Reply

\n"); stralloc_cats(&page, "
\n"); stralloc_cats(&page, "\n"); stralloc_cats(&page, "Subject :
\n"); stralloc_cats(&page, "\n
"); stralloc_cats(&page, "\n
"); stralloc_cats(&page, "
\n"); stralloc_cats(&page, "
\n"); stralloc_0(&page); sqlite3_finalize(res); rc = sqlite3_prepare_v2(db, update_seen_sql, -1, &res, 0); if (rc != SQLITE_OK) { sqlite3_finalize(res); sqlite3_close(db); free(page.s); return NULL; } sqlite3_bind_int(res, 1, id); sqlite3_step(res); sqlite3_finalize(res); sqlite3_close(db); return page.s; } char *www_email_summary(struct user_record *user) { stralloc page = EMPTY_STRALLOC; char pathbuf[PATH_MAX]; sqlite3 *db; sqlite3_stmt *res; int rc; char *email_summary_sql = "SELECT id,sender,subject,seen,date FROM email WHERE recipient LIKE ?"; 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);"; snprintf(pathbuf, sizeof pathbuf, "%s/email.sq3", conf.bbs_path); rc = sqlite3_open(pathbuf, &db); if (rc != SQLITE_OK) { sqlite3_close(db); return NULL; } sqlite3_busy_timeout(db, 5000); 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_finalize(res); sqlite3_close(db); return NULL; } sqlite3_bind_text(res, 1, user->loginname, -1, 0); stralloc_copys(&page, "

Your Email

\n"); stralloc_cats(&page, "\n"); stralloc_cats(&page, "
\n"); while (sqlite3_step(res) == SQLITE_ROW) { char datebuf[32]; ++msgid; int id = sqlite3_column_int(res, 0); const char *from = (const char *)sqlite3_column_text(res, 1); const char *subject = (const char *)sqlite3_column_text(res, 2); int seen = sqlite3_column_int(res, 3); struct tm msg_date; time_t date = (time_t)sqlite3_column_int(res, 4); localtime_r(&date, &msg_date); stralloc_cats(&page, "
"); stralloc_cat_long(&page, msgid); stralloc_cats(&page, "
"); stralloc_cats(&page, from); stralloc_cats(&page, "
"); if (conf.date_style == 1) strftime(datebuf, sizeof datebuf, "%H:%M %m-%d-%y", &msg_date); else strftime(datebuf, sizeof datebuf, "%H:%M %d-%m-%y", &msg_date); stralloc_cats(&page, datebuf); stralloc_cats(&page, "
\n"); } stralloc_cats(&page, "
\n"); stralloc_0(&page); sqlite3_finalize(res); sqlite3_close(db); return page.s; } #endif