-
Notifications
You must be signed in to change notification settings - Fork 2
/
mubot.coffee
194 lines (161 loc) · 5.97 KB
/
mubot.coffee
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
## Description:
# A Marking U Bot.
#
# Dependencies:
# bitmarkd must be running
# bitmark-cli must be in path
# wallet must be funded
#
#
# Configuration:
#
#
# Commands:
# + <times> <user> [ <reason> ] - Marks the specified user.
# withdraw <address> <amount> - withdraw to address amount.
# deposit - Display your address.
# marks [ <user> ] - Marking statistics for specified user or of onself.
# + <times> - mark the last user with his comment as the reason.
#
# Author:
# Project Bitmark
#
# requires
exec = require('child_process').exec;
# sqlite3 = require('sqlite3').verbose();
# init
# db = new sqlite3.Database('marks');
marks = {} # simple key value store or URI / balance for now
symbol = '₥'
last = 'mubot'
secret = process.env.HUBOT_DEPOSIT_SECRET
if process.env.HUBOT_ADAPTER is 'irc'
adapter = 'irc'
irc_server = process.env.HUBOT_IRC_SERVER
else if process.env.HUBOT_ADAPTER is 'slack'
adapter = 'slack'
slack_team = process.env.HUBOT_SLACK_TEAM
else if process.env.HUBOT_ADAPTER is 'shell'
adapter = 'shell'
else
adapter = 'irc' or throw new Error('HUBOT_ADAPTER env variable is required')
# functions
to_URI = ( id ) ->
id = id.toLowerCase()
if id.indexOf(':') != -1
id
else if adapter is 'irc'
'irc://' + id + '@' + irc_server + '/'
else if adapter is 'slack'
'https://' + slack_team + '.slack.com/team/' + id + '#this'
else if adapter is 'shell'
'urn:shell:' + id
else
id
from_URI = ( URI ) ->
if URI.indexOf('irc://') is 0 and adapter is 'irc'
URI.split(":")[1].substring(2).split('@')[0]
else if URI.indexOf('https://' + slack_team + '.slack.com/team/') is 0 and URI.indexOf('#this') != -1 and adapter is 'slack'
URI.split(":")[1].substring(2).split('/')[2].split('#')[0]
else
URI
# Decommisioned
# deposit <user> <amount> <secret> - deposit amount using shared secret
deposit_marks = (msg, URI, amount, robot) ->
robot.brain.data.marks[URI] ?= 0
robot.brain.data.marks[URI] += parseFloat(amount)
msg.send amount + symbol + ' to ' + from_URI(URI)
transfer_marks = (msg, URI, amount, robot) ->
why_context = msg.match[3] or msg.match[2]
if !why_context? or why_context is from_URI(URI)
why_context = "N/A"
if robot.brain.data.marks[to_URI(msg.message.user.name)] >= parseFloat(amount)
robot.brain.data.marks[URI] ?= 0
robot.brain.data.marks[URI] += parseFloat(amount)
robot.brain.data.marks[to_URI(msg.message.user.name)] -= parseFloat(amount)
msg.send msg.message.user.name + ' has marked ' + from_URI(URI) + ' ' + amount + symbol + '. ( ' + why_context + ' )'
else
msg.send 'Sorry, no one has marked you yet. Try the deposit command.'
withdraw_marks = (msg, address, amount, robot) ->
if robot.brain.data.marks[to_URI(msg.message.user.name)] >= parseFloat(amount)
command = 'bitmark-cli sendtoaddress ' + address + ' ' + ( parseFloat(amount) / 1000.0 )
console.log(command)
exec command, (error, stdout, stderr) ->
console.log(error)
console.log(stdout)
console.log(stderr)
robot.brain.data.marks[to_URI(msg.message.user.name)] -= parseFloat(amount)
msg.send stdout
else
msg.send 'Sorry, you have not been marked that many times yet.'
save = (robot) ->
robot.brain.data.marks = robot.brain.data.marks
# MAIN
module.exports = (robot) ->
robot.brain.on 'loaded', ->
robot.brain.data.marks ?= {}
marks = robot.brain.data.marks or {}
robot.brain.resetSaveInterval(1)
# SETTING INITIAL BALANCE
# robot.brain.data.marks[URI] ?= 0 Uncomment this line and replace URI with your usename.
# For example bellow is how I set my initial balance to 12000 marks:
robot.brain.data.marks['irc://leathan@irc.swiftirc.net/'] ?= 12000
# If I was using the slack adapter and not the irc adapter I would do:
# robot.brain.data.marks['https://projectbitmark.slack.com/team/leathan#this'] ?= 12000
# DEPOSIT
robot.hear /deposit\s+(\d+)\s+([\w\S]+)\s+([\w\S]*)$/i, (msg) ->
if msg.match[3] is secret
msg.send 'deposit to ' + msg.match[2] + ' ' + msg.match[1]
deposit_marks(msg, to_URI(msg.match[2]), msg.match[1], robot)
save(robot)
# TRANSFER
robot.hear /^mark @?([\w\S]+) (\d+)$/i, (msg) ->
transfer_marks(msg, to_URI(msg.match[1]), msg.match[2], robot)
save(robot)
robot.hear /^mark @?([\w\S]+)$/i, (msg) ->
transfer_marks(msg, to_URI(msg.match[1]), 1, robot)
save(robot)
robot.hear /^\+(\d+)\s@?([\w\S]+) ?(.*)?$/i, (msg) ->
plus = msg.match[1]
if plus <= 25
transfer_marks(msg, to_URI(msg.match[2]), plus, robot)
else
msg.send 'Max is +25'
save(robot)
robot.hear /^\+(\d+)$/i, (msg) ->
plus = msg.match[1]
if plus <= 25
console.log last
transfer_marks(msg, to_URI(last), plus, robot)
else
msg.send 'Max is +25'
save(robot)
# WITHDRAW
robot.hear /withdraw\s+([\w\S]+)\s+(\d+)\s*$/i, (msg) ->
destination = msg.match[1]
if destination is 'foundation'
destination = 'bQmnzVS5M4bBdZqBTuHrjnzxHS6oSUz6cG'
withdraw_marks(msg, destination, msg.match[2], robot)
save(robot)
# BALANCE
robot.hear /^marks\s+@?([\w\S]+)\s*$/i, (msg) ->
URI = to_URI(msg.match[1])
robot.brain.data.marks[URI] ?= 0
msg.send from_URI(URI) + ' has ' + robot.brain.data.marks[URI] + symbol
robot.hear /^marks\s*$/i, (msg) ->
URI = to_URI(msg.message.user.name)
robot.brain.data.marks[URI] ?= 0
msg.send from_URI(URI) + ' has ' + robot.brain.data.marks[URI] + symbol + '.'
# WEB
robot.router.get "/#{robot.name}/marks", (req, res) ->
res.end JSON.stringify(robot.brain.data.marks)
# LISTEN
robot.hear /.*/i, (msg) ->
last = msg.message.user.name
#CreateMarking =
# console.log("[" + (new Date).toLocaleTimeString() + "] " + msg.message.text)
#data = JSON.stringify({
# foo: 'bar'
#})
#console.log data
#console.log JSON.stringify(data)