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 | Factoids = require './factoids-core'
|
29 |
|
30 | module.exports = (robot) ->
|
31 | @factoids = new Factoids robot
|
32 | robot.router.get "/#{robot.name}/factoids", (req, res) =>
|
33 | res.end JSON.stringify @factoids.data, null, 2
|
34 |
|
35 | prefix = process.env.HUBOT_FACTOID_PREFIX or '!'
|
36 |
|
37 | robot.hear new RegExp("^[#{prefix}]([\\w\\s-]{2,}\\w)( @.+)?", 'i'), (msg) =>
|
38 | fact = @factoids.get msg.match[1]
|
39 | to = msg.match[2]
|
40 | if not fact? or fact.forgotten
|
41 | msg.reply "Not a factoid"
|
42 | else
|
43 | fact.popularity++
|
44 | to ?= msg.message.user.name
|
45 | msg.send "#{to.trim()}: #{fact.value}"
|
46 |
|
47 | robot.respond new RegExp("[#{prefix}]([\\w\\s-]{2,}\\w)", 'i'), (msg) =>
|
48 | fact = @factoids.get msg.match[1]
|
49 | if not fact? or fact.forgotten
|
50 | msg.reply "Not a factoid"
|
51 | else
|
52 | fact.popularity++
|
53 | msg.send "#{fact.value}"
|
54 |
|
55 | robot.respond /learn (.{3,}) = ([^@].+)/i, (msg) =>
|
56 | [key, value] = [msg.match[1], msg.match[2]]
|
57 | factoid = @factoids.set key, value, msg.message.user.name
|
58 |
|
59 | if factoid.value?
|
60 | msg.reply "OK, #{key} is now #{factoid.value}"
|
61 |
|
62 | robot.respond /learn (.{3,}) =~ s\/(.+)\/(.+)\/(.*)/i, (msg) =>
|
63 | key = msg.match[1]
|
64 | re = new RegExp(msg.match[2], msg.match[4])
|
65 | fact = @factoids.get key
|
66 | value = fact?.value.replace re, msg.match[3]
|
67 |
|
68 | factoid = @factoids.set key, value, msg.message.user.name if value?
|
69 |
|
70 | if factoid? and factoid.value?
|
71 | msg.reply "OK, #{key} is now #{factoid.value}"
|
72 | else
|
73 | msg.reply 'Not a factoid'
|
74 |
|
75 | robot.respond /forget (.{3,})/i, (msg) =>
|
76 | if @factoids.forget msg.match[1]
|
77 | msg.reply "OK, forgot #{msg.match[1]}"
|
78 | else
|
79 | msg.reply 'Not a factoid'
|
80 |
|
81 | robot.respond /remember (.{3,})/i, (msg) =>
|
82 | factoid = @factoids.remember msg.match[1]
|
83 | if factoid? and not factoid.forgotten
|
84 | msg.reply "OK, #{msg.match[1]} is #{factoid.value}"
|
85 | else
|
86 | msg.reply 'Not a factoid'
|
87 |
|
88 | robot.respond /list all factoids/i, (msg) =>
|
89 | all = @factoids.getAll()
|
90 | out = ''
|
91 |
|
92 | if not all? or Object.keys(all).length is 0
|
93 | msg.reply "No factoids defined"
|
94 | else
|
95 | for f of all
|
96 | out += prefix + f + ': ' + all[f] + "\n"
|
97 | msg.reply "All factoids: \n" + out
|
98 |
|
99 | robot.respond /factoids?/i, (msg) =>
|
100 | url = process.env.HUBOT_BASE_URL or "http://not-yet-set/"
|
101 | msg.reply "#{url.replace /\/$/, ''}/#{robot.name}/factoids"
|
102 |
|
103 | robot.respond /search (.{3,})/i, (msg) =>
|
104 | factoids = @factoids.search msg.match[1]
|
105 |
|
106 | if factoids.length > 0
|
107 | found = prefix + factoids.join("*, *#{prefix}")
|
108 | msg.reply "Matched the following factoids: *#{found}*"
|
109 | else
|
110 | msg.reply 'No factoids matched'
|
111 |
|
112 | robot.respond /alias (.{3,}) = (.{3,})/i, (msg) =>
|
113 | who = msg.message.user.name
|
114 | alias = msg.match[1]
|
115 | target = msg.match[2]
|
116 | msg.reply "OK, aliased #{alias} to #{target}" if @factoids.set msg.match[1], "@#{msg.match[2]}", msg.message.user.name, false
|
117 |
|
118 | robot.respond /drop (.{3,})/i, (msg) =>
|
119 | user = msg.envelope.user
|
120 | isAdmin = robot.auth?.hasRole(user, 'factoids-admin') or robot.auth?.hasRole(user, 'admin')
|
121 | if isAdmin or not robot.auth?
|
122 | factoid = msg.match[1]
|
123 | if @factoids.drop factoid
|
124 | msg.reply "OK, #{factoid} has been dropped"
|
125 | else msg.reply "Not a factoid"
|
126 | else msg.reply "You don't have permission to do that."
|