# HG changeset patch # User Stiletto # Date 1349437279 -14400 # Node ID e27ed261417d714e8425db5fe72398fbcc48e3b5 Initial commit diff -r 000000000000 -r e27ed261417d .hgignore --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/.hgignore Fri Oct 05 15:41:19 2012 +0400 @@ -0,0 +1,3 @@ +^config.ini$ +^iswydt-bot$ +.swp$ diff -r 000000000000 -r e27ed261417d Makefile --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Makefile Fri Oct 05 15:41:19 2012 +0400 @@ -0,0 +1,10 @@ +all: iswydt-bot + +VALAC = valac +LIBS := gee-1.0 loudmouth-1.0 +VALALIBS := $(patsubst %, --pkg %, $(LIBS)) +VFLAGS = + +iswydt-bot: iswydt.vala + $(VALAC) $(VFLAGS) $(VALALIBS) -o $@ $^ + diff -r 000000000000 -r e27ed261417d config.ini.example --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/config.ini.example Fri Oct 05 15:41:19 2012 +0400 @@ -0,0 +1,8 @@ +[default] +jid=fuck@example.com +password=shit +server=talk.example.com +resource=iswydt +port=5222 +ssl=yes + diff -r 000000000000 -r e27ed261417d iswydt.vala --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/iswydt.vala Fri Oct 05 15:41:19 2012 +0400 @@ -0,0 +1,142 @@ + +class Config : Gee.HashMap { + public Config.from_file(string filename) { + base(); + IOChannel cfg; + /* try { */ + cfg = new IOChannel.file(filename,"r"); + /*} catch (FileError e) { + File.new_for_path(filename);*/ + do { + string str; + size_t length, termpos; + IOStatus stat = cfg.read_line(out str,out length,out termpos); + if (stat == IOStatus.NORMAL) { + string[] parts = str[0:(long)termpos].split("=",2); + if (parts.length==2) { + this[parts[0]] = parts[1]; + stdout.printf("LOL: key: %s value: %s\n",parts[0],parts[1]); + } + } else + break; + } while (true); + + + } +} + +class Account : Object { + protected Lm.Connection cn; + protected string jid; + protected string password; + protected string resource; + protected string server; + protected int port; + protected bool ssl; + + public enum State { + DISCONNECTED, + CONNECTING, + AUTHENTICATING, + CONNECTED + } + + protected State _state; + + public State state { + get { return _state; } + } + + public signal void state_changed(State old_state, State new_state, string description); + protected void _change_state(State new_state, string description) { + var old_state = this._state; + if (old_state != new_state) { + this._state = new_state; + stderr.printf("State changed %s -> %s : %s\n",old_state.to_string(), new_state.to_string(), description); + state_changed(old_state, new_state, description); + } else + stderr.printf("State not changed %s : %s\n",old_state.to_string(), description); + } + + public Account(Config cfg) { + this.jid = cfg["jid"]; + this.password = cfg["password"]; + + if (cfg.has_key("server")) + this.server = cfg["server"]; + else + this.server = cfg["jid"].split("@",2)[1]; + + if (cfg.has_key("port")) + this.port = cfg["port"].to_int(); + else + this.port = Lm.Connection.DEFAULT_PORT; + + if (cfg.has_key("ssl")) + this.ssl = (cfg["ssl"]=="yes"); + else + this.ssl = false; + + if (cfg.has_key("resource")) + this.resource = cfg["resource"]; + else + this.resource = "iwydt"; + + cn = new Lm.Connection(this.server); + cn.set_disconnect_function((connection, reason) => { + stderr.printf("Disconnected: %s\n",reason.to_string()); + _change_state(State.DISCONNECTED,reason.to_string()); + }, null); + //cn.register_message_handler( + } + public void open() { + + stderr.printf("Connecting to %s:%d as %s\n",server,port,jid); + cn.set_port(port); + cn.set_jid(jid); + _change_state(State.CONNECTING,""); + try { + var result = cn.open((connection, consuc) => { + stderr.printf("Connection success: %s\n",consuc.to_string()); + if (consuc) { + _change_state(State.AUTHENTICATING,""); + stderr.printf("Authing\n"); + cn.authenticate(jid.split("@",2)[0],password, resource, (connection, authsuc) => { + stderr.printf("Auth success: %s\n",authsuc.to_string()); + if (authsuc) { + _change_state(State.CONNECTED,""); + } else + cn.close(); + }, null); + } else { + _change_state(State.DISCONNECTED, "Connection not successful"); + } + }, null); + if (!result) + _change_state(State.DISCONNECTED, "Open failed"); + } catch (Error e) { + stderr.printf("Error: %s\n", e.message); + _change_state(State.DISCONNECTED, e.message); + } + } + +} + + +int main(string[] args) { + if (args.length<2) { + stderr.printf("Usage: %s \n",args[0]); + return 1; + } + /*Log.set_handler("LM", (LogLevelFlags)65535, (domain, levels, message) => { + stderr.printf("--- %s\n", message); + });*/ + log("LM", LogLevelFlags.LEVEL_DEBUG, "HATE HATE"); + var cfg = new Config.from_file(args[1]); + var loop = new MainLoop(); + var account = new Account(cfg); + account.open(); + stdout.printf("Fuck yeah\n"); + loop.run(); + return 0; +}