Subversion Repositories Kolibri OS

Rev

Rev 4139 | 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=RESOLVE; 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=RESOLVE;
  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 == RESOLVE)
  85.                                 {
  86.                                         SetLoginStatus(1, "Resolving server address...");      
  87.                                        
  88.                                         sockaddr.sin_family = AF_INET4;
  89.                                         AX = POP_server_port;
  90.                                         $xchg   al, ah
  91.                                         sockaddr.sin_port = AX;
  92.                                         sockaddr.sin_addr = GetIPfromAdress(#POP_server_path);                                 
  93.                                         if (!sockaddr.sin_addr) { SetLoginStatus(12, "Can't obtain server IP."); aim = FAILED; break;}
  94.  
  95.                                         aim = OPEN_CONNECTION; 
  96.                                 }
  97.                                
  98.                                 if (aim == OPEN_CONNECTION)
  99.                                 {
  100.                                         SetLoginStatus(1, "Connecting to server...");  
  101.                                        
  102.                                         socketnum = Socket(AF_INET4, SOCK_STREAM, 0);
  103.                                         if (socketnum == 0xffffffff) { SetLoginStatus(13, "Cannot open socket."); aim = FAILED; break;}
  104.                                         Connect(socketnum, #sockaddr, 16);
  105.                                         SetLoginStatus(55, "Connection established. Waiting for answer...");
  106.                                         aim = GET_ANSWER_CONNECT;      
  107.                                 }
  108.  
  109.                
  110.                                 if (aim == GET_ANSWER_CONNECT)
  111.                                 {
  112.                                         ticks = Receive(socketnum, #immbuffer, BUFFERSIZE, 0);
  113.                                         if ((ticks == 0xffffff) || (ticks < 2)) { SetLoginStatus(61, "Connection failed"); aim = FAILED; break;}
  114.                                         immbuffer[ticks]='\0';                                 
  115.                                        
  116.                                         if (immbuffer[ticks-2]=='\n')
  117.                                         {
  118.                                                 debug(#immbuffer);
  119.                                                 if (strstr(#immbuffer,"+OK"))
  120.                                                 {
  121.                                                         SetLoginStatus(60, "Verifying username...");
  122.                                                         aim = SEND_USER;
  123.                                                 }
  124.                                                 else
  125.                                                 {
  126.                                                         //aim=NULL; //may don't need retry?
  127.                                                         SetLoginStatus(55, "Failed to connect to server. Retry...");
  128.                                                 }
  129.                                         }
  130.                                         else
  131.                                         {
  132.                                                 SetLoginStatus(103, "Connection failed");
  133.                                         }
  134.                                 }
  135.  
  136.                                 if (aim == SEND_USER)
  137.                                 {
  138.                                         request_len = GetRequest("USER", #login);
  139.                                         Send(socketnum, #request, request_len, 0);
  140.                                         if (EAX == 0xffffffff) { SetLoginStatus(60, "Failed to send USER. Retry..."); break;}
  141.                                         SetLoginStatus(70, "Login verifying...");
  142.                                         debug("Send USER, awaiting answer...");
  143.                                         aim = GET_ANSWER_USER;
  144.                                 }
  145.  
  146.                                 if (aim == GET_ANSWER_USER)
  147.                                 {
  148.                                         ticks = Receive(socketnum, #immbuffer, BUFFERSIZE, 0);
  149.                                         if ((ticks == 0xffffffff) || (ticks < 2)) { SetLoginStatus(81, "Connection failed"); break;}
  150.                                         immbuffer[ticks]='\0';                                         
  151.                                        
  152.                                         if (immbuffer[ticks-2]=='\n')
  153.                                         {
  154.                                                 debug("GOT::");
  155.                                                 debug(#immbuffer);
  156.                                                 if (strstr(#immbuffer,"+OK"))
  157.                                                         { aim = SEND_PASS; SetLoginStatus(80, "Verifying password...");}
  158.                                                 else
  159.                                                         { notify("Wrong username"); aim=NULL;}
  160.                                         }
  161.                                         else
  162.                                         {
  163.                                                 SetLoginStatus(103, "Connection failed");
  164.                                         }
  165.                                 }
  166.  
  167.                                 if (aim == SEND_PASS)
  168.                                 {              
  169.                                         debug("\n Send PASS, awaiting answer...");                     
  170.                                         request_len = GetRequest("PASS", #pass_text);
  171.                                         Send(socketnum, #request, request_len, 0);
  172.                                         if (EAX == 0xffffffff) { SetLoginStatus(80, "Failed to send PASS. Retry..."); break;}
  173.                                         aim = GET_ANSWER_PASS;
  174.                                 }
  175.  
  176.                                 if (aim == GET_ANSWER_PASS)
  177.                                 {      
  178.                                         ticks = Receive(socketnum, #immbuffer, BUFFERSIZE, 0);
  179.                                         if ((ticks == 0xffffff) || (ticks < 2)) { SetLoginStatus(101, "Server disconnected"); break;}
  180.                                         immbuffer[ticks]='\0';                                         
  181.                                        
  182.                                         if (immbuffer[ticks-2]=='\n')
  183.                                         {
  184.                                                 debug("GOT::");
  185.                                                 debug(#immbuffer);
  186.                                                 if (strstr(#immbuffer,"+OK"))
  187.                                                 {
  188.                                                         SetLoginStatus(100, "Entering mailbox...");
  189.                                                         aim=SEND_NSTAT;
  190.                                                         MailBoxLoop();
  191.                                                 }
  192.                                                 else
  193.                                                 {
  194.                                                         notify("Wrong password");
  195.                                                         aim=NULL;
  196.                                                 }
  197.                                         }
  198.                                         else
  199.                                         {
  200.                                                 SetLoginStatus(103, "Connection failed");
  201.                                         }
  202.                                
  203.                                 }
  204.                                
  205.                 }
  206.         }
  207. }
  208.  
  209.  
  210. void DrawLoginScreen()
  211. {                              
  212.         panel_x = Form.cwidth - PANEL_W /2;
  213.         panel_y = Form.cheight - PANEL_H /2 - 5;
  214.  
  215.         DrawBar(0,0, Form.cwidth, Form.cheight, sc.work);
  216.        
  217.         WriteText(panel_x,panel_y,0x80,sc.work_text,"Your Email:");
  218.         DrawRectangle(panel_x, panel_y+12, PANEL_W,20, sc.work_graph); //border
  219.         DrawRectangle3D(panel_x+1, panel_y+13, PANEL_W-2,18, 0xDDDddd, 0xFFFfff); //shadow
  220.         DrawRectangle(panel_x+2, panel_y+14, PANEL_W-4,16, 0xFFFfff);
  221.         login_box.left = panel_x+3;
  222.         login_box.top = panel_y+15;
  223.         edit_box_draw stdcall(#login_box);
  224.        
  225.         WriteText(panel_x,panel_y+40,0x80,sc.work_text,"Password:");
  226.         DrawRectangle(panel_x, panel_y+52, PANEL_W,20, sc.work_graph); //border
  227.         DrawRectangle3D(panel_x+1, panel_y+53, PANEL_W-2,18, 0xDDDddd, 0xFFFfff); //shadow
  228.         DrawRectangle(panel_x+2, panel_y+54, PANEL_W-4,16, 0xFFFfff);
  229.         pass_box.left = panel_x+3;
  230.         pass_box.top = panel_y+55;
  231.         edit_box_draw stdcall(#pass_box);
  232.        
  233.         if (!aim)
  234.         {
  235.                 DrawCaptButton(panel_x,panel_y+90,100,20,11,sc.work_button, sc.work_button_text,"Settings");
  236.                 DrawCaptButton(panel_x+120,panel_y+90,100,20,12,sc.work_button, sc.work_button_text,"Enter >");
  237.         }
  238.         else DrawCaptButton(panel_x+120,panel_y+90,100,20,12,sc.work_button, sc.work_button_text,"Stop");
  239.        
  240.         SetLoginStatus(cur_st_percent, cur_st_text);
  241. }
  242.  
  243.  
  244. int GetRequest(dword command, text)
  245. {
  246.         strcpy(#request, command);
  247.         if (text)
  248.         {
  249.                 chrcat(#request, ' ');
  250.                 strcat(#request, text);
  251.         }
  252.         strcat(#request, "\n");
  253.         return strlen(#request);
  254. }
  255.  
  256. void GetServerPathAndLogin()
  257. {
  258.         int i=strchr(#email_text,'@');
  259.        
  260.         POP_server_path=login=NULL;
  261.  
  262.         if (i)
  263.         {
  264.                 strcpy(#POP_server_path, "pop.");
  265.                 strcat(#POP_server_path, #email_text+i);
  266.         }
  267.         strcpy(#login, #email_text);
  268.         login[i-1]=NULL;
  269. }
  270.  
  271.  
  272. void GetSettings()
  273. {
  274.         GetServerPathAndLogin(); //get
  275.         if (checked[CUSTOM])
  276.         {
  277.                 if (!strcmp(#POP_server_path, "pop.gmail.com")) POP_server_port = 995;
  278.                 else POP_server_port = DEFAULT_POP_PORT;
  279.         }
  280.         if (checked[MANUAL])
  281.         {
  282.                 strcpy(#POP_server_path, #POP_server1);
  283.                 POP_server_port = atoi(#POP_server_port1);
  284.         }
  285.         debug(#POP_server_path);
  286.         debug(itoa(POP_server_port));
  287. }
  288.  
  289. void SetLoginStatus(dword percent1, text1)
  290. {
  291.         DrawProgressBar(10, Form.cheight-22, PANEL_W, 12, sc.work, sc.work_graph, 0x54B1D6, sc.work_text, percent1, text1);
  292.         cur_st_percent = percent1;
  293.         cur_st_text = text1;
  294. }