public class CoreConnector { //unowned Thread thread; //bool running; //Erl.Node node; //Erl.Connection con; //Erl.Term self; Eva.PacketHandler con; public signal void update_contact(Contact contact); public signal void new_conversation(string jid); public signal void receive_message(string jid, string type, string message); public signal void sent_message(string jid, string type, string message); /*private class TermStore { public Erl.Term term; }*/ /*static construct { Erl.init(); }*/ public CoreConnector() { //running = false; } public void start() { /*node = Erl.Node("ephraim-gtk", "magiccookie", 0); con = node.connect("ephraim-core@avalon.local"); self = Erl.mk_self_pid(node); con.reg_send("ephraim", Erl.format("{register_ui,~w}", self)); try { thread = Thread.create(receive, true); return true; } catch(ThreadError e) { return false; }*/ con = new Eva.PacketHandler(new UnixInputStream(3, true), new UnixOutputStream(4, true), 4); con.received_term.connect(handle_term); con.start(); } public void stop() { /*if(!running) return; running = false; receive.end();*/ //con.reg_send("ephraim", Erl.format("{unregister_ui,~w}", self)); con.send(new Eva.Atom("stop")); } private static string from_utf8(Eva.Binary bin) { string ret = ((string)bin.data).ndup(bin.len); warn_if_fail(ret.validate()); return ret; } private void handle_term(Eva.Term term) { Gee.Map match; if((match = term.match(Eva.parse("{roster_update,JID,Name,Subscription,Groups,Resources,Avatar}"))) != null) { Eva.Binary jid_term = match["JID"] as Eva.Binary; if(jid_term == null) // TODO Debug output return; string jid = from_utf8(jid_term); Eva.Binary name_term = match["Name"] as Eva.Binary; string? name; if (name_term is Eva.Binary) name = from_utf8(name_term); else name = null; Contact contact = new Contact(jid, name); Eva.Cons groups = match["Groups"] as Eva.Cons; while(groups != null) { Eva.Binary group_term = groups.head as Eva.Binary; if(group_term != null) { string group = Eva.binary_to_string(group_term.data, group_term.len); contact.add_group(group); } groups = groups.tail as Eva.Cons; } Eva.Cons resources = match["Resources"] as Eva.Cons; while(resources != null) { Gee.Map r; if((r = resources.head.match(Eva.parse("{Name,{resource_entry,Priority,Show,Status}}"))) != null) { Eva.Binary rname_term = r["Name"] as Eva.Binary; Eva.Numeric prio_term = r["Priority"] as Eva.Numeric; Eva.Atom show_term = r["Show"] as Eva.Atom; Eva.Binary status_term = r["Status"] as Eva.Binary; if(rname_term != null && prio_term != null && show_term != null) { string rname = from_utf8(rname_term); int prio = prio_term.int_value; Contact.Show show = Contact.Show.UNDEFINED; switch(show_term.value) { case "online": show = Contact.Show.ONLINE; break; case "away": show = Contact.Show.AWAY; break; case "chat": show = Contact.Show.CHAT; break; case "dnd": show = Contact.Show.DND; break; case "xa": show = Contact.Show.XA; break; } string? status = null; if(status_term != null) status = from_utf8(status_term); contact.update_resource(rname, new Contact.Resource(prio, show, status)); } } resources = resources.tail as Eva.Cons; } Gee.Map avatar = match["Avatar"].match(Eva.parse("{avatar,Data}")); if(avatar != null) { Eva.Binary avatarData = avatar["Data"] as Eva.Binary; if(avatarData != null) { InputStream avatarStream = new MemoryInputStream.from_data(avatarData.data, avatarData.len, null); try { contact.avatar = new Gdk.Pixbuf.from_stream_at_scale(avatarStream, 32, 32, true, null); avatarStream.close(null); } catch(Error e) { // TODO Debug output } } } update_contact(contact); } else if((match = term.match(Eva.parse("{new_conversation,JID}"))) != null) { Eva.Binary jid_term = match["JID"] as Eva.Binary; if(jid_term == null) // TODO Debug output return; string jid = from_utf8(jid_term); new_conversation(jid); } else if((match = term.match(Eva.parse("{receive_message,JID,Type,Body}"))) != null) { Eva.Binary jid_term = match["JID"] as Eva.Binary; if(jid_term == null) // TODO Debug output return; string jid = from_utf8(jid_term); Eva.Atom type_term = match["Type"] as Eva.Atom; if(type_term == null) // TODO Debug output return; string type = type_term.value; Eva.Binary body_term = match["Body"] as Eva.Binary; if(body_term == null) // TODO Debug output return; string body = from_utf8(body_term); receive_message(jid, type, body); } else if((match = term.match(Eva.parse("{sent_message,JID,Type,Body}"))) != null) { Eva.Binary jid_term = match["JID"] as Eva.Binary; if(jid_term == null) // TODO Debug output return; string jid = from_utf8(jid_term); Eva.Atom type_term = match["Type"] as Eva.Atom; if(type_term == null) // TODO Debug output return; string type = type_term.value; Eva.Binary body_term = match["Body"] as Eva.Binary; if(body_term == null) // TODO Debug output return; string body = from_utf8(body_term); sent_message(jid, type, body); } else { stdout.printf("%s\n", term.to_string()); } } public void start_conversation(string jid) { char[] jid_utf8 = jid.to_utf8(); con.send(Eva.parse("{start_conversation,~w}", new Eva.Binary(jid_utf8))); } public void send_message(string jid, string type, string message) { char[] jid_utf8 = jid.to_utf8(); char[] message_utf8 = message.to_utf8(); con.send(Eva.parse("{send_message,~w,~a,~w}", new Eva.Binary(jid_utf8), type, new Eva.Binary(message_utf8))); } }