Subversion Repositories HomeAutomation

Rev

Rev 1412 | 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.myEggdropIRCpost.postAndDisconnect("#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.myQuitCallback = null;
  38. EggdropIRCpost.prototype.mySendChn = null;
  39. EggdropIRCpost.prototype.mySendText = null;
  40.  
  41. EggdropIRCpost.prototype.myHostname = null;
  42. EggdropIRCpost.prototype.myPort = null;
  43. EggdropIRCpost.prototype.myUsername = null;
  44. EggdropIRCpost.prototype.myPassword = null;
  45.  
  46.  
  47. EggdropIRCpost.prototype.connect = function()
  48. {
  49.     if (this.mySocket)
  50.     {
  51.         this.mySocket.stop();
  52.     }
  53.     var self = this;
  54.  
  55.     this.mySocket = new Socket(function(event, data) { self.socketCallback(event, data); }, this.myHostname, this.myPort, 0);
  56.     this.mySocket.start();
  57.     this.myState = "CONNECTING";
  58. }
  59.  
  60. EggdropIRCpost.prototype.disconnect = function()
  61. {
  62.     if (this.mySocket && this.myState == "CONNECTED")
  63.     {
  64.         /* Just send text ".quit" and the host will disconnect us */
  65.         this.mySocket.send(".quit\n");
  66.         this.myState = "DISCONNECTING"
  67.     }
  68.     else if (this.myState == "CONNECTING")
  69.     {
  70.         var self = this;
  71.         this.myQuitCallback = function() { self.disconnect(); }
  72.     }
  73. }
  74.    
  75. EggdropIRCpost.prototype.post = function(channel, text)
  76. {
  77.     this.mySendChn = channel;
  78.     this.mySendText = text;
  79.     var self = this;
  80.    
  81.     if (this.mySocket && this.myState == "CONNECTED")
  82.     {
  83.         /* To send text to a channel send text ".say <channelname> <text>" */
  84.         this.mySocket.send(".say "+channel+" "+text+"\n");
  85.         if (this.myQuitCallback)
  86.         {
  87.             this.myQuitCallback();
  88.             this.myQuitCallback = null;
  89.         }
  90.     }
  91.     else if (this.myState == "DISCONNECTED")
  92.     {
  93.         /* Create the callback which should be called from socketCallback when reaching state==CONNECTED */
  94.         this.myDataCallback = function() { self.DataCallback(); }
  95.         this.connect();
  96.     }
  97.     else if (this.myState == "CONNECTING")
  98.     {
  99.         this.myDataCallback = function() { self.DataCallback(); }
  100.     }
  101. }
  102.  
  103. EggdropIRCpost.prototype.postAndDisconnect = function(channel, text)
  104. {
  105.     var self = this;
  106.     this.myQuitCallback = function() { self.disconnect(); }
  107.     this.post(channel, text);
  108. }
  109.  
  110. EggdropIRCpost.prototype.DataCallback = function()
  111. {
  112.     if (this.mySocket && this.myState == "CONNECTED")
  113.     {
  114.         /* To send text to a channel send text ".say <channelname> <text>" */
  115.         this.mySocket.send(".say "+this.mySendChn+" "+this.mySendText+"\n");
  116.     }
  117.     if (this.myQuitCallback)
  118.     {
  119.         this.myQuitCallback();
  120.         this.myQuitCallback = null;
  121.     }
  122.     this.myDataCallback = null;
  123. }
  124.  
  125. EggdropIRCpost.prototype.socketCallback = function(event, data)
  126. {
  127.     switch (event)
  128.     {
  129.     case "CONNECTED":
  130.     break;
  131.    
  132.     case "DATA":
  133.     /* When connected the host will first ask us for the username */
  134.     if (data.indexOf("Please enter your nickname") > -1 && this.myState == "CONNECTING")
  135.     {
  136.         this.mySocket.send(this.myUsername+"\n");
  137.     }
  138.     /* Then the host will ask us for the password */
  139.     if (data.indexOf("Enter your password") > -1 && this.myState == "CONNECTING")
  140.     {
  141.         this.mySocket.send(this.myPassword+"\n");
  142.     }
  143.     /* If credentials is correct we are connected */
  144.     if (data.indexOf("joined the party line") > -1 && this.myState == "CONNECTING")
  145.     {
  146.         this.myState = "CONNECTED";
  147.         if (this.myDataCallback)
  148.         {
  149.             this.myDataCallback();
  150.         }
  151.     }
  152.     break;
  153.    
  154.     case "RESET":
  155.     case "CLOSED":
  156.     case "DIED":
  157.     this.mySocket.stop();
  158.     this.mySocket = null;
  159.     this.myState = "DISCONNECTED";
  160.     break;
  161.     }
  162. }
  163.  
  164.