spunk 0.0.3 → 0.1.0

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.
@@ -6,5 +6,5 @@ require 'spunk/helpers'
6
6
  require 'spunk/origin'
7
7
  require 'spunk/bot'
8
8
 
9
- SPUNK_VERSION = '0.0.1'
9
+ SPUNK_VERSION = '0.1.0'
10
10
 
@@ -1,9 +1,15 @@
1
1
  module Spunk
2
2
  module Processor
3
3
  class Base
4
- def call(bot, origin, command, parameters)
4
+ def call(hash)
5
+ bot = hash[:bot]
6
+ command = hash[:command]
7
+ parameters = hash[:parameters]
8
+ origin = hash[:origin]
9
+
5
10
  if command =~ /^INVITE #{bot.nickname}$/i
6
- room = Spunk::Helpers.parse_room(parameters)
11
+ room = Helpers.parse_room(hash[:parameters])
12
+ hash[:logger].debug "Recieved invite for room #{room}"
7
13
  if room
8
14
  bot.join_room(room)
9
15
  end
@@ -1,12 +1,15 @@
1
1
  require 'socket'
2
2
  require 'openssl'
3
+ require 'mutex_m'
4
+ require 'logger'
3
5
 
4
6
  module Spunk
5
7
  class Bot
6
- attr_accessor :nickname, :server, :joined_rooms, :ssl, :server, :rooms, :token
8
+ attr_accessor :nickname, :server, :joined_rooms, :ssl, :server, :rooms, :token, :logger
7
9
  attr_reader :processors, :request_processors, :response_processors, :hostname
8
10
 
9
11
  def initialize(options = {})
12
+ #self.extend Mutex_m
10
13
  options.each do |option, value|
11
14
  instance_variable_set("@#{option}", value)
12
15
  end
@@ -21,9 +24,22 @@ module Spunk
21
24
  add_request_processor(Spunk::Processor::Ping.new)
22
25
  add_request_processor(Spunk::Processor::Base.new)
23
26
  @rooms = options[:rooms] ||= []
27
+ if options[:logger].class == String
28
+ @logger = Logger.new(options[:logger])
29
+ @logger.level = Logger::INFO
30
+ elsif options[:logger].nil?
31
+ @logger = Logger.new(STDOUT)
32
+ @logger.level = Logger::INFO
33
+ elsif options[:logger].class == Hash
34
+ @logger = Logger.new(options[:logger][:file])
35
+ @logger.level = options[:logger][:level] ||= Logger::INFO
36
+ elsif options[:logger].class == Logger
37
+ @logger = options[:logger]
38
+ end
24
39
  end
25
40
 
26
41
  def start
42
+ @logger.info "Starting Bot"
27
43
  loop do
28
44
  @buffer ||= ""
29
45
  if @ssl
@@ -75,10 +91,11 @@ module Spunk
75
91
  origin = if !prefix.nil? && prefix != ""
76
92
  Origin.new(prefix)
77
93
  end
78
-
94
+ hash = Helpers.hashify(self, origin, command, parameters, @logger)
95
+ @logger.debug "Processing request: origin=#{hash[:origin]} :: command=#{hash[:command]} :: parameters=#{hash[:parameters]}"
79
96
  (@request_processors + @processors).each do |processor|
80
97
  begin
81
- processor.call(self, origin, command, parameters)
98
+ processor.call(hash)
82
99
  rescue => e
83
100
  puts e.class.name + ": " + e.message
84
101
  puts e.backtrace.join("\n")
@@ -89,7 +106,8 @@ module Spunk
89
106
  def process_response(origin, command, parameters)
90
107
  (@response_processors + @processors).each do |processor|
91
108
  begin
92
- processor.call(self, origin, command, parameters)
109
+ hash = Helpers.hashify(self, origin, command, parameters, @logger)
110
+ processor.call(hash)
93
111
  rescue => e
94
112
  puts e.class.name + ": " + e.message
95
113
  puts e.backtrace.join("\n")
@@ -115,8 +133,10 @@ module Spunk
115
133
  end
116
134
 
117
135
  def connect
136
+ @logger.info "Starting connection to #{@server[:hostname]}:#{@server[:port]}"
118
137
  @socket = TCPSocket.new(@server[:hostname], @server[:port])
119
138
  if @ssl == true
139
+ @logger.debug "Detected SSL connection"
120
140
  @ssl_context = OpenSSL::SSL::SSLContext.new()
121
141
  @ssl_context.verify_mode = OpenSSL::SSL::VERIFY_NONE
122
142
  @socket = OpenSSL::SSL::SSLSocket.new(@socket, @ssl_context)
@@ -132,7 +152,9 @@ module Spunk
132
152
  end
133
153
 
134
154
  def authenticate
155
+ @logger.debug "Starting authentication section"
135
156
  unless @token.nil?
157
+ @logger.debug "Token detected.. sending token"
136
158
  send_message "PASS #{@token}"
137
159
  end
138
160
  send_message "NICK #{@nickname}"
@@ -140,16 +162,19 @@ module Spunk
140
162
  end
141
163
 
142
164
  def join_room(room, password = nil)
165
+ @logger.debug "Joining room #{room}"
143
166
  @joined_rooms << room
144
167
  @joined_rooms.uniq!
145
168
  send_message("JOIN #{room}" + (password ? " #{password}" : ""))
146
169
  end
147
170
 
148
171
  def say(to, message)
172
+ @logger.debug "saying message: PRIVMSG #{to} :#{message}"
149
173
  send_message "PRIVMSG #{to} :#{message}"
150
174
  end
151
175
 
152
176
  def send_message(message)
177
+ @logger.debug "Sending message: #{message}"
153
178
  command, parameters = message.strip.split(/\:/, 2)
154
179
  process_response(origin, command.to_s.strip, parameters)
155
180
  end
@@ -1,12 +1,15 @@
1
1
  module Spunk
2
2
  module Helpers
3
3
  def Helpers.parse_room(command)
4
+ if command.nil?
5
+ return nil
6
+ end
4
7
  command.match(/(\#\S+\b)/i)
5
8
  return $1
6
9
  end
7
10
 
8
- def Helpers.hashify(bot, origin, command, msg)
9
- hash = {:bot=>bot, :origin=>origin, :command => command, :msg => msg, :room => nil}
11
+ def Helpers.hashify(bot, origin, command, parameters, logger)
12
+ hash = {:bot=>bot, :origin=>origin, :command => command, :msg => parameters, :parameters=>parameters, :room => nil, :logger=>logger}
10
13
  hash[:room] = Helpers.parse_room(command)
11
14
  return hash
12
15
  end
@@ -9,6 +9,10 @@ module Spunk
9
9
  def nickname
10
10
  @prefix.to_s.split(/!/, 2)[0]
11
11
  end
12
+
13
+ def username
14
+ @prefix.to_s.split(/!/, 2)[1].split("@").first
15
+ end
12
16
 
13
17
  def to_s
14
18
  case
@@ -1,9 +1,9 @@
1
1
  module Spunk
2
2
  module Processor
3
3
  class Ping
4
- def call(bot, origin, command, parameters)
5
- if command =~ /^PING$/
6
- bot.send_message("PONG :#{parameters}")
4
+ def call(hash)
5
+ if hash[:command] =~ /^PING$/
6
+ hash[:bot].send_message("PONG :#{hash[:parameters]}")
7
7
  end
8
8
  end
9
9
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spunk
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: