UNPKG

4.3 kBtext/coffeescriptView Raw
1# Description:
2# A better implementation of factoid support for your hubot.
3# Supports history (in case you need to revert a change), as
4# well as factoid popularity, aliases and @mentions.
5#
6# Dependencies:
7# None
8#
9# Configuration:
10# HUBOT_FACTOID_PREFIX - prefix character to use for retrieving a factoid (! is the default)
11# HUBOT_BASE_URL - URL of Hubot (ex. http://myhubothost.com:5555/)
12#
13# Commands:
14# hubot learn <factoid> = <details> - learn a new factoid
15# hubot learn <factoid> =~ s/expression/replace/gi - edit a factoid
16# hubot alias <factoid> = <factoid>
17# hubot forget <factoid> - forget a factoid
18# hubot remember <factoid> - remember a factoid
19# hubot drop <factoid> - permanently forget a factoid
20# hubot factoids - get a link to the raw factoid data
21# hubot list all factoids - list all factoids
22# hubot search <substring> - list factoids which match (by factoid key or result)
23# !<factoid> - play back a factoid
24#
25# Author:
26# therealklanni
27
28Factoids = require './factoids-core'
29
30module.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."