Subversion Repositories Kolibri OS

Rev

Rev 3368 | Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. //Leency & SoUrcerer, LGPL
  2.  
  3. //LoginPanel
  4. #define PANEL_W 220
  5. #define PANEL_H 140
  6. int panel_x, panel_y;
  7.  
  8. //edit_boxes at LoginPanel
  9. int     mouse_dd;
  10. unsigned char email_text[128];
  11. unsigned char pass_text[32];
  12. edit_box login_box= {PANEL_W-6,207,16,0xffffff,0x94AECE,0xffffff,0xffffff,0,sizeof(email_text)+2,#email_text,#mouse_dd,0b10};
  13. edit_box pass_box= {PANEL_W-6,207,16,0xffffff,0x94AECE,0xffffff,0xffffff,0,sizeof(pass_text)+2,#pass_text,#mouse_dd,0b1};
  14.  
  15.  
  16. void LoginBoxLoop()
  17. {
  18.         int key, id;
  19.         char socket_char;
  20.  
  21.         SetLoginStatus(NULL,NULL);
  22.  
  23.         goto _LB_DRAW;
  24.         loop()
  25.         {
  26.                 WaitEventTimeout(2);
  27.                 switch(EAX & 0xFF)
  28.                 {
  29.                         case evMouse:
  30.                                 IF (GetProcessSlot(Form.ID)-GetActiveProcess()!=0) break;
  31.                                 edit_box_mouse stdcall (#login_box);
  32.                                 edit_box_mouse stdcall (#pass_box);
  33.                                 break;
  34.                                
  35.                         case evButton:
  36.                                 id = GetButtonID();
  37.                                 if (id==1) SaveAndExit();
  38.                                 if (id==11)     OptionsLoop();
  39.                                 if (id==12)
  40.                                 {
  41.                                         if (!aim) aim=GET_PORT; else aim=NULL;
  42.                                         GetSettings();
  43.                                         SetLoginStatus(NULL, NULL);
  44.                                         DrawLoginScreen();
  45.                                 }
  46.                                 break;
  47.                                
  48.                         case evKey:
  49.                                 key = GetKey();
  50.                                 if (key==9)
  51.                                 {
  52.                                         if (login_box.flags & 0b10)
  53.                                                 { pass_box.flags = 0b11; login_box.flags = 0; }
  54.                                         else
  55.                                                 { pass_box.flags = 0b1; login_box.flags = 0b10; }
  56.                                         edit_box_draw stdcall(#login_box);
  57.                                         edit_box_draw stdcall(#pass_box);
  58.                                         break;                         
  59.                                 }
  60.                                 if (key==13)
  61.                                 {
  62.                                         if (aim) break;
  63.                                         aim=GET_PORT;
  64.                                         GetSettings();
  65.                                         SetLoginStatus(NULL, NULL);
  66.                                         DrawLoginScreen();
  67.                                 }
  68.                                 EAX=key<<8;
  69.                                 edit_box_key stdcall(#login_box);
  70.                                 edit_box_key stdcall(#pass_box);
  71.                                 break;
  72.                                
  73.                         case evReDraw: _LB_DRAW:
  74.                                 if !(DefineWindow(LOGIN_HEADER)) break;
  75.                                 DrawLoginScreen();
  76.                                 break;
  77.  
  78.                         default:
  79.                                 if (!aim) { SetLoginStatus(NULL, NULL); break; }
  80.                                 if (!email_text) { notify("Enter email!"); aim=NULL; }
  81.                                 if (!pass_text) { notify("Enter password!"); aim=NULL; }
  82.                                 if ((!login) || (!POP_server_path)) { notify("Email should be such as username@somesite.com"); aim=NULL; }
  83.                                
  84.                                 if (aim == GET_PORT)
  85.                                 {
  86.                                         SetLoginStatus(5, "Search for free local port...");
  87.                                         local_port = GetFreePort(1000);
  88.                                         if (!local_port) { notify("Error: There is no free local ports"); aim=NULL; break;}
  89.                                         SetLoginStatus(12, "Obtain server IP...");
  90.                                         aim = GET_SERVER_IP;
  91.                                 }
  92.  
  93.                                 if (aim == GET_SERVER_IP)
  94.                                 {
  95.                                         POP_server_IP = GetIPfromAdress(#POP_server_path);
  96.                                         if (!POP_server_IP) { SetLoginStatus(12, "Can't obtain server IP. Retry..."); break; }
  97.                                         SetLoginStatus(25, "Obtain to open socket..."w);
  98.                                         aim = GET_SOCKET;
  99.                                 }
  100.  
  101.                                 if (aim == GET_SOCKET)
  102.                                 {
  103.                                         socket = OpenSocket(local_port, POP_server_port, POP_server_IP, SOCKET_ACTIVE);
  104.                                         if (socket == 0xffffffff) { SetLoginStatus(25, "Error obtaining socket. Retry..."); break;}
  105.                                         SetLoginStatus(40, "Establish a connection...");
  106.                                         aim = CONNECT;
  107.                                 }
  108.  
  109.                                 if (aim == CONNECT)
  110.                                 {
  111.                                         connection_status=StatusSocket(socket);
  112.                                         if (connection_status==0) {notify("Connection to server isn't possible"); aim=NULL; break; };
  113.                                         if (connection_status==7) {SetLoginStatus(40, "Server disconnected. Retry..."); break; };
  114.                                         if (connection_status!=4) break; //0-connection isn't possible, 2-connecting, 4-connected, 7-server disconnected
  115.                                         SetLoginStatus(55, "Connection established. Reading answer...");
  116.                                         immfree();
  117.                                         aim = GET_ANSWER_CONNECT;
  118.                                 }
  119.                                
  120.                                 if (aim == GET_ANSWER_CONNECT)
  121.                                 {
  122.                                         if (!PollSocket(socket)) break;
  123.                                         socket_char=ReadSocket(socket);
  124.                                         immputc(socket_char);
  125.                                        
  126.                                         if (socket_char=='\n')
  127.                                         {
  128.                                                 debug(#immbuffer);
  129.                                                 if (strstr(#immbuffer,"+OK"))
  130.                                                 {
  131.                                                         SetLoginStatus(60, "Verifying username...");
  132.                                                         aim = SEND_USER;
  133.                                                         immfree();
  134.                                                 }
  135.                                                 else
  136.                                                 {
  137.                                                         immfree();
  138.                                                         //aim=NULL; //may don't need retry?
  139.                                                         SetLoginStatus(55, "Failed to connect to server. Retry...");
  140.                                                 }
  141.                                         }
  142.                                 }
  143.  
  144.                                 if (aim == SEND_USER)
  145.                                 {
  146.                                         request_len = GetRequest("USER", #login);
  147.                                         WriteSocket(socket,request_len,#request);
  148.                                         if (EAX == 0xffffffff) { SetLoginStatus(60, "Failed to send USER. Retry..."); break;}
  149.                                         SetLoginStatus(70, "Login verifying...");
  150.                                         debug("Send USER, awaiting answer...");
  151.                                         aim = GET_ANSWER_USER;
  152.                                 }
  153.  
  154.                                 if (aim == GET_ANSWER_USER)
  155.                                 {
  156.                                         if (!PollSocket(socket)) break;
  157.                                         socket_char=ReadSocket(socket);
  158.                                         immputc(socket_char);
  159.                                        
  160.                                         if (socket_char=='\n')
  161.                                         {
  162.                                                 debug("GOT::");
  163.                                                 debug(#immbuffer);
  164.                                                 if (strstr(#immbuffer,"+OK"))
  165.                                                         { aim = SEND_PASS; SetLoginStatus(80, "Verifying password..."); immfree(); }
  166.                                                 else
  167.                                                         { notify("Wrong username"); immfree(); aim=NULL;}
  168.                                         }
  169.                                 }
  170.  
  171.                                 if (aim == SEND_PASS)
  172.                                 {              
  173.                                         debug("\n Send PASS, awaiting answer...");                     
  174.                                         request_len = GetRequest("PASS", #pass_text);
  175.                                         WriteSocket(socket,request_len,#request);
  176.                                         if (EAX == 0xffffffff) { SetLoginStatus(80, "Failed to send PASS. Retry..."); break;}
  177.                                         aim = GET_ANSWER_PASS;
  178.                                 }
  179.  
  180.                                 if (aim == GET_ANSWER_PASS)
  181.                                 {      
  182.                                         if (!PollSocket(socket)) break;
  183.                                         socket_char=ReadSocket(socket);
  184.                                         immputc(socket_char);
  185.                                        
  186.                                         if (socket_char=='\n')
  187.                                         {
  188.                                                 debug("GOT::");
  189.                                                 debug(#immbuffer);
  190.                                                 if (strstr(#immbuffer,"+OK"))
  191.                                                 {
  192.                                                         SetLoginStatus(100, "Entering mailbox...");
  193.                                                         aim=SEND_NSTAT;
  194.                                                         immfree();
  195.                                                         MailBoxLoop();
  196.                                                 }
  197.                                                 else
  198.                                                 {
  199.                                                         notify("Wrong password");
  200.                                                         aim=NULL;
  201.                                                         immfree();
  202.                                                 }
  203.                                         }
  204.                                
  205.                                 }
  206.                                
  207.                 }
  208.         }
  209. }
  210.  
  211.  
  212. void DrawLoginScreen()
  213. {                              
  214.         panel_x = Form.cwidth - PANEL_W /2;
  215.         panel_y = Form.cheight - PANEL_H /2 - 5;
  216.  
  217.         DrawBar(0,0, Form.cwidth, Form.cheight, sc.work);
  218.        
  219.         WriteText(panel_x,panel_y,0x80,sc.work_text,"Your Email:");
  220.         DrawRectangle(panel_x, panel_y+12, PANEL_W,20, sc.work_graph); //border
  221.         DrawRectangle3D(panel_x+1, panel_y+13, PANEL_W-2,18, 0xDDDddd, 0xFFFfff); //shadow
  222.         DrawRectangle(panel_x+2, panel_y+14, PANEL_W-4,16, 0xFFFfff);
  223.         login_box.left = panel_x+3;
  224.         login_box.top = panel_y+15;
  225.         edit_box_draw stdcall(#login_box);
  226.        
  227.         WriteText(panel_x,panel_y+40,0x80,sc.work_text,"Password:");
  228.         DrawRectangle(panel_x, panel_y+52, PANEL_W,20, sc.work_graph); //border
  229.         DrawRectangle3D(panel_x+1, panel_y+53, PANEL_W-2,18, 0xDDDddd, 0xFFFfff); //shadow
  230.         DrawRectangle(panel_x+2, panel_y+54, PANEL_W-4,16, 0xFFFfff);
  231.         pass_box.left = panel_x+3;
  232.         pass_box.top = panel_y+55;
  233.         edit_box_draw stdcall(#pass_box);
  234.        
  235.         if (!aim)
  236.         {
  237.                 DrawCaptButton(panel_x,panel_y+90,100,20,11,sc.work_button, sc.work_button_text,"Settings");
  238.                 DrawCaptButton(panel_x+120,panel_y+90,100,20,12,sc.work_button, sc.work_button_text,"Enter >");
  239.         }
  240.         else DrawCaptButton(panel_x+120,panel_y+90,100,20,12,sc.work_button, sc.work_button_text,"Stop");
  241.        
  242.         SetLoginStatus(cur_st_percent, cur_st_text);
  243. }
  244.  
  245.  
  246. int GetRequest(dword command, text)
  247. {
  248.         strcpy(#request, command);
  249.         if (text)
  250.         {
  251.                 strcat(#request, " ");
  252.                 strcat(#request, text);
  253.         }
  254.         strcat(#request, "\n");
  255.         return strlen(#request);
  256. }
  257.  
  258. void GetServerPathAndLogin()
  259. {
  260.         int i=strchr(#email_text,'@');
  261.        
  262.         POP_server_path=login=NULL;
  263.  
  264.         if (i)
  265.         {
  266.                 strcpy(#POP_server_path, "pop.");
  267.                 strcat(#POP_server_path, #email_text+i);
  268.         }
  269.         strcpy(#login, #email_text);
  270.         login[i-1]=NULL;
  271. }
  272.  
  273.  
  274. void GetSettings()
  275. {
  276.         GetServerPathAndLogin(); //get
  277.         if (checked[CUSTOM])
  278.         {
  279.                 if (!strcmp(#POP_server_path, "pop.gmail.com")) POP_server_port = 995;
  280.                 else POP_server_port = DEFAULT_POP_PORT;
  281.         }
  282.         if (checked[MANUAL])
  283.         {
  284.                 strcpy(#POP_server_path, #POP_server1);
  285.                 POP_server_port = atoi(#POP_server_port1);
  286.         }
  287.         debug(#POP_server_path);
  288.         debug(itoa(POP_server_port));
  289. }
  290.  
  291. void SetLoginStatus(dword percent1, text1)
  292. {
  293.         DrawProgressBar(10, Form.cheight-22, PANEL_W, 12, sc.work, sc.work_graph, 0x54B1D6, sc.work_text, percent1, text1);
  294.         cur_st_percent = percent1;
  295.         cur_st_text = text1;
  296. }