Skip to content

Commit

Permalink
Moved client class from webric.rb to lib/client.rb
Browse files Browse the repository at this point in the history
  • Loading branch information
jamez01 committed Dec 4, 2014
1 parent 719962a commit 744809e
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 90 deletions.
91 changes: 91 additions & 0 deletions lib/client.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
module WebRic

class Client
include WebRic::Commands # Include commands that are parsed from JS
attr_accessor :nick, :server, :port, :user_info, :peer_ip, :peer_port
attr_reader :channels, :bot
def initialize(ws)
@nick = "WebRic#{Time.new.to_i}" # Gotta have a name
@channels ||= [] # List of channels for this websocket connection
@port ||= 6667 # Default port
@server ||= "localhost" # Default Server

@ws = ws

end

# Setup is called after JS client connects to WebSocket
def setup(options)
@nick = options['nick'] if options['nick']
@server = options['server'] if options['server']
@port = options['port'].to_i if options['port']
connect
end

# Connect to IRC
def connect
# Cinch plugins that handle IRC messages, etc.
plugins = [WebRic::Plugin::Errors, WebRic::Plugin::Connect,WebRic::Plugin::Messages,WebRic::Plugin::Join]
@bot = Cinch::Bot.new

# Configure Cinch
@bot.configure do |c|
c.message_split_end = ""
c.message_split_start = ""
c.server = @server
c.nick = @nick
c.port = @port
c.channels = ["#WebRicIRC"]
c.plugins.plugins = plugins
plugins.each do |plug|
c.plugins.options[plug] = {webclient: self}
end
end

# Start Cinch in thread to prevent blocking
EM.defer{
@bot.start
}

end

# Helper method to provide direct access to websocket
def socket
@ws
end

# Send list of users in channel to websocket
def names(channel)
target = bot.Channel(channel)
users = target.users.map { |u,m| "#{'@' if m.include? "o" }#{'+' if m.include? "v"}#{u}"}.sort
send_command("names",users: users, channel: channel)
end

# Send errors, notices, etc to client
def systemmsg(head, message)
send_command(:systemmsg, head: head, message: message )
end

# Send channel messages / private messages
def privmsg(channel,nick,message)
send_command(:privmsg, nick: nick, channel: channel , message: "#{Filter.new(message)}")
end

# Send channel messages / private messages
def action(channel,nick,message)
send_command(:action, nick: nick, channel: channel , message: "#{Filter.new(message)}")
end

# Send a command to the web client.
def send_command(command,args)
puts "Send Command #{command}: #{args}"
send_socket({command: command, args: args}.to_json)
end

private
# wrapper methods
def send_socket(msg)
@ws.send(msg)
end
end
end
91 changes: 1 addition & 90 deletions webric.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,98 +9,9 @@
require_relative 'lib/plugins/messages.rb'
require_relative 'lib/plugins/join.rb'
require_relative 'lib/commands.rb'
require_relative 'lib/client.rb'

module WebRic

class Client
include WebRic::Commands # Include commands that are parsed from JS
attr_accessor :nick, :server, :port, :user_info, :peer_ip, :peer_port
attr_reader :channels, :bot
def initialize(ws)
@nick = "WebRic#{Time.new.to_i}" # Gotta have a name
@channels ||= [] # List of channels for this websocket connection
@port ||= 6667 # Default port
@server ||= "localhost" # Default Server

@ws = ws

end

# Setup is called after JS client connects to WebSocket
def setup(options)
@nick = options['nick'] if options['nick']
@server = options['server'] if options['server']
@port = options['port'].to_i if options['port']
connect
end

# Connect to IRC
def connect
# Cinch plugins that handle IRC messages, etc.
plugins = [WebRic::Plugin::Errors, WebRic::Plugin::Connect,WebRic::Plugin::Messages,WebRic::Plugin::Join]
@bot = Cinch::Bot.new

# Configure Cinch
@bot.configure do |c|
c.message_split_end = ""
c.message_split_start = ""
c.server = @server
c.nick = @nick
c.port = @port
c.channels = ["#WebRicIRC"]
c.plugins.plugins = plugins
plugins.each do |plug|
c.plugins.options[plug] = {webclient: self}
end
end

# Start Cinch in thread to prevent blocking
EM.defer{
@bot.start
}

end

# Helper method to provide direct access to websocket
def socket
@ws
end

# Send list of users in channel to websocket
def names(channel)
target = bot.Channel(channel)
users = target.users.map { |u,m| "#{'@' if m.include? "o" }#{'+' if m.include? "v"}#{u}"}.sort
send_command("names",users: users, channel: channel)
end

# Send errors, notices, etc to client
def systemmsg(head, message)
send_command(:systemmsg, head: head, message: message )
end

# Send channel messages / private messages
def privmsg(channel,nick,message)
send_command(:privmsg, nick: nick, channel: channel , message: "#{Filter.new(message)}")
end

# Send channel messages / private messages
def action(channel,nick,message)
send_command(:action, nick: nick, channel: channel , message: "#{Filter.new(message)}")
end

# Send a command to the web client.
def send_command(command,args)
puts "Send Command #{command}: #{args}"
send_socket({command: command, args: args}.to_json)
end

private
# wrapper methods
def send_socket(msg)
@ws.send(msg)
end
end
end

## Setup eventmachine to handle websocket connections
EventMachine.run do
Expand Down

0 comments on commit 744809e

Please sign in to comment.