-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpaste.coffee
More file actions
100 lines (81 loc) · 3.67 KB
/
Copy pathpaste.coffee
File metadata and controls
100 lines (81 loc) · 3.67 KB
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
# Description
# Universal Paste API
# Dpaste.com, Sprunge.us, and Pastebin.com
#
# Configuration:
# PASTEBIN_API_KEY
#
# Commands:
# hubot dpaste <content> - Creates a new paste with <content> on Dpaste.com
# hubot sprunge <content> - Creates a new paste with <content> on Sprunge.us
# hubot pastebin <content> - Creates a new paste with <content> on Pastebin.com
#
# Author:
# MrSaints
PASTEBIN_API_KEY = process.env.PASTEBIN_API_KEY
DPASTE_API_URL = "http://dpaste.com/api/v2/"
PASTEBIN_API_URL = "http://pastebin.com/api/api_post.php"
SPRUNGE_API_URL = "http://sprunge.us"
DEF_SERVER_ERROR = "We are unable to process your request at this time due to a server error. Please try again later."
DEF_CREDIT = "Generated by ClaudeBot's hubot-paste"
class IPaste
constructor: (@robot) ->
create: (err, link, callback) ->
if err
link = DEF_SERVER_ERROR
return @robot.logger.error "hubot-paste: #{err}"
callback link
class Dpaste extends IPaste
create: (text, expire = 1, callback = false) ->
data = "content=#{encodeURIComponent(text)}&expiry_days=#{expire}"
data += "&poster=#{@robot.name}&title=#{encodeURIComponent(DEF_CREDIT)}"
@robot.http(DPASTE_API_URL)
.header("content-type", "application/x-www-form-urlencoded")
.post(data) (err, httpRes, body) =>
@robot.logger.debug "hubot-paste: #{httpRes.headers}"
super err, httpRes.headers.location, callback
class Sprunge extends IPaste
create: (text, expire = false, callback = false) ->
data = "sprunge=#{encodeURIComponent(text)}"
@robot.http(SPRUNGE_API_URL)
.header("content-type", "application/x-www-form-urlencoded")
.post(data) (err, httpRes, body) =>
@robot.logger.debug "hubot-paste: #{httpRes.headers}"
super err, body, callback
class Pastebin extends IPaste
create: (text, expire = "1D", callback = false) ->
data = "api_dev_key=#{encodeURIComponent(PASTEBIN_API_KEY)}&api_option=paste"
data += "&api_paste_code=#{encodeURIComponent(text)}&api_paste_expire_date=#{expire}"
data += "&api_paste_name=#{encodeURIComponent(DEF_CREDIT)}"
@robot.http(PASTEBIN_API_URL)
.header("content-type", "application/x-www-form-urlencoded")
.post(data) (err, httpRes, body) =>
@robot.logger.debug "hubot-paste: #{httpRes.headers}"
super err, body, callback
class PasteBot
constructor: (@robot, listener) ->
@registerListeners() if listener
registerListeners: ->
@robot.respond /dpaste ([\s\S]+)/i, id: "paste.dpaste.new", (res) =>
@dpaste res.match[1], 1, (link) ->
res.reply link
@robot.respond /sprunge ([\s\S]+)/i, id: "paste.sprunge.new", (res) =>
@sprunge res.match[1], (link) ->
res.reply link
if PASTEBIN_API_KEY?
@robot.respond /pastebin ([\s\S]+)/i, id: "paste.pastebin.new", (res) =>
@pastebin res.match[1], "1D", (link) ->
res.reply link
else
@robot.logger.warning "hubot-paste: Missing PASTEBIN_API_KEY in environment. Pastebin services will not be available."
dpaste: (text, expire = "1", callback) ->
service = new Dpaste @robot
service.create text, expire, callback
sprunge: (text, callback) ->
service = new Sprunge @robot
service.create text, false, callback
pastebin: (text, expire = "1D", callback) ->
service = new Pastebin @robot
service.create text, expire, callback
module.exports = (robot, listener = true) ->
new PasteBot robot, listener