UNPKG

5.44 kBtext/coffeescriptView Raw
1# Description:
2# A Marking U Bot.
3#
4# Dependencies:
5# bitmarkd must be running
6# bitmark-cli must be in path
7# wallet must be funded
8#
9# Configuration:
10# None
11#
12# Commands:
13# + <times> <user> <reason> - Marks the specified user.
14# withdraw <address> <amount> - withdraw to address amount.
15# deposit - Display the your address.
16# marks <user> - balance for a user (will be depreciated soon).
17# + <times> - mark the last user with his comment as the reason.
18#
19# Author:
20# Project Bitmark
21#
22
23
24# requires
25exec = require('child_process').exec;
26sqlite3 = require('sqlite3').verbose();
27
28
29# init
30db = new sqlite3.Database('marks');
31
32credits = {} # simple key value store or URI / balance for now
33symbol = '₥'
34last = 'mubot'
35secret = process.env.HUBOT_DEPOSIT_SECRET
36
37if process.env.HUBOT_ADAPTER is 'irc'
38 adapter = 'irc'
39 irc_server = process.env.HUBOT_IRC_SERVER
40else if process.env.HUBOT_ADAPTER is 'slack'
41 adapter = 'slack'
42 slack_team = process.env.HUBOT_SLACK_TEAM
43else if process.env.HUBOT_ADAPTER is 'shell'
44 adapter = 'shell'
45else
46 throw new Error('HUBOT_ADAPTER env variable is required')
47 #adapter = 'slack'
48
49
50# functions
51to_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
64from_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# Decommisioned
73# deposit <user> <amount> <secret> - deposit amount using shared secret
74deposit_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
79transfer_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
89withdraw_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
103save = (robot) ->
104 robot.brain.data.credits = robot.brain.data.credits
105
106
107# MAIN
108module.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 # DEPOSIT
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 # TRANSFER
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 # WITHDRAW
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 # BALANCE
147 robot.hear /^balance\s+@?([\w\S]+)\s*$/i, (msg) ->
148 #redis-brain.getData()
149 URI = to_URI(msg.match[1])
150 #msg.send('to URI is : ' + URI)
151 #msg.send('from URI is : ' + from_URI(URI))
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 #robot.brain.data.credits['irc://leathan@irc.swiftirc.net/'] ?= 12000
157 msg.send robot.brain.data.credits['irc://leathan@irc.swiftirc.net/']
158 URI = to_URI(msg.message.user.name)
159 #msg.send('to URI is : ' + URI)
160 #msg.send('from URI is : ' + from_URI(URI))
161 robot.brain.data.credits[URI] ?= 0
162 msg.send from_URI(URI) + ' has ' + robot.brain.data.credits[URI] + symbol
163
164 # WEB
165
166 robot.router.get "/marks", (req, res) ->
167 res.end JSON.stringify(robot.brain.data.credits)
168
169 # LISTEN
170 robot.hear /.*/i, (msg) ->
171 last = msg.message.user.name
172 console.log("[" + (new Date).toLocaleTimeString() + "] " + msg.message.text)