Ruby-IRC 1.0.7 → 1.0.10

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.
data/lib/IRC.rb CHANGED
@@ -66,13 +66,17 @@ class IRC
66
66
 
67
67
  IRCConnection.handle_connection(@server, @port, @nick, @realname) do
68
68
  # Log in information moved to IRCConnection
69
- threads = []
69
+ @threads = []
70
70
  IRCConnection.main do |event|
71
- threads << Thread.new(event) {|localevent|
72
- localevent.process
73
- }
71
+ if event.kind_of?(Array)
72
+ event.each {|event|
73
+ thread_event(event)
74
+ }
75
+ else
76
+ thread_event(event)
77
+ end
74
78
  end
75
- threads.each {|thr| thr.join }
79
+ @threads.each {|thr| thr.join }
76
80
  end
77
81
  end
78
82
 
@@ -89,7 +93,17 @@ class IRC
89
93
  @channels.delete_if {|chan| chan.name == channel }
90
94
  end
91
95
  end
92
-
96
+
97
+ # kicks a user from a channel (does not check for operator privledge)
98
+ def kick(channel, user, message)
99
+ IRCConnection.send_to_server("KICK #{channel} #{user} :#{message || user || 'kicked'}")
100
+ end
101
+
102
+ # sets the topic of the given channel
103
+ def set_topic(channel, topic)
104
+ IRCConnection.send_to_server("TOPIC #{channel} :#{topic}");
105
+ end
106
+
93
107
  # Sends a private message, or channel message
94
108
  def send_message(to, message)
95
109
  IRCConnection.send_to_server("privmsg #{to} :#{message}");
@@ -140,4 +154,10 @@ class IRC
140
154
  def get_user_info(user)
141
155
  IRCConnection.send_to_server("WHO #{user}")
142
156
  end
157
+ private
158
+ def thread_event (event)
159
+ @threads << Thread.new(event) {|localevent|
160
+ localevent.process
161
+ }
162
+ end
143
163
  end
