Liza 0.9.2: better code, small fixes

git-svn-id: svn://kolibrios.org@4169 a494cfbc-eb01-0410-851d-a64ba20cac60
This commit is contained in:
Kirill Lipatov (Leency) 2013-11-05 19:38:16 +00:00
parent fc383603e2
commit 7dc9cd558f
3 changed files with 222 additions and 261 deletions

View File

@ -45,9 +45,9 @@ enum {
#define WIN_H 440
#define WIN_MIN_W 500
#define WIN_MIN_H 380
#define LOGIN_HEADER "Login - Email client Liza 0.9e"
#define OPTIONS_HEADER "Options - Email client Liza 0.9e"
#define MAILBOX_HEADER "Mail Box - Email client Liza 0.9e"
#define LOGIN_HEADER "Login - Email client Liza 0.9.2"
#define OPTIONS_HEADER "Options - Email client Liza 0.9.2"
#define MAILBOX_HEADER "Mail Box - Email client Liza 0.9.2"
#define BUFFERSIZE 512
proc_info Form;
system_colors sc;
@ -117,7 +117,7 @@ void OpenMailDat()
ReadFile(0, 512, #read_data, "/sys/network/mail.dat");
if (!read_data)
{
strcpy(#email_text, "testliza@ya.ru"); //temporarily, for testing
strcpy(#email_text, "testliza@ya.ru");
strcpy(#pass_text, "kolibri");
}
else
@ -131,8 +131,6 @@ void OpenMailDat()
login_box.size = login_box.pos = strlen(#email_text);
}
void SaveAndExit()
{
char write_data[512], pass_b64[256];
@ -145,6 +143,7 @@ void SaveAndExit()
ExitProcess();
}
int GetRequest(dword command, text)
{
strcpy(#request, command);
@ -161,6 +160,7 @@ void StopConnect(dword message)
{
if (message) notify(message);
aim = STOP;
Close(socketnum);
}
stop:

View File

@ -13,15 +13,106 @@ edit_box login_box= {PANEL_W-6,207,16,0xffffff,0x94AECE,0xffffff,0xffffff,0,size
edit_box pass_box= {PANEL_W-6,207,16,0xffffff,0x94AECE,0xffffff,0xffffff,0,sizeof(pass_text)+2,#pass_text,#mouse_dd,0b1};
void LoginNetworkProcess()
{
if (aim) switch(aim)
{
case RESOLVE:
if (!email_text) StopConnect("Enter email!");
if (!pass_text) StopConnect("Enter password!");
if ((!login) || (!POP_server_path)) StopConnect("Email should be such as username@somesite.com");
if (!aim) return;
sockaddr.sin_family = AF_INET4;
AX = POP_server_port;
$xchg al, ah
sockaddr.sin_port = AX;
sockaddr.sin_addr = GetIPfromAdress(#POP_server_path);
if (!sockaddr.sin_addr) { StopConnect("Can't obtain server IP."); break;}
else aim = OPEN_CONNECTION;
break;
case OPEN_CONNECTION:
socketnum = Socket(AF_INET4, SOCK_STREAM, 0);
if (socketnum == 0xffffffff) { StopConnect("Cannot open socket."); break;}
Connect(socketnum, #sockaddr, 16);
aim = GET_ANSWER_CONNECT;
break;
case GET_ANSWER_CONNECT:
ticks = Receive(socketnum, #immbuffer, BUFFERSIZE, 0);
if ((ticks == 0xffffff) || (ticks < 2)) { StopConnect("Connection failed"); break;}
immbuffer[ticks]='\0';
if (immbuffer[ticks-2]=='\n')
{
if (strstr(#immbuffer,"+OK"))
aim = SEND_USER;
else
StopConnect("Failed to connect to server. Retry...");
}
break;
case SEND_USER:
request_len = GetRequest("USER", #login);
if (Send(socketnum, #request, request_len, 0) == 0xffffffff)
{
SetLoginStatus("Failed to send USER. Retry...");
break;
}
else aim = GET_ANSWER_USER;
break;
case GET_ANSWER_USER:
ticks = Receive(socketnum, #immbuffer, BUFFERSIZE, 0);
if ((ticks == 0xffffffff) || (ticks < 2)) { SetLoginStatus("Connection failed"); break;}
immbuffer[ticks]='\0';
if (immbuffer[ticks-2]=='\n')
{
if (strstr(#immbuffer,"+OK"))
aim = SEND_PASS;
else
{
debug(#immbuffer);
StopConnect("Wrong username");
}
}
else
{
StopConnect("Connection failed");
}
break;
case SEND_PASS:
request_len = GetRequest("PASS", #pass_text);
if (Send(socketnum, #request, request_len, 0) == 0xffffffff)
{
SetLoginStatus("Failed to send PASS. Retry...");
break;
}
else aim = GET_ANSWER_PASS;
break;
case GET_ANSWER_PASS:
ticks = Receive(socketnum, #immbuffer, BUFFERSIZE, 0);
if ((ticks == 0xffffff) || (ticks < 2)) { SetLoginStatus("Server disconnected"); break;}
immbuffer[ticks]='\0';
if (immbuffer[ticks-2]=='\n')
{
if (strstr(#immbuffer,"+OK")) MailBoxLoop();
else
{
StopConnect("Wrong password or POP3 disabled");
debug(#immbuffer);
}
}
else
{
StopConnect("Connection failed");
}
}
}
void LoginBoxLoop()
{
int key, id;
char socket_char;
SetLoginStatus(NULL);
goto _LB_DRAW;
loop()
{
@ -75,114 +166,7 @@ void LoginBoxLoop()
break;
default:
if (!aim) break;
if (!email_text) StopConnect("Enter email!");
if (!pass_text) StopConnect("Enter password!");
if ((!login) || (!POP_server_path)) StopConnect("Email should be such as username@somesite.com");
if (aim == RESOLVE)
{
sockaddr.sin_family = AF_INET4;
AX = POP_server_port;
$xchg al, ah
sockaddr.sin_port = AX;
sockaddr.sin_addr = GetIPfromAdress(#POP_server_path);
if (!sockaddr.sin_addr) { StopConnect("Can't obtain server IP."); break;}
aim = OPEN_CONNECTION;
break;
}
if (aim == OPEN_CONNECTION)
{
socketnum = Socket(AF_INET4, SOCK_STREAM, 0);
if (socketnum == 0xffffffff) { StopConnect("Cannot open socket."); break;}
Connect(socketnum, #sockaddr, 16);
aim = GET_ANSWER_CONNECT;
break;
}
if (aim == GET_ANSWER_CONNECT)
{
ticks = Receive(socketnum, #immbuffer, BUFFERSIZE, 0);
if ((ticks == 0xffffff) || (ticks < 2)) { StopConnect("Connection failed"); break;}
immbuffer[ticks]='\0';
if (immbuffer[ticks-2]=='\n')
{
if (strstr(#immbuffer,"+OK"))
aim = SEND_USER;
else
StopConnect("Failed to connect to server. Retry...");
}
}
if (aim == SEND_USER)
{
request_len = GetRequest("USER", #login);
if (Send(socketnum, #request, request_len, 0) == 0xffffffff)
{
SetLoginStatus("Failed to send USER. Retry...");
break;
}
aim = GET_ANSWER_USER;
}
if (aim == GET_ANSWER_USER)
{
ticks = Receive(socketnum, #immbuffer, BUFFERSIZE, 0);
if ((ticks == 0xffffffff) || (ticks < 2)) { SetLoginStatus("Connection failed"); break;}
immbuffer[ticks]='\0';
if (immbuffer[ticks-2]=='\n')
{
if (strstr(#immbuffer,"+OK"))
aim = SEND_PASS;
else
{
debug(#immbuffer);
StopConnect("Wrong username");
}
}
else
{
StopConnect("Connection failed");
}
}
if (aim == SEND_PASS)
{
request_len = GetRequest("PASS", #pass_text);
if (Send(socketnum, #request, request_len, 0) == 0xffffffff)
{
SetLoginStatus("Failed to send PASS. Retry...");
break;
}
aim = GET_ANSWER_PASS;
}
if (aim == GET_ANSWER_PASS)
{
ticks = Receive(socketnum, #immbuffer, BUFFERSIZE, 0);
if ((ticks == 0xffffff) || (ticks < 2)) { SetLoginStatus("Server disconnected"); break;}
immbuffer[ticks]='\0';
if (immbuffer[ticks-2]=='\n')
{
if (strstr(#immbuffer,"+OK")) MailBoxLoop();
else
{
StopConnect("Wrong password or POP3 disabled");
debug(#immbuffer);
}
}
else
{
StopConnect("Connection failed");
}
}
LoginNetworkProcess();
}
}
}
@ -223,27 +207,15 @@ void DrawLoginScreen()
}
void GetServerPathAndLogin()
{
int i=strchr(#email_text,'@');
POP_server_path=login=NULL;
if (i)
{
strcpy(#POP_server_path, "pop.");
strcat(#POP_server_path, #email_text+i);
}
strcpy(#login, #email_text);
login[i-1]=NULL;
}
void GetSettings()
{
GetServerPathAndLogin();
int at_pos = strchr(#email_text,'@');
strlcpy(#login, #email_text, at_pos-1);
if (checked[CUSTOM])
{
strcpy(#POP_server_path, "pop.");
strcat(#POP_server_path, #email_text+at_pos);
POP_server_port = DEFAULT_POP_PORT;
}
if (checked[MANUAL])

View File

@ -27,6 +27,117 @@ enum {
CLOSE_CHANGE_CHARSET
};
void MailBoxNetworkProcess()
{
int load_persent;
if (aim) switch(aim)
{
case SEND_NSTAT:
SetLoginStatus("Counting mail, awaiting answer...");
request_len = GetRequest("STAT", NULL);
Send(socketnum, #request, request_len, 0);
if (EAX == 0xffffffff) { debug("Error sending STAT. Retry..."w); break;}
aim = GET_ANSWER_NSTAT;
break;
case GET_ANSWER_NSTAT:
ticks = Receive(socketnum, #immbuffer, BUFFERSIZE, 0);
if ((ticks == 0xffffff) || (ticks < 2)) break;
if (immbuffer[ticks-2]=='\n')
{
debug(#immbuffer);
if (strstr(#immbuffer,"+OK"))
{
strcpyb(#immbuffer, #param, "+OK ", " ");
mail_list.count = atoi(#param);
free(listbuffer);
listbuffer = mem_Alloc(30*mail_list.count); //24* original
listpointer = listbuffer;
aim = SEND_NLIST;
debug("Receiving mail list...");
}
else
{
StopConnect("Sorry, can't recieve your mail");
}
}
break;
case SEND_NLIST:
WriteText(5, Form.cheight-11, 0x80, sc.work_text, "Send LIST, awaiting answer...");
request_len = GetRequest("LIST", NULL);
Send(socketnum, #request, request_len, 0);
if (EAX == 0xffffffff) {debug("Error while sending LIST. Retry..."); break;}
else aim = GET_ANSWER_NLIST;
break;
case GET_ANSWER_NLIST:
ticks = Receive(socketnum, listpointer, listbuffer + 30*mail_list.count - listpointer, MSG_DONTWAIT);
if (ticks == 0xffffffff) break;
listpointer = listpointer + ticks;
if (listpointer - listbuffer < 5) break;
if (strncmp(listpointer-5,"\n.\n",5)==0) // note that c-- assembles "\n.\n" to 0x0d, 0x0a, 0x2e, 0x0d, 0x0a
{
aim = SEND_RETR;
debug("Got mail list");
DrawMailBox();
*listpointer='\0';
atr.CreateArray();
atr.SetSizes();
}
break;
case SEND_RETR:
from = to = date = subj = cur_charset = NULL;
letter_view.ClearList();
DrawMailBox();
debug("Send RETR, awaiting answer...");
request_len = GetRequest("RETR", itoa(mail_list.current+1));
if (Send(socketnum, #request, request_len, 0) == 0xffffffff)
{
StopConnect("Error while trying to get letter from server");
break;
}
mailsize = atr.GetSize(mail_list.current+1) + 1024;
free(mailstart);
if (!mailstart = malloc(mailsize))
{
debug("alloc error!");
aim=NULL;
break;
}
mailend = mailstart;
aim = GET_ANSWER_RETR;
case GET_ANSWER_RETR:
ticks = Receive(socketnum, mailend, mailsize + mailstart - mailend, MSG_DONTWAIT);
if (ticks == 0xffffffff) break;
mailend = mailend + ticks;
if (!aim) break;
if (mailsize + mailstart - mailend - 2 < 0)
{
debug("Resizing buffer");
mailsize += 4096;
mailstart = realloc(mailstart, mailsize);
if (!mailstart) { StopConnect("Realloc error!"); break;}
}
//if (mailsize>9000)
{
load_persent = mailend - mailstart * 100 ;
load_persent /= mailsize - 1024;
if (load_persent != cur_st_percent) SetMailBoxStatus( load_persent , NULL);
}
ParseMail();
}
}
void MailBoxLoop()
{
@ -159,121 +270,7 @@ void MailBoxLoop()
break;
default:
if (aim == SEND_NSTAT)
{
SetLoginStatus("Counting mail, awaiting answer...");
request_len = GetRequest("STAT", NULL);
Send(socketnum, #request, request_len, 0);
if (EAX == 0xffffffff) { debug("Error sending STAT. Retry..."w); break;}
aim = GET_ANSWER_NSTAT;
}
if (aim == GET_ANSWER_NSTAT)
{
ticks = Receive(socketnum, #immbuffer, BUFFERSIZE, 0);
if ((ticks == 0xffffff) || (ticks < 2)) break;
if (immbuffer[ticks-2]=='\n')
{
debug(#immbuffer);
if (strstr(#immbuffer,"+OK"))
{
mail_list.count = GetMailCount();
free(listbuffer);
listbuffer = mem_Alloc(30*mail_list.count); //24* original
listpointer = listbuffer;
aim = SEND_NLIST;
debug("Receiving mail list...");
}
else
{
StopConnect("Sorry, can't recieve your mail");
}
}
}
if (aim == SEND_NLIST)
{
WriteText(5, Form.cheight-11, 0x80, sc.work_text, "Send LIST, awaiting answer...");
request_len = GetRequest("LIST", NULL);
Send(socketnum, #request, request_len, 0);
if (EAX == 0xffffffff) {debug("Error while sending LIST. Retry..."); break;}
aim = GET_ANSWER_NLIST;
}
if (aim == GET_ANSWER_NLIST)
{
ticks = Receive(socketnum, listpointer, listbuffer + 30*mail_list.count - listpointer, MSG_DONTWAIT);
if (ticks == 0xffffffff) break;
listpointer = listpointer + ticks;
if (listpointer - listbuffer < 5) break;
if (strncmp(listpointer-5,"\n.\n",5)==0) // note that c-- assembles "\n.\n" to 0x0d, 0x0a, 0x2e, 0x0d, 0x0a
{
aim = SEND_RETR;
debug("Got mail list");
DrawMailBox();
*listpointer='\0';
atr.CreateArray();
atr.SetSizes();
}
}
if (aim == SEND_RETR)
{
from = to = date = subj = cur_charset = NULL;
letter_view.ClearList();
DrawMailBox();
debug("Send RETR, awaiting answer...");
request_len = GetRequest("RETR", itoa(mail_list.current+1));
if (Send(socketnum, #request, request_len, 0) == 0xffffffff)
{
StopConnect("Error while trying to get letter from server");
break;
}
mailsize = atr.GetSize(mail_list.current+1) + 1024;
free(mailstart);
if (!mailstart = malloc(mailsize))
{
debug("alloc error!");
aim=NULL;
break;
}
mailend = mailstart;
aim = GET_ANSWER_RETR;
}
if (aim == GET_ANSWER_RETR)
{
ticks = Receive(socketnum, mailend, mailsize + mailstart - mailend, MSG_DONTWAIT);
if (ticks == 0xffffffff) break;
mailend = mailend + ticks;
if (!aim) break;
if (mailsize + mailstart - mailend - 2 < 0)
{
debug("Resizing buffer");
mailsize += 4096;
mailstart = realloc(mailstart, mailsize);
if (!mailstart) { StopConnect("Realloc error!"); break;}
}
if (mailsize>9000)
{
id = mailend - mailstart * 100 ;
id /= mailsize - 1024;
if (id!=cur_st_percent) SetMailBoxStatus( id , NULL);
}
//debug(mailstart);
//pause(10);
//debug("======================");
ParseMail();
}
MailBoxNetworkProcess();
}
}
}
@ -293,15 +290,12 @@ void DrawToolbar()
#define BUT_Y 7
#define BUT_H 22
#define BUT_W 74
#define BUT_SPACE 11
int toolbar_w = BUT_Y + BUT_H + BUT_Y + 3;
mail_list.SetSizes(0, toolbar_w, Form.cwidth - scroll1.size_x - 1, mail_list.h, 60,18);
DrawBar(0,0, Form.cwidth,toolbar_w-3, sc.work);
DrawCaptButton(10 , BUT_Y, BUT_W, BUT_H, GET_MAIL, sc.work_button, sc.work_button_text,"Get mail");
DrawCaptButton(BUT_W+BUT_SPACE + 10, BUT_Y, BUT_W, BUT_H, SEND_MAIL, sc.work_button, sc.work_button_text,"Send Email");
DrawCaptButton(BUT_W+BUT_SPACE*2 + 10, BUT_Y, BUT_W, BUT_H, DELETE_LETTER, sc.work_button, sc.work_button_text,"Delete");
DrawCaptButton(BUT_W+BUT_SPACE*3 + 10, BUT_Y, BUT_W, BUT_H, SAVE_LETTER, sc.work_button, sc.work_button_text,"Save");
DrawCaptButton(BUT_W+ 20, BUT_Y, BUT_W+10, BUT_H, SAVE_LETTER, sc.work_button, sc.work_button_text,"Save letter");
DrawCaptButton(Form.cwidth-BUT_W - 10, BUT_Y, BUT_W, BUT_H, EXIT_MAIL, sc.work_button, sc.work_button_text,"< Exit");
DrawBar(0, mail_list.y-3, mail_list.w,1, sc.work_graph);
@ -428,7 +422,8 @@ void DrawStatusBar()
void SetMailBoxStatus(dword percent1, text1)
{
if (text1) WriteText(3, Form.cheight -status_bar_h + 3, 0x80, sc.work_text, text1);
DrawProgressBar(3, Form.cheight -status_bar_h + 1, 220, 12, sc.work, 0xC3C3C3, 0x54B1D6, sc.work_text, percent1, text1);
cur_st_percent = percent1;
cur_st_text = text1;
}
@ -440,12 +435,6 @@ void StopLoading()
to = from = date = subj = cur_charset = NULL;
}
int GetMailCount(){
char tmpbuf4[512];
strcpyb(#immbuffer, #tmpbuf4, "+OK ", " ");
return atoi(#tmpbuf4);
}
int GetLetterSize_(int number)
{
char search_num[24];