@@ -7,11 +7,14 @@ class IRC
7
7
8
8
# @return [ISupport]
9
9
attr_reader :isupport
10
+
10
11
# @return [Bot]
11
12
attr_reader :bot
13
+
12
14
# @return [Symbol] The detected network or `:other` if no network
13
15
# was detected.
14
16
attr_reader :network
17
+
15
18
def initialize ( bot )
16
19
@bot = bot
17
20
@isupport = ISupport . new
@@ -21,8 +24,8 @@ def initialize(bot)
21
24
# @return [void]
22
25
# @since 2.0.0
23
26
def setup
24
- @registration = [ ]
25
- @network = :other
27
+ @registration = [ ]
28
+ @network = :other
26
29
@whois_updates = Hash . new { |h , k | h [ k ] = { } }
27
30
@in_lists = Set . new
28
31
end
@@ -31,6 +34,7 @@ def setup
31
34
# @return [Boolean] True if the connection could be established
32
35
def connect
33
36
tcp_socket = nil
37
+
34
38
begin
35
39
Timeout ::timeout ( @bot . config . timeouts . connect ) do
36
40
tcp_socket = TCPSocket . new ( @bot . config . server , @bot . config . port , @bot . config . local_host )
@@ -52,9 +56,9 @@ def connect
52
56
@socket = tcp_socket
53
57
end
54
58
55
- @socket = Net ::BufferedIO . new ( @socket )
59
+ @socket = Net ::BufferedIO . new ( @socket )
56
60
@socket . read_timeout = @bot . config . timeouts . read
57
- @queue = MessageQueue . new ( @socket , @bot )
61
+ @queue = MessageQueue . new ( @socket , @bot )
58
62
59
63
return true
60
64
end
@@ -63,23 +67,26 @@ def connect
63
67
# @return [void]
64
68
# @since 2.0.0
65
69
def setup_ssl ( socket )
70
+ # require openssl in this method so the bot doesn't break for
71
+ # people who don't have SSL but don't want to use SSL anyway.
66
72
require 'openssl'
67
73
68
74
ssl_context = OpenSSL ::SSL ::SSLContext . new
69
75
70
76
if @bot . config . ssl . is_a? ( Configuration ::SSL )
71
77
if @bot . config . ssl . client_cert
72
78
ssl_context . cert = OpenSSL ::X509 ::Certificate . new ( File . read ( @bot . config . ssl . client_cert ) )
73
- ssl_context . key = OpenSSL ::PKey ::RSA . new ( File . read ( @bot . config . ssl . client_cert ) )
79
+ ssl_context . key = OpenSSL ::PKey ::RSA . new ( File . read ( @bot . config . ssl . client_cert ) )
74
80
end
75
- ssl_context . ca_path = @bot . config . ssl . ca_path
81
+
82
+ ssl_context . ca_path = @bot . config . ssl . ca_path
76
83
ssl_context . verify_mode = @bot . config . ssl . verify ? OpenSSL ::SSL ::VERIFY_PEER : OpenSSL ::SSL ::VERIFY_NONE
77
84
else
78
85
ssl_context . verify_mode = OpenSSL ::SSL ::VERIFY_NONE
79
86
end
80
87
@bot . loggers . info "Using SSL with #{ @bot . config . server } :#{ @bot . config . port } "
81
88
82
- @socket = OpenSSL ::SSL ::SSLSocket . new ( socket , ssl_context )
89
+ @socket = OpenSSL ::SSL ::SSLSocket . new ( socket , ssl_context )
83
90
@socket . sync = true
84
91
@socket . connect
85
92
end
@@ -140,8 +147,9 @@ def start_ping_thread
140
147
Thread . new do
141
148
while true
142
149
sleep @bot . config . ping_interval
143
- send ( "PING 0" ) # PING requires a single argument. In our
144
- # case the value doesn't matter though.
150
+ # PING requires a single argument. In our case the value
151
+ # doesn't matter though.
152
+ send ( "PING 0" )
145
153
end
146
154
end
147
155
end
@@ -156,7 +164,7 @@ def start
156
164
send_login
157
165
reading_thread = start_reading_thread
158
166
sending_thread = start_sending_thread
159
- ping_thread = start_ping_thread
167
+ ping_thread = start_ping_thread
160
168
161
169
reading_thread . join
162
170
sending_thread . kill
@@ -169,6 +177,7 @@ def start
169
177
def parse ( input )
170
178
return if input . chomp . empty?
171
179
@bot . loggers . incoming ( input )
180
+
172
181
msg = Message . new ( input , @bot )
173
182
events = [ [ :catchall ] ]
174
183
@@ -252,9 +261,11 @@ def on_kick(msg, events)
252
261
253
262
def on_kill ( msg , events )
254
263
user = User ( msg . params [ 1 ] )
264
+
255
265
@bot . channel_list . each do |channel |
256
266
channel . remove_user ( user )
257
267
end
268
+
258
269
user . unsync_all
259
270
user . online = false
260
271
@@ -266,13 +277,16 @@ def on_mode(msg, events)
266
277
if msg . channel?
267
278
add_and_remove = @bot . irc . isupport [ "CHANMODES" ] [ "A" ] + @bot . irc . isupport [ "CHANMODES" ] [ "B" ] + @bot . irc . isupport [ "PREFIX" ] . keys
268
279
269
- param_modes = { :add => @bot . irc . isupport [ "CHANMODES" ] [ "C" ] + add_and_remove ,
270
- :remove => add_and_remove }
280
+ param_modes = {
281
+ :add => @bot . irc . isupport [ "CHANMODES" ] [ "C" ] + add_and_remove ,
282
+ :remove => add_and_remove
283
+ }
271
284
272
285
modes = ModeParser . parse_modes ( msg . params [ 1 ] , msg . params [ 2 ..-1 ] , param_modes )
273
286
modes . each do |direction , mode , param |
274
287
if @bot . irc . isupport [ "PREFIX" ] . keys . include? ( mode )
275
288
target = User ( param )
289
+
276
290
# (un)set a user-mode
277
291
if direction == :add
278
292
msg . channel . users [ target ] << mode unless msg . channel . users [ User ( param ) ] . include? ( mode )
@@ -340,6 +354,7 @@ def on_nick(msg, events)
340
354
def on_part ( msg , events )
341
355
msg . channel . remove_user ( msg . user )
342
356
msg . user . channels_unsynced . delete msg . channel
357
+
343
358
if msg . user == @bot
344
359
@bot . channels . delete ( msg . channel )
345
360
end
@@ -387,10 +402,6 @@ def on_002(msg, events)
387
402
# than one argument and does not use full banmasks in
388
403
# RPL_BANLIST
389
404
@network = "jtv"
390
- else
391
- # this catches all other networks that do not require custom
392
- # behaviour.
393
- @network = :other
394
405
end
395
406
end
396
407
@@ -451,16 +462,16 @@ def on_318(msg, events)
451
462
452
463
def on_319 ( msg , events )
453
464
# RPL_WHOISCHANNELS
454
- user = User ( msg . params [ 1 ] )
465
+ user = User ( msg . params [ 1 ] )
455
466
channels = msg . params [ 2 ] . scan ( /#{ @isupport [ "CHANTYPES" ] . join } [^ ]+/o ) . map { |c | Channel ( c ) }
456
467
user . sync ( :channels , channels , true )
457
468
end
458
469
459
470
def on_324 ( msg , events )
460
471
# RPL_CHANNELMODEIS
461
-
462
- modes = { }
472
+ modes = { }
463
473
arguments = msg . params [ 3 ..-1 ]
474
+
464
475
msg . params [ 2 ] [ 1 ..-1 ] . split ( "" ) . each do |mode |
465
476
if ( @isupport [ "CHANMODES" ] [ "B" ] + @isupport [ "CHANMODES" ] [ "C" ] ) . include? ( mode )
466
477
modes [ mode ] = arguments . shift
@@ -474,8 +485,9 @@ def on_324(msg, events)
474
485
475
486
def on_330 ( msg , events )
476
487
# RPL_WHOISACCOUNT
477
- user = User ( msg . params [ 1 ] )
488
+ user = User ( msg . params [ 1 ] )
478
489
authname = msg . params [ 2 ]
490
+
479
491
@whois_updates [ user ] . merge! ( { :authname => authname } )
480
492
end
481
493
@@ -505,7 +517,7 @@ def on_353(msg, events)
505
517
nick = user
506
518
prefixes = [ ]
507
519
end
508
- user = User ( nick )
520
+ user = User ( nick )
509
521
user . online = true
510
522
msg . channel . add_user ( user , prefixes )
511
523
user . channels_unsynced << msg . channel unless user . channels_unsynced . include? ( msg . channel )
@@ -539,7 +551,7 @@ def on_367(msg, events)
539
551
by = nil
540
552
end
541
553
542
- at = Time . at ( msg . params [ 4 ] . to_i )
554
+ at = Time . at ( msg . params [ 4 ] . to_i )
543
555
ban = Ban . new ( mask , by , at )
544
556
msg . channel . bans_unsynced << ban
545
557
end
0 commit comments