This repository has been archived by the owner on Aug 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathirc.cr
229 lines (201 loc) · 5.55 KB
/
irc.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
# 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 "socket"
require "openssl"
class IRCConnection
@socket : TCPSocket | OpenSSL::SSL::Socket::Client | Nil
@orig_socket : TCPSocket | Nil
@channels_regex : Regex?
def initialize(@options : ChatOptions)
@channels = {} of String => Channel(Message)
end
macro method_missing(call)
@options.irc_{{call}}
end
private def connect
socket = TCPSocket.new(host, port)
socket.read_timeout = read_timeout
socket.write_timeout = write_timeout
socket.keepalive = true
@socket = @orig_socket = socket
if ssl
@socket = socket = OpenSSL::SSL::Socket::Client.new(socket)
end
sleep 10.seconds
write "NICK #{nick}"
write "USER #{username} #{hostname} unused :#{realname}"
if password
# write "PASS #{password}"
write "PRIVMSG NickServ :identify #{password}"
end
sleep 2.seconds
@channels.each_key do |channel|
write "JOIN #{channel}"
sleep 0.3.seconds
end
end
def finalize
write "QUIT :#{quit_reason}"
@socket.try &.close
rescue
end
def write(line)
if password
p line.gsub(password!, "[...]")
else
p line
end
@socket.not_nil! << line << "\r\n"
@socket.not_nil!.flush
end
def subscribe(channel) : Channel
@channels[channel.downcase] = result = Channel(Message).new
recipients = (@channels.keys + [nick]).map { |k| Regex.escape(k) } .join("|")
@channels_regex = /^:([^ ]+)![^ ]+ +PRIVMSG +(#{recipients}) :(.+)/i
#write "JOIN #{channel}" rescue nil
result
end
def run
wait_time = 2.0
timeout = false
loop do
begin
connect
loop do
begin
line = @socket.not_nil!.gets
raise "Disconnected" if !line || line.empty?
timeout = false
line = line.not_nil!.strip
case line
when /^PING\b(.*)/i
write "PONG#{$~[1]}"
when @channels_regex
_, sender, recipient, msg = $~
next if sender.downcase == nick.downcase
if action = !!(msg =~ /^\001ACTION (.*)\001$/i)
msg = $~[1]
end
msg = msg.gsub /\x03[0-9][0-9](?:,[0-9][0-9])?|[\x00-\x1f]/, ""
puts "IRC: #{recipient} <#{sender}> #{msg.inspect}"
if priv = (recipient.downcase == nick.downcase)
recipient = @channels.keys[0]
end
@channels[recipient.downcase].send Message.new(
sender, msg, priv: priv, action: action
)
else#when /^[^ ]+ +JOIN\b.*/i
p line
end
wait_time = {wait_time / 2, 2.0}.max
rescue e : InvalidByteSequenceError
puts "#{e.class}: #{e.message}"
rescue e : IO::TimeoutError
puts "#{e.class}: #{e.message}"
write "PING :#{hostname}"
raise e if timeout
timeout = true
end
end
rescue e
puts "#{e.class}: #{e.message}"
@socket.try &.close rescue nil
sleep wait_time
wait_time *= 2
end
end
end
end
class IRC
@@connections = {} of {String, String} => IRCConnection
@connection : IRCConnection
@pipe : Channel(Message)
def initialize(@options : ChatOptions)
@connection = @@connections.fetch({host, nick}) {
@@connections[{host, nick}] = conn = IRCConnection.new(@options)
spawn { conn.run }
conn
}
@pipe = @connection.subscribe channel
end
macro method_missing(call)
@options.irc_{{call}}
end
def write(line)
@connection.write line
end
def location
"IRC (#{channel} on #{host})"
end
def run
loop do
yield @pipe.receive
end
end
def send(msg : String, action = false, priv = false)
ending = ""
if action
msg = "\001ACTION #{msg}"
ending = "\001"
end
msg = "PRIVMSG #{channel} :#{msg}"
cutoff = 450
if msg.bytesize <= cutoff
write msg + ending
return
end
until (msg.byte_at cutoff - 1).chr.whitespace? || cutoff <= 410
cutoff -= 1
end
write msg.byte_slice(0, cutoff) + ending
send "\u{02}...\u{0f} " + msg.byte_slice(cutoff), action, priv
end
def send(msg : Message)
lines = msg.text.split('\n')
nlines = lines.size
i = 0
code_start = nil
while i < lines.size
line = lines[i].strip
if !code_start
if line.starts_with?("```") && !line.includes?(' ')
code_start = i
end
else
if line == "```"
sub = lines[code_start+1..i-1].join(" ⏎ ")
if i - code_start - 1 > 5 || sub.size > 90
sub = "\u{1d}code paste, see link\u{0f}"
end
lines[code_start..i] = "```#{sub}```"
i = code_start
end
end
i += 1
end
sender = msg.action ? "* #{msg.sender}" : "<#{msg.sender}>"
finish = ""
if lines.size > 5
lines.delete_at(5..-1)
finish = " \u{02}...\u{0f}"
end
text = "\u{02}#{sender}\u{0f} " + lines.join(" ⏎ ")
if text.size > 750
text = text[0...750]
finish = " \u{02}...\u{0f}"
end
text += finish
if text.size > 750 || nlines > 3
text += " [#{msg.permalink}]" if msg.permalink
end
send text, action: msg.action, priv: msg.priv
end
def tell(msg : Message)
if msg.priv
write "PRIVMSG #{msg.sender} :#{msg.text}"
else
send "#{msg.sender}, #{msg.text}"
end
end
end