Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require "IRCHelp"
- require "IRCConnection"
- require "rfc2812"
- require "irc_numerics"
- #require "IRCInfoHolder"
- require "observer"
- require "rexml/document"
- require "IRCChannel"
- require "IRCModes"
- require "Mktree"
- require "IRCUserManager"
- #this class will maintain a single IRC session with a specified network.
- #Use IRCManager in order to create a session, rather than using solitary
- #IRCSession objects.
- class IRCSession < IRCConnection
- @@debug = 2000
- STATUS_DISCONNECTED = 0
- STATUS_CONNECTING = 1
- STATUS_CONNECTED = 2
- include RFC2812
- include Observable
- include IRCModes
- include Mktree
- include IRCHelp
- #This method is supplied with a pre-chewed
- #REXML document object and a network
- #name. If the network exists in the
- #document, the session runs through a
- #startup procedure. If not, the session
- #object raises an error, and should be
- #discarded at that point.
- #=
- #=
- #Once started, two threads will handle input
- #and output.
- #=
- #=
- #tba: The session object should bar access
- #to methods, and/or set an accessor to
- #return a known constant if the connection
- #is currently unavailable. Connections
- #should have the capability to be
- #re-initialized, maybe on a different
- #server within the same group.
- def initialize(network, conf_doc, rejoin=nil)
- super()
- @rejoin = rejoin #Auto rejoin after kicks if non-nil.
- @local_rejoins = {} #This hash will store true/nil values
- #indexed by downcased/stripped
- #channel name.
- @user_mgr = IRCUserManager.instance #IRCUser objects will
- #work to handle some
- #user-specific
- #information and
- #processing.
- STDERR.puts "#{self.class}:#{self} user manager object: #{@user_mgr}"
- @doc = conf_doc.elements["*/networks/"]
- @addr = nil
- @port = nil
- @nick = nil
- @realname = nil
- @userid = nil
- @pass = nil
- @ns_pass = nil #Nickserv password. Will probably remove the
- #current nickserv auth system in favour of a
- #<insert service name here> authentication
- #system.
- @doc.elements.each{|e|
- puts e
- }
- @prefixes = ["@+", "ov"] #Prefixes are used when parsing
- #channel information to quickly
- #decide who is an op or not.
- #The 005 info message will be parsed
- #and used to rewrite this variable.
- @current_network = nil
- @status = STATUS_DISCONNECTED
- @current_server = nil
- @quit_msg = "\0035IRCSession v0.01a\017 (closing link)"
- STDERR.puts "#{self.class}:#{self} Loading details for #{network}"
- count = 0
- working_doc = nil
- @doc.elements.each{|element|
- STDERR.puts "#{self.class}:#{self} scanning element: #{element}"
- STDERR.puts "#{self.class}:#{self} #{element.attributes['name']}"
- if /^#{network}$/i.match(element.attributes['name'].strip) then
- STDERR.puts "#{self.class}:#{self} matched!"
- count += 1
- STDERR.puts "#{self.class}:attributes: #{@doc.attributes}"
- @current_network = element.attributes['name'].strip
- @nick = element.attributes['nick']
- @userid = element.attributes['userid']
- @realname = element.attributes['realname']
- @ns_pass = element.attributes['nickserv']
- STDERR.puts "#{self.class}:getting servers document" if @@debug < 1
- working_doc = element
- STDERR.puts "#{self.class}:working_doc is: #{working_doc}" if @@debug < 1
- STDERR.puts "#{self.class}:breaking out" if @@debug < 1
- break
- end
- if count > 0 then
- STDERR.puts "#{self.class}:breaking out." if @@debug < 1
- break
- end
- }
- if count < 1 then
- raise "#{self.class}: initialize: Could not find entry for network #{network}: #{$!}"
- end
- @server_list = []
- working_doc.elements.each{|e|
- STDERR.puts "#{self.class}:server: #{e}" if @@debug < 1
- @server_list.push(e)
- }
- begin
- log_in(network)
- rescue
- raise "#{self.class}: initialize: Failed to log into network #{network}: #{$!}"
- end
- #connect(@addr,@port,@nick,@realname,@userid,pass)
- #@evcap = IRCEventCapturer.new(self)
- #@info = IRCInfoHolder.new
- @motd = ""
- @motdwritten = 0
- @motdwriting = 0
- @writingnames = []
- @statusbuffer = []
- @statusbuffersize = 500
- #known_users hash table contains IRCChannel objects referenced
- #by channel name.
- @known_channels = Hash.new
- #known_users hash table contains mode strings referenced by
- #nickname.
- @known_users = Hash.new
- @anotherextendedinputthread = Thread.new {
- while (true == true)
- @inputvalid = nil
- @input = getline
- irc_parsed = irc_parse(@input)
- if irc_parsed then
- STDERR.puts "#{self.class}:#{self.class}: irc_parsed[1] = #{irc_parsed[1]}" if @@debug < 1
- if irc_parsed[1] == RPL_WELCOME then
- STDERR.puts "#{self.class}:#{self} RPL_WELCOME intercepted." if @@debug < 5
- @status = STATUS_CONNECTED
- STDERR.puts "#{self.class}:#{self} Sending login details to Nickserv." if @@debug < 5
- cmd_privmsg("NickServ", "IDENTIFY #{@ns_pass}")
- STDERR.puts "#{self.class}:#{self} done!" if @@debug < 5
- STDERR.puts "#{self.class}:#{self} status is CONNECTED." if @@debug < 5
- end
- STDERR.puts "#{self.class}:#{self.class}: welcome match passed" if @@debug < 5
- if @@debug < 1 then
- STDERR.puts "#{self.class}:#{self.class}: irc_parsed contents:" if @@debug < 1
- irc_parsed.each{|el|
- STDERR.puts "#{self.class}:el: #{el}"
- STDERR.puts "#{self.class}:el class: #{el.class}"
- }
- STDERR.puts "#{self.class}:#{self.class}: current network: #{@current_network}"
- end
- changed
- STDERR.puts "#{self.class}:#{self.class}: changed" if @@debug < 1
- notify_observers(irc_parsed[0], irc_parsed[1], irc_parsed[2], @current_network)
- STDERR.puts "#{self.class}:#{self.class}: calling info parser" if @@debug < 1
- _info_parser(irc_parsed[0], irc_parsed[1], irc_parsed[2])
- STDERR.puts "#{self.class}:#{self.class}: info parser called" if @@debug < 1
- else
- sleep 0.1
- end
- end
- }
- #@anotherextendedinputthread.join
- end
- #this method will return true if the session is currently connected.
- def is_active?
- if @status == STATUS_CONNECTED then
- return true
- else
- return nil
- end
- end
- #this method will set the bot mute on a specific channel.
- def mute(chan)
- return set_mute(chan)
- end
- #this method will unset the bot mute on a specified channel.
- def unmute(chan)
- return set_mute(chan, 1)
- end
- def set_mute(chan, value)
- dchan = chan.downcase
- if !@known_channels.has_key?(dchan) then
- return nil
- end
- @known_channels[dchan].set_mute(value)
- return true
- end
- #this method will return true or false depending on whether
- #the session is currently listening to the specified channel.
- def in_channel?(chan)
- dchan = chan.downcase
- if @known_channels.has_key?(dchan) then
- if !@known_channels[dchan].joined then
- return nil
- end
- if nick != nil then
- if !@known_channels[dchan].is_here?(nick) then
- return nil
- end
- end
- return true
- else
- return nil
- end
- return nil
- end
- #this method sends a raw IRC message, with trailing cr/lf attached.
- def cmd_raw(msg)
- STDERR.puts "#{self.class}:intercepted message." if @@debug < 1
- msg = /^(.*)[\n\r]?$/.match(msg)[1]
- print "sending: #{msg}\n\r" if @@debug < 1
- putline("#{msg}\r\n")
- end
- #This method will send an INVITE message to the intended target, to join the specified channel.
- def cmd_invite(target, channel)
- begin
- output = "INVITE #{target.strip} #{channel.strip}\n\r"
- STDERR.puts "#{self.class}:sending: #{output}" if @@debug < 3000
- putline(output)
- return true
- rescue
- STDERR.puts "#{self.class}:#{self.class}: cmd_privmsg: failure: #{$!}" if @@debug < 5
- return nil
- end
- end
- #this method sends a PRIVMSG message to a specified target.
- def cmd_privmsg(target, msg)
- begin
- output = "PRIVMSG #{target} :#{msg.strip}\n\r"
- STDERR.puts "#{self.class}:sending: #{output}" if @@debug < 1
- putline(output)
- return true
- rescue
- STDERR.puts "#{self.class}:#{self.class}: cmd_privmsg: failure: #{$!}" if @@debug < 5
- return nil
- end
- end
- #this method sends a CTCP ACTION message to a specified target.
- def cmd_action(target, msg)
- output = "\000PRIVMSG #{target} :#{msg.strip}\000\r\n"
- STDERR.puts "#{self.class}:#{self.class}: sending: #{output}" if @@debug < 1
- putline(output)
- end
- #this method lists all visible IRC nicks/users.
- def cmd_list_users
- output = {}
- @known_channels.each{|k, v|
- output[k] = []
- v.users.each{|u|
- STDERR.puts "#{self.class}:#{self.class}: channel #{k}: #{u}" if @@debug < 1
- output[k].push u
- }
- }
- return output
- end
- #this method lists all known channels.
- def cmd_list_chans
- output = []
- @known_channels.each{|k, v|
- output.push k
- }
- if output.length > 0 then
- return output
- end
- return nil
- end
- #this method lists all IRC nicks in a specified channel
- def cmd_list_chan_users(chan)
- STDERR.puts "#{self.class}:#{self.class}: returning info for channel #{chan}" if @@debug < 2
- dchan = chan.downcase
- if @known_channels.has_key?(dchan) then
- STDERR.puts "#{self.class}:#{self.class}: channel is known." if @@debug < 2
- out = []
- @known_channels[dchan].users.each{|k, v|
- STDERR.puts "#{self.class}:#{self.class}: pushing: [#{v.dup.join(',')}][0]" if @@debug < 2
- out.push v[0]
- }
- return out
- end
- return nil
- end
- #this method gets the current status feed for the session.
- def cmd_get_status
- if (@statusbuffer.length > 0) then
- tmp = @statusbuffer.shift
- STDERR.puts "#{self.class}:#{self.class}: getting value: #{tmp}" if @@debug < 1
- return tmp
- else
- #puts "<statusgets@ExtendedIRCConnection> no value to get."
- return nil
- end
- end
- #this method sends a JOIN command with the specified channel as a parameter.
- def cmd_join_chan(chan)
- puts "#{self.class}: cmd_join_chan triggered for channel #{chan}" if @@debug < 10000
- cmd = "JOIN :#{chan}\r\n"
- putline(cmd)
- return true
- end
- #this method sends a PART command with the specified channel as a parameter.
- def cmd_part_chan(chan)
- cmd = "PART :#{chan}\r\n"
- putline(cmd)
- return true
- end
- #this method will disconnect from the server and close the session.
- def cmd_quit(message=@quit_msg)
- disconnect!(message)
- @status = STATUS_DISCONNECTED
- end
- #this method will send a WHOIS command.
- def cmd_whois(nick, target=nil)
- if target == nil then
- cmd = "WHOIS #{nick}\r\n"
- else
- cmd = "WHOIS #{target} #{nick}\r\n"
- end
- putline(cmd)
- end
- def rejoin
- return @rejoin
- end
- def rejoin=(i)
- if i then
- @rejoin = true
- else
- @rejoin = nil
- end
- return @rejoin
- end
- def cmd_set_rejoin(chan, value=true)
- v = nil
- if value then
- v = true
- end
- dchan = chan.downcase.strip
- @local_rejoins[dchan] = v
- puts "#{self.class}: cmd_set_rejoin: local rejoins set: #{dchan} = '#{v}'" if @@debug < 10000
- return v
- end
- def cmd_unset_rejoin(chan)
- @local_rejoins.delete(chan.downcase.strip)
- end
- def cmd_whowas(nick, num=nil, target=nil)
- end
- def cmd_who(text, target=nil)
- end
- def cmd_squery(service, text)
- end
- #this method will log into a specified network
- def log_in(network)
- STDERR.puts "#{self.class}:#{self.class}: attempting log in sequence for #{network}" if @@debug < 1
- if @status == STATUS_DISCONNECTED then
- @status = STATUS_CONNECTING
- count = 0
- begin
- e = @server_list[count]
- if @@debug < 1 then
- STDERR.puts "#{self.class}:#{self.object_id}: e.class: #{e.class}"
- STDERR.puts "#{self.class}:#{self.object_id}: e contents follow..."
- e.attributes.each{|k, v| STDERR.puts "#{self.class}:#{k} => #{v}"}
- end
- @current_server = e.attributes['name']
- STDERR.puts "#{self.class}:#{self.object_id}: Opening connection to #{@current_server}"
- @addr = e.attributes['host']
- @port = e.attributes['port']
- STDERR.puts "#{self.class}:#{self.object_id}: #{@addr}:#{@port}"
- @known_channels = Hash.new
- if @@debug < 1 then
- puts @nick
- puts @userid
- puts @realname
- puts @ns_pass
- end
- connect(@addr, @port, @nick, @userid, @realname, nil, @ns_pass)
- STDERR.puts "#{self.class}:#{self.class}: starting up new network entry for #{network}" if @@debug < 1
- @user_mgr.new_network(network)
- rescue
- STDERR.puts "#{self.class}:#{self.class}: Connection attempt failed: #{$!}"
- STDERR.puts "#{self.class}:#{self.class}: backtrace follows..."
- prefix = ""
- $!.backtrace.each{|tline|
- STDERR.puts "#{self.class}:#{prefix}#{tline}"
- }
- count += 1
- if count > @server_list.length then
- count = 0
- end
- retry
- end
- @status = STATUS_DISCONNECTED
- else
- return nil
- end
- end
- #this method will return true if the specified nick is an operator.
- def is_op?(chan, nick)
- STDERR.puts "#{self.class}:#{self.class}: #{@current_network}: checking #{chan} for ops on #{nick}" if @@debug < 1
- dchan = chan.downcase
- if @known_channels.has_key?(dchan) then
- STDERR.puts "#{self.class}:#{self.class}: #{@current_network}: channel exists" if @@debug < 1
- return @known_channels[dchan].is_op?(nick)
- end
- return nil
- end
- attr_reader(:writingnames, :known_channels, :nick)
- attr_accessor(:info)
- protected
- def statusputs(val)
- #puts "<statusputs@ExtendedIRCConnection> Triggered"
- if (@statusbuffer.length <= @statusbuffersize) then
- #puts "<statusputs@ExtendedIRCConnection> putting value #{val}"
- @statusbuffer.push(val)
- return nil
- else
- #puts "<statusputs@ExtendedIRCConnection> shifting and putting value #{val}"
- tmp = @statusbuffer.shift
- @statusbuffer.push(val)
- return val
- end
- end
- private
- def _info_parser(prefix, cmd, params)
- #changed
- #notify_observers(prefix, cmd, params, @current_network)
- begin
- case cmd
- when "PRIVMSG"
- STDERR.puts "#{self.class}:#{self.class}: PRIVMSG" if @@debug < 2001
- _event_privmsg(prefix, cmd, params)
- when "NICK"
- STDERR.puts "#{self.class}:NICK" if @@debug < 2001
- _event_nick(prefix, cmd, params)
- when "JOIN"
- STDERR.puts "#{self.class}:JOIN" if @@debug < 2001
- _event_join(prefix, cmd, params)
- when "PART"
- STDERR.puts "#{self.class}:PART" if @@debug < 2001
- _event_part(prefix, cmd, params)
- when "QUIT"
- STDERR.puts "#{self.class}:QUIT" if @@debug < 2001
- _event_quit(prefix, cmd, params)
- when "MODE"
- STDERR.puts "#{self.class}:MODE" if @@debug < 2001
- _event_mode(prefix, cmd, params)
- when "KICK"
- STDERR.puts "#{self.class}:KICK" if @@debug < 3001
- _event_kick(prefix, cmd, params)
- when RPL_NAMREPLY
- STDERR.puts "#{self.class}:RPL_NAMREPLY" if @@debug < 2001
- _event_rpl_namreply(prefix, cmd, params)
- when RPL_ENDOFNAMES
- STDERR.puts "#{self.class}:RPL_ENDOFNAMES" if @@debug < 2001
- _event_rpl_endofnames(prefix, cmd, params)
- when RPL_BOUNCE
- STDERR.puts "#{self.class}:RPL_BOUNCE" if @@debug < 2001
- _event_rpl_bounce(prefix, cmd, params)
- when RPL_NICKSERV_AUTHED
- STDERR.puts "#{self.class}:RPL_NICKSERV_AUTHED" if @@debug < 2001
- _event_rpl_nickserv_authed(prefix, cmd, params)
- when RPL_WHOISUSER
- STDERR.puts "#{self.class}:RPL_WHOISUSER" if @@debug < 2001
- when RPL_WHOISSERVER
- STDERR.puts "#{self.class}:RPL_WHOISSERVER" if @@debug < 2001
- when RPL_WHOISOPERATOR
- STDERR.puts "#{self.class}:RPL_WHOISOPERATOR" if @@debug < 2001
- when RPL_WHOISCHANNELS
- STDERR.puts "#{self.class}: RPL_WHOISCHANNELS" if @@debug < 2001
- when RPL_WHOISIDLE
- STDERR.puts "#{self.class}: RPL_WHOISIDLE" if @@debug < 2001
- when RPL_ENDOFWHOIS
- STDERR.puts "#{self.class}: RPL_ENDOFWHOIS" if @@debug < 2001
- end
- rescue
- if @@debug < 4000 then
- STDERR.puts "#{self.class}: Error in #_info_parser: #{$!}"
- STDERR.puts "#{self.class}: Backtrace follows..."
- pre = ""
- $!.backtrace.each{|tr|
- puts "#{self.class}: #{pre}#{tr}"
- pre << " "
- }
- end
- end
- end
- def _event_rpl_whoisuser(prefix, cmd, params)
- end
- def _event_privmsg(prefix, cmd, params)
- prematch = /^\:(.*)/.match(prefix)
- if prematch then
- prefix = prematch[1]
- end
- @user_mgr.new_user(@current_network, prefix)
- return true
- end
- #This method needs to be improved so it will only rejoin if the kick
- #target is the bot's current nick/mask.
- def _event_kick(prefix, cmd, params)
- STDERR.puts "#{self.class}:#{self} event KICK triggered." if @@debug < 10000
- pmatch = /^(.*)\:(.*)$/.match(params)
- STDERR.puts "#{self.class}:#{self} kick pmatch[1]: #{pmatch[1]}" if @@debug < 10000
- chan, nick = pmatch[1].strip.downcase.split(/\s/)
- #dnick = nick.tr!("^ -~", "")
- #dchan = chan.tr!("^ -~", "")
- dnick = nick.strip
- dchan = chan.strip
- if @rejoin then
- puts "#{self.class}: _event_kick: @rejoin is non-nil." if @@debug < 10000
- cmd_join_chan(dchan) if @local_rejoins[dchan] == nil
- else
- puts "#{self.class}: _event_kick: @rejoin is nil." if @@debug < 10000
- puts "#{self.class}: local rejoins for #{dchan} is '#{@local_rejoins[dchan]}'"
- if @local_rejoins[dchan] != nil
- puts "#{self.class}: _event_kick: local join enabled. Joining channel #{dchan}." if @@debug < 10000
- cmd_join_chan(dchan)
- end
- end
- begin
- return @known_channels[dchan].del_user(dnick)
- rescue
- STDERR.puts "#{self.class}:#{self} Error removing user: #{$!}" if @@debug < 5
- #raise "#{self.class}: _event_kick: #{$!}"
- return nil
- end
- end
- #Updates user or channel mode strings.
- def _event_mode(prefix, cmd, params)
- STDERR.puts "#{self.class}:#{self} event MODE triggered." if @@debug < 1
- p_prefix = irc_parse_prefix(prefix)
- prefix = /^\:?(.*)$/.match(prefix)[1]
- STDERR.puts "#{self.class}:#{self} prefix trimmed to: #{prefix}" if @@debug < 1
- if p_prefix then
- begin
- user = nil
- type = irc_obj_type(prefix)
- p_params = nil
- nicklist = nil
- case type[0]
- when IRC_TYPE_NICK
- STDERR.puts "#{self.class}:#{self} parsing parameters as USER MODE message" if @@debug < 1
- p_params = irc_parse_user_mode_params(params)
- when IRC_TYPE_MASK
- STDERR.puts "#{self.class}:#{self} parsing parameters as CHANNEL MODE message" if @@debug < 1
- p_params = irc_parse_chan_mode_params(params)
- modes = p_params[1].strip
- list = p_params[2]
- chan = p_params[0].strip
- dchan = chan.downcase
- nicklist = irc_calc_chan_modes(list, modes)
- if nicklist then
- STDERR.puts "#{self.class}:#{self} nick list:" if @@debug < 1
- nicklist.each{|k, v|
- STDERR.puts "#{self.class}:#{self} #{k}: #{v}" if @@debug < 1
- @user_mgr.new_user(@current_network, prefix)
- if k != nil then
- t_user = @user_mgr.get_user(@current_network, k.strip)
- if t_user != nil then
- #user = t_user.strip
- t_user.put_message(prefix, cmd, params, @current_network)
- #user.mod_chan_flags(chan, v)
- @known_channels[dchan].set_user_mode(k, v)
- STDERR.puts "#{self.class}:#{self} user modified. New channel modes: #{@known_channels[dchan].get_user_modes(k)}" if @@debug < 1
- end
- end
- }
- return true
- else
- STDERR.puts "#{self.class}:#{self} unable to parse nick list" if @@debug < 1
- return nil
- end
- else
- STDERR.puts "#{self.class}:type: #{type[0]}" if @@debug < 1
- end
- STDERR.puts "#{self.class}:params parsed." if @@debug < 1
- if p_params then
- if type[0] == IRC_TYPE_NICK then
- STDERR.puts "#{self.class}:#{self} parsing user mode params for #{p_params[0]}" if @@debug < 1
- else
- STDERR.puts "#{self.class}:#{self} parsing channel mode params for #{p_params[0]}" if @@debug < 1
- end
- else
- STDERR.puts "#{self.class}:#{self} invalid parameters." if @@debug < 1
- end
- rescue
- STDERR.puts "#{self.class}:poo: #{$!}" if @@debug < 5
- STDERR.puts "#{self.class}:#{self.class}: backtrace follows"
- pre = ""
- $!.backtrace.each{|tr|
- STDERR.puts "#{self.class}:#{pre}#{tr}"
- pre << " "
- }
- return nil
- end
- else
- STDERR.puts "#{self.class}:#{self} invalid prefix." if @@debug < 1
- end
- end
- def _event_nick(prefix, cmd, params)
- STDERR.puts "#{self.class}:#{self} event NICK triggered." if @@debug < 1
- p_prefix = irc_parse_prefix(prefix)
- newnick = /^\:?(.*)$/.match(params.strip)[1]
- STDERR.puts "#{self.class}:#{self} #{p_prefix[0]}'s nick is now #{newnick}" if @@debug < 1
- if /#{newnick}/i.match(@nick) then
- STDERR.puts "#{self.class}:nick is own nick" if @@debug < 1
- @nick = newnick
- end
- STDERR.puts "#{self.class}:#{self.class}: getting/creating new user" if @@debug < 1
- begin
- @user_mgr.new_user(@current_network, prefix)
- @user_mgr.get_user(@current_network, p_prefix[0]).put_message(prefix, cmd, params, @current_network)
- #@user_mgr.get_user!(@current_network, p_prefix[0]).put_message(prefix, cmd, params, @current_network)
- rescue
- STDERR.puts "#{self.class}:#{self.class}: ERROR IN NICK CHANGE: #{$!}: backtrace follows"
- pre = ""
- $!.backtrace.each{|tr|
- STDERR.puts "#{self.class}:#{pre}#{tr}"
- }
- end
- STDERR.puts "#{self.class}:#{self.class}: new user got/created" if @@debug < 1
- #@user_mgr.deactivate_user(@current_network, p_prefix[0])
- #puts "#{self} old nick deactivated."
- #newuser = @user_mgr.get_user!(@current_network, newnick)
- #newuser.put_message(prefix, cmd, params, @current_network)
- #puts "#{self} new user entry set."
- #@user_mgr.activate_user(@current_network, newnick)
- #puts "#{self} entry activated."
- @user_mgr.get_user(@current_network, p_prefix[0]).name = newnick
- STDERR.puts "#{self.class}:#{self.class}: user entry changed to #{newnick}" if @@debug < 1
- @known_channels.each{|key, chan|
- STDERR.puts "#{self.class}:#{self} examining channel #{chan.name}" if @@debug < 1
- chan.nick_change(p_prefix[0], newnick)
- }
- end
- def _event_join(prefix, cmd, params)
- STDERR.puts "#{self.class}:#{self} _event_join triggered"
- p_prefix = irc_parse_prefix(prefix)
- if p_prefix then
- if p_prefix.length > 1 then
- begin
- param_match = /^\:?(\S*)/.match(params.strip)[1]
- STDERR.puts "#{self.class}:#{self} params matched" if @@debug < 2
- rescue
- STDERR.puts "#{self.class}:#{self} invalid parameter" if @@debug < 2
- return nil
- end
- nick = p_prefix[0]
- STDERR.puts "#{self.class}:#{self} nick detected." if @@debug < 2
- chan = param_match
- STDERR.puts "#{self.class}:#{self} chan detected: #{chan}!" if @@debug < 2
- dchan = chan.downcase.strip
- dchan.tr!("^ -~", "")
- @user_mgr.new_user(@current_network, prefix)
- a = @user_mgr.get_user(@current_network, nick)
- a.put_message(prefix, cmd, params, @current_network)
- STDERR.puts "#{self.class}:#{self} user got from manager." if @@debug < 2
- STDERR.puts "#{self.class}:#{self} event_join: user #{p_prefix[0]} is joining channel #{chan} with entry name: #{dchan}!" if @@debug < 2
- if !@known_channels.has_key?(dchan) then
- STDERR.puts "#{self.class}:#{self} Creating record for channel #{dchan}" if @@debug < 2
- @known_channels[dchan] = IRCChannel.new(chan)
- else
- @known_channels[dchan].join
- end
- @known_channels[dchan].add_user(nick)
- if @@debug < 2 then
- STDERR.puts "#{self.class}:#{self} new channel list:"
- @known_channels.each{|k, v|
- puts v.name
- if v.users.length > 0 then
- STDERR.puts "#{self.class}: user list for #{v.name}"
- v.users.each{|e|
- STDERR.puts "#{self.class}: #{e}"
- }
- end
- }
- end
- else
- STDERR.puts "#{self.class}:#{self} invalid prefix type in _event_join" if @@debug < 2
- return nil
- end
- else
- return nil
- end
- end
- def _event_part(prefix, cmd, params)
- parsed_prefix = irc_parse_prefix(prefix)
- if parsed_prefix then
- if parsed_prefix.length > 1 then
- if /\:/.match(params.strip) then
- chan, trailing = /^([^\:]*)\:(.*)$/.match(params.strip)[1..2]
- else
- chan = params.strip
- end
- if chan then
- dchan = chan.downcase.strip
- dchan.tr!("^ -~", "")
- nick = parsed_prefix[0]
- dnick = nick.downcase.strip
- dnick.tr!("^ -~", "")
- STDERR.puts "#{self.class}:#{self} user #{parsed_prefix[0]} parting from channel #{chan}" if @@debug < 2
- begin
- if !@known_channels.has_key?(dchan) then
- STDERR.puts "#{self.class}:#{self} Creating record for channel #{chan}" if @@debug < 2
- @known_channels[dchan] = IRCChannel.new(chan)
- end
- rescue
- STDERR.puts "#{self.class}:#{self} oops: #{$!}" if @@debug < 5
- end
- STDERR.puts "#{self.class}:#{self} removing user #{parsed_prefix[0]} from channel list #{dchan}" if @@debug < 2
- STDERR.puts "#{self.class}:#{self.class}: object class is: #{parsed_prefix[0].class}" if @@debug < 2
- begin
- @known_channels[dchan].del_user(parsed_prefix[0].to_s)
- rescue
- STDERR.puts "#{self.class}:#{self} Error removing user: #{$!}" if @@debug < 5
- raise "#{self.class}: _event_part: #{$!}"
- end
- if @@debug < 2 then
- STDERR.puts "#{self.class}:#{self} User removed. New channel list:"
- STDERR.puts "#{self.class}:#{self} new channel list:"
- @known_channels.each{|k, v|
- puts v.name
- if v.users.length > 0 then
- STDERR.puts "#{self.class}: user list for #{v.name}"
- v.users.each{|e|
- STDERR.puts "#{self.class}: #{e}"
- }
- end
- }
- end
- @user_mgr.new_user(@current_network, prefix)
- a = @user_mgr.get_user(@current_network, parsed_prefix[0])
- #puts "#{self} removing channel entry from IRCUser object"
- #@user_mgr.get_user!(@current_network, parsed_prefix[0]).del_chan(dchan)
- #puts "#{self} entry removed!"
- else
- STDERR.puts "#{self.class}:#{self} mangled params for part message!" if @@debug < 2
- return nil
- end
- else
- return nil
- end
- else
- return nil
- end
- end
- def _event_quit(prefix, cmd, params)
- parsed_prefix = irc_parse_prefix(prefix)
- if parsed_prefix then
- if parsed_prefix.length > 1 then
- parsed_params = /^\:?(.*)$/.match(params.strip)
- if parsed_params then
- nick = parsed_prefix[0]
- @known_channels.each{|k, v|
- v.del_user(nick)
- }
- STDERR.puts "#{self.class}:#{self} user #{nick} quit (#{parsed_params})" if @@debug < 2
- else
- STDERR.puts "#{self.class}:#{self} mangled params for quit message!" if @@debug < 2
- return nil
- end
- else
- return nil
- end
- else
- return nil
- end
- end
- def _event_rpl_namreply(prefix, cmd, params)
- begin
- STDERR.puts "#{self.class}:#{self} event namreply..." if @@debug < 1
- parsed_params = irc_parse_namreply_params(params)
- if parsed_params.class == Array then
- chan = parsed_params[1].strip
- dchan = chan.downcase
- if !@writingnames.include?(dchan) then
- STDERR.puts "#{self.class}:#{self} new name list. Deleting old data." if @@debug < 1
- @known_channels[dchan].clear_users
- @writingnames.push dchan
- end
- STDERR.puts "#{self.class}:#{self.class} scanning newly-recieved users list." if @@debug < 1
- parsed_params[2].each{|raw_name|
- begin
- user_prefixes = ""
- name = raw_name
- STDERR.puts "#{self.class}:#{self} matching first character: #{raw_name[0,1]}" if @@debug < 1
- @prefixes[0].each_byte{|bt|
- STDERR.puts "#{self.class}:#{self} matching against #{bt.chr}" if @@debug < 1
- if raw_name[0,1] == bt.chr then
- STDERR.puts "#{self.class}:match!" if @@debug < 1
- name = raw_name[1..raw_name.length]
- user_prefixes << raw_name[0,1]
- end
- }
- user_modes = ""
- user_prefixes.split('').each{|el|
- STDERR.puts "#{self.class}:#{self} converting prefix #{el}" if @@debug < 1
- @prefixes[0].split('').each_index{|idx|
- STDERR.puts "#{self.class}:#{self} is it #{@prefixes[0][idx].chr}?" if @@debug < 1
- if el == @prefixes[0][idx].chr then
- user_modes << @prefixes[1][idx].chr
- STDERR.puts "#{self.class}:yes!" if @@debug < 1
- STDERR.puts "#{self.class}:adding prefix: #{@prefixes[1][idx].chr}" if @@debug < 1
- else
- STDERR.puts "#{self.class}:no!" if @@debug < 1
- end
- }
- }
- STDERR.puts "#{self.class}:#{self} getting/creating user #{name}" if @@debug < 1
- a_user = @user_mgr.get_user!(@current_network, name)
- #a_user.mod_chan_flags(chan, "+#{user_prefixes}") if user_prefixes != ""
- STDERR.puts "#{self.class}:#{self} adding name to session list" if @@debug < 1
- @known_channels[dchan].add_user(name)
- STDERR.puts "#{self.class}:#{self} applying modestring if needed. user_prefixes: #{user_prefixes}, user_modes: #{user_modes}" if @@debug < 1
- @known_channels[dchan].set_user_mode!(name, "+#{user_modes}")
- rescue
- STDERR.puts "#{self.class}:#{self} oh crap: #{$!}" if @@debug < 1
- raise $!
- end
- }
- end
- rescue
- raise "#{self.class}: _event_rpl_namreply: #{$!}"
- end
- end
- def _event_rpl_endofnames(prefix, cmd, params)
- STDERR.puts "#{self.class}:#{self} event rpl_endofnames triggered." if @@debug < 1
- parsed_params = irc_parse_twoarg_params(params)
- STDERR.puts "#{self.class}:#{self} params preprocessed." if @@debug < 1
- nick, chan = parsed_params[0], parsed_params[1]
- STDERR.puts "#{self.class}:#{self} nick and chan assigned." if @@debug < 1
- STDERR.puts "#{self.class}:#{self} nick value: #{nick}" if @@debug < 1
- STDERR.puts "#{self.class}:#{self} chan value: #{chan}" if @@debug < 1
- @writingnames.delete_if{|el| el == chan.downcase}
- STDERR.puts "#{self.class}:#{self} channel #{chan} is no longer writing names." if @@debug < 1
- end
- def _event_rpl_bounce(prefix, cmd, params)
- STDERR.puts "#{self.class}:#{self} _event_rpl_bounce: params:" if @@debug < 1
- puts params
- p_match1 = /^(\S*)\s(.*)(\:.*$)?/.match(params.strip)
- if p_match1 then
- if @@debug < 1
- STDERR.puts "#{self.class}:RPL_BOUNCE has extended info. Initial results:"
- STDERR.puts "#{self.class}:user: #{p_match1[1]}"
- STDERR.puts "#{self.class}:msg: #{p_match1[2]}"
- STDERR.puts "#{self.class}:end: #{p_match1[3]}" if p_match1[3]
- end
- p_match2 = p_match1[2].split(' ')
- p_match_hsh = {}
- p_match2.each{|el|
- k, v = el.split('=')
- p_match_hsh[k] = v
- }
- if @@debug < 1
- STDERR.puts "#{self.class}:RPL_BOUNCE filtered results:"
- p_match_hsh.each{|k, v|
- STDERR.puts "#{self.class}:#{k}: #{v}"
- }
- end
- if p_match_hsh.has_key?("PREFIX") then
- v = p_match_hsh["PREFIX"]
- prefix_match = /^\((\S)\)(\S)$/.match(v.strip)
- @prefixes = [prefix_match[1], prefix_match[2]] if prefix_match
- STDERR.puts "#{self.class}:#{self} changed prefix values." if @@debug < 1
- STDERR.puts "#{self.class}:chars: #{@prefixes[0]}, modes: #{@prefixes[1]}" if @@debug < 1
- end
- else
- STDERR.puts "#{self.class}:#{self} This may be an rfc2812 RPL_BOUNCE. Write a fallback to cope here..." if @@debug < 5
- end
- end
- def _event_rpl_nickserv_authed(prefix, cmd, params)
- p_params = /^(\S)\s(\S)/.match(params.strip)
- begin
- @irc_mgr.get_user(p_params[2]).set_auth
- rescue
- STDERR.puts "#{self.class}:#{self} _event_rpl_nickserv_authed: #{$!}" if @@debug < 5
- end
- end
- def _save_channels
- @known_channels.each{|k, v|
- }
- end
- end
- #(self.methods - Object.methods).each{|el|
- # puts el
- #}
- def login_test
- doc = nil
- File.open("bot_conf.xml", "r"){|fo|
- STDERR.puts "#{self.class}:file open."
- doc = REXML::Document.new(fo)
- STDERR.puts "#{self.class}:document loaded."
- puts doc.elements["*/networks/*"]
- }
- #umgr = IRCUserManager.instance
- #umgr.start("test")
- begin
- a = IRCSession.new("Espernet", doc)
- sleep 10
- a.cmd_join_chan("#crystalbot")
- sleep 1.0
- a.cmd_privmsg("#crystalbot", "IRCSession object seems to be working.")
- sleep 3.0
- a.cmd_quit("test complete.")
- sleep 1.0
- rescue
- STDERR.puts "#{self.class}:the shit hit the fan here: #{$!}"
- end
- end
- def xmltest
- doc = nil
- File.open("irc_conf.xml", "r"){|fo|
- STDERR.puts "#{self.class}:file open."
- doc = REXML::Document.new(fo)
- STDERR.puts "#{self.class}:document loaded."
- puts doc
- STDERR.puts "#{self.class}:filtered results:"
- puts doc.elements["*/networks/*"]
- }
- return doc
- end
- def standalone_session
- standalone = 1
- if standalone > 0 then
- #@a = IRCSession.new("192.168.0.1",6670,"RubyBot","RubyBot","RubyBot",nil, "lol")
- @a = IRCSession.new("EsperNet", xmltest)
- @t = Thread.new{
- tstr = Time.now.strftime("%Y%m%d%H%M%S")
- @fileobj = File.open("#{self.class}#{tstr}.txt", "w+"){|fobj|
- while true == true
- tmp = @a.cmd_get_status
- print "#{tmp.strip}\n" if tmp
- fobj.print "#{tmp.strip}\n" if tmp
- fobj.flush if fobj
- end
- STDERR.puts "#{self.class}:<@t-thread> Out of main loop."
- }
- }
- STDERR.puts "#{self.class}:Bot awake. Joining channels."
- @a.cmd_join_chan("#crystalbot")
- @a.cmd_join_chan("#crystalbot2")
- @a.cmd_join_chan("#crystalbot3")
- @a.cmd_privmsg("#crystalbot", "Hello, World.")
- STDERR.puts "#{self.class}:Sent privmsg"
- @t.join
- while true == true
- end
- end
- end
- #standalone_session
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement