Subversion Repositories HomeAutomation

Rev

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

  1. /*
  2. Usage:
  3. First create an EggdropIRCpost object, then when needed call post(channelname, text) for each line to post on IRC, when done disconnect with disconnect().
  4.  
  5. SensorPrint.prototype.myEggdropIRCpost = null;
  6. ...
  7. this.myEggdropIRCpost = new EggdropIRCpost("hostname", port, "username", "password");
  8. ...
  9. this.myEggdropIRCpost.post("#myplaychannel", "Hello world!");
  10. ...
  11. this.myEggdropIRCpost.disconnect();
  12.  
  13.     OR
  14. If you only intend to post one line you can use postAndDisconnect(channelname, text).
  15.  
  16. SensorPrint.prototype.myEggdropIRCpost = null;
  17. ...
  18. this.myEggdropIRCpost = new EggdropIRCpost("hostname", port, "username", "password");
  19. ...
  20. this.myEggdropIRCpostAndDisconnect.post("#myplaychannel", "Hello world!");
  21.  
  22.  
  23. */
  24.  
  25. function EggdropIRCpost(hostname, port, username, password)
  26. {
  27.     this.myPort = port;
  28.     this.myHostname = hostname;
  29.     this.myUsername = username;
  30.     this.myPassword = password;
  31.     this.myState = "DISCONNECTED";
  32. }
  33.  
  34. EggdropIRCpost.prototype.mySocket = null;
  35. EggdropIRCpost.prototype.myState = null;
  36. EggdropIRCpost.prototype.myDataCallback = null;
  37. EggdropIRCpost.prototype.mySendChn = null;
  38. EggdropIRCpost.prototype.mySendText = null;
  39.  
  40. EggdropIRCpost.prototype.myHostname = null;
  41. EggdropIRCpost.prototype.myPort = null;
  42. EggdropIRCpost.prototype.myUsername = null;
  43. EggdropIRCpost.prototype.myPassword = null;
  44.  
  45.  
  46. EggdropIRCpost.prototype.connect = function()
  47. {
  48.     if (this.mySocket)
  49.     {
  50.         this.mySocket.stop();
  51.     }
  52.     var self = this;
  53.  
  54.     this.mySocket = new Socket(function(event, data) { self.socketCallback(event, data); }, this.myHostname, this.myPort, 0);
  55.     this.mySocket.start();
  56.     this.myState = "CONNECTING";
  57. }
  58.  
  59. EggdropIRCpost.prototype.disconnect = function()
  60. {
  61.     if (this.mySocket)
  62.     {
  63.         /* Just send text ".quit" and the host will disconnect us */
  64.         this.mySocket.send(".quit\n");
  65.         this.myState = "DISCONNECTING"
  66.     }
  67. }
  68.    
  69. EggdropIRCpost.prototype.post = function(channel, text)
  70. {
  71.     this.mySendChn = channel;
  72.     this.mySendText = text;
  73.     var self = this;
  74.    
  75.     if (this.mySocket && this.myState == "CONNECTED")
  76.     {
  77.         /* To send text to a channel send text ".say <channelname> <text>" */
  78.         this.mySocket.send(".say "+channel+" "+text+"\n");
  79.     }
  80.     else if (this.myState == "DISCONNECTED")
  81.     {
  82.         /* Create the callback which should be called from socketCallback when reaching state==CONNECTED */
  83.         this.myDataCallback = function() { self.DataCallback(); }
  84.         this.connect();
  85.     }
  86.     else if (this.myState == "CONNECTING")
  87.     {
  88.         this.myDataCallback = function() { self.DataCallback(); }
  89.     }
  90. }
  91.  
  92. EggdropIRCpost.prototype.postAndDisconnect = function(channel, text)
  93. {
  94.     this.post(channel, text);
  95.     this.disconnect();
  96.    
  97. }
  98.  
  99. EggdropIRCpost.prototype.DataCallback = function()
  100. {
  101.     if (this.mySocket && this.myState == "CONNECTED")
  102.     {
  103.         /* To send text to a channel send text ".say <channelname> <text>" */
  104.         this.mySocket.send(".say "+this.mySendChn+" "+this.mySendText+"\n");
  105.     }
  106.    
  107.     this.myDataCallback = null;
  108. }
  109.  
  110. EggdropIRCpost.prototype.socketCallback = function(event, data)
  111. {
  112.     switch (event)
  113.     {
  114.     case "CONNECTED":
  115.     break;
  116.    
  117.     case "DATA":
  118.     /* When connected the host will first ask us for the username */
  119.     if (data.indexOf("Please enter your nickname") > -1 && this.myState == "CONNECTING")
  120.     {
  121.         this.mySocket.send(this.myUsername+"\n");
  122.     }
  123.     /* Then the host will ask us for the password */
  124.     if (data.indexOf("Enter your password") > -1 && this.myState == "CONNECTING")
  125.     {
  126.         this.mySocket.send(this.myPassword+"\n");
  127.     }
  128.     /* If credentials is correct we are connected */
  129.     if (data.indexOf("joined the party line") > -1 && this.myState == "CONNECTING")
  130.     {
  131.         this.myState = "CONNECTED";
  132.         if (this.myDataCallback)
  133.         {
  134.             this.myDataCallback();
  135.         }
  136.     }
  137.     break;
  138.    
  139.     case "RESET":
  140.     case "CLOSED":
  141.     case "DIED":
  142.     this.mySocket.stop();
  143.     this.mySocket = null;
  144.     this.myState = "DISCONNECTED";
  145.     break;
  146.     }
  147. }
  148.  
  149.