Added email listing to www server
This commit is contained in:
parent
ce15bedcde
commit
a42963a77d
@ -4,9 +4,9 @@ DEPS = bbs.h
|
||||
JAMLIB = jamlib/jamlib.a
|
||||
ZMODEM = Xmodem/libzmodem.a
|
||||
LUA = lua/liblua.a
|
||||
MICROHTTPD=-lmicrohttpd
|
||||
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
|
||||
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)
|
||||
|
||||
|
3
bbs.c
3
bbs.c
@ -24,7 +24,7 @@ int sshBBS;
|
||||
int usertimeout;
|
||||
int timeoutpaused;
|
||||
|
||||
char *ipaddress;
|
||||
char *ipaddress = NULL;
|
||||
|
||||
void sigterm_handler2(int s)
|
||||
{
|
||||
@ -512,6 +512,7 @@ void runbbs_real(int socket, char *ip, int ssh) {
|
||||
|
||||
st.sa_handler = sigterm_handler2;
|
||||
sigemptyset(&st.sa_mask);
|
||||
st.sa_flags = SA_SIGINFO;
|
||||
if (sigaction(SIGTERM, &st, NULL) == -1) {
|
||||
dolog("Failed to setup sigterm handler.");
|
||||
exit(1);
|
||||
|
1
bbs.h
1
bbs.h
@ -205,6 +205,7 @@ extern void chomp(char *string);
|
||||
#if defined(ENABLE_WWW)
|
||||
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);
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
2
email.c
2
email.c
@ -296,7 +296,7 @@ void list_emails(struct user_record *user) {
|
||||
return;
|
||||
}
|
||||
|
||||
msgid = 0;
|
||||
msgid = 0;
|
||||
s_printf(get_string(63));
|
||||
while (sqlite3_step(res) == SQLITE_ROW) {
|
||||
from = strdup((char *)sqlite3_column_text(res, 0));
|
||||
|
118
www.c
118
www.c
@ -5,6 +5,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <b64/cdecode.h>
|
||||
#include "bbs.h"
|
||||
|
||||
extern struct bbs_config conf;
|
||||
@ -72,6 +73,59 @@ char *www_get_mime_type(const char *extension) {
|
||||
return default_mime_type;
|
||||
}
|
||||
|
||||
int www_401(char *header, char *footer, struct MHD_Connection * connection) {
|
||||
char buffer[4096];
|
||||
char *page;
|
||||
struct stat s;
|
||||
char *whole_page;
|
||||
struct MHD_Response *response;
|
||||
int ret;
|
||||
FILE *fptr;
|
||||
|
||||
snprintf(buffer, 4096, "%s/401.tpl", conf.www_path);
|
||||
|
||||
page = NULL;
|
||||
|
||||
if (stat(buffer, &s) == 0) {
|
||||
page = (char *)malloc(s.st_size + 1);
|
||||
if (page == NULL) {
|
||||
return -1;
|
||||
}
|
||||
memset(page, 0, s.st_size + 1);
|
||||
fptr = fopen(buffer, "r");
|
||||
if (fptr) {
|
||||
fread(page, s.st_size, 1, fptr);
|
||||
fclose(fptr);
|
||||
} else {
|
||||
free(page);
|
||||
page = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
if (page == NULL) {
|
||||
page = (char *)malloc(16);
|
||||
if (page == NULL) {
|
||||
return -1;
|
||||
}
|
||||
sprintf(page, "Missing Content");
|
||||
}
|
||||
|
||||
whole_page = (char *)malloc(strlen(header) + strlen(page) + strlen(footer) + 1);
|
||||
|
||||
sprintf(whole_page, "%s%s%s", header, page, footer);
|
||||
|
||||
response = MHD_create_response_from_buffer (strlen(whole_page), (void*)whole_page, MHD_RESPMEM_PERSISTENT);
|
||||
|
||||
MHD_add_response_header(response, "WWW-Authenticate", "Basic realm=\"BBS Area\"");
|
||||
|
||||
ret = MHD_queue_response (connection, 401, response);
|
||||
MHD_destroy_response (response);
|
||||
free(whole_page);
|
||||
free(page);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int www_404(char *header, char *footer, struct MHD_Connection * connection) {
|
||||
char buffer[4096];
|
||||
char *page;
|
||||
@ -174,6 +228,40 @@ int www_403(char *header, char *footer, struct MHD_Connection * connection) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct user_record *www_auth_ok(struct MHD_Connection *connection, const char *url) {
|
||||
char *user_password = MHD_lookup_connection_value(connection, MHD_HEADER_KIND, "Authorization");
|
||||
base64_decodestate state;
|
||||
char decoded_pass[34];
|
||||
int len;
|
||||
char *username;
|
||||
char *password;
|
||||
int i;
|
||||
|
||||
if (user_password == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (strncasecmp(user_password, "basic ", 6) == 0) {
|
||||
if (strlen(&user_password[6]) <= 48) {
|
||||
base64_init_decodestate(&state);
|
||||
len = base64_decode_block(&user_password[6], strlen(&user_password[6]), decoded_pass, &state);
|
||||
decoded_pass[len] = '\0';
|
||||
|
||||
username = decoded_pass;
|
||||
for (i=0;i<strlen(decoded_pass);i++) {
|
||||
if (decoded_pass[i] == ':') {
|
||||
decoded_pass[i] = '\0';
|
||||
password = &decoded_pass[i+1];
|
||||
break;
|
||||
}
|
||||
}
|
||||
return check_user_pass(username, password);
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
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) {
|
||||
struct MHD_Response *response;
|
||||
|
||||
@ -188,6 +276,18 @@ int www_handler(void * cls, struct MHD_Connection * connection, const char * url
|
||||
char *mime;
|
||||
int i;
|
||||
int fno;
|
||||
const char *url_ = url;
|
||||
struct user_record *user;
|
||||
|
||||
|
||||
|
||||
static int apointer;
|
||||
|
||||
if (&apointer != *ptr) {
|
||||
*ptr = &apointer;
|
||||
return MHD_YES;
|
||||
}
|
||||
*ptr = NULL;
|
||||
|
||||
snprintf(buffer, 4096, "%s/header.tpl", conf.www_path);
|
||||
|
||||
@ -284,6 +384,24 @@ 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) {
|
||||
user = www_auth_ok(connection, url_);
|
||||
|
||||
if (user == NULL) {
|
||||
www_401(header, footer, connection);
|
||||
free(header);
|
||||
free(footer);
|
||||
return MHD_YES;
|
||||
}
|
||||
page = www_email_summary(user);
|
||||
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
|
||||
|
2
www/401.tpl
Normal file
2
www/401.tpl
Normal file
@ -0,0 +1,2 @@
|
||||
<h1>401 - Not authorized</h1>
|
||||
The page you are looking for can not be accessed.
|
107
www_email.c
Normal file
107
www_email.c
Normal file
@ -0,0 +1,107 @@
|
||||
#if defined(ENABLE_WWW)
|
||||
|
||||
#include <string.h>
|
||||
#include <sqlite3.h>
|
||||
#include <time.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "bbs.h"
|
||||
|
||||
|
||||
extern struct bbs_config conf;
|
||||
|
||||
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);
|
||||
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;
|
||||
}
|
||||
|
||||
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) {
|
||||
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\">%d:%2d %d-%d-%d</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);
|
||||
if (len + strlen(buffer) > max_len - 1) {
|
||||
max_len += 4096;
|
||||
page = (char *)realloc(page, max_len);
|
||||
}
|
||||
strcat(page, buffer);
|
||||
} else {
|
||||
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\">%d:%2d %d-%d-%d</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);
|
||||
if (len + strlen(buffer) > max_len - 1) {
|
||||
max_len += 4096;
|
||||
page = (char *)realloc(page, max_len);
|
||||
}
|
||||
strcat(page, buffer);
|
||||
}
|
||||
free(from);
|
||||
free(subject);
|
||||
}
|
||||
sqlite3_finalize(res);
|
||||
sqlite3_close(db);
|
||||
return page;
|
||||
}
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user