summaryrefslogtreecommitdiff
path: root/lib/panda.js
blob: 0a6b520ea42fa94675586bf51d68b3194449ecaf (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
var irc = require('irc')
var async = require('async')

var TASK_DELAY = 800

var panda = {}

var client = panda.client = new irc.Client( process.env.IRC_SERVER, process.env.IRC_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: process.env.IRC_CHANNELS.split(","),
  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.query = function(msg, cb){
  message_queue.push({
    msg: msg,
    cb: cb,
  })
}

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

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("plinko", task.msg)
}, 1)

console.log("connecting..")

client.addListener('registered', function (message) {
  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, message) {
  console.log("* " + nick + " joined " + channel)
  if (nick == 'panda' && channel == '#sally') {
    console.log("logging in..")
    panda.query("login b1gb34rc4t", function(msg){
      console.log("..logged in", msg)
      panda.ready = true
    })
  }
})
client.addListener('pm', function (nick, text, message) {
  if (! current_task) return
  if (nick === "plinko") {
    clearTimeout(current_task.timeout)
    current_task.timeout = setTimeout(task_over, TASK_DELAY)
    current_task.response.push(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