iswydt.vala
changeset 1 76caf6a3f413
parent 0 e27ed261417d
child 2 4e050075fab9
equal deleted inserted replaced
0:e27ed261417d 1:76caf6a3f413
     1 
     1 
     2 class Config : Gee.HashMap<string,string> {
     2 class Connection : Object {
     3     public Config.from_file(string filename) {
     3     public Lm.Connection cn;
     4         base();
     4     public string jid;
     5         IOChannel cfg;
     5     public string password;
     6         /* try { */
     6     public string resource;
     7             cfg = new IOChannel.file(filename,"r");
     7     public string server;
     8         /*} catch (FileError e) {
     8 	public int port;
     9             File.new_for_path(filename);*/
     9     public bool ssl;
    10         do {
       
    11             string str;
       
    12             size_t length, termpos;
       
    13             IOStatus stat = cfg.read_line(out str,out length,out termpos);
       
    14             if (stat == IOStatus.NORMAL) {
       
    15                 string[] parts = str[0:(long)termpos].split("=",2);
       
    16                 if (parts.length==2) {
       
    17                     this[parts[0]] = parts[1];
       
    18                     stdout.printf("LOL: key: %s value: %s\n",parts[0],parts[1]);
       
    19                 }
       
    20             } else
       
    21                 break;
       
    22         } while (true);
       
    23 
       
    24 
       
    25     }
       
    26 }
       
    27 
       
    28 class Account : Object {
       
    29     protected Lm.Connection cn;
       
    30     protected string jid;
       
    31     protected string password;
       
    32     protected string resource;
       
    33     protected string server;
       
    34 	protected int port;
       
    35     protected bool ssl;
       
    36 
    10 
    37     public enum State {
    11     public enum State {
    38         DISCONNECTED,
    12         DISCONNECTED,
    39         CONNECTING,
    13         CONNECTING,
    40         AUTHENTICATING,
    14         AUTHENTICATING,
    41         CONNECTED
    15         CONNECTED
    42     }
    16     }
    43 
    17         
    44     protected State _state;
    18     protected State _state;
    45 
    19 
    46     public State state {
    20     public State state {
    47         get { return _state; }
    21         get { return _state; }
    48     }
    22     }
       
    23 
       
    24     public Module[] modules;
    49 
    25 
    50     public signal void state_changed(State old_state, State new_state, string description);
    26     public signal void state_changed(State old_state, State new_state, string description);
    51     protected void _change_state(State new_state, string description) {
    27     protected void _change_state(State new_state, string description) {
    52         var old_state = this._state;
    28         var old_state = this._state;
    53         if (old_state != new_state) {
    29         if (old_state != new_state) {
    56             state_changed(old_state, new_state, description);
    32             state_changed(old_state, new_state, description);
    57         } else
    33         } else
    58             stderr.printf("State not changed %s : %s\n",old_state.to_string(), description);
    34             stderr.printf("State not changed %s : %s\n",old_state.to_string(), description);
    59     }
    35     }
    60 
    36 
    61     public Account(Config cfg) {
    37     public Connection(Config cfg) {
    62         this.jid = cfg["jid"];
    38         this.modules = new Module[10];
    63         this.password = cfg["password"];
    39         this.jid = cfg["server","jid"];
       
    40         this.password = cfg["server","password"];
    64 
    41 
    65         if (cfg.has_key("server"))
    42         this.server = cfg.get_default("server", "server", this.jid.split("@",2)[1]);
    66             this.server = cfg["server"];
       
    67         else
       
    68             this.server = cfg["jid"].split("@",2)[1];
       
    69 
    43 
    70         if (cfg.has_key("port"))
    44         if (cfg.has_key("server", "port"))
    71             this.port = cfg["port"].to_int();
    45             this.port = cfg["server", "port"].to_int();
    72         else
    46         else
    73             this.port = Lm.Connection.DEFAULT_PORT;
    47             this.port = Lm.Connection.DEFAULT_PORT;
    74 
    48 
    75         if (cfg.has_key("ssl"))
    49         this.ssl = (cfg.get_default("server", "ssl", "yes")=="yes");
    76             this.ssl = (cfg["ssl"]=="yes");
       
    77         else
       
    78             this.ssl = false;
       
    79 
    50 
    80         if (cfg.has_key("resource"))
    51         this.resource = cfg.get_default("server", "resource", "iswydt");
    81             this.resource = cfg["resource"];
       
    82         else
       
    83             this.resource = "iwydt";
       
    84 
    52 
    85         cn = new Lm.Connection(this.server);
    53         cn = new Lm.Connection(this.server);
    86         cn.set_disconnect_function((connection, reason) => {
    54         cn.set_disconnect_function((connection, reason) => {
    87             stderr.printf("Disconnected: %s\n",reason.to_string());
    55             stderr.printf("Disconnected: %s\n",reason.to_string());
    88             _change_state(State.DISCONNECTED,reason.to_string());
    56             _change_state(State.DISCONNECTED,reason.to_string());
    89         }, null);
    57         }, null);
    90         //cn.register_message_handler(
    58         var msg_handler = new Lm.MessageHandler((handler, connection, message) => {
       
    59             stdout.printf("Got msg\n");
       
    60             var node = message.get_node();
       
    61             var body = node.find_child("body");
       
    62             if (body!=null) {
       
    63                 stdout.printf("MSG %s to %s: %s\n", node.get_attribute("from"), node.get_attribute("to"), body.get_value());
       
    64             }
       
    65             return Lm.HandlerResult.ALLOW_MORE_HANDLERS;
       
    66         }, null);
       
    67         cn.register_message_handler(msg_handler, Lm.MessageType.MESSAGE, Lm.HandlerPriority.NORMAL);
       
    68         /*var muc_handler = new Lm.MessageHandler((handler, connection, message) => {
       
    69             var node = message.node;
       
    70             var from = node.get_attribute("from");
       
    71             if (from != null) {
       
    72                 var froms = from.split("/",2);
       
    73                 //stdout.printf("From: %s %s\n",node.to_string(),message.get_type().to_string());
       
    74                 if (rooms.has_key(froms[0])) {
       
    75                     return rooms[froms[0]].muc_handler(handler, connection, message);
       
    76                 }
       
    77             }
       
    78             return Lm.HandlerResult.ALLOW_MORE_HANDLERS;
       
    79         }, null);
       
    80         cn.register_message_handler(muc_handler, Lm.MessageType.MESSAGE, Lm.HandlerPriority.NORMAL);
       
    81         cn.register_message_handler(muc_handler, Lm.MessageType.PRESENCE, Lm.HandlerPriority.NORMAL);
       
    82         cn.register_message_handler(muc_handler, Lm.MessageType.IQ, Lm.HandlerPriority.NORMAL);*/
       
    83         var module = new ModuleMuc(cfg, this);
       
    84         modules = {module};
       
    85         state_changed.connect( (olds, news, desc) => {
       
    86             if (news == Connection.State.CONNECTED) {
       
    87                 cn.send_raw("<message to='stiletto@stiletto.name'><body>ТХБ</body></message>");
       
    88                 /*var room = new Conference(this, "говнохост@conference.blasux.ru", "Ζαλυπα");
       
    89                 room.join("Oh hai");
       
    90                 this.rooms[room.jid] = room;*/
       
    91             }
       
    92         });
    91     }
    93     }
    92     public void open() {
    94     public void open() {
    93 
    95 
    94         stderr.printf("Connecting to %s:%d as %s\n",server,port,jid);
    96         stderr.printf("Connecting to %s:%d as %s\n",server,port,jid);
    95         cn.set_port(port);
    97         cn.set_port(port);
   126 int main(string[] args) {
   128 int main(string[] args) {
   127     if (args.length<2) {
   129     if (args.length<2) {
   128         stderr.printf("Usage: %s <config file>\n",args[0]);
   130         stderr.printf("Usage: %s <config file>\n",args[0]);
   129         return 1;
   131         return 1;
   130     }
   132     }
   131     /*Log.set_handler("LM", (LogLevelFlags)65535, (domain, levels, message) => {
   133     Log.set_handler("Muc", (LogLevelFlags)65535, (domain, levels, message) => {
   132         stderr.printf("--- %s\n", message);
   134         stderr.printf("--- %s\n", message);
   133     });*/
   135     });
   134     log("LM", LogLevelFlags.LEVEL_DEBUG, "HATE HATE");
   136     log("LM", LogLevelFlags.LEVEL_DEBUG, "HATE HATE");
   135     var cfg = new Config.from_file(args[1]);
   137     var cfg = new Config.from_file(args[1]);
   136     var loop = new MainLoop();
   138     var loop = new MainLoop();
   137     var account = new Account(cfg);
   139     var account = new Connection(cfg);
   138     account.open();
   140     account.open();
   139     stdout.printf("Fuck yeah\n");
   141     stdout.printf("Fuck yeah\n");
   140     loop.run();
   142     loop.run();
   141     return 0;
   143     return 0;
   142 }
   144 }