Skip to content

Commit 0cc72ae

Browse files
committed
code cleanups
1 parent 9f146d0 commit 0cc72ae

File tree

2 files changed

+52
-32
lines changed

2 files changed

+52
-32
lines changed

lib/cinch/bot.rb

+18-10
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010

1111
require "cinch/handler"
1212
require "cinch/helpers"
13+
1314
require "cinch/logger_list"
1415
require "cinch/logger"
16+
1517
require "cinch/logger/formatted_logger"
1618
require "cinch/syncable"
1719
require "cinch/message"
@@ -28,11 +30,13 @@
2830
require "cinch/plugin"
2931
require "cinch/pattern"
3032
require "cinch/mode_parser"
33+
3134
require "cinch/handler_list"
3235
require "cinch/cached_list"
3336
require "cinch/channel_list"
3437
require "cinch/user_list"
3538
require "cinch/plugin_list"
39+
3640
require "cinch/timer"
3741
require "cinch/formatting"
3842

@@ -249,7 +253,8 @@ def configure
249253
# @return [void]
250254
def quit(message = nil)
251255
@quitting = true
252-
command = message ? "QUIT :#{message}" : "QUIT"
256+
command = message ? "QUIT :#{message}" : "QUIT"
257+
253258
@irc.send command
254259
end
255260

@@ -261,6 +266,7 @@ def quit(message = nil)
261266
def start(plugins = true)
262267
@reconnects = 0
263268
@plugins.register_plugins(@config.plugins.plugins) if plugins
269+
264270
begin
265271
@user_list.each do |user|
266272
user.in_whois = false
@@ -352,24 +358,26 @@ def strict?
352358
def initialize(&b)
353359
@loggers = LoggerList.new
354360
@loggers << Logger::FormattedLogger.new($stderr)
355-
@config = Configuration::Bot.new
356-
@handlers = HandlerList.new
361+
362+
@config = Configuration::Bot.new
363+
@handlers = HandlerList.new
357364
@semaphores_mutex = Mutex.new
358-
@semaphores = Hash.new { |h,k| h[k] = Mutex.new }
359-
@callback = Callback.new(self)
360-
@channels = []
361-
@quitting = false
362-
@modes = []
365+
@semaphores = Hash.new { |h, k| h[k] = Mutex.new }
366+
@callback = Callback.new(self)
367+
@channels = []
368+
@quitting = false
369+
@modes = []
363370

364-
@user_list = UserList.new(self)
371+
@user_list = UserList.new(self)
365372
@channel_list = ChannelList.new(self)
366-
@plugins = PluginList.new(self)
373+
@plugins = PluginList.new(self)
367374

368375
instance_eval(&b) if block_given?
369376
end
370377

371378
# @since 2.0.0
372379
def bot
380+
# This method is needed for the Helpers interface
373381
self
374382
end
375383

lib/cinch/irc.rb

+34-22
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@ class IRC
77

88
# @return [ISupport]
99
attr_reader :isupport
10+
1011
# @return [Bot]
1112
attr_reader :bot
13+
1214
# @return [Symbol] The detected network or `:other` if no network
1315
# was detected.
1416
attr_reader :network
17+
1518
def initialize(bot)
1619
@bot = bot
1720
@isupport = ISupport.new
@@ -21,8 +24,8 @@ def initialize(bot)
2124
# @return [void]
2225
# @since 2.0.0
2326
def setup
24-
@registration = []
25-
@network = :other
27+
@registration = []
28+
@network = :other
2629
@whois_updates = Hash.new {|h, k| h[k] = {}}
2730
@in_lists = Set.new
2831
end
@@ -31,6 +34,7 @@ def setup
3134
# @return [Boolean] True if the connection could be established
3235
def connect
3336
tcp_socket = nil
37+
3438
begin
3539
Timeout::timeout(@bot.config.timeouts.connect) do
3640
tcp_socket = TCPSocket.new(@bot.config.server, @bot.config.port, @bot.config.local_host)
@@ -52,9 +56,9 @@ def connect
5256
@socket = tcp_socket
5357
end
5458

55-
@socket = Net::BufferedIO.new(@socket)
59+
@socket = Net::BufferedIO.new(@socket)
5660
@socket.read_timeout = @bot.config.timeouts.read
57-
@queue = MessageQueue.new(@socket, @bot)
61+
@queue = MessageQueue.new(@socket, @bot)
5862

5963
return true
6064
end
@@ -63,23 +67,26 @@ def connect
6367
# @return [void]
6468
# @since 2.0.0
6569
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.
6672
require 'openssl'
6773

6874
ssl_context = OpenSSL::SSL::SSLContext.new
6975

