This repository has been archived by the owner on Aug 2, 2023. It is now read-only.
/
gitter.cr
241 lines (199 loc) · 4.99 KB
/
gitter.cr
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# Copyright (C) 2016 Oleh Prypin <oleh@pryp.in>
# This file is part of Critter.
# Released under the terms of the MIT license (see LICENSE).
require "http"
require "json"
require "markdown"
class Gitter
@room_id : String
@user_id : String
@user_name : String
@participants = Set(String).new
def initialize(@options : ChatOptions)
@headers = HTTP::Headers{
"Authorization" => "Bearer #{api_key}",
"Accept" => "application/json",
"Content-Type" => "application/json",
}
js = JSON.parse(
request :POST, "rooms", uri: room
)
@room_id = js["id"].as_s
js = JSON.parse(request :GET, "user")
@user_id = js[0]["id"].as_s
@user_name = js[0]["username"].as_s
end
macro method_missing(call)
@options.gitter_{{call}}
end
def location
"https://gitter.im/#{room}"
end
def nick
@user_name
end
private def request(_method, _path, **arguments)
url = "https://api.gitter.im/v1/#{_path}"
puts "#{_method} #{url} #{arguments.to_json}"
HTTP::Client.exec(
_method.to_s, url, headers: @headers.dup, body: arguments.to_json
).body
end
def send(msg : String, action = false)
request :POST, "rooms/#{@room_id}/chatMessages", text: msg, status: action
end
private def escape_emojis(s : String)
s.gsub(
/:-?[)\[@(*\/S|$O]|[:;]-?[\]DP]|X-D|:['’]-?\(|;-?\)|:-X|<\/?3|:[+\-]1:/i
) {
if $~.begin == 0 || s[$~.begin.not_nil! - 1].whitespace? || $~.end == s.size
$~[0].insert($~[0].size/2, '\u{2060}')
else; $~[0]; end
}
end
def send(msg : Message)
sender = msg.action ? "\\* #{msg.sender}" : "<#{msg.sender}>"
text = msg.text
if insert_mentions
text = text.sub /^([^ ]+)[,:] / {
"#{"@" if @participants.includes? $~[1]}#{$~[0]}"
}
end
if prevent_emojis
# Add zero-width joiner to disrupt emoticon replacement
text = String.build do |io|
prev = 0
# But not inside code blocks
text.scan(/(?<!\\)`([^`]|\\`)+`/) do |m|
io << escape_emojis(text[prev ... m.begin.not_nil!])
io << m[0]
prev = m.end.not_nil!
end
io << escape_emojis(text[prev..-1])
end
end
send "**#{sender}** #{text}"
end
def tell(msg : Message)
send "@#{msg.sender}, #{msg.text}"
end
def run
wait_time = 1.0
stream_url = "https://stream.gitter.im/v1/rooms/#{@room_id}/chatMessages"
loop do
begin
puts "GET #{stream_url}"
HTTP::Client.get(stream_url, headers: @headers.dup) do |resp|
puts "Connected to Gitter #{room}"
resp.body_io.each_line do |line|
line = line.strip
next if line.empty?
msg = JSON.parse(line)
sender = msg["fromUser"]["username"].as_s
next if sender == @user_name
@participants << sender
id = msg["id"].as_s
text = msg["text"].as_s.strip
if action = !!msg["status"]?
text = text.split(2)[-1] # Drop @nickname
end
puts "Gitter: #{room} <#{sender}> #{text.inspect}"
text = String.build do |io|
renderer = MarkdownTextRenderer.new(io)
Markdown::Parser.new(text, renderer).parse
end
yield Message.new(sender, text, action: action, permalink: "#{location}?at=#{id}")
wait_time = {wait_time / 2, 1.0}.max
# Mark as read
request :POST, "user/#{@user_id}/rooms/#{room}/unreadItems", chat: [id]
end
end
rescue e
puts "#{e.class}: #{e.message}"
sleep wait_time
wait_time *= 2
end
end
end
end
private class MarkdownTextRenderer
include Markdown::Renderer
@url : String?
@list_index = 0
def initialize(@io : IO)
end
def begin_paragraph
end
def end_paragraph
end
def begin_italic
@io << "*"
end
def end_italic
@io << "*"
end
def begin_bold
@io << "*"
end
def end_bold
@io << "*"
end
def begin_header(level)
@io << (level == 1 ? "===" : "---")
end
def end_header(level)
@io << (level == 1 ? "===" : "---")
end
def begin_inline_code
@io << "`"
end
def end_inline_code
@io << "`"
end
def begin_code(language)
@io << "```\n"
end
def end_code
@io << "\n```"
end
def begin_quote
@io << "> "
end
def end_quote
end
def begin_unordered_list
@list_index = 0
end
def end_unordered_list
end
def begin_ordered_list
@list_index = 0
end
def end_ordered_list
end
def begin_list_item
@io << "\n" if @list_index > 0
@io << (@list_index += 1) << ") "
end
def end_list_item
end
def begin_link(@url)
end
def end_link
@io << " (" << @url << ")"
@url = nil
end
def image(url, alt)
@io << "(" unless @url
unless @url && url =~ %r(/thumb/[^/]+$)
@io << "(" << url << ")"
end
@io << ")" unless @url
end
def text(text)
@io << text
end
def horizontal_rule
@io << "\n"
end
end