iswydt.vala
changeset 0 e27ed261417d
child 1 76caf6a3f413
equal deleted inserted replaced
-1:000000000000 0:e27ed261417d
       
     1 
       
     2 class Config : Gee.HashMap<string,string> {
       
     3     public Config.from_file(string filename) {
       
     4         base();
       
     5         IOChannel cfg;
       
     6         /* try { */
       
     7             cfg = new IOChannel.file(filename,"r");
       
     8         /*} catch (FileError e) {
       
     9             File.new_for_path(filename);*/
       
    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 
       
    37     public enum State {
       
    38         DISCONNECTED,
       
    39         CONNECTING,
       
    40         AUTHENTICATING,
       
    41         CONNECTED
       
    42     }
       
    43 
       
    44     protected State _state;
       
    45 
       
    46     public State state {
       
    47         get { return _state; }
       
    48     }
       
    49 
       
    50     public signal void state_changed(State old_state, State new_state, string description);
       
    51     protected void _change_state(State new_state, string description) {
       
    52         var old_state = this._state;
       
    53         if (old_state != new_state) {
       
    54             this._state = new_state;
       
    55             stderr.printf("State changed %s -> %s : %s\n",old_state.to_string(), new_state.to_string(), description);
       
    56             state_changed(old_state, new_state, description);
       
    57         } else
       
    58             stderr.printf("State not changed %s : %s\n",old_state.to_string(), description);
       
    59     }
       
    60 
       
    61     public Account(Config cfg) {
       
    62         this.jid = cfg["jid"];
       
    63         this.password = cfg["password"];
       
    64 
       
    65         if (cfg.has_key("server"))
       
    66             this.server = cfg["server"];
       
    67         else
       
    68             this.server = cfg["jid"].split("@",2)[1];
       
    69 
       
    70         if (cfg.has_key("port"))
       
    71             this.port = cfg["port"].to_int();
       
    72         else
       
    73             this.port = Lm.Connection.DEFAULT_PORT;
       
    74 
       
    75         if (cfg.has_key("ssl"))
       
    76             this.ssl = (cfg["ssl"]=="yes");
       
    77         else
       
    78             this.ssl = false;
       
    79 
       
    80         if (cfg.has_key("resource"))
       
    81             this.resource = cfg["resource"];
       
    82         else
       
    83             this.resource = "iwydt";
       
    84 
       
    85         cn = new Lm.Connection(this.server);
       
    86         cn.set_disconnect_function((connection, reason) => {
       
    87             stderr.printf("Disconnected: %s\n",reason.to_string());
       
    88             _change_state(State.DISCONNECTED,reason.to_string());
       
    89         }, null);
       
    90         //cn.register_message_handler(
       
    91     }
       
    92     public void open() {
       
    93 
       
    94         stderr.printf("Connecting to %s:%d as %s\n",server,port,jid);
       
    95         cn.set_port(port);
       
    96         cn.set_jid(jid);
       
    97         _change_state(State.CONNECTING,"");
       
    98         try {
       
    99             var result = cn.open((connection, consuc) => {
       
   100                 stderr.printf("Connection success: %s\n",consuc.to_string());
       
   101                 if (consuc) {
       
   102                     _change_state(State.AUTHENTICATING,"");
       
   103                     stderr.printf("Authing\n");
       
   104                     cn.authenticate(jid.split("@",2)[0],password, resource, (connection, authsuc) => {
       
   105                         stderr.printf("Auth success: %s\n",authsuc.to_string());
       
   106                         if (authsuc) {
       
   107                             _change_state(State.CONNECTED,"");
       
   108                         } else
       
   109                             cn.close();
       
   110                     }, null);
       
   111                 } else {
       
   112                     _change_state(State.DISCONNECTED, "Connection not successful");
       
   113                 }
       
   114             }, null);
       
   115             if (!result)
       
   116                 _change_state(State.DISCONNECTED, "Open failed");
       
   117         } catch (Error e) {
       
   118             stderr.printf("Error: %s\n", e.message);
       
   119             _change_state(State.DISCONNECTED, e.message);
       
   120         }
       
   121     }
       
   122 
       
   123 }
       
   124     
       
   125 
       
   126 int main(string[] args) {
       
   127     if (args.length<2) {
       
   128         stderr.printf("Usage: %s <config file>\n",args[0]);
       
   129         return 1;
       
   130     }
       
   131     /*Log.set_handler("LM", (LogLevelFlags)65535, (domain, levels, message) => {
       
   132         stderr.printf("--- %s\n", message);
       
   133     });*/
       
   134     log("LM", LogLevelFlags.LEVEL_DEBUG, "HATE HATE");
       
   135     var cfg = new Config.from_file(args[1]);
       
   136     var loop = new MainLoop();
       
   137     var account = new Account(cfg);
       
   138     account.open();
       
   139     stdout.printf("Fuck yeah\n");
       
   140     loop.run();
       
   141     return 0;
       
   142 }