Class: IRC

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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', options = {})
  @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.message
      end
    }
  }
  
  IRCEvent.add_handler('332', topic_proc)
  IRCEvent.add_handler('topic', topic_proc)
  @@options = options;

end

Instance Attribute Details

#nickObject (readonly)

Returns the value of attribute nick.



40
41
42
# File 'lib/IRC.rb', line 40

def nick
  @nick
end

#portObject (readonly)

Returns the value of attribute port.



40
41
42
# File 'lib/IRC.rb', line 40

def port
  @port
end

#serverObject (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

#channelsObject

Returns a list of channels joined



49
50
51
# File 'lib/IRC.rb', line 49

def channels
  @channels
end

#connectObject

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 (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, message)
  IRCConnection.send_to_server("KICK #{channel} #{user} :#{message || 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, message)
  IRCConnection.send_to_server("privmsg #{to} :\001#{type} #{message}\001");
end

#send_ctcpreply(to, type, message) ⇒ Object



128
129
130
# File 'lib/IRC.rb', line 128

def send_ctcpreply(to, type, message)
  send_notice(to, "\001#{type} #{message}\001")
end

#send_message(to, message) ⇒ Object

Sends a private message, or channel message



109
110
111
# File 'lib/IRC.rb', line 109

def send_message(to, message)
  IRCConnection.send_to_server("privmsg #{to} :#{message}");
end

#send_notice(to, message) ⇒ Object

Sends a notice



114
115
116
# File 'lib/IRC.rb', line 114

def send_notice(to, message)
  IRCConnection.send_to_server("NOTICE #{to} :#{message}");
end

#send_quitObject

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

#startObject

Alias for IRC.connect



54
55
56
# File 'lib/IRC.rb', line 54

def start
  self.connect
end