iswydt.vala
changeset 3 dd7a02c6d476
parent 2 4e050075fab9
child 7 a49fb1ec5e2a
--- a/iswydt.vala	Tue Oct 23 18:33:49 2012 +0400
+++ b/iswydt.vala	Tue Oct 23 23:50:28 2012 +0400
@@ -21,7 +21,7 @@
         get { return _state; }
     }
 
-    public Module[] modules;
+    public Gee.ArrayList<Module> modules;
 
     public signal void state_changed(State old_state, State new_state, string description);
     public signal void check_time();
@@ -36,7 +36,7 @@
     }
 
     public Connection(Config cfg) {
-        this.modules = new Module[10];
+        this.modules = new Gee.ArrayList<Module>();
         this.jid = cfg["server","jid"];
         this.password = cfg["server","password"];
 
@@ -81,8 +81,6 @@
         cn.register_message_handler(muc_handler, Lm.MessageType.MESSAGE, Lm.HandlerPriority.NORMAL);
         cn.register_message_handler(muc_handler, Lm.MessageType.PRESENCE, Lm.HandlerPriority.NORMAL);
         cn.register_message_handler(muc_handler, Lm.MessageType.IQ, Lm.HandlerPriority.NORMAL);*/
-        var module = new ModuleMuc(cfg, this);
-        modules = {module};
         state_changed.connect( (olds, news, desc) => {
             if (news == Connection.State.CONNECTED) {
                 cn.send_raw("<message to='stiletto@stiletto.name'><body>ТХБ</body></message>");
@@ -96,6 +94,9 @@
                 open();
         });
     }
+    public void add_module(Module module) {
+        modules.add(module);
+    }
     public void open() {
 
         stderr.printf("Connecting to %s:%d as %s\n",server,port,jid);
@@ -126,10 +127,15 @@
             _change_state(State.DISCONNECTED, e.message);
         }
     }
-
+    public void close() {
+        cn.close();
+    }
 }
     
 
+static Connection account;
+static MainLoop loop;
+
 int main(string[] args) {
     if (args.length<2) {
         stderr.printf("Usage: %s <config file>\n",args[0]);
@@ -138,10 +144,13 @@
     Log.set_handler("Muc", (LogLevelFlags)65535, (domain, levels, message) => {
         stderr.printf("--- %s\n", message);
     });
+    Log.set_handler("muc_log", (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 Connection(cfg);
+    loop = new MainLoop();
+    account = new Connection(cfg);
     //account.open();
     stdout.printf("Fuck yeah\n");
     var checktimer = new TimeoutSource(2000);
@@ -151,6 +160,24 @@
         return true;
     });
     checktimer.attach(loop.get_context());
+
+    account.add_module(new ModuleMuc(cfg, account));
+    account.add_module(new ModuleMucLog(cfg, account));
+
+    Posix.sigaction_t action = Posix.sigaction_t();
+    action.sa_handler = ((i) => {
+        if (account.state != Connection.State.CONNECTED)
+            loop.quit();
+        else {
+            account.state_changed.connect( (olds, news, desc) => {
+                if (news == Connection.State.DISCONNECTED)
+                    loop.quit();
+            });
+            account.close();
+        }
+    });
+    Posix.sigaction(Posix.SIGINT, action, null);
+
     loop.run();
     return 0;
 }