@@ -0,0 +1,153 @@
1
+
2
+ require 'socket'
3
+ require 'IRCConnection'
4
+ require 'IRCEvent'
5
+ require 'IRCChannel'
6
+ require 'IRCUser'
7
+ require 'IRCUtil'
8
+
9
+
10
+
11
+ # Class IRC is a master class that handles connection to the irc
12
+ # server and pasring of IRC events, through the IRCEvent class.
13
+ class IRC
14
+ @channels = nil
15
+ # Create a new IRC Object instance
16
+ def initialize( nick, server, port, realname='RBot')
17
+ @nick = nick
18
+ @server = server
19
+ @port = port
20
+ @realname = realname
21
+ @channels = Array.new(0)
22
+ # Some good default Event handlers. These can and will be overridden by users.
23
+ # Thses make changes on the IRCbot object. So they need to be here.
24
+
25
+ # Topic events can come on two tags, so we create on proc to handle them.
26
+
27
+ topic_proc = Proc.new { |event|
28
+ self.channels.each { |chan|
29
+ if chan == event.channel
30
+ chan.topic = event.message
31
+ end
32
+ }
33
+ }
34
+
35
+ IRCEvent.add_handler('332', topic_proc)
36
+ IRCEvent.add_handler('topic', topic_proc)
37
+
38
+
39
+ end
40
+
41
+ attr_reader :nick, :server, :port
42
+
43
+ # Join a channel, adding it to the list of joined channels
44
+ def add_channel channel
45
+ join(channel)
46
+ self
47
+ end
48
+
49
+ # Returns a list of channels joined
50
+ def channels
51
+ @channels
52
+ end
53
+
54
+ # Alias for IRC.connect
55
+ def start
56
+ self.connect
57
+ end
58
+
59
+ # Open a connection to the server using the IRC Connect
60
+ # method. Events yielded from the IRCConnection handler are
61
+ # processed and then control is returned to IRCConnection
62
+ def connect
63
+ quithandler = lambda { send_quit(); IRCConnection.quit }
64
+ trap("INT", quithandler)
65
+ trap("TERM", quithandler)
66
+
67
+ IRCConnection.handle_connection(@server, @port, @nick, @realname) do
68
+ # Log in information moved to IRCConnection
69
+ threads = []
70
+ IRCConnection.main do |event|
71
+ threads << Thread.new(event) {|localevent|
72
+ localevent.process
73
+ }
74
+ end
75
+ threads.each {|thr| thr.join }
76
+ end
77
+ end
78
+
79
+ # Joins a channel on a server.
80
+ def join(channel)
81
+ if (IRCConnection.send_to_server("JOIN #{channel}"))
82
+ @channels.push(IRCChannel.new(channel));
83
+ end
84
+ end
85
+
86
+ # Leaves a channel on a server
87
+ def part(channel)
88
+ if (IRCConnection.send_to_server("PART #{channel}"))
89
+ @channels.delete_if {|chan| chan.name == channel }
90
+ end
91
+ end
92
+
93
+ # kicks a user from a channel (does not check for operator privledge)
94
+ def kick(channel, user, message)
95
+ IRCConnection.send_to_server("KICK #{channel} #{user} :#{message || user || 'kicked'}")
96
+ end
97
+
98
+ # sets the topic of the given channel
99
+ def set_topic(channel, topic)
100
+ IRCConnection.send_to_server("TOPIC #{channel} :#{topic}");
101
+ end
102
+
103
+ # Sends a private message, or channel message
104
+ def send_message(to, message)
105
+ IRCConnection.send_to_server("privmsg #{to} :#{message}");
106
+ end
107
+
108
+ # Sends a notice
109
+ def send_notice(to, message)
110
+ IRCConnection.send_to_server("NOTICE #{to} :#{message}");
111
+ end
112
+
113
+ # performs an action
114
+ def send_action(to, action)
115
+ send_ctcp(to, 'ACTION', action);
116
+ end
117
+
118
+ # send CTCP
119
+ def send_ctcp(to, type, message)
120
+ IRCConnection.send_to_server("privmsg #{to} :\001#{type} #{message}");
121
+ end
122
+
123
+ # Quits the IRC Server
124
+ def send_quit
125
+ IRCConnection.send_to_server("QUIT : Quit ordered by user")
126
+ end
127
+
128
+ # Ops selected user.
129
+ def op(channel, user)
130
+ IRCConnection.send_to_server("MODE #{channel} +o #{user}")
131
+ end
132
+
133
+ # Changes the current nickname
134
+ def ch_nick(nick)
135
+ IRCConnection.send_to_server("NICK #{nick}")
136
+ @nick = nick
137
+ end
138
+
139
+ # Removes operator status from a user
140
+ def deop(channel, user)
141
+ IRCConnection.send_to_server("MODE #{channel} -o #{user}")
142
+ end
143
+
144
+ # Changes target users mode
145
+ def mode(channel, user, mode)
146
+ IRCConnection.send_to_server("MODE #{channel} #{mode} #{user}")
147
+ end
148
+
149
+ # Retrievs user information from the server
150
+ def get_user_info(user)
151
+ IRCConnection.send_to_server("WHO #{user}")
152
+ end
153
+ end
@@ -1,9 +1,12 @@
1
1
 
2
2
  # Handles connection to IRC Server
3
3
  class IRCConnection
4
- @@quit = 0
5
- @@readsockets = Array.new(0)
6
- @@events = Hash.new()
4
+ @@quit = 0
5
+ @@readsockets = Array.new(0)
6
+ @@output_buffer = Array.new(0)
7
+ @@events = Hash.new()
8
+ @@last_send = Time.now.to_f
9
+ @@message_delay = 0.2 # Default delay to 1 fifth of a second.
7
10
  # Creates a socket connection and then yields.
8
11
  def IRCConnection.handle_connection(server, port, nick='ChangeMe', realname='MeToo' )
9
12
  @server = server;
@@ -45,6 +48,12 @@ class IRCConnection
45
48
  @@socket.write(line + "\n")
46
49
  end
47
50
 
