Advertisement
smiche

WebNotify

Jul 10th, 2012
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.67 KB | None | 0 0
  1. import **.sauronsoftware.*******.********;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.BufferedWriter;
  5. import java.io.FileReader;
  6. import java.io.FileWriter;
  7. import java.io.IOException;
  8. import java.io.InputStreamReader;
  9. import java.io.OutputStreamWriter;
  10. import java.net.MalformedURLException;
  11. import java.net.URL;
  12. import java.net.URLConnection;
  13. import java.net.URLEncoder;
  14.  
  15. import javax.imageio.ImageIO;
  16. import javax.swing.JFrame;
  17. import javax.swing.JLabel;
  18.  
  19. import java.awt.AWTException;
  20. import java.awt.Font;
  21. import java.awt.HeadlessException;
  22. import java.awt.Image;
  23. import java.awt.MenuItem;
  24. import java.awt.PopupMenu;
  25. import java.awt.SystemTray;
  26. import java.awt.TrayIcon;
  27.  
  28. import javax.swing.JTextField;
  29. import javax.swing.JPasswordField;
  30. import javax.swing.JButton;
  31. import javax.swing.UIManager;
  32. import javax.swing.UnsupportedLookAndFeelException;
  33.  
  34.  
  35. import java.awt.event.ActionListener;
  36. import java.awt.event.ActionEvent;
  37.  
  38.  
  39.  
  40. public class WebNotify extends JFrame {
  41.     //promenlivi   
  42.     public static boolean newMessages = false;
  43.     public static boolean newPosts = false;
  44.     private JTextField textField;
  45.     private JPasswordField passwordField;
  46.     public static String user = "User";
  47.     public static String pass = "pass";
  48.     public static String mins = "2";
  49.     public static WebNotify frame;
  50.     public static String cookie;
  51.     public static TrayIcon icon;
  52.     static BufferedReader ini;
  53.     static String read;
  54.     private JTextField textField_1;
  55.     //constructor na panela
  56.     public WebNotify() {
  57.         setResizable(false);
  58.         getContentPane().setLayout(null);
  59.        
  60.         JLabel label = new JLabel("\u0418\u043C\u0435:");
  61.         label.setFont(new Font("Tahoma", Font.BOLD, 16));
  62.         label.setBounds(10, 11, 106, 25);
  63.         getContentPane().add(label);
  64.        
  65.         JLabel label_1 = new JLabel("\u041F\u0430\u0440\u043E\u043B\u0430:");
  66.         label_1.setFont(new Font("Tahoma", Font.BOLD, 16));
  67.         label_1.setBounds(10, 47, 106, 25);
  68.         getContentPane().add(label_1);
  69.        
  70.         textField = new JTextField(user);
  71.         textField.setBounds(114, 14, 147, 22);
  72.         getContentPane().add(textField);
  73.         textField.setColumns(10);
  74.        
  75.         passwordField = new JPasswordField(pass);
  76.         passwordField.setBounds(114, 47, 147, 22);
  77.         getContentPane().add(passwordField);
  78.        
  79.    
  80.        
  81.        
  82.         JLabel label_2 = new JLabel("\u041E\u043F\u0440\u0435\u0441\u043D\u044F\u0432\u0430\u043D\u0435(\u043C\u0438\u043D\u0443\u0442\u0438)");
  83.         label_2.setBounds(10, 84, 147, 25);
  84.         getContentPane().add(label_2);
  85.        
  86.         textField_1 = new JTextField(mins);
  87.         textField_1.setBounds(180, 87, 114, 20);
  88.         getContentPane().add(textField_1);
  89.         textField_1.setColumns(10);
  90.         JButton button = new JButton("\u0412\u043B\u0435\u0437");
  91.         button.addActionListener(new ActionListener() {
  92.             public void actionPerformed(ActionEvent arg0) {
  93.                 user=textField.getText();
  94.                 pass=passwordField.getText();
  95.                 mins=textField_1.getText();
  96.                 frame.setVisible(false);
  97.                 writeIni();
  98.                 performChecks();
  99.             }
  100.         });
  101.         button.setBounds(114, 115, 89, 23);
  102.         getContentPane().add(button);
  103.     }
  104.  
  105.     public static void readIni() {
  106.         try {
  107.             ini = new BufferedReader(new FileReader("account.ini"));
  108.             read = ini.readLine(); 
  109.             if(read!=null){
  110.             String saved = ******.decode(read);
  111.            
  112.             String[] ips = saved.split(":");
  113.            
  114.            
  115.             user = ips[0];
  116.             pass = ips[1];
  117.             mins = ips[2];
  118.             }
  119.        
  120.         } catch (IOException e) {
  121.  
  122.         }
  123.     }
  124.     public static void writeIni(){
  125.             try {
  126.                 BufferedWriter outi = new BufferedWriter(new FileWriter(
  127.                         "account.ini"));
  128.                 outi.write(*******.encode(user + ":" + pass +":"+mins));
  129.                 outi.flush();
  130.                 outi.close();
  131.             } catch (IOException e2) {
  132.                 e2.printStackTrace();
  133.             }
  134.     }
  135.     public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException {
  136.         UIManager.setLookAndFeel("com.jtattoo.plaf.hifi.HiFiLookAndFeel");
  137.         icon = new TrayIcon(getIconGreen(),
  138.              
  139.                 "antday помощник", createPopupMenu());
  140.           try {
  141.                 SystemTray.getSystemTray().add(icon);
  142.             } catch (AWTException e) {
  143.                 // TODO Auto-generated catch block
  144.                 e.printStackTrace();
  145.             }
  146.         readIni();
  147.         frame = new WebNotify();
  148.         frame.setBounds(200,200,330,200);
  149.         frame.setTitle("Настройки");
  150.         frame.setResizable(false);
  151.         frame.setVisible(true);
  152.  
  153.        
  154.     }
  155.     private static PopupMenu createPopupMenu() throws
  156.    
  157.     HeadlessException {
  158.  
  159.         PopupMenu menu = new PopupMenu();
  160.  
  161.         MenuItem exit = new MenuItem("Изход");
  162.  
  163.         exit.addActionListener(new ActionListener() {
  164.  
  165.             public void actionPerformed(ActionEvent e) {
  166.  
  167.                 System.exit(0);
  168.  
  169.             }
  170.  
  171.         });
  172.  
  173.         menu.add(exit);
  174.  
  175.         return menu;
  176.        
  177.     }
  178.    
  179.    
  180.     //metodi za "hva6tane" na ikonkata za taskbara
  181.     private static Image getIconGreen() throws HeadlessException {
  182.         Image img = null;
  183.         try {
  184.             img = ImageIO.read(WebNotify.class.getResource("/resources/green.png"));
  185.         } catch (IOException e2) {
  186.             e2.printStackTrace();
  187.         }
  188.        
  189.         return img;
  190.        
  191.        
  192.     }
  193.     private static Image getIconOrange() throws HeadlessException {
  194.         Image img = null;
  195.         try {
  196.             img = ImageIO.read(WebNotify.class.getResource("/resources/orange.png"));
  197.         } catch (IOException e2) {
  198.             e2.printStackTrace();
  199.         }
  200.        
  201.         return img;
  202.        
  203.        
  204.     }
  205.     private static Image getIconRed() throws HeadlessException {
  206.         Image img = null;
  207.         try {
  208.             img = ImageIO.read(WebNotify.class.getResource("/resources/red.png"));
  209.         } catch (IOException e2) {
  210.             e2.printStackTrace();
  211.         }
  212.        
  213.         return img;
  214.        
  215.     }
  216.    
  217.     public static void performChecks(){
  218.          new Thread(new Runnable() {
  219.           public void run() {
  220.         while(true){
  221.         try {
  222.             // parametri za POST funkciqta za login
  223.             String data = URLEncoder.encode("username", "UTF-8") + "=" + URLEncoder.encode(user, "UTF-8");
  224.             data += "&" + URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(pass, "UTF-8");
  225.             data += "&" + URLEncoder.encode("login", "UTF-8") + "=" + URLEncoder.encode("", "UTF-8");
  226.            
  227.  
  228.             // pravim vr1zkata k1m saita
  229.             URL url = new URL("http://forum.antday.com/ucp.php");
  230.             URLConnection conn = url.openConnection();
  231.            
  232.             //cookieto da go grabnem :D
  233.          
  234.            //da pozvolim pra6tane k1m vr1zkata         
  235.             conn.setDoOutput(true);
  236.           //s tova 6te izpra6tame zaqvki
  237.             OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
  238.             wr.write(data);//POST'vame parametrite za login
  239.             wr.flush();
  240.               for (int i=0; ; i++) {
  241.                 String headerName = conn.getHeaderFieldKey(i);
  242.                 String headerValue = conn.getHeaderField(i);
  243.  
  244.                 if (headerName == null && headerValue == null) {
  245.                     // No more headers
  246.                     break;
  247.                 }
  248.                 if ("Set-Cookie".equalsIgnoreCase(headerName)) {
  249.                     // Parse cookie
  250.                     String[] fields = headerValue.split(";\\s*");
  251.  
  252.                     String cookieValue = fields[0];
  253.                     String expires = null;
  254.                     String path = null;
  255.                     String domain = null;
  256.                     boolean secure = false;
  257.  
  258.                     // Parse each field
  259.                     for (int j=1; j<fields.length; j++) {
  260.                         if ("secure".equalsIgnoreCase(fields[j])) {
  261.                             secure = true;
  262.                         } else if (fields[j].indexOf('=') > 0) {
  263.                             String[] f = fields[j].split("=");
  264.                             if ("expires".equalsIgnoreCase(f[0])) {
  265.                                 expires = f[1];
  266.                             } else if ("domain".equalsIgnoreCase(f[0])) {
  267.                                 domain = f[1];
  268.                             } else if ("path".equalsIgnoreCase(f[0])) {
  269.                                 path = f[1];
  270.                             }
  271.  
  272.                         }
  273.                     }
  274.                     if(cookieValue.contains("sid")){
  275.                         cookie = cookieValue+"; "+expires+"; "+path+"; "+domain+"; "+secure;
  276.                     }
  277.                 }
  278.             }
  279.             // O4akvame otgovor        
  280.            BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
  281.             String line;
  282.             while ((line = rd.readLine()) != null) {
  283.                 //proverki za novi li4ni s1ob6teniq
  284.                 if(line.contains("0</strong> new messages") || line.contains("0</strong> нови съобщения")){
  285.                     if(newMessages != false){
  286.                         //setTrayIcon(3);
  287.                     }
  288.                     newMessages=false;
  289.                     break;
  290.                 } else {
  291.                     newMessages=true;
  292.                 }
  293.             }
  294.             rd.close();
  295.             wr.close();
  296.         } catch (Exception e) {
  297.         }
  298.         newPostsCheck();
  299.         try {
  300.             Thread.sleep(Integer.parseInt(mins)*60000);
  301.         } catch (InterruptedException e1) {
  302.             // TODO Auto-generated catch block
  303.             e1.printStackTrace();
  304.         }
  305.         }
  306.           }}).start();
  307.     }
  308.     public static void newPostsCheck(){
  309.         try {
  310.             URL url2 = new URL("http://forum.antday.com/search.php?search_id=newposts");  
  311.             URLConnection conn2 = url2.openConnection();
  312.             conn2.setRequestProperty("Cookie", cookie);
  313.             BufferedReader rd2 = new BufferedReader(new InputStreamReader(conn2.getInputStream()));
  314.             String line2;
  315.             while ((line2 = rd2.readLine()) != null) {
  316.                 //proverki za novi li4ni s1ob6teniq
  317.                 if(line2.contains("Нищо РЅРµ Рµ намерено.") || line2.contains("No suitable matches were found.")){
  318.                     if(newPosts != false){
  319.                         //setTrayIcon(3);
  320.                     }
  321.                     newPosts=false;
  322.  
  323.                     break;
  324.                 } else {
  325.                     newPosts=true;                 
  326.                    
  327.                 }
  328.                
  329.             }
  330.             if(newPosts == false && newMessages == true){
  331.                         icon.setImage(getIconRed());
  332.                     } else if(newPosts == true && newMessages == false){
  333.                         icon.setImage(getIconOrange());
  334.                     } else if(newPosts == true && newMessages == true){
  335.                         icon.setImage(getIconRed());
  336.                     } else if(newPosts == false && newMessages == false){
  337.                         icon.setImage(getIconGreen());
  338.                     }
  339.             rd2.close();
  340.            
  341.         } catch (MalformedURLException e) {
  342.             // TODO Auto-generated catch block
  343.             e.printStackTrace();
  344.         } catch (IOException e) {
  345.             // TODO Auto-generated catch block
  346.             e.printStackTrace();
  347.         }
  348.      
  349.     }
  350. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement