Rewrote email to use sqlite3

This commit is contained in:
Andrew Pamment 2016-04-01 14:33:37 +10:00
parent 212f46a820
commit 88566cfd51
6 changed files with 390 additions and 416 deletions

View File

@ -3,7 +3,7 @@ CFLAGS=-I/usr/local/include
DEPS = bbs.h
JAMLIB = jamlib/jamlib.a
OBJ = inih/ini.o bbs.o main.o users.o main_menu.o mail_menu.o doors.o bbs_list.o chat_system.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
%.o: %.c $(DEPS)
$(CC) -c -o $@ $< $(CFLAGS)

5
bbs.c
View File

@ -307,8 +307,6 @@ static int handler(void* user, const char* section, const char* name,
conf->ansi_path = strdup(value);
} else if (strcasecmp(name, "bbs path") == 0) {
conf->bbs_path = strdup(value);
} else if (strcasecmp(name, "email path") == 0) {
conf->email_path = strdup(value);
}
} else if (strcasecmp(section, "mail conferences") == 0) {
if (conf->mail_conference_count == 0) {
@ -558,9 +556,6 @@ void display_last10_callers(int socket, struct user_record *user, int record) {
void display_info(int socket) {
char buffer[256];
struct utsname name;
int mailwaiting;
mailwaiting = mail_getemailcount(gUser);
uname(&name);

7
bbs.h
View File

@ -82,7 +82,6 @@ struct bbs_config {
char *ansi_path;
char *bbs_path;
char *email_path;
char *default_tagline;
char *irc_server;
@ -147,12 +146,16 @@ extern void list_users(int socket, struct user_record *user);
extern void main_menu(int socket, struct user_record *user);
extern void mail_scan(int socket, struct user_record *user);
extern int mail_getemailcount(struct user_record *user);
extern int mail_menu(int socket, struct user_record *user);
extern char *editor(int socket, struct user_record *user, char *quote, char *from);
extern int door_menu(int socket, struct user_record *user);
extern void bbs_list(int socket, struct user_record *user);
extern void chat_system(int sock, struct user_record *user);
extern int mail_getemailcount(struct user_record *user);
extern void send_email(int socket, struct user_record *user);
extern void list_emails(int socket, struct user_record *user);
#endif

View File

@ -11,7 +11,6 @@ Default Tagline = Brought to you by Another Magicka BBS!
[paths]
ANSI Path = /home/andrew/MagickaBBS/ansis
BBS Path = /home/andrew/MagickaBBS
Email Path = /home/andrew/MagickaBBS/msgs/email
[mail conferences]
Local Mail = config/localmail.ini

381
email.c Normal file
View File

@ -0,0 +1,381 @@
#include <sqlite3.h>
#include <string.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include "bbs.h"
extern struct bbs_config conf;
void send_email(int socket, struct user_record *user) {
char buffer[256];
sqlite3 *db;
sqlite3_stmt *res;
int rc;
char *recipient;
char *subject;
char *msg;
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;
s_putstring(socket, "\r\nTO: ");
s_readstring(socket, buffer, 16);
if (strlen(buffer) == 0) {
s_putstring(socket, "\r\nAborted\r\n");
return;
}
if (check_user(buffer)) {
s_putstring(socket, "\r\n\r\nInvalid Username\r\n");
return;
}
recipient = strdup(buffer);
s_putstring(socket, "\r\nSUBJECT: ");
s_readstring(socket, buffer, 25);
if (strlen(buffer) == 0) {
free(recipient);
s_putstring(socket, "\r\nAborted\r\n");
return;
}
subject = strdup(buffer);
// post a message
msg = editor(socket, user, NULL, NULL);
if (msg != NULL) {
sprintf(buffer, "%s/email.sq3", conf.bbs_path);
rc = sqlite3_open(buffer, &db);
if (rc != SQLITE_OK) {
fprintf(stderr, "Cannot open database: %s\n", sqlite3_errmsg(db));
sqlite3_close(db);
exit(1);
}
rc = sqlite3_exec(db, csql, 0, 0, &err_msg);
if (rc != SQLITE_OK ) {
fprintf(stderr, "SQL error: %s\n", err_msg);
sqlite3_free(err_msg);
sqlite3_close(db);
return;
}
rc = sqlite3_prepare_v2(db, isql, -1, &res, 0);
if (rc == SQLITE_OK) {
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, msg, -1, 0);
sqlite3_bind_int(res, 5, time(NULL));
} else {
fprintf(stderr, "Failed to execute statement: %s\n", sqlite3_errmsg(db));
sqlite3_finalize(res);
sqlite3_close(db);
s_putstring(socket, "\r\nNo such email\r\n");
return;
}
sqlite3_step(res);
sqlite3_finalize(res);
sqlite3_close(db);
free(msg);
}
free(subject);
free(recipient);
}
void show_email(int socket, struct user_record *user, int msgno) {
char buffer[256];
sqlite3 *db;
sqlite3_stmt *res;
int rc;
char *sql = "SELECT id,sender,subject,body,date FROM email WHERE recipient LIKE ? LIMIT ?, 1";
char *dsql = "DELETE FROM email WHERE id=?";
char *isql = "INSERT INTO email (sender, recipient, subject, body, date, seen) VALUES(?, ?, ?, ?, ?, 0)";
int id;
char *sender;
char *subject;
char *body;
time_t date;
struct tm msg_date;
int z;
int lines;
char c;
char *replybody;
sprintf(buffer, "%s/email.sq3", conf.bbs_path);
rc = sqlite3_open(buffer, &db);
if (rc != SQLITE_OK) {
fprintf(stderr, "Cannot open database: %s\n", sqlite3_errmsg(db));
sqlite3_close(db);
exit(1);
}
rc = sqlite3_prepare_v2(db, sql, -1, &res, 0);
if (rc == SQLITE_OK) {
sqlite3_bind_text(res, 1, user->loginname, -1, 0);
sqlite3_bind_int(res, 2, msgno);
} else {
fprintf(stderr, "Failed to execute statement: %s\n", sqlite3_errmsg(db));
sqlite3_finalize(res);
sqlite3_close(db);
s_putstring(socket, "\r\nNo such email\r\n");
return;
}
if (sqlite3_step(res) == SQLITE_ROW) {
id = sqlite3_column_int(res, 0);
sender = 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);
sprintf(buffer, "\e[2J\e[1;32mFrom : \e[1;37m%s\r\n", sender);
s_putstring(socket, buffer);
sprintf(buffer, "\e[1;32mSubject : \e[1;37m%s\r\n", subject);
s_putstring(socket, buffer);
localtime_r(&date, &msg_date);
sprintf(buffer, "\e[1;32mDate : \e[1;37m%s", asctime(&msg_date));
buffer[strlen(buffer) - 1] = '\0';
strcat(buffer, "\r\n");
s_putstring(socket, buffer);
s_putstring(socket, "\e[1;30m-------------------------------------------------------------------------------\e[0m\r\n");
lines = 0;
for (z=0;z<strlen(body);z++) {
if (body[z] == '\r') {
s_putstring(socket, "\r\n");
lines++;
if (lines == 18) {
s_putstring(socket, "Press a key to continue...\r\n");
s_getc(socket);
lines = 0;
}
} else {
s_putchar(socket, body[z]);
}
}
s_putstring(socket, "Press R to reply, D to delete Enter to quit...\r\n");
c = s_getc(socket);
sqlite3_finalize(res);
if (tolower(c) == 'r') {
if (subject != NULL) {
sprintf(buffer, "RE: %s", subject);
free(subject);
}
subject = (char *)malloc(strlen(buffer) + 1);
strcpy(subject, buffer);
replybody = editor(socket, user, body, sender);
if (replybody != NULL) {
rc = sqlite3_prepare_v2(db, isql, -1, &res, 0);
if (rc == SQLITE_OK) {
sqlite3_bind_text(res, 1, user->loginname, -1, 0);
sqlite3_bind_text(res, 2, sender, -1, 0);
sqlite3_bind_text(res, 3, subject, -1, 0);
sqlite3_bind_text(res, 4, replybody, -1, 0);
sqlite3_bind_int(res, 5, time(NULL));
} else {
fprintf(stderr, "Failed to execute statement: %s\n", sqlite3_errmsg(db));
sqlite3_finalize(res);
sqlite3_close(db);
s_putstring(socket, "\r\nNo such email\r\n");
return;
}
sqlite3_step(res);
sqlite3_finalize(res);
free(replybody);
}
free(sender);
free(subject);
free(body);
sqlite3_close(db);
} else if (tolower(c) == 'd') {
free(sender);
free(subject);
free(body);
rc = sqlite3_prepare_v2(db, dsql, -1, &res, 0);
if (rc == SQLITE_OK) {
sqlite3_bind_int(res, 1, id);
} else {
fprintf(stderr, "Failed to execute statement: %s\n", sqlite3_errmsg(db));
sqlite3_finalize(res);
sqlite3_close(db);
s_putstring(socket, "\r\nNo such email\r\n");
return;
}
sqlite3_step(res);
sqlite3_finalize(res);
sqlite3_close(db);
}
} else {
printf("Failed\n");
sqlite3_finalize(res);
sqlite3_close(db);
}
}
void list_emails(int socket, struct user_record *user) {
char buffer[256];
sqlite3 *db;
sqlite3_stmt *res;
int rc;
char *sql = "SELECT sender,subject,seen,date FROM email WHERE recipient LIKE ?";
char *subject;
char *from;
char *to;
char *body;
time_t date;
int seen;
int msgid;
int msgtoread;
struct tm msg_date;
sprintf(buffer, "%s/email.sq3", conf.bbs_path);
rc = sqlite3_open(buffer, &db);
if (rc != SQLITE_OK) {
fprintf(stderr, "Cannot open database: %s\n", sqlite3_errmsg(db));
sqlite3_close(db);
exit(1);
}
rc = sqlite3_prepare_v2(db, sql, -1, &res, 0);
if (rc == SQLITE_OK) {
sqlite3_bind_text(res, 1, user->loginname, -1, 0);
} else {
fprintf(stderr, "Failed to execute statement: %s\n", sqlite3_errmsg(db));
sqlite3_finalize(res);
sqlite3_close(db);
s_putstring(socket, "\r\nYou have no email\r\n");
return;
}
msgid = 0;
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, "\e[1;30m[\e[1;34m%4d\e[1;30m]\e[1;32m*\e[1;37m%-39.39s \e[1;32m%-16.16s \e[1;35m%02d:%02d %02d-%02d-%02d\e[0m\r\n", msgid, subject, from, msg_date.tm_hour, msg_date.tm_min, msg_date.tm_mday, msg_date.tm_mon + 1, msg_date.tm_year - 100);
} else {
sprintf(buffer, "\e[1;30m[\e[1;34m%4d\e[1;30m] \e[1;37m%-39.39s \e[1;32m%-16.16s \e[1;35m%02d:%02d %02d-%02d-%02d\e[0m\r\n", msgid, subject, from, msg_date.tm_hour, msg_date.tm_min, msg_date.tm_mday, msg_date.tm_mon + 1, msg_date.tm_year - 100);
}
s_putstring(socket, buffer);
free(from);
free(subject);
if (msgid % 22 && msgid != 0) {
s_putstring(socket, "Enter # to read, Q to quit or Enter to continue\r\n");
s_readstring(socket, buffer, 5);
if (strlen(buffer) > 0) {
if (tolower(buffer[0]) == 'q') {
sqlite3_finalize(res);
sqlite3_close(db);
return;
} else {
msgtoread = atoi(buffer);
sqlite3_finalize(res);
sqlite3_close(db);
show_email(socket, user, msgtoread);
return;
}
}
}
msgid++;
}
if (msgid == 0) {
s_putstring(socket, "\r\nYou have no email\r\n");
} else {
s_putstring(socket, "Enter # to read, or Enter to quit\r\n");
s_readstring(socket, buffer, 5);
if (strlen(buffer) > 0) {
msgtoread = atoi(buffer);
sqlite3_finalize(res);
sqlite3_close(db);
show_email(socket, user, msgtoread);
return;
}
}
sqlite3_finalize(res);
sqlite3_close(db);
}
int mail_getemailcount(struct user_record *user) {
char *sql = "SELECT COUNT(*) FROM email WHERE recipient LIKE ?";
int count;
char buffer[256];
sqlite3 *db;
sqlite3_stmt *res;
int rc;
sprintf(buffer, "%s/email.sq3", conf.bbs_path);
rc = sqlite3_open(buffer, &db);
if (rc != SQLITE_OK) {
fprintf(stderr, "Cannot open database: %s\n", sqlite3_errmsg(db));
sqlite3_close(db);
exit(1);
}
rc = sqlite3_prepare_v2(db, sql, -1, &res, 0);
if (rc == SQLITE_OK) {
sqlite3_bind_text(res, 1, user->loginname, -1, 0);
} else {
fprintf(stderr, "Failed to execute statement: %s\n", sqlite3_errmsg(db));
sqlite3_finalize(res);
sqlite3_close(db);
return 0;
}
count = 0;
if (sqlite3_step(res) == SQLITE_ROW) {
count = sqlite3_column_int(res, 0);
}
sqlite3_finalize(res);
sqlite3_close(db);
return count;
}

View File

@ -1293,380 +1293,14 @@ int mail_menu(int socket, struct user_record *user) {
break;
case 'e':
{
s_putstring(socket, "\r\nTO: ");
s_readstring(socket, buffer, 16);
if (strlen(buffer) == 0) {
s_putstring(socket, "\r\nAborted\r\n");
break;
}
if (check_user(buffer)) {
s_putstring(socket, "\r\n\r\nInvalid Username\r\n");
break;
}
to = strdup(buffer);
s_putstring(socket, "\r\nSUBJECT: ");
s_readstring(socket, buffer, 25);
if (strlen(buffer) == 0) {
free(to);
s_putstring(socket, "\r\nAborted\r\n");
break;
}
subject = strdup(buffer);
// post a message
msg = editor(socket, user, NULL, NULL);
if (msg != NULL) {
jb = open_jam_base(conf.email_path);
if (!jb) {
printf("Error opening JAM base.. %s\n", conf.email_path);
free(msg);
free(to);
free(subject);
break;
}
JAM_ClearMsgHeader( &jmh );
jmh.DateWritten = (uint32_t)time(NULL);
jmh.Attribute |= MSG_PRIVATE;
strcpy(buffer, user->loginname);
jsp = JAM_NewSubPacket();
jsf.LoID = JAMSFLD_SENDERNAME;
jsf.HiID = 0;
jsf.DatLen = strlen(buffer);
jsf.Buffer = (char *)buffer;
JAM_PutSubfield(jsp, &jsf);
jsf.LoID = JAMSFLD_RECVRNAME;
jsf.HiID = 0;
jsf.DatLen = strlen(to);
jsf.Buffer = (char *)to;
JAM_PutSubfield(jsp, &jsf);
jsf.LoID = JAMSFLD_SUBJECT;
jsf.HiID = 0;
jsf.DatLen = strlen(subject);
jsf.Buffer = (char *)subject;
JAM_PutSubfield(jsp, &jsf);
while (1) {
z = JAM_LockMB(jb, 100);
if (z == 0) {
break;
} else if (z == JAM_LOCK_FAILED) {
sleep(1);
} else {
free(msg);
free(to);
free(subject);
printf("Failed to lock msg base!\n");
break;
}
}
if (z != 0) {
JAM_CloseMB(jb);
break;
}
if (JAM_AddMessage(jb, &jmh, jsp, (char *)msg, strlen(msg))) {
printf("Failed to add message\n");
}
JAM_UnlockMB(jb);
JAM_DelSubPacket(jsp);
free(msg);
JAM_CloseMB(jb);
}
free(to);
free(subject);
send_email(socket, user);
}
break;
case 'r':
{
// Read your email...
s_putstring(socket, "\r\n");
// list mail in message base
jb = open_jam_base(conf.email_path);
if (!jb) {
printf("Error opening JAM base.. %s\n", conf.mail_conferences[user->cur_mail_conf]->mail_areas[user->cur_mail_area]->path);
break;
} else {
jam_crc = JAM_Crc32((char *)user->loginname, strlen(user->loginname));
lastmsg = 0;
while (JAM_FindUser(jb, jam_crc, lastmsg, &currmsg) == 0) {
if (JAM_ReadMsgHeader(jb, currmsg, &jmh, &jsp) != 0) {
lastmsg = currmsg + 1;
continue;
}
if (jmh.Attribute & MSG_DELETED) {
JAM_DelSubPacket(jsp);
lastmsg = currmsg + 1;
continue;
}
if (jmh.Attribute & MSG_NODISP) {
JAM_DelSubPacket(jsp);
lastmsg = currmsg + 1;
continue;
}
subject = NULL;
from = NULL;
for (z=0;z<jsp->NumFields;z++) {
if (jsp->Fields[z]->LoID == JAMSFLD_SUBJECT) {
subject = (char *)malloc(jsp->Fields[z]->DatLen + 1);
memset(subject, 0, jsp->Fields[z]->DatLen + 1);
memcpy(subject, jsp->Fields[z]->Buffer, jsp->Fields[z]->DatLen);
}
if (jsp->Fields[z]->LoID == JAMSFLD_SENDERNAME) {
from = (char *)malloc(jsp->Fields[z]->DatLen + 1);
memset(from, 0, jsp->Fields[z]->DatLen + 1);
memcpy(from, jsp->Fields[z]->Buffer, jsp->Fields[z]->DatLen);
}
}
sprintf(buffer, "[%3d]. %s From %s\r\n", currmsg, subject, from);
if (subject != NULL) {
free(subject);
}
if (from != NULL) {
free(from);
}
s_putstring(socket, buffer);
JAM_DelSubPacket(jsp);
lastmsg = currmsg + 1;
}
if (lastmsg == 0) {
s_putstring(socket, "\r\nYou have no mail...\r\n");
JAM_CloseMB(jb);
} else {
s_putstring(socket, "(#) View Message\r\n");
s_readstring(socket, buffer, 5);
currmsg = atoi(buffer);
if (JAM_ReadMsgHeader(jb, currmsg, &jmh, &jsp) != 0) {
JAM_CloseMB(jb);
break;
}
if (jmh.Attribute & MSG_DELETED) {
JAM_DelSubPacket(jsp);
JAM_CloseMB(jb);
break;
}
if (jmh.Attribute & MSG_NODISP) {
JAM_DelSubPacket(jsp);
JAM_CloseMB(jb);
break;
}
subject = NULL;
from = NULL;
to = NULL;
for (z=0;z<jsp->NumFields;z++) {
if (jsp->Fields[z]->LoID == JAMSFLD_SUBJECT) {
subject = (char *)malloc(jsp->Fields[z]->DatLen + 1);
memset(subject, 0, jsp->Fields[z]->DatLen + 1);
memcpy(subject, jsp->Fields[z]->Buffer, jsp->Fields[z]->DatLen);
}
if (jsp->Fields[z]->LoID == JAMSFLD_SENDERNAME) {
from = (char *)malloc(jsp->Fields[z]->DatLen + 1);
memset(from, 0, jsp->Fields[z]->DatLen + 1);
memcpy(from, jsp->Fields[z]->Buffer, jsp->Fields[z]->DatLen);
}
if (jsp->Fields[z]->LoID == JAMSFLD_RECVRNAME) {
to = (char *)malloc(jsp->Fields[z]->DatLen + 1);
memset(to, 0, jsp->Fields[z]->DatLen + 1);
memcpy(to, jsp->Fields[z]->Buffer, jsp->Fields[z]->DatLen);
}
}
if (strcasecmp(from, user->loginname) == 0) {
sprintf(buffer, "\e[2J\e[1;32mFrom : \e[1;37m%s\r\n", from);
s_putstring(socket, buffer);
sprintf(buffer, "\e[1;32mTo : \e[1;37m%s\r\n", to);
s_putstring(socket, buffer);
sprintf(buffer, "\e[1;32mSubject : \e[1;37m%s\r\n", subject);
s_putstring(socket, buffer);
localtime_r((time_t *)&jmh.DateWritten, &msg_date);
sprintf(buffer, "\e[1;32mDate : \e[1;37m%s", asctime(&msg_date));
buffer[strlen(buffer) - 1] = '\0';
strcat(buffer, "\r\n");
s_putstring(socket, buffer);
s_putstring(socket, "\e[1;30m-------------------------------------------------------------------------------\e[0m\r\n");
body = (char *)malloc(jmh.TxtLen);
JAM_ReadMsgText(jb, jmh.TxtOffset, jmh.TxtLen, (char *)body);
JAM_CloseMB(jb);
lines = 0;
for (z=0;z<jmh.TxtLen;z++) {
if (body[z] == '\r') {
s_putstring(socket, "\r\n");
lines++;
if (lines == 18) {
s_putstring(socket, "Press a key to continue...\r\n");
s_getc(socket);
lines = 0;
}
} else {
s_putchar(socket, body[z]);
}
}
s_putstring(socket, "Press R to reply, D to delete or any other key to quit...\r\n");
c = s_getc(socket);
if (tolower(c) == 'r') {
if (subject != NULL) {
sprintf(buffer, "RE: %s", subject);
free(subject);
}
subject = (char *)malloc(strlen(buffer) + 1);
strcpy(subject, buffer);
if (from != NULL) {
strcpy(buffer, from);
free(from);
}
from = (char *)malloc(strlen(user->loginname) + 1);
strcpy(from, user->loginname);
if (to != NULL) {
free(to);
}
to = (char *)malloc(strlen(buffer) + 1);
strcpy(to, buffer);
replybody = editor(socket, user, body, to);
if (replybody != NULL) {
jb = open_jam_base(conf.email_path);
if (!jb) {
printf("Error opening JAM base.. %s\n", conf.email_path);
free(replybody);
free(body);
free(subject);
free(to);
free(from);
break;
}
JAM_ClearMsgHeader( &jmh );
jmh.DateWritten = (uint32_t)time(NULL);
jmh.Attribute |= MSG_PRIVATE;
jsp = JAM_NewSubPacket();
jsf.LoID = JAMSFLD_SENDERNAME;
jsf.HiID = 0;
jsf.DatLen = strlen(from);
jsf.Buffer = (char *)from;
JAM_PutSubfield(jsp, &jsf);
jsf.LoID = JAMSFLD_RECVRNAME;
jsf.HiID = 0;
jsf.DatLen = strlen(to);
jsf.Buffer = (char *)to;
JAM_PutSubfield(jsp, &jsf);
jsf.LoID = JAMSFLD_SUBJECT;
jsf.HiID = 0;
jsf.DatLen = strlen(subject);
jsf.Buffer = (char *)subject;
JAM_PutSubfield(jsp, &jsf);
free(body);
free(subject);
free(to);
free(from);
while (1) {
z = JAM_LockMB(jb, 100);
if (z == 0) {
break;
} else if (z == JAM_LOCK_FAILED) {
sleep(1);
} else {
free(replybody);
printf("Failed to lock msg base!\n");
break;
}
}
if (z != 0) {
break;
}
if (JAM_AddMessage(jb, &jmh, jsp, (char *)replybody, strlen(replybody))) {
printf("Failed to add message\n");
}
JAM_UnlockMB(jb);
JAM_DelSubPacket(jsp);
free(replybody);
JAM_CloseMB(jb);
}
} else if (tolower(c) == 'd') {
jb = open_jam_base(conf.email_path);
if (!jb) {
printf("Error opening JAM base.. %s\n", conf.email_path);
free(body);
free(subject);
free(to);
free(from);
break;
}
while (1) {
z = JAM_LockMB(jb, 100);
if (z == 0) {
break;
} else if (z == JAM_LOCK_FAILED) {
sleep(1);
} else {
free(body);
free(subject);
free(to);
free(from);
printf("Failed to lock msg base!\n");
break;
}
}
if (z != 0) {
break;
}
JAM_DeleteMessage(jb, currmsg);
JAM_UnlockMB(jb);
JAM_CloseMB(jb);
free(body);
free(subject);
free(to);
free(from);
}
} else {
s_putstring(socket, "\r\nInvalid E-Mail\r\n");
JAM_CloseMB(jb);
}
}
}
list_emails(socket, user);
}
break;
case '}':
@ -1787,42 +1421,4 @@ void mail_scan(int socket, struct user_record *user) {
}
}
int mail_getemailcount(struct user_record *user) {
s_JamBase *jb;
s_JamBaseHeader jbh;
s_JamMsgHeader jmh;
s_JamSubPacket* jsp;
s_JamSubfield jsf;
uint32_t jam_crc;
uint32_t lastmsg, currmsg;
int msg_count = 0;
jb = open_jam_base(conf.email_path);
if (!jb) {
printf("Error opening JAM base.. %s\n", conf.mail_conferences[user->cur_mail_conf]->mail_areas[user->cur_mail_area]->path);
} else {
jam_crc = JAM_Crc32((char *)user->loginname, strlen(user->loginname));
lastmsg = 0;
while (JAM_FindUser(jb, jam_crc, lastmsg, &currmsg) == 0) {
if (JAM_ReadMsgHeader(jb, currmsg, &jmh, &jsp) != 0) {
continue;
}
if (jmh.Attribute & MSG_DELETED) {
JAM_DelSubPacket(jsp);
continue;
}
if (jmh.Attribute & MSG_NODISP) {
JAM_DelSubPacket(jsp);
continue;
}
JAM_DelSubPacket(jsp);
msg_count++;
lastmsg = currmsg + 1;
}
JAM_CloseMB(jb);
}
return msg_count;
}