7076
if @bot.config.ssl.is_a?(Configuration::SSL)
7177
if @bot.config.ssl.client_cert
7278
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))
7480
end
75-
ssl_context.ca_path = @bot.config.ssl.ca_path
81+
82+
ssl_context.ca_path = @bot.config.ssl.ca_path
7683
ssl_context.verify_mode = @bot.config.ssl.verify ? OpenSSL::SSL::VERIFY_PEER : OpenSSL::SSL::VERIFY_NONE
7784
else
7885
ssl_context.verify_mode = OpenSSL::SSL::VERIFY_NONE
7986
end
8087
@bot.loggers.info "Using SSL with #{@bot.config.server}:#{@bot.config.port}"
8188

82-
@socket = OpenSSL::SSL::SSLSocket.new(socket, ssl_context)
89+
@socket = OpenSSL::SSL::SSLSocket.new(socket, ssl_context)
8390
@socket.sync = true
8491
@socket.connect
8592
end
@@ -140,8 +147,9 @@ def start_ping_thread
140147
Thread.new do
141148
while true
142149
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")
145153
end
146154
end
147155
end
@@ -156,7 +164,7 @@ def start
156164
send_login
157165
reading_thread = start_reading_thread
158166
sending_thread = start_sending_thread
159-
ping_thread = start_ping_thread
167+
ping_thread = start_ping_thread
160168

161169
reading_thread.join
162170
sending_thread.kill
@@ -169,6 +177,7 @@ def start
169177
def parse(input)
170178
return if input.chomp.empty?
171179
@bot.loggers.incoming(input)
180+
172181
msg = Message.new(input, @bot)
173182
events = [[:catchall]]
174183

@@ -252,9 +261,11 @@ def on_kick(msg, events)
252261

253262
def on_kill(msg, events)
254263
user = User(msg.params[1])
264+
255265
@bot.channel_list.each do |channel|
256266
channel.remove_user(user)
257267
end
268+
258269
user.unsync_all
259270
user.online = false
260271

@@ -266,13 +277,16 @@ def on_mode(msg, events)
266277
if msg.channel?
267278
add_and_remove = @bot.irc.isupport["CHANMODES"]["A"] + @bot.irc.isupport["CHANMODES"]["B"] + @bot.irc.isupport["PREFIX"].keys
268279

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+
}
271284

272285
modes = ModeParser.parse_modes(msg.params[1], msg.params[2..-1], param_modes)
273286
modes.each do |direction, mode, param|
274287
if @bot.irc.isupport["PREFIX"].keys.include?(mode)
275288
target = User(param)
289+
276290
# (un)set a user-mode
277291
if direction == :add
278292
msg.channel.users[target] << mode unless msg.channel.users[User(param)].include?(mode)
@@ -340,6 +354,7 @@ def on_nick(msg, events)
340354
def on_part(msg, events)
341355
msg.channel.remove_user(msg.user)
342356
msg.user.channels_unsynced.delete msg.channel
357+
343358
if msg.user == @bot
344359
@bot.channels.delete(msg.channel)
345360
end
@@ -387,10 +402,6 @@ def on_002(msg, events)
387402
# than one argument and does not use full banmasks in
388403
# RPL_BANLIST
389404
@network = "jtv"
390-
else
391-
# this catches all other networks that do not require custom
392-
# behaviour.
393-
@network = :other
394405
end
395406
end
396407

@@ -451,16 +462,16 @@ def on_318(msg, events)
451462

452463
def on_319(msg, events)
453464
# RPL_WHOISCHANNELS
454-
user = User(msg.params[1])
465+
user = User(msg.params[1])
455466
channels = msg.params[2].scan(/#{@isupport["CHANTYPES"].join}[^ ]+/o).map {|c| Channel(c) }
456467
user.sync(:channels, channels, true)
457468
end
458469

459470
def on_324(msg, events)
460471
# RPL_CHANNELMODEIS
461-
462-
modes = {}
472+
modes = {}
463473
arguments = msg.params[3..-1]
474+
464475
msg.params[2][1..-1].split("").each do |mode|
465476
if (@isupport["CHANMODES"]["B"] + @isupport["CHANMODES"]["C"]).include?(mode)
466477
modes[mode] = arguments.shift
@@ -474,8 +485,9 @@ def on_324(msg, events)
474485

475486
def on_330(msg, events)
476487
# RPL_WHOISACCOUNT
477-
user = User(msg.params[1])
488+
user = User(msg.params[1])
478489
authname = msg.params[2]
490+
479491
@whois_updates[user].merge!({:authname => authname})
480492
end
481493

@@ -505,7 +517,7 @@ def on_353(msg, events)
505517
nick = user
506518
prefixes = []
507519
end
508-
user = User(nick)
520+
user = User(nick)
509521
user.online = true
510522
msg.channel.add_user(user, prefixes)
511523
user.channels_unsynced << msg.channel unless user.channels_unsynced.include?(msg.channel)
@@ -539,7 +551,7 @@ def on_367(msg, events)
539551
by = nil
540552
end
541553

542-
at = Time.at(msg.params[4].to_i)
554+
at = Time.at(msg.params[4].to_i)
543555
ban = Ban.new(mask, by, at)
544556
msg.channel.bans_unsynced << ban
545557
end

0 commit comments

Comments
 (0)