Advertisement
Guest User

Untitled

a guest
Sep 18th, 2017
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.56 KB | None | 0 0
  1. import java.io.*;
  2. import java.net.*;
  3.  
  4. public class POP3 {
  5.  
  6.     private final static String DEF_POP = "ziggo.nl";
  7.     private final static String DEF_USER = "jorrit.de.boer";
  8.     private final static String DEF_PASS = "netwerken12345";
  9.     private final static int DEF_PORT = 110;
  10.     private String[] actions = {"STAT", "LIST", "USER", "PASS", "DELE", "RETR", "QUIT"};
  11.     private String pop;
  12.     private Socket sock;
  13.     private int port;
  14.     private BufferedReader rdr;
  15.     private BufferedReader socketRdr;
  16.     private BufferedWriter socketWrtr;
  17.    
  18.     public POP3(){
  19.         rdr = new BufferedReader(new InputStreamReader(System.in));
  20.         sock = new Socket();
  21.     }
  22.    
  23.     public boolean connect(){
  24.         String servMes;
  25.        
  26.         try{
  27.             sock.connect(new InetSocketAddress(pop, port));
  28.             socketRdr = new BufferedReader(new InputStreamReader(sock.getInputStream()));
  29.             socketWrtr = new BufferedWriter(new OutputStreamWriter(sock.getOutputStream()));
  30.            
  31.             servMes = readResponse();
  32.             if(servMes.contains("-ERR")){
  33.                 System.out.println("An error has occurred.");
  34.                 return false;
  35.             }
  36.             socketWrtr.write("USER " + actions[2] +"\n");
  37.             socketWrtr.flush();
  38.            
  39.             servMes = readResponse();
  40.             if(servMes.contains("-ERR")){
  41.                 System.out.println("Invalid username.");
  42.                 return false;
  43.             }
  44.             socketWrtr.write("PASS " + actions[3] +"\n");
  45.             socketWrtr.flush();
  46.            
  47.             servMes = readResponse();
  48.             if(servMes.contains("-ERR")){
  49.                 System.out.println("Invalid password.");
  50.                 return false;
  51.             }
  52.             return true;
  53.         } catch(IOException e) {
  54.             System.out.println("Could not establish a connection with these settings: " + pop + ":" + port + "\n");
  55.         }
  56.         return false;
  57.     }
  58.    
  59.     public boolean isConnected() {
  60.         return sock != null && sock.isConnected();
  61.     }
  62.    
  63.     public void setPop() throws IOException {
  64.         System.out.println("Enter pop address.");
  65.         System.out.println("If unspecified, [pop." + DEF_POP + "] will be used.");
  66.         pop = input();
  67.         if(pop.isEmpty()){
  68.             pop = DEF_POP;
  69.         }
  70.        
  71.         System.out.println("Enter port address.");
  72.         System.out.println("If unspecified, [" + DEF_PORT + "] will be used.");
  73.         String portStr = input();
  74.         if(portStr.isEmpty()){
  75.             port = DEF_PORT;
  76.         } else {
  77.             port = Integer.parseInt(portStr);
  78.         }
  79.        
  80.         System.out.println("Enter username.");
  81.         System.out.println("If unspecified, [" + DEF_USER + "] will be used.");
  82.         actions[2] = input();
  83.         if(actions[2].isEmpty()){
  84.             actions[2] = DEF_USER;
  85.         }
  86.        
  87.         System.out.println("Enter password.");
  88.         System.out.println("If unspecified, [" + DEF_PASS + "] will be used.");
  89.         actions[3] = input();
  90.         if(actions[3].isEmpty()){
  91.             actions[3] = "mail";
  92.         }
  93.     }
  94.    
  95.     public String input() throws IOException{
  96.         String str = new String();
  97.         str = rdr.readLine();
  98.         return str;
  99.     }
  100.    
  101.     public boolean execute(String inputStr) {
  102.         String[] tmpStr = inputStr.split("@");
  103.         int option = 6;
  104.        
  105.         if(!inputStr.isEmpty()){
  106.             option = Integer.parseInt(tmpStr[0]);
  107.         }
  108.        
  109.         switch (option) {
  110.         case 1:
  111.             return stat();
  112.         case 2:
  113.             return list();
  114.         case 3:
  115.             return inputStr.contains("@") ? retrieve(tmpStr[1]) : false;
  116.         case 4:
  117.             return inputStr.contains("@") ? delete(tmpStr[1]) : false;
  118.         case 5:
  119.             return quit();
  120.         default:
  121.             return false;
  122.         }
  123.     }
  124.  
  125.     private boolean stat() {
  126.         try {
  127.             socketWrtr.write("STAT"+"\n");
  128.             socketWrtr.flush();
  129.         } catch (IOException e) {
  130.             e.printStackTrace();
  131.             return false;
  132.         }
  133.         return true;
  134.     }
  135.  
  136.     private boolean list() {
  137.         try {
  138.             socketWrtr.write("LIST"+"\n");
  139.             socketWrtr.flush();
  140.         } catch (IOException e) {
  141.             e.printStackTrace();
  142.             return false;
  143.         }
  144.         return true;
  145.     }
  146.    
  147.     private boolean retrieve(String str) {
  148.         str = str.trim();
  149.         try {
  150.             socketWrtr.write("RETR " + str + "\n");
  151.             socketWrtr.flush();
  152.         } catch (IOException e) {
  153.             e.printStackTrace();
  154.             return false;
  155.         }
  156.         return true;
  157.     }
  158.  
  159.     private boolean delete(String str) {
  160.         try {
  161.             socketWrtr.write("DELE " + str + "\n");
  162.             socketWrtr.flush();
  163.         } catch (IOException e) {
  164.             e.printStackTrace();
  165.             return false;
  166.         }
  167.         return true;
  168.     }
  169.    
  170.     public String readResponse() {
  171.         String str = "";
  172.         if(!sock.isClosed()){
  173.             try {
  174.                 str = socketRdr.readLine() + "\n";
  175.                 while(socketRdr.ready()){
  176.                     str += socketRdr.readLine() + "\n";
  177.                 }
  178.                 System.out.println("Server response: " + str);
  179.             } catch (IOException e) {
  180.                 e.printStackTrace();
  181.             }
  182.         }
  183.         return str;
  184.     }
  185.    
  186.     public boolean quit() {
  187.         try {
  188.             socketWrtr.write("QUIT"+"\n");
  189.             socketWrtr.flush();
  190.         } catch (IOException e) {
  191.             e.printStackTrace();
  192.             return false;
  193.         }
  194.         return true;
  195.     }
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement