Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store private messages to SQLite3 database #3

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ SRCS=$(INDEX_SRCS) $(DBTOOL_SRCS)
INDEX_OBJS=$(INDEX_SRCS:.cpp=.o)
DBTOOL_OBJS=$(DBTOOL_SRCS:.cpp=.o)

INDEX_LIBS=-lsqlite3
DBTOOL_LIBS=-lsqlite3

.SUFFIXES: .cpp .o
.cpp.o:
$(CXX) $(CXXFLAGS) -c $*.cpp
Expand All @@ -37,10 +40,10 @@ all: index.cgi dbtool
@echo Compiling Done

index.cgi: $(INDEX_OBJS) .depend
$(CXX) $(CXXFLAGS) -o index.cgi -Wl,-\( $(INDEX_OBJS) -Wl,-\)
$(CXX) $(CXXFLAGS) -o index.cgi -Wl,-\( $(INDEX_OBJS) -Wl,-\) $(INDEX_LIBS)

dbtool: $(DBTOOL_OBJS) .depend
$(CXX) $(CXXFLAGS) -o dbtool -Wl,-\( $(DBTOOL_OBJS) -Wl,-\)
$(CXX) $(CXXFLAGS) -o dbtool -Wl,-\( $(DBTOOL_OBJS) -Wl,-\) $(DBTOOL_LIBS)

clean:
@rm -f *.o .depend
Expand Down
26 changes: 26 additions & 0 deletions dbaseutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "basetypes.h"
#include "error.h"
#include "messages.h"
#include <sqlite3.h>

DWORD Fsize(const char *s)
{
Expand Down Expand Up @@ -128,3 +129,28 @@ DWORD TranslateMsgIndexDel(DWORD root)

return i;
}

/** Executes SQL query on the given database. Query updates database and does not return column data. */
int execute_update(char *dbname, char *query) {
sqlite3 *db;
int rc;
char *zErrMsg = 0;

rc = sqlite3_open(dbname, &db);
if( rc ){
print2log("Can't open database: %s\n", sqlite3_errmsg(db));
sqlite3_close(db);
return(1);
}

rc = sqlite3_exec(db, query, NULL, 0, &zErrMsg);
if( rc!=SQLITE_OK ){
print2log("SQL error: %s\n", zErrMsg);
sqlite3_free(zErrMsg);
return 1;
}

sqlite3_close(db);

return 0;
}
1 change: 1 addition & 0 deletions dbaseutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ int WriteDBMessage(DWORD midx, SMessage *mes);
int WriteDBMessageBody(char *buf, DWORD index, DWORD size);
DWORD VIndexCountInDB();
DWORD TranslateMsgIndexDel(DWORD root);
int execute_update(char *dbname, char *query);

