NullLogic

Class file providing POP3 client support

#!/usr/bin/nsp class POP3 { function POP3() { //this.debug=true; this.username=''; this.password=''; this.host='localhost'; this.port=110; this.use_tls=false; } /* lower level tcp functions */ function open() { this.sock=new net.socket(); this.sock.connect(this.host, this.port, this.use_tls); if (this.sock.gettype()!='sock4') { if (debug) print("can't connect to pop3 server\n"); return false; } line=this.read(); this.login(this.username, this.password); return true; } function read() { inp=sock.gets(); if (typeof(inp)!='string') { if (debug) print("input error\n"); return null; } if (debug) print(inp+"\n"); return inp; } function write(text) { if (debug) print(text); return sock.write(text); } function close() { sock.write("QUIT\r\n"); sock.gets(); sock.close(); delete sock; } /* now some higher level stuff */ function login(user, pass) { // try to start TLS if not already started if (net.have_tls && !sock.use_tls) { this.write("STLS\r\n"); line=this.read(); if (string.sub(line, 0, 3)!="+OK") { if (debug) print("pop3 error: "+line+"\n"); this.close(); throw "login error - no tls"; } sock.tlsconnect(); } this.write("USER "+user+"\r\n"); line=this.read(); if (string.sub(line, 0, 3)!="+OK") { if (debug) print("pop3 error: "+line+"\n"); this.close(); throw "login error"; } this.write("PASS "+pass+"\r\n"); line=this.read(); if (string.sub(line, 0, 3)!="+OK") { if (debug) print("pop3 error: "+line+"\n"); this.close(); throw "login error"; } return true; } function stat() { this.write("STAT\r\n"); status=string.split(this.read(), " "); return { count=tonumber(status[1]); }; //return tonumber(status[1]); } function uidl(i) { this.write("UIDL "+(i)+"\r\n"); line=this.read(); if (string.sub(line, 0, 3)!="+OK") return null; x=string.split(line, " "); return=x[2]; } function head(i) { this.write("HEAD "+i+"\r\n"); line=this.read(); if (string.sub(line, 0, 3)!="+OK") return null; msg=""; for (line=this.read(sock);line!=".";line=this.read()) { msg+=line+"\r\n"; } if (line!=".") return null; return msg; } function retr(i) { this.write("RETR "+i+"\r\n"); line=this.read(); if (string.sub(line, 0, 3)!="+OK") return null; msg=""; for (line=this.read(sock);line!=".";line=this.read()) { msg+=line+"\r\n"; } if (line!=".") return null; return msg; } function dele(i) { this.write("DELE "+(i)+"\r\n"); line=this.read(); if (string.sub(line, 0, 3)!="+OK") return false; return=true; } function size(i) { this.write("LIST"+i+"\r\n"); status=string.split(this.read(), " "); return tonumber(status[2]); } }