Class: IRC
- Inherits:
-
Object
- Object
- IRC
- Defined in:
- lib/IRC.rb
Overview
Class IRC is a master class that handles connection to the irc server and pasring of IRC events, through the IRCEvent class.
Instance Attribute Summary collapse
-
#nick ⇒ Object
readonly
Returns the value of attribute nick.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
-
#server ⇒ Object
readonly
Returns the value of attribute server.
Instance Method Summary collapse
-
#add_channel(channel) ⇒ Object
Join a channel, adding it to the list of joined channels.
-
#ch_nick(nick) ⇒ Object
Changes the current nickname.
-
#channels ⇒ Object
Returns a list of channels joined.
-
#connect ⇒ Object
Open a connection to the server using the IRC Connect method.
-
#deop(channel, user) ⇒ Object
Removes operator status from a user.
-
#get_user_info(user) ⇒ Object
Retrievs user information from the server.
-
#initialize(nick, server, port, realname = 'RBot', options = {}) ⇒ IRC
constructor
Create a new IRC Object instance.
-
#join(channel) ⇒ Object
Joins a channel on a server.
-
#kick(channel, user, message) ⇒ Object
kicks a user from a channel (does not check for operator privledge).
-
#mode(channel, user, mode) ⇒ Object
Changes target users mode.
-
#op(channel, user) ⇒ Object
Ops selected user.
-
#part(channel) ⇒ Object
Leaves a channel on a server.
-
#send_action(to, action) ⇒ Object
performs an action.
-
#send_ctcp(to, type, message) ⇒ Object
send CTCP.
- #send_ctcpreply(to, type, message) ⇒ Object
-
#send_message(to, message) ⇒ Object
Sends a private message, or channel message.
-
#send_notice(to, message) ⇒ Object
Sends a notice.
-
#send_quit ⇒ Object
Quits the IRC Server.
-
#set_topic(channel, topic) ⇒ Object
sets the topic of the given channel.
-
#start ⇒ Object
Alias for IRC.connect.
Constructor Details
#initialize(nick, server, port, realname = 'RBot', options = {}) ⇒ IRC
Create a new IRC Object instance
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/IRC.rb', line 16 def initialize( nick, server, port, realname='RBot', = {}) @nick = nick @server = server @port = port @realname = realname @channels = Array.new(0) # Some good default Event handlers. These can and will be overridden by users. # Thses make changes on the IRCbot object. So they need to be here. # Topic events can come on two tags, so we create one proc to handle them. topic_proc = Proc.new { |event| self.channels.each { |chan| if chan == event.channel chan.topic = event. end } } IRCEvent.add_handler('332', topic_proc) IRCEvent.add_handler('topic', topic_proc) @@options = ; end |
Instance Attribute Details
#nick ⇒ Object (readonly)
Returns the value of attribute nick.
40 41 42 |
# File 'lib/IRC.rb', line 40 def nick @nick end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
40 41 42 |
# File 'lib/IRC.rb', line 40 def port @port end |
#server ⇒ Object (readonly)
Returns the value of attribute server.
40 41 42 |
# File 'lib/IRC.rb', line 40 def server @server end |
Instance Method Details
#add_channel(channel) ⇒ Object
Join a channel, adding it to the list of joined channels
43 44 45 46 |
# File 'lib/IRC.rb', line 43 def add_channel channel join(channel) self end |
#ch_nick(nick) ⇒ Object
Changes the current nickname
143 144 145 146 |
# File 'lib/IRC.rb', line 143 def ch_nick(nick) IRCConnection.send_to_server("NICK #{nick}") @nick = nick end |
#channels ⇒ Object
Returns a list of channels joined
49 50 51 |
# File 'lib/IRC.rb', line 49 def channels @channels end |
#connect ⇒ Object
Open a connection to the server using the IRC Connect method. Events yielded from the IRCConnection handler are processed and then control is returned to IRCConnection
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/IRC.rb', line 61 def connect quithandler = lambda { send_quit(); IRCConnection.quit } trap("INT", quithandler) trap("TERM", quithandler) IRCConnection.handle_connection(@server, @port, @nick, @realname, @@options) do # Log in information moved to IRCConnection @threads = [] IRCConnection.main do |event| if event.kind_of?(Array) event.each {|event| thread_event(event) } else thread_event(event) end # Memory leak patch thanks to Patrick Sinclair @threads.delete_if {|thr| thr.stop? } end @threads.each {|thr| thr.join } end end |
#deop(channel, user) ⇒ Object
Removes operator status from a user
149 150 151 |
# File 'lib/IRC.rb', line 149 def deop(channel, user) IRCConnection.send_to_server("MODE #{channel} -o #{user}") end |
#get_user_info(user) ⇒ Object
Retrievs user information from the server
159 160 161 |
# File 'lib/IRC.rb', line 159 def get_user_info(user) IRCConnection.send_to_server("WHO #{user}") end |
#join(channel) ⇒ Object
Joins a channel on a server.
85 86 87 88 89 |
# File 'lib/IRC.rb', line 85 def join(channel) if (IRCConnection.send_to_server("JOIN #{channel}")) @channels.push(IRCChannel.new(channel)); end end |
#kick(channel, user, message) ⇒ Object
kicks a user from a channel (does not check for operator privledge)
99 100 101 |
# File 'lib/IRC.rb', line 99 def kick(channel, user, ) IRCConnection.send_to_server("KICK #{channel} #{user} :#{ || user || 'kicked'}") end |
#mode(channel, user, mode) ⇒ Object
Changes target users mode
154 155 156 |
# File 'lib/IRC.rb', line 154 def mode(channel, user, mode) IRCConnection.send_to_server("MODE #{channel} #{mode} #{user}") end |
#op(channel, user) ⇒ Object
Ops selected user.
138 139 140 |
# File 'lib/IRC.rb', line 138 def op(channel, user) IRCConnection.send_to_server("MODE #{channel} +o #{user}") end |
#part(channel) ⇒ Object
Leaves a channel on a server
92 93 94 95 96 |
# File 'lib/IRC.rb', line 92 def part(channel) if (IRCConnection.send_to_server("PART #{channel}")) @channels.delete_if {|chan| chan.name == channel } end end |
#send_action(to, action) ⇒ Object
performs an action
119 120 121 |
# File 'lib/IRC.rb', line 119 def send_action(to, action) send_ctcp(to, 'ACTION', action); end |
#send_ctcp(to, type, message) ⇒ Object
send CTCP
124 125 126 |
# File 'lib/IRC.rb', line 124 def send_ctcp(to, type, ) IRCConnection.send_to_server("privmsg #{to} :\001#{type} #{}\001"); end |
#send_ctcpreply(to, type, message) ⇒ Object
128 129 130 |
# File 'lib/IRC.rb', line 128 def send_ctcpreply(to, type, ) send_notice(to, "\001#{type} #{}\001") end |
#send_message(to, message) ⇒ Object
Sends a private message, or channel message
109 110 111 |
# File 'lib/IRC.rb', line 109 def (to, ) IRCConnection.send_to_server("privmsg #{to} :#{}"); end |
#send_notice(to, message) ⇒ Object
Sends a notice
114 115 116 |
# File 'lib/IRC.rb', line 114 def send_notice(to, ) IRCConnection.send_to_server("NOTICE #{to} :#{}"); end |
#send_quit ⇒ Object
Quits the IRC Server
133 134 135 |
# File 'lib/IRC.rb', line 133 def send_quit IRCConnection.send_to_server("QUIT : Quit ordered by user") end |
#set_topic(channel, topic) ⇒ Object
sets the topic of the given channel
104 105 106 |
# File 'lib/IRC.rb', line 104 def set_topic(channel, topic) IRCConnection.send_to_server("TOPIC #{channel} :#{topic}"); end |
#start ⇒ Object
Alias for IRC.connect
54 55 56 |
# File 'lib/IRC.rb', line 54 def start self.connect end |