summaryrefslogtreecommitdiffstats
path: root/roboctl/lib/irc.js
blob: 5f904a741d9eceeeb58e03aca557096518faa7bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67

var Client = function (config) {
  var client = this;
  var net = require('net');
  var sys = require('sys');
  var log = function (x) {
    sys.puts('TCP server: ' + x);
  };

  client.connect = function (callback) {
    var stream = net.createConnection(config.port, config.server);
    stream.on('connect', function () {
      stream.write(
        'NICK ' + config.nick + '\n' +
        'USER ' + config.nick + ' 0 *:Karl Koch\n' +
        'JOIN ' + config.channel + '\n'
      );
      //client.write = function (text) {
      //  stream.write('PRIVMSG ' + config.channel + ' :' + text);
      //};
      client.write = msg_start_send;
      callback();
    });
    //stream.on('secure', function () {
    //});

    var msg = [];

    var msg_start_send = function (x) {
      client.write = msg_append;
      setTimeout(function () {
        var x = msg.join('\n') + '\n';
        msg = [];
        client.write = msg_start_send;
        stream.write('PRIVMSG ' + config.channel + ' :' + x);
      }, 1000);
    };

    var msg_append = function (x) {
      msg[msg.length] = x;
    };


    stream.on('data', function (data) {
      data = String(data);
      log('' + data + '');
      if (data.substring(0,4) === 'PING') {
        log('PONG!');
        stream.write('PONG ' + data.substring(4));
      }
    });
    //stream.on('end', function () {
    //});
    //stream.on('timeout', function () {
    //});
    //stream.on('drain', function () {
    //});
    //stream.on('error', function (exception) {
    //});
    //stream.on('clonse', function (exception) {
    //});
  };
};

exports.createClient = function (config) {
  return new Client(config);
};