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 | exec = require('child_process').exec;
|
26 | sqlite3 = require('sqlite3').verbose();
|
27 |
|
28 |
|
29 |
|
30 | db = new sqlite3.Database('marks');
|
31 |
|
32 | credits = {}
|
33 | symbol = '₥'
|
34 | last = 'mubot'
|
35 | secret = process.env.HUBOT_DEPOSIT_SECRET
|
36 |
|
37 | if process.env.HUBOT_ADAPTER is 'irc'
|
38 | adapter = 'irc'
|
39 | irc_server = process.env.HUBOT_IRC_SERVER
|
40 | else if process.env.HUBOT_ADAPTER is 'slack'
|
41 | adapter = 'slack'
|
42 | slack_team = process.env.HUBOT_SLACK_TEAM
|
43 | else if process.env.HUBOT_ADAPTER is 'shell'
|
44 | adapter = 'shell'
|
45 | else
|
46 | throw new Error('HUBOT_ADAPTER env variable is required')
|
47 |
|
48 |
|
49 |
|
50 |
|
51 | to_URI = ( id ) ->
|
52 | id = id.toLowerCase()
|
53 | if id.indexOf(':') != -1
|
54 | id
|
55 | else if adapter is 'irc'
|
56 | 'irc://' + id + '@' + irc_server + '/'
|
57 | else if adapter is 'slack'
|
58 | 'https://' + slack_team + '.slack.com/team/' + id + '#this'
|
59 | else if adapter is 'shell'
|
60 | 'urn:shell:' + id
|
61 | else
|
62 | id
|
63 |
|
64 | from_URI = ( URI ) ->
|
65 | if URI.indexOf('irc://') is 0 and adapter is 'irc'
|
66 | URI.split(":")[1].substring(2).split('@')[0]
|
67 | else if URI.indexOf('https://' + slack_team + '.slack.com/team/') is 0 and URI.indexOf('#this') != -1 and adapter is 'slack'
|
68 | URI.split(":")[1].substring(2).split('/')[2].split('#')[0]
|
69 | else
|
70 | URI
|
71 |
|
72 |
|
73 |
|
74 | deposit_credits = (msg, URI, amount, robot) ->
|
75 | robot.brain.data.credits[URI] ?= 0
|
76 | robot.brain.data.credits[URI] += parseFloat(amount)
|
77 | msg.send amount + symbol + ' to ' + from_URI(URI)
|
78 |
|
79 | transfer_credits = (msg, URI, amount, robot) ->
|
80 | if robot.brain.data.credits[to_URI(msg.message.user.name)] >= parseFloat(amount)
|
81 | robot.brain.data.credits[URI] ?= 0
|
82 | robot.brain.data.credits[URI] += parseFloat(amount)
|
83 | robot.brain.data.credits[to_URI(msg.message.user.name)] -= parseFloat(amount)
|
84 | msg.send amount + symbol + ' has been awarded to ' + from_URI(URI)
|
85 | else
|
86 | msg.send 'sorry, not enough funds'
|
87 |
|
88 |
|
89 | withdraw_credits = (msg, address, amount, robot) ->
|
90 | if robot.brain.data.credits[to_URI(msg.message.user.name)] >= parseFloat(amount)
|
91 | command = 'bitmark-cli sendtoaddress ' + address + ' ' + ( parseFloat(amount) / 1000.0 )
|
92 | console.log(command)
|
93 | exec command, (error, stdout, stderr) ->
|
94 | console.log(error)
|
95 | console.log(stdout)
|
96 | console.log(stderr)
|
97 | robot.brain.data.credits[to_URI(msg.message.user.name)] -= parseFloat(amount)
|
98 | msg.send stdout
|
99 | else
|
100 | msg.send 'not enough funds'
|
101 |
|
102 |
|
103 | save = (robot) ->
|
104 | robot.brain.data.credits = robot.brain.data.credits
|
105 |
|
106 |
|
107 |
|
108 | module.exports = (robot) ->
|
109 | robot.brain.on 'loaded', ->
|
110 | robot.brain.data.credits ?= {}
|
111 | credits = robot.brain.data.credits or {}
|
112 | robot.brain.resetSaveInterval(1)
|
113 |
|
114 |
|
115 | robot.hear /deposit\s+(\d+)\s+([\w\S]+)\s+([\w\S]*)$/i, (msg) ->
|
116 | if msg.match[3] is secret
|
117 | msg.send 'deposit to ' + msg.match[2] + ' ' + msg.match[1]
|
118 | deposit_credits(msg, to_URI(msg.match[2]), msg.match[1], robot)
|
119 | save(robot)
|
120 |
|
121 |
|
122 | robot.hear /^(transfer|mark)\s+@?([\w\S]+)\s*(\d+)\s*$/i, (msg) ->
|
123 | transfer_credits(msg, to_URI(msg.match[2]), msg.match[3], robot)
|
124 | save(robot)
|
125 |
|
126 | robot.hear /^(transfer|mark)\s+@?([\w\S]+)\s*$/i, (msg) ->
|
127 | transfer_credits(msg, to_URI(msg.match[2]), 1, robot)
|
128 | save(robot)
|
129 |
|
130 | robot.hear /^\+(\d+)\s*$/i, (msg) ->
|
131 | plus = msg.match[1]
|
132 | if plus <= 25
|
133 | transfer_credits(msg, to_URI(last), plus, robot)
|
134 | else
|
135 | msg.send 'Max is +25'
|
136 | save(robot)
|
137 |
|
138 |
|
139 | robot.hear /withdraw\s+([\w\S]+)\s+(\d+)\s*$/i, (msg) ->
|
140 | destination = msg.match[1]
|
141 | if destination is 'foundation'
|
142 | destination = 'bQmnzVS5M4bBdZqBTuHrjnzxHS6oSUz6cG'
|
143 | withdraw_credits(msg, destination, msg.match[2], robot)
|
144 | save(robot)
|
145 |
|
146 |
|
147 | robot.hear /^balance\s+@?([\w\S]+)\s*$/i, (msg) ->
|
148 |
|
149 | URI = to_URI(msg.match[1])
|
150 |
|
151 |
|
152 | robot.brain.data.credits[URI] ?= 0
|
153 | msg.send from_URI(URI) + ' has ' + robot.brain.data.credits[URI] + symbol
|
154 |
|
155 | robot.hear /^balance\s*$/i, (msg) ->
|
156 |
|
157 | msg.send robot.brain.data.credits['irc://leathan@irc.swiftirc.net/']
|
158 | URI = to_URI(msg.message.user.name)
|
159 |
|
160 |
|
161 | robot.brain.data.credits[URI] ?= 0
|
162 | msg.send from_URI(URI) + ' has ' + robot.brain.data.credits[URI] + symbol
|
163 |
|
164 |
|
165 |
|
166 | robot.router.get "/marks", (req, res) ->
|
167 | res.end JSON.stringify(robot.brain.data.credits)
|
168 |
|
169 |
|
170 | robot.hear /.*/i, (msg) ->
|
171 | last = msg.message.user.name
|
172 | console.log("[" + (new Date).toLocaleTimeString() + "] " + msg.message.text)
|