class Config : Gee.HashMap<string,string> {
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 <config file>\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;
}