tkellem 0.8.4 → 0.8.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -153,6 +153,7 @@ class Bouncer
153
153
  alias_method :log_name, :name
154
154
 
155
155
  def send_msg(msg)
156
+ return unless @conn
156
157
  trace "to server: #{msg}"
157
158
  @conn.send_data("#{msg}\r\n")
158
159
  end
@@ -31,10 +31,12 @@ module BouncerConnection
31
31
  end
32
32
 
33
33
  def post_init
34
- if ssl
35
- start_tls :verify_peer => false
36
- else
37
- ssl_handshake_completed
34
+ failsafe(:post_init) do
35
+ if ssl
36
+ start_tls :verify_peer => false
37
+ else
38
+ ssl_handshake_completed
39
+ end
38
40
  end
39
41
  end
40
42
 
@@ -66,45 +68,39 @@ module BouncerConnection
66
68
  end
67
69
 
68
70
  def receive_line(line)
69
- trace "from client: #{line}"
70
- msg = IrcMessage.parse(line)
71
-
72
- command = msg.command
73
- if @user && command == 'PRIVMSG' && msg.args.first == '-tkellem'
74
- msg_tkellem(IrcMessage.new(nil, 'TKELLEM', [msg.args.last]))
75
- elsif command == 'TKELLEM'
76
- msg_tkellem(msg)
77
- elsif command == 'CAP'
78
- # TODO: full support for CAP -- this just gets mobile colloquy connecting
79
- if msg.args.first =~ /req/i
80
- send_msg("CAP NAK")
81
- end
82
- elsif command == 'PASS' && @state == :auth
83
- @password = msg.args.first
84
- elsif command == 'NICK' && @state == :auth
85
- @connecting_nick = msg.args.first
86
- maybe_connect
87
- elsif command == 'QUIT'
88
- close_connection
89
- elsif command == 'USER' && @state == :auth
90
- unless @username
91
- @username, @conn_info = msg.args.first.strip.split('@', 2).map { |a| a.downcase }
71
+ failsafe("message: {#{line}}") do
72
+ trace "from client: #{line}"
73
+ msg = IrcMessage.parse(line)
74
+
75
+ command = msg.command
76
+ if @user && command == 'PRIVMSG' && msg.args.first == '-tkellem'
77
+ msg_tkellem(IrcMessage.new(nil, 'TKELLEM', [msg.args.last]))
78
+ elsif command == 'TKELLEM'
79
+ msg_tkellem(msg)
80
+ elsif command == 'CAP'
81
+ # TODO: full support for CAP -- this just gets mobile colloquy connecting
82
+ if msg.args.first =~ /req/i
83
+ send_msg("CAP NAK")
84
+ end
85
+ elsif command == 'PASS' && @state == :auth
86
+ @password = msg.args.first
87
+ elsif command == 'NICK' && @state == :auth
88
+ @connecting_nick = msg.args.first
89
+ maybe_connect
90
+ elsif command == 'QUIT'
91
+ close_connection
92
+ elsif command == 'USER' && @state == :auth
93
+ unless @username
94
+ @username, @conn_info = msg.args.first.strip.split('@', 2).map { |a| a.downcase }
95
+ end
96
+ maybe_connect
97
+ elsif @state == :auth
98
+ error!("Protocol error. You must authenticate first.")
99
+ elsif @state == :connected
100
+ @bouncer.client_msg(self, msg)
101
+ else
102
+ say_as_tkellem("You must connect to an irc network to do that.")
92
103
  end
93
- maybe_connect
94
- elsif @state == :auth
95
- error!("Protocol error. You must authenticate first.")
96
- elsif @state == :connected
97
- @bouncer.client_msg(self, msg)
98
- else
99
- say_as_tkellem("You must connect to an irc network to do that.")
100
- end
101
-
102
- rescue => e
103
- error "Error handling message: {#{msg}} #{e}"
104
- e.backtrace.each { |l| error l }
105
- begin
106
- error! "Internal Tkellem error."
107
- rescue
108
104
  end
109
105
  end
110
106
 
@@ -148,7 +144,9 @@ module BouncerConnection
148
144
  end
149
145
 
150
146
  def unbind
151
- @bouncer.disconnect_client(self) if @bouncer
147
+ failsafe(:unbind) do
148
+ @bouncer.disconnect_client(self) if @bouncer
149
+ end
152
150
  end