51
+ # Adds data an output buffer. This let's us keep a handle on how
52
+ # fast we send things. Yay.
53
+ def IRCConnection.output_push(line)
54
+ @@output_buffer.push(line)
55
+ end
56
+
48
57
  # This loop monitors all IO_Sockets IRCConnection controls
49
58
  # (including the IRC socket) and yields events to the IO_Sockets
50
59
  # event handler.
@@ -59,24 +68,38 @@ class IRCConnection
59
68
  # Makes one single loop pass, checking all sockets for data to read,
60
69
  # and yields the data to the sockets event handler.
61
70
  def IRCConnection.do_one_loop
62
- read_sockets = select(@@readsockets, nil, nil, nil);
63
- read_sockets[0].each {|sock|
64
- if sock.eof? && sock == @@socket
65
- p "Detected Socket Close"
66
- remove_IO_socket(sock)
67
- sleep 10
68
- handle_connection(@server, @port, @nick, @realname)
69
- else
70
- yield @@events[sock.to_i].call(sock)
71
+ read_sockets = select(@@readsockets, nil, nil, 0.1);
72
+ if !read_sockets.nil?
73
+ read_sockets[0].each {|sock|
74
+ if sock.eof? && sock == @@socket
75
+ p "Detected Socket Close"
76
+ remove_IO_socket(sock)
77
+ sleep 10
78
+ handle_connection(@server, @port, @nick, @realname)
79
+ else
80
+ yield @@events[sock.to_i].call(sock)
81
+ end
82
+ }
83
+ end
84
+ if @@output_buffer.length > 0
85
+ timer = Time.now.to_f
86
+ if (timer > @@last_send + @@message_delay)
87
+ message = @@output_buffer.shift();
88
+ if !message.nil?
89
+ IRCConnection.send_to_server(message);
90
+ @@last_send = timer
91
+ end
71
92
  end
72
- }
93
+ end
73
94
  end
74
95
 
75
96
  # Ends connection to the irc server
76
97
  def IRCConnection.quit
77
98
  @@quit = 1
78
99
  end
79
-
100
+ def IRCConnection.delay=(delay)
101
+ @@message_delay = delay.to_f
102
+ end
80
103
  # Retrieves user info from the server
81
104
  def IRCConnection.get_user_info(user)
82
105
  IRCConnection.send_to_server("WHOIS #{user}")
@@ -88,7 +111,7 @@ class IRCConnection
88
111
  @@events[socket.to_i] = event_generator
89
112
  end
90
113
 
91
- def IRCConnection.remove_IO_socket(socket)
114
+ def IRCConnection.remove_IO_socket(sock)
92
115
  sock.close
93
116
  @@readsockets.delete_if {|item| item == sock }
94
117
  end
