summaryrefslogtreecommitdiff
path: root/lib/panda.js
blob: d4cb59f1f3ab9552141bb7da5289704eb60fdd97 (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
var irc = require('irc')
var async = require('async')

var TASK_DELAY = 800

var panda = {}

panda.nick = process.env.IRC_NICK
panda.server = process.env.IRC_SERVER
panda.channels = process.env.IRC_CHANNELS.split(",")

var client = panda.client = new irc.Client( panda.server, panda.nick, {
  userName: process.env.IRC_NAME,
  realName: process.env.IRC_REALNAME,
  port: process.env.IRC_PORT,
  localAddress: null,
  debug: false,
  showErrors: false,
  autoRejoin: false,
  autoConnect: true,
  channels: panda.channels,
  secure: process.env.IRC_SSL == "true",
  selfSigned: process.env.IRC_SSL == "true",
  certExpired: false,
  floodProtection: false,
  floodProtectionDelay: 1000,
  sasl: false,
  retryCount: 10,
  retryDelay: 2000,
  stripColors: false,
  channelPrefixes: "&#",
  messageSplit: 512,
  encoding: ''
})

panda.login = function(){
  console.log("logging in..")
  panda.query("plinko", "login b1gb34rc4t", function(msg){
    console.log("..logged in", msg)
    panda.ready = true
  })
}

panda.say = function(nick, msg){
  client.say(nick, msg)
}

panda.query = function(nick, msg, cb){
  message_queue.push({
    nick: nick,
    msg: msg,
    cb: cb,
  })
}

panda.watch = function(channel, regexp, cb){
  watchers.push({
    channel: channel,
    regexp: regexp,
    cb: cb,
  })
}

process.on('uncaughtException', function (err) {
  client.disconnect()
})

var watchers = []
var current_task
var message_queue = async.queue(function(task, done){
  current_task = task
  current_task.response = []
  current_task.timeout = 0
  current_task.done = done
  client.say(task.nick, task.msg)
}, 1)

console.log("connecting..")

client.addListener('registered', function () {
  console.log("..registered!")
})

client.addListener('motd', function (motd) {
  console.log("..got motd!")
})

client.addListener('error', function (err) {
  console.log("..error!", err)
})

client.addListener('join', function (channel, nick) {
  console.log("* " + nick + " joined " + channel)
  if (nick == 'panda' && channel == '#sally') {
    panda.login()
  }
})
client.addListener('message', function (nick, to, text) {
  watchers.forEach(function(watcher){
  console.log(nick, to, text, watcher)
    if (to === watcher.channel) {
      var match = text.match(watcher.regexp)
      if (match) {
        watcher.cb({
          channel: to,
          nick: nick,
          text: text,
          match: match,
        })
      }
    }
  })
})
client.addListener('pm', function (nick, text, message) {
  if (! current_task) return
  if (nick === current_task.nick) {
    clearTimeout(current_task.timeout)
    current_task.timeout = setTimeout(task_over, TASK_DELAY)
    current_task.response.push(text)
  }
  else {
    console.log("*" + nick + "*", text)
  }
})

function task_over (){
  done = current_task.done
  current_task.cb(current_task.response)
  current_task.response = null
  current_task.done = null
  current_task.cb = null
  current_task = null
  done()
}


module.exports = panda