Subversion Repositories HomeAutomation

Rev

Rev 1412 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  1.  
  2. function EggdropIRCpost(hostname, port, username, password)
  3. {
  4.     this.myPort = port;
  5.     this.myHostname = hostname;
  6.     this.myUsername = username;
  7.     this.myPassword = password;
  8.     this.myState = "DISCONNECTED";
  9. }
  10.  
  11. EggdropIRCpost.prototype.connect = function()
  12. {
  13.     if (this.mySocket)
  14.     {
  15.         this.mySocket.stop();
  16.     }
  17.    
  18.     var self = this;
  19.  
  20.     this.mySocket = new Socket(function(event, data) { self.socketCallback(event, data); }, this.myHostname, this.myPort, 0);
  21.     this.mySocket.start();
  22.     this.myState = "CONNECTING";
  23. }
  24.  
  25. EggdropIRCpost.prototype.disconnect = function()
  26. {
  27.     if (this.mySocket)
  28.     {
  29.         /* Just send text ".quit" and the host will disconnect us */
  30.         this.mySocket.send(".quit\n");
  31.         this.myState = "DISCONNECTING"
  32.     }
  33. }
  34.    
  35. EggdropIRCpost.prototype.post = function(channel, text)
  36. {
  37.     this.mySendChn = channel;
  38.     this.mySendText = text;
  39.    
  40.     if (this.mySocket && this.myState == "CONNECTED")
  41.     {
  42.         /* To send text to a channel send text ".say <channelname> <text>" */
  43.         this.mySocket.send(".say "+channel+" "+text+"\n");
  44.     }
  45.     else if (this.myState == "DISCONNECTED")
  46.     {
  47.         /* Create the callback which should be called from socketCallback when reaching state==CONNECTED */
  48.         this.myDataCallback = function() { self.DataCallback(); }
  49.         this.connect();
  50.     }
  51.     else if (this.myState == "CONNECTING")
  52.     {
  53.         this.myDataCallback = function() { self.DataCallback(); }
  54.     }
  55. }
  56.  
  57. EggdropIRCpost.prototype.DataCallback = function()
  58. {
  59.     if (this.mySocket && this.myState == "CONNECTED")
  60.     {
  61.         /* To send text to a channel send text ".say <channelname> <text>" */
  62.         this.mySocket.send(".say "+this.mySendChn+" "+this.mySendText+"\n");
  63.     }
  64.    
  65.     this.myDataCallback = null;
  66. }
  67.  
  68. EggdropIRCpost.prototype.mySocket = null;
  69. EggdropIRCpost.prototype.myState = null;
  70. EggdropIRCpost.prototype.myDataCallback = null;
  71. EggdropIRCpost.prototype.mySendChn = null;
  72. EggdropIRCpost.prototype.mySendText = null;
  73.  
  74. EggdropIRCpost.prototype.myHostname = null;
  75. EggdropIRCpost.prototype.myPort = null;
  76. EggdropIRCpost.prototype.myUsername = null;
  77. EggdropIRCpost.prototype.myPassword = null;
  78.  
  79. EggdropIRCpost.prototype.socketCallback = function(event, data)
  80. {
  81.     switch (event)
  82.     {
  83.     case "CONNECTED":
  84.     break;
  85.    
  86.     case "DATA":
  87.     /* When connected the host will first ask us for the username */
  88.     if (data.indexOf("Please enter your nickname") > -1 && this.myState == "CONNECTING")
  89.     {
  90.         this.mySocket.send(this.myUsername+"\n");
  91.     }
  92.     /* Then the host will ask us for the password */
  93.     if (data.indexOf("Enter your password") > -1 && this.myState == "CONNECTING")
  94.     {
  95.         this.mySocket.send(this.myPassword+"\n");
  96.     }
  97.     /* If credentials is correct we are connected */
  98.     if (data.indexOf("joined the party line") > -1 && this.myState == "CONNECTING")
  99.     {
  100.         this.myState = "CONNECTED";
  101.         if (this.myDataCallback)
  102.         {
  103.             this.myDataCallback();
  104.         }
  105.     }
  106.     break;
  107.    
  108.     case "RESET":
  109.     case "CLOSED":
  110.     case "DIED":
  111.     this.mySocket.stop();
  112.     this.mySocket = null;
  113.     this.myState = "DISCONNECTED";
  114.     break;
  115.     }
  116. }
  117.  
  118.