Subversion Repositories HomeAutomation

Rev

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