#endif
155 changes: 69 additions & 86 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1619,12 +1619,14 @@ int CheckAndCreateProfile(SProfile_UserInfo *ui, SProfile_FullUserInfo *fui, cha
print2log("Profiles database error: DB ERROR, deal=%s", deal);
#endif
printhtmlerror();
// fall through

case PROFILE_RETURN_INVALID_FORMAT:
#if ENABLE_LOG >= 1
print2log("Profiles database error: INVALID FORMAT, deal=%s", deal);
#endif
printhtmlerror();
// fall through

case PROFILE_RETURN_INVALID_LOGIN:
if(op == 1 || op == 2)
Expand Down Expand Up @@ -2187,6 +2189,7 @@ static void PrepareActionResult(int action, const char **c_par1, const char **c_
case MSG_CHK_ERROR_INVISIBLE:
*c_par1 = MESSAGEMAN_invisible;
*c_par2 = MESSAGEMAN_invisible2;
// fall through
default:
*c_par1 = MESSAGEMAIN_unknownerr;
*c_par2 = MESSAGEMAIN_unknownerr2;
Expand Down Expand Up @@ -4746,75 +4749,49 @@ print2log("incor pass %s", par);
}

if(strncmp(deal, "persmsg", 7) == 0) {
if(ULogin.LU.UniqID != 0) {
if(ULogin.LU.UniqID != 0) {
// personal messages
char *sn;
DWORD type = 0;
DWORD type = 0;// show either limited num of messages (0) or all messages (1)
DWORD limit = 10;// show no more messages than limit
DWORD offset = 0;// show N messages starting from 'offset'
if((sn = strget(deal, "persmsg=", 255 - 1, '&')) != NULL) {
if(strcmp(sn, "all") == 0) {
type = 1;
}
free(sn);
//free(sn);
}
if((sn = strget(deal, "from=", 255 - 1, '&')) != NULL) {
offset = strtoul(sn, NULL, 10);
//free(sn);
}
free(sn);
print2log("offset = %d", offset);

CProfiles prof;
SPersonalMessage *msg, *frommsg;
DWORD *tt, *ft;
SPersonalMessage *msg;
DWORD *tt;
if(type) {
tt = NULL;
ft = NULL;
}
else {
tt = (DWORD*)malloc(sizeof(DWORD));
*tt = 10;
ft = (DWORD*)malloc(sizeof(DWORD));
*ft = 0;
*tt = limit;
}
// let's read to messages (maybe from too)
if(prof.ReadPersonalMessages(NULL, ULogin.LU.SIndex, &msg, tt, &frommsg, ft) != PROFILE_RETURN_ALLOK)
// let's read messages
int itt = tt ? *tt : -1;
print2log("ReadPersonalMessages tt = %d", itt);
if(prof.ReadPersonalMessages(NULL, ULogin.LU.SIndex, &msg, tt, offset) != PROFILE_RETURN_ALLOK)
printhtmlerror();

// let's get received message count
DWORD cnt = 0, postedcnt = 0;
DWORD cnt = 0;
if(msg) {
while(msg[cnt].Prev != 0xffffffff) cnt++;
cnt++;
}

if(ft) {
if(cnt) {
SPersonalMessage *msg1;
time_t ld = msg[cnt-1].Date;

if(prof.ReadPersonalMessagesByDate(NULL, ULogin.LU.SIndex, &msg1, 0, &frommsg, ld) != PROFILE_RETURN_ALLOK)
printhtmlerror();

// let's get posted message count
if(frommsg) {
while(frommsg[postedcnt].Prev != 0xffffffff) postedcnt++;
postedcnt++;
}
}
else {
*ft = 10;
// let's read to messages (maybe from too)
if(prof.ReadPersonalMessages(NULL, ULogin.LU.SIndex, &msg, NULL, &frommsg, ft) != PROFILE_RETURN_ALLOK)
printhtmlerror();
// let's get posted message count
if(frommsg) {
while(frommsg[postedcnt].Prev != 0xffffffff) postedcnt++;
postedcnt++;
}
}
}
else {
// let's get posted message count
if(frommsg) {
while(frommsg[postedcnt].Prev != 0xffffffff) postedcnt++;
postedcnt++;
}
}

print2log("cnt = %d", cnt);
// render priv messages //
Tittle_cat(TITLE_PrivateMsg);

PrintHTMLHeader(HEADERSTRING_RETURN_TO_MAIN_PAGE | HEADERSTRING_DISABLE_PRIVATEMSG, MAINPAGE_INDEX);
Expand All @@ -4838,34 +4815,25 @@ print2log("incor pass %s", par);
char *ss;
SPersonalMessage *pmsg;
DWORD i = 0;
DWORD j = 0;
int received = 0; // posted or received
int self = 0; // message from user to himself
for(;;) {
// check exit expression
if(i == cnt && j == postedcnt) break;
if(i == cnt) {
pmsg = &(frommsg[j]);
j++;
if(i == cnt) break;
pmsg = &(msg[i]);
print2log("NameTo = %s, NameFrom = %s, ui.name = %s", pmsg->NameTo, pmsg->NameFrom, ULogin.pui->username);
i++;
if(strcmp(pmsg->NameTo, pmsg->NameFrom) == 0) {
self = 1;
received = 0;
} else {
if(j == postedcnt) {
pmsg = &(msg[i]);
i++;
received = 1;
}
else {
if(frommsg[j].Date > msg[i].Date) {
pmsg = &(frommsg[j]);
j++;
received = 0;
}
else {
pmsg = &(msg[i]);
i++;
received = 1;
}
}
} else if (strcmp(pmsg->NameTo, ULogin.pui->username) == 0) {
received = 1;
self = 0;
}
else {
received = 0;
self = 0;
}

if(!received) {
DB.Profile_UserName(pmsg->NameTo, tostr, 1);
Expand Down Expand Up @@ -4898,35 +4866,50 @@ print2log("incor pass %s", par);

st1_f = FilterBiDi(st1);

if(!received) {
if (self) {
printf(DESIGN_PRIVATEMSG_FRAME, ss, MESSAGEMAIN_privatemsg_self, tostr, CodeHttpString(pmsg->NameTo, 0),
MESSAGEMAIN_privatemsg_write, DESIGN_PRIVATEMSG_FRAME_SELF, st1_f);
}
else if(!received) {
printf(DESIGN_PRIVATEMSG_FRAME, ss, MESSAGEMAIN_privatemsg_touser, tostr, CodeHttpString(pmsg->NameTo, 0),
MESSAGEMAIN_privatemsg_write, DESIGN_PRIVATEMSG_FRAME_OUT, st1_f);
}else{
} else {
printf(DESIGN_PRIVATEMSG_FRAME, ss, MESSAGEMAIN_privatemsg_fromuser, tostr, CodeHttpString(pmsg->NameFrom, 0),
MESSAGEMAIN_privatemsg_answer, DESIGN_PRIVATEMSG_FRAME_IN, st1_f);
}

if(st) free(st);
if(st1) free(st1);
if (st1_f)
free(st1_f);
}
if (st1_f) free(st1_f);
}//for

printf("<div style=\"text-align:center; width:100%%\">");
if (offset > 0) {
DWORD new_offset = offset < limit ? 0 : offset - limit;
printf("<A HREF=\"" MY_CGI_URL "?persmsg&from=%lu\" STYLE=\"text-decoration:underline;\">����� ������</A><BR>", new_offset);
}
printf("&nbsp;...&nbsp;");
if (offset + limit < ULogin.pui->persmescnt + ULogin.pui->postedmescnt) {
DWORD new_offset = offset + limit;
printf("<A HREF=\"" MY_CGI_URL "?persmsg&from=%lu\" STYLE=\"text-decoration:underline;\">����� ������</A><BR>", new_offset);
}
printf("</div>");


ULogin.pui->readpersmescnt = ULogin.pui->persmescnt;
prof.SetUInfo(ULogin.LU.SIndex, ULogin.pui);

PrintBottomLines();
if(msg) free(msg);
if(frommsg) free(frommsg);
}
else {
Tittle_cat(TITLE_Error);

PrintHTMLHeader(HEADERSTRING_RETURN_TO_MAIN_PAGE, MAINPAGE_INDEX);
PrintBoardError(MESSAGEMAIN_privatemsg_denyunreg, MESSAGEMAIN_privatemsg_denyunreg2, 0);
PrintBottomLines();
}
goto End_part;
}// if UniqID
else {
Tittle_cat(TITLE_Error);

PrintHTMLHeader(HEADERSTRING_RETURN_TO_MAIN_PAGE, MAINPAGE_INDEX);
PrintBoardError(MESSAGEMAIN_privatemsg_denyunreg, MESSAGEMAIN_privatemsg_denyunreg2, 0);
PrintBottomLines();
}
goto End_part;
}

if(strncmp(deal, "globann", 7) == 0) {
Expand Down Expand Up @@ -5381,7 +5364,7 @@ print2log("incor pass %s", par);
printf("<BR><BR><center>");
printf("<font color=\"red\">%s</font><br>\n",
fDelete ?
"��������� ������������ ���� ������� �������:" :
"��������� ������������ ���� ������� �������:" :
"������ ������������� ���������� ��������:"
);

Expand Down
4 changes: 3 additions & 1 deletion messages.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ extern const char *MESSAGEHEAD_timetypes[4];
#define MESSAGEMAIN_register_full_name "���� ������ ���:"
#define MESSAGEMAIN_register_validemail_req "�� ������ ������� ����������� E-Mail ����� <BR>(�� ����� �������������� ��� ��������� ������ � ������ �� �����������)"
#define MESSAGEMAIN_register_bot "��������!<br>���� �� ������ ��� ����, �� ������� e-mail ������<br>�� ������, �������� ������ ������!<br>Attention!<br>If you see two fields, fill with e-mail only<br>the second one, leave the first one blank!"
#define MESSAGEMAIN_register_email "E-Mail ����� (*):"
#define MESSAGEMAIN_register_email "E-Mail ����� (*):"
#define MESSAGEMAIN_register_email_pub "��������� ������ � ������ E-Mail"
#define MESSAGEMAIN_register_homepage "����� ����� �������� ��������:"
#define MESSAGEMAIN_register_icq "ICQ :"
Expand Down Expand Up @@ -340,6 +340,7 @@ extern const char *UserRight_List[USERRIGHT_COUNT];

#define MESSAGEMAIN_privatemsg_fromuser "�� ������������:"
#define MESSAGEMAIN_privatemsg_touser "������������:"
#define MESSAGEMAIN_privatemsg_self "��� ���������:"

#define MESSAGEMAIN_privatemsg_showall "����������� ��� ���������"

Expand Down Expand Up @@ -816,6 +817,7 @@ extern char DESIGN_break[10];
"%s</A><div class=\"%s\">%s</div></div>"
#define DESIGN_PRIVATEMSG_FRAME_IN "pr_from"
#define DESIGN_PRIVATEMSG_FRAME_OUT "pr_to"
#define DESIGN_PRIVATEMSG_FRAME_SELF "pr_self"

/********************** announces ***********************/
#define DESIGN_GLOBALANN_FRAME "<div class=\"an\">%s<BR>%s %s (%s) %s</div>"
Expand Down
Loading