-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
executable file
·130 lines (115 loc) · 3.4 KB
/
index.js
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
#!/usr/bin/env node
var irc = require('irc')
var minimist = require('minimist')
var hypercore = require('hypercore')
var discovery = require('hyperdiscovery')
var extend = require('xtend')
var prettyTime = require('pretty-time')
var argv = minimist(process.argv.slice(2), {
alias: {
tail: 'key',
channel: 'c',
echoChannel: 'echo',
cwd: 'd',
server: 's',
name: 'n',
port: 'p',
ircPort: 'irc-port'
},
default: {
port: 3282,
cwd: 'standup-data',
name: 'standup-bot',
server: 'irc.freenode.net'
},
boolean: []
})
var started = process.hrtime()
var client = null
var server = null
var feed = hypercore(argv.cwd, argv.key, {valueEncoding: 'json'})
feed.ready(function () {
discovery(feed, {live: true})
console.log(`Sharing feed ${feed.key.toString('hex')}`)
if (argv.channel || argv.echoChannel) joinIrc()
})
function joinIrc () {
var channels = []
if (argv.channel) channels.push(argv.channel)
if (argv.echoChannel) channels.push(argv.echoChannel)
var ircOpts = extend({}, argv, {
channels: channels,
retryCount: 1000,
autoRejoin: true
})
ircOpts.port = argv.ircPort
console.log('Connecting to IRC', argv.server, 'as', argv.name)
client = new irc.Client(argv.server, argv.name, ircOpts)
client.on('registered', function (msg) {
console.log('Connected to IRC, listening for messages')
})
client.on('error', function (err) {
console.error('IRC Error', err)
})
if (argv.echoChannel) {
if (!argv.tail) echoFeed()
feed.on('sync', echoFeed) // do not echo old data
}
if (argv.channel) {
client.on('message', function (from, to, message) {
var op = parse(message, from)
var channel = (to === argv.name) ? from : argv.channel
if (!op) {
if (message.indexOf('!standup') === -1) return
var err = new Error('Could not parse standup message.')
return sendMessage(err, channel)
}
switch (op.command) {
case 'standup':
delete op.command // don't need this in our hypercore feed
feed.append(op, function (err) {
if (err) return sendMessage(err, channel)
})
return
case 'status':
return status(function (err, msg) {
sendMessage(err, channel, msg)
})
default:
console.error(op, 'bad command')
// sendMessage(new Error('Did not understand your command. Sad beep boop.'), channel)
return
}
})
}
}
function sendMessage (err, channel, msg) {
if (err) return client.say(channel, 'Error: ' + err.message)
client.say(channel, msg)
}
function status (cb) {
var msg = `Uptime: ${prettyTime(process.hrtime(started))},`
if (feed.length) msg += ` Standups: ${feed.length},`
msg += ` Key: ${feed.key.toString('hex')}`
cb(null, msg)
}
function echoFeed () {
feed.createReadStream({live: true, start: feed.length})
.on('data', function (data) {
// Echo to our standup channel
sendMessage(null, argv.echoChannel, `${data.person}: ${data.standup}`)
})
}
function parse (message, from) {
message = message.trim()
if (message[0] !== '!') return // Only want ! command
message = message.slice(1)
if (message.indexOf(' ') === -1) return {command: message, standup: null}
var parts = message.split(' ')
return {
time: new Date().toUTCString(),
command: parts.shift(),
standup: parts.join(' '),
person: from
}
}