@@ -0,0 +1,97 @@
1
+
2
+ # Handles connection to IRC Server
3
+ class IRCConnection
4
+ @@quit = 0
5
+ @@readsockets = Array.new(0)
6
+ @@events = Hash.new()
7
+ # Creates a socket connection and then yields.
8
+ def IRCConnection.handle_connection(server, port, nick='ChangeMe', realname='MeToo' )
9
+ @server = server;
10
+ @port = port
11
+ @nick = nick
12
+ @realname = realname
13
+ socket = create_tcp_socket(server, port)
14
+ add_IO_socket(socket) {|sock|
15
+ begin
16
+ IRCEvent.new(sock.readline.chomp)
17
+ rescue Errno::ECONNRESET
18
+ # Catches connection reset by peer, attempts to reconnect
19
+ # after sleeping for 10 second.
20
+ remove_IO_socket(sock)
21
+ sleep 10
22
+ handle_connection(@server, @port, @nick, @realname)
23
+ end
24
+ }
25
+ send_to_server "NICK #{nick}"
26
+ send_to_server "USER #{nick} 8 * :#{realname}"
27
+ if block_given?
28
+ yield
29
+ @@socket.close
30
+ end
31
+ end
32
+
33
+ def IRCConnection.create_tcp_socket(server, port)
34
+ @@socket = TCPsocket.open(server, port)
35
+ if block_given?
36
+ yield
37
+ @@socket.close
38
+ return
39
+ end
40
+ return @@socket
41
+ end
42
+
43
+ # Sends a line of text to the server
44
+ def IRCConnection.send_to_server(line)
45
+ @@socket.write(line + "\n")
46
+ end
47
+
48
+ # This loop monitors all IO_Sockets IRCConnection controls
49
+ # (including the IRC socket) and yields events to the IO_Sockets
50
+ # event handler.
51
+ def IRCConnection.main
52
+ while(@@quit == 0)
53
+ do_one_loop { |event|
54
+ yield event
55
+ }
56
+ end
57
+ end
58
+
59
+ # Makes one single loop pass, checking all sockets for data to read,
60
+ # and yields the data to the sockets event handler.
61
+ def IRCConnection.do_one_loop
62
+ read_sockets = select(@@readsockets, nil, nil, nil);
63
+ read_sockets[0].each {|sock|
64
+ if sock.eof? && sock == @@socket
65
+ p "Detected Socket Close"
66
+ remove_IO_socket(sock)
67
+ sleep 10
68
+ handle_connection(@server, @port, @nick, @realname)
69
+ else
70
+ yield @@events[sock.to_i].call(sock)
71
+ end
72
+ }
73
+ end
74
+
75
+ # Ends connection to the irc server
76
+ def IRCConnection.quit
77
+ @@quit = 1
78
+ end
79
+
80
+ # Retrieves user info from the server
81
+ def IRCConnection.get_user_info(user)
82
+ IRCConnection.send_to_server("WHOIS #{user}")
83
+ end
84
+
85
+ # Adds a new socket to the list of sockets to monitor for new data.
86
+ def IRCConnection.add_IO_socket(socket, &event_generator)
87
+ @@readsockets.push(socket)
88
+ @@events[socket.to_i] = event_generator
89
+ end
90
+
91
+ def IRCConnection.remove_IO_socket(socket)
92
+ sock.close
93
+ @@readsockets.delete_if {|item| item == sock }
94
+ end
95
+ end
96
+
97
+
@@ -40,7 +40,8 @@ class IRCEvent
40
40
  @from = @stats[0]
41
41
  @user = IRCUser.create_user(@from)
42
42
  end
43
- @hostmask = @stats[1] if %W(privmsg join).include? @event_type
43
+ # FIXME: this list would probably be more accurate to exclude commands than to include them
44
+ @hostmask = @stats[1] if %W(topic privmsg join).include? @event_type
44
45
  @channel = @stats[3] if @stats[3] && !@channel
45
46
  @target = @stats[5] if @stats[5]
46
47
  @mode = @stats[4] if @stats[4]
metadata CHANGED
@@ -1,10 +1,10 @@
1
1
  --- !ruby/object:Gem::Specification
2
- rubygems_version: 0.8.11
2
+ rubygems_version: 0.9.0
3
3
  specification_version: 1
4
4
  name: Ruby-IRC
5
5
  version: !ruby/object:Gem::Version
6
- version: 1.0.7
7
- date: 2006-12-14 00:00:00 -08:00
6
+ version: 1.0.10
7
+ date: 2007-11-07 00:00:00 -08:00
8
8
  summary: An IRC Client library
9
9
  require_paths:
10
10
  - lib
@@ -25,16 +25,19 @@ required_ruby_version: !ruby/object:Gem::Version::Requirement
25
25
  platform: ruby
26
26
  signing_key:
27
27
  cert_chain:
28
+ post_install_message:
28
29
  authors:
29
30
  - Chris Boyer
30
31
  files:
31
32
  - lib/IRCEvent.rb
32
33
  - lib/IRC.rb
34
+ - lib/IRC.rb.~1.8.~
33
35
  - lib/IRCChannel.rb
34
36
  - lib/IRCConnection.rb
35
37
  - lib/IRCUser.rb
36
38
  - lib/IRCUtil.rb
37
39
  - lib/eventmap.yml
40
+ - lib/IRCConnection.rb.~1.4.~
38
41
  - README
39
42
  test_files: []
40
43