153
151
  end
154
152
 
@@ -16,27 +16,35 @@ module IrcServerConnection
16
16
  end
17
17
 
18
18
  def post_init
19
- if @ssl
20
- @bouncer.debug "starting TLS"
21
- # TODO: support strict cert checks
22
- start_tls :verify_peer => false
23
- else
24
- ssl_handshake_completed
19
+ failsafe(:post_init) do
20
+ if @ssl
21
+ @bouncer.debug "starting TLS"
22
+ # TODO: support strict cert checks
23
+ start_tls :verify_peer => false
24
+ else
25
+ ssl_handshake_completed
26
+ end
25
27
  end
26
28
  end
27
29
 
28
30
  def ssl_handshake_completed
29
- EM.next_tick { @bouncer.connection_established(self) }
31
+ failsafe(:ssl_handshake_completed) do
32
+ EM.next_tick { @bouncer.connection_established(self) }
33
+ end
30
34
  end
31
35
 
32
36
  def receive_line(line)
33
- trace "from server: #{line}"
34
- msg = IrcMessage.parse(line)
35
- @bouncer.server_msg(msg)
37
+ failsafe(:receive_line) do
38
+ trace "from server: #{line}"
39
+ msg = IrcMessage.parse(line)
40
+ @bouncer.server_msg(msg)
41
+ end
36
42
  end
37
43
 
38
44
  def unbind
39
- @bouncer.disconnected!
45
+ failsafe(:unbind) do
46
+ @bouncer.disconnected!
47
+ end
40
48
  end
41
49
  end
42
50
 
@@ -1,3 +1,3 @@
1
1
  module Tkellem
2
- VERSION = "0.8.4"
2
+ VERSION = "0.8.5"
3
3
  end
data/lib/tkellem.rb CHANGED
@@ -4,11 +4,16 @@ module Tkellem
4
4
 
5
5
  def self.logger=(new_logger)
6
6
  @logger = new_logger
7
+ if @logger.is_a?(Logger)
8
+ @logger.formatter = proc do |severity, time, progname, msg|
9
+ obj, msg = msg if msg.is_a?(Array)
10
+ "#{time.strftime('%y-%m-%dT%H:%M:%S')} #{severity[0,3]} #{(obj && obj.log_name) || 'tkellem'} (#{obj && obj.object_id}): #{msg}\n"
11
+ end
12
+ end
7
13
  end
8
14
  def self.logger
9
15
  return @logger if @logger
10
- @logger = Logger.new(STDERR)
11
- @logger.datetime_format = "%Y-%m-%d"
16
+ self.logger = Logger.new(STDERR)
12
17
  @logger
13
18
  end
14
19
 
@@ -20,18 +25,31 @@ module Tkellem
20
25
  end
21
26
 
22
27
  def log_name
23
- ""
28
+ nil
24
29
  end
25
30
 
26
31
  def trace(msg)
27
32
  puts("TRACE: #{log_name}: #{msg}") if EasyLogger.trace
28
33
  end
29
34
 
35
+ def failsafe(event)
36
+ yield
37
+ rescue => e
38
+ # if the failsafe rescue fails, we're in a really bad state and should probably just die
39
+ self.error "exception while handling #{event}"
40
+ self.error e.to_s
41
+ (e.backtrace || []).each { |line| self.error line }
42
+ end
43
+
30
44
  ::Logger::Severity.constants.each do |level|
31
45
  next if level == "UNKNOWN"
32
46
  module_eval(<<-EVAL, __FILE__, __LINE__)
33
- def #{level.downcase}(msg)
34
- EasyLogger.logger.#{level.downcase}("\#{log_name} (\#{object_id}): \#{msg}")
47
+ def #{level.downcase}(msg, &block)
48
+ if block
49
+ EasyLogger.logger.#{level.downcase} { [self, block.call] }
50
+ else
51
+ EasyLogger.logger.#{level.downcase}([self, msg])
52
+ end
35
53
  end
36
54
  EVAL
37
55
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: tkellem
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.8.4
5
+ version: 0.8.5
6
6
  platform: ruby
7
7
  authors:
8
8
  - Brian Palmer
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-06-17 00:00:00 -06:00
13
+ date: 2011-06-18 00:00:00 -06:00
14
14
  default_executable: tkellem
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency