import java.net.*; import java.util.*; import java.io.*; public class JPOP { String nl = "\n"; String host; String user; String password; int port; int status; /* -1 : Errore 0 : Pronto 1 : Connesso all'host 2 : Connesso al POP server */ Socket sock; BufferedInputStream in; BufferedOutputStream out; int tot_msg; int IN_DEBUG = 0; public JPOP() { host=""; user=""; password=""; port=110; status=0; tot_msg=0; set_newline(); } public JPOP(String phost, String puser, String ppassword) { host=phost; user=puser; password=ppassword; port=110; status=0; tot_msg=0; set_newline(); } public JPOP(String phost, String puser, String ppassword,int pport) { host=phost; user=puser; password=ppassword; status=0; tot_msg=0; set_newline(); if (pport>0 && pport<=65535) port=pport; else status=-1; } public boolean is_ready() { return (status==0) ? true : false; } public boolean is_error() { return (status==-1) ? true : false; } public boolean is_host_connected() { return (status==1) ? true : false; } public boolean is_pop_connected() { return (status==2) ? true : false; } public void host_connect() { if (!is_ready()) { status = -1; return; } status = -1; try { sock = new Socket(InetAddress.getByName(host), port); } catch (Exception ex) { return; } status = 1; return; } public void pop_connect() { if (!is_host_connected()) { status = -1; return; } try { in = new BufferedInputStream(sock.getInputStream()); out = new BufferedOutputStream(sock.getOutputStream()); String data = ""; data = get_line(); if (data.charAt(0) != '+') throw new Exception(); } catch (Exception ex) { close(); System.out.println("Errore di comunicazione con il server "); return; } String data; System.out.println("Invio User "+user); try { put_line("USER "+user); } catch (Exception ex) { close(); return; } System.out.println("In attesa di risposta..."); try { data=get_line(); } catch (Exception ex) { close(); return; } System.out.println("Risposta: "+data); if (data.indexOf("+")!=0) { close(); return;} System.out.println("Invio Password "+password); try { put_line("PASS "+password); } catch (Exception ex) { close(); return; } System.out.println("In attesa di risposta..."); try { data=get_line(); } catch (Exception ex) { close(); return; } System.out.println("Risposta: "+data); if (data.indexOf("+")!=0) { close(); return;} // se sono qui vuol dire che il login e' andato a buon fine :-) String dat[]; try { dat = data.split(" "); tot_msg = Integer.parseInt(dat[1]); status = 2; } catch (Exception ex) { status = -1; close(); } } public int get_tot_msg() { if (is_pop_connected()) return tot_msg; else return -1; } public String get_msg(int number) throws Exception { String messaggio=""; String linea=""; if (!is_pop_connected() || tot_msg<=0 || number>tot_msg || number<0) return ""; else { put_line("RETR "+number); linea = get_line(); if (linea.charAt(0)!='+') throw new Exception(); while (true) { linea = get_line(); //System.out.println("Linea: '"+linea+"'"); if (linea.length()==1 && linea.charAt(0)=='.') break; else messaggio+=nl+linea; } } return messaggio; } public String get_head(int number) throws Exception { String head=""; String linea=""; if (!is_pop_connected() || tot_msg<=0 || number>tot_msg || number<0) return ""; else { put_line("TOP "+number+" 0"); linea = get_line(); if (linea.charAt(0)!='+') throw new Exception(); while (true) { linea = get_line(); //System.out.println("Linea: '"+linea+"'"); if (linea.length()==1 && linea.charAt(0)=='.') break; else head+=nl+linea; } } return head; } public String get_host_connection_data() { return "'"+host+":"+port+"'"; } public String get_pop_connection_data() { return "user: '"+user+"', password: '"+password+"'"; } public void close() { try { if (is_pop_connected()) out.write(("QUIT\n").getBytes()); out.flush(); } catch (Exception ex1) {}; try { out.close(); in.close(); } catch (Exception ex2) {}; try { if (sock.isConnected()) sock.close(); } catch (Exception ex3) {}; status = -1; } public String get_line() throws Exception { if (!is_host_connected() && !is_pop_connected()) { return ""; } String str=""; int ch; ch=0; while (ch!=13) { //if (IN_DEBUG==1) System.out.print(ch+" "); ch=in.read(); if (ch==-1) throw new Exception(); // break; if (ch==10) continue; if (ch!=13) str=str+(char)ch; } return str; } public void put_line(String str) throws Exception { if (!is_host_connected() && !is_pop_connected()) { return; } out.write((str+"\n").getBytes()); out.flush(); } void set_newline() { String os; os = System.getProperty("os.name"); System.out.println("OS : "+os); os = os.toUpperCase(); if (os.indexOf("WINDOWS")>=0) nl = "\n\r"; if (os.indexOf("WIN")>=0) nl = "\n\r"; else nl = "\n"; } }