commit a2dd04048730be04cdc3527c5dfc7ae08b3ba9a0
parent 3e2921fdee3ae350224406bb16feabeba55186f6
Author: Tomas Hlavaty <tom@logand.com>
Date: Sun, 16 Jan 2011 16:09:54 +0100
dirpop3d.vala deleted
Diffstat:
D | dirpop3d.vala | | | 264 | ------------------------------------------------------------------------------- |
1 file changed, 0 insertions(+), 264 deletions(-)
diff --git a/dirpop3d.vala b/dirpop3d.vala
@@ -1,264 +0,0 @@
-// dirpop3d.vala -- Directory serving pop3 server based on glib.
-// (c) 2011 Tomas Hlavaty
-
-using Gee;
-
-abstract class Pop3Server {
- SocketConnection c;
- DataInputStream i;
- DataOutputStream o;
- State s;
-
- enum State {
- START,
- USER,
- PASS,
- LIST
- }
-
- public Pop3Server (SocketConnection _c) {
- c = _c;
- i = new DataInputStream (c.input_stream);
- o = new DataOutputStream (c.output_stream);
- s = State.START;
- }
-
- protected void prinl (string x) throws GLib.IOError, GLib.Error {
- o.put_string ("%s\n".printf (x));
- //o.flush ();
- stderr.printf ("%s\n", x);
- //stderr.flush ();
- }
-
- protected void ok (string msg) throws GLib.IOError, GLib.Error {
- o.put_string ("+OK %s\n".printf (msg));
- //o.flush ();
- stderr.printf ("+OK %s\n", msg);
- //stderr.flush ();
- }
-
- protected void ok2 (string msg1, string msg2) throws GLib.IOError, GLib.Error {
- o.put_string ("+OK %s %s\n".printf (msg1, msg2));
- //o.flush ();
- stderr.printf ("+OK %s %s\n", msg1, msg2);
- //stderr.flush ();
- }
-
- protected void err (string msg) throws GLib.IOError, GLib.Error {
- o.put_string ("-ERR error\n");
- //o.flush ();
- stderr.printf ("-ERR error\n");
- //stderr.flush ();
- }
-
- public async void process_request_async () {
- try {
- ready ();
- string line = null;
- while ((line = yield i.read_line_async (Priority.HIGH_IDLE)) != null) {
- stderr.printf ("%s\n", line);
- var cmd = parse_line (line);
- if(!process_command (cmd)) break;
- }
- } catch (Error e) {
- stderr.printf ("Error: %s\n", e.message);
- }
- }
-
- protected string[] parse_line (string line) {
- return line.chomp().split(" ");
- }
-
- protected bool process_command (string[] cmd) throws GLib.IOError, GLib.Error {
- switch(cmd[0]) {
- case "QUIT":
- return quit (cmd);
- case "USER":
- if(s != State.START) return false;
- var x = user (cmd);
- if(x) s = State.USER;
- return x;
- case "PASS":
- if(s != State.USER) return false;
- var x = pass (cmd);
- if(x) s = State.PASS;
- return x;
- case "STAT":
- if(s != State.PASS && s != State.LIST) return false;
- var x = stat (cmd);
- if(x) s = State.LIST;
- return x;
- case "LIST":
- if(s != State.PASS && s != State.LIST) return false;
- var x = list (cmd);
- if(x) s = State.LIST;
- return x;
- case "RETR":
- if(s != State.LIST) return false;
- return retr (cmd);
- case "DELE":
- if(s != State.LIST) return false;
- return dele (cmd);
- default: return otherwise (cmd);
- }
- }
-
- protected abstract void ready () throws GLib.IOError, GLib.Error;
- protected abstract bool quit (string[] cmd) throws GLib.IOError, GLib.Error;
- protected abstract bool user (string[] cmd) throws GLib.IOError, GLib.Error;
- protected abstract bool pass (string[] cmd) throws GLib.IOError, GLib.Error;
- protected abstract bool stat (string[] cmd) throws GLib.IOError, GLib.Error;
- protected abstract bool list (string[] cmd) throws GLib.IOError, GLib.Error;
- protected abstract bool retr (string[] cmd) throws GLib.IOError, GLib.Error;
- protected abstract bool dele (string[] cmd) throws GLib.IOError, GLib.Error;
- protected abstract bool otherwise (string[] cmd) throws GLib.IOError, GLib.Error;
-}
-
-static string root_dirname () { // TODO config
- return Environment.get_home_dir () + "/.w3maild";
-}
-
-class Dirpop3Server : Pop3Server {
- string usr = "";
- string pwd = "";
- ArrayList<FileInfo> lst = new ArrayList<FileInfo> ();
-
- public Dirpop3Server (SocketConnection c) {
- base (c);
- }
-
- protected override void ready () throws GLib.IOError, GLib.Error {
- ok ("dirpop3d ready");
- }
-
- protected override bool quit (string[] cmd) throws GLib.IOError, GLib.Error {
- ok2 (usr, "dirpop3d bye");
- return false;
- }
-
- string data_dirname () {
- return root_dirname () + "/" + usr;
- }
-
- File data_dir () {
- return File.new_for_path (data_dirname ());
- }
-
- bool user_exists () {
- if(usr.length < 1) return false;
- return data_dir ().query_file_type (0) == FileType.DIRECTORY;
- }
-
- protected override bool user (string[] cmd) throws GLib.IOError, GLib.Error {
- usr = cmd[1];
- if(!user_exists ()) return false;
- ok ("User accepted");
- return true;
- }
-
- protected override bool pass (string[] cmd) throws GLib.IOError, GLib.Error {
- pwd = cmd[1];
- ok ("Pass accepted");
- return true;
- }
-
- void update_lst () {
- lst.clear ();
- try {
- File dir = data_dir ();
- var i = dir.enumerate_children (FILE_ATTRIBUTE_STANDARD_NAME + "," +
- FILE_ATTRIBUTE_STANDARD_TYPE,
- FileQueryInfoFlags.NONE,
- null);
- FileInfo info;
- while ((info = i.next_file (null)) != null)
- if(info.get_file_type () == FileType.REGULAR)
- lst.add (info);
- } catch (Error e) {
- stderr.printf ("Error: %s\n", e.message);
- }
- }
-
- void collect_stat (out int cnt, out uint64 siz) {
- siz = 0;
- update_lst ();
- for (cnt = 0; cnt < lst.size; cnt++)
- siz += lst[cnt].get_size ();
- }
-
- protected override bool stat (string[] cmd) throws GLib.IOError, GLib.Error {
- int cnt = 0;
- uint64 siz = 0;
- collect_stat (out cnt, out siz);
- ok (cnt.to_string () + " " + siz.to_string ());
- return true;
- }
-
- protected override bool list (string[] cmd) throws GLib.IOError, GLib.Error {
- int cnt = 0;
- uint64 siz = 0;
- collect_stat (out cnt, out siz);
- ok (cnt.to_string () + " messages (" + siz.to_string () + " octets)");
- siz = 0;
- for (int i = 0; i < lst.size; i++) {
- prinl ((i + 1).to_string () + " " + lst[i].get_size ().to_string ());
- }
- prinl (".");
- return true;
- }
-
- string data_filename (string name) {
- return data_dirname () + "/" + name;
- }
-
- File data_file (string name) {
- return File.new_for_path (data_filename (name));
- }
-
- protected override bool retr (string[] cmd) throws GLib.IOError, GLib.Error {
- var i = cmd[1].to_int ();
- if (lst[i] == null) return false;
- ok (lst[i].get_size ().to_string () + " octets");
- var file = data_file (lst[i].get_name ());
- try {
- var dis = new DataInputStream (file.read ());
- string line;
- while ((line = dis.read_line (null)) != null)
- prinl (line == "." ? ".." : line);
- prinl (".");
- return true;
- } catch (Error e) {
- stderr.printf ("%s\n", e.message);
- }
- return false;
- }
-
- protected override bool dele (string[] cmd) throws GLib.IOError, GLib.Error {
- var i = cmd[1].to_int ();
- data_file (lst[i].get_name ()).delete ();
- ok ("message " + i.to_string () + " deleted");
- return true;
- }
-
- protected override bool otherwise (string[] cmd) throws GLib.IOError, GLib.Error {
- return false;
- }
-}
-
-static bool on_incoming_connection (SocketConnection c) {
- var s = new Dirpop3Server (c);
- s.process_request_async.begin ();
- return true;
-}
-
-void main () {
- try {
- var srv = new SocketService ();
- srv.add_inet_port (3333, null); // TODO config port
- srv.incoming.connect (on_incoming_connection);
- srv.start ();
- new MainLoop ().run ();
- } catch (Error e) {
- stderr.printf ("%s\n", e.message);
- }
-}