ircguerilla-irc 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (69) hide show
  1. data/lib/irc/client/client_error.rb +35 -0
  2. data/lib/irc/client/command_handler.rb +113 -0
  3. data/lib/irc/client/connected_state.rb +95 -0
  4. data/lib/irc/client/connection.rb +119 -0
  5. data/lib/irc/client/connection_listener.rb +97 -0
  6. data/lib/irc/client/connection_state.rb +88 -0
  7. data/lib/irc/client/context.rb +250 -0
  8. data/lib/irc/client/disconnected_state.rb +132 -0
  9. data/lib/irc/client/message_handler.rb +87 -0
  10. data/lib/irc/client/registered_state.rb +90 -0
  11. data/lib/irc/client/unregistered_state.rb +113 -0
  12. data/lib/irc/commands/command.rb +57 -0
  13. data/lib/irc/commands/invalid_command.rb +35 -0
  14. data/lib/irc/commands/join_command.rb +122 -0
  15. data/lib/irc/commands/nick_command.rb +86 -0
  16. data/lib/irc/commands/part_command.rb +84 -0
  17. data/lib/irc/commands/password_command.rb +72 -0
  18. data/lib/irc/commands/ping_command.rb +79 -0
  19. data/lib/irc/commands/pong_command.rb +68 -0
  20. data/lib/irc/commands/quit_command.rb +77 -0
  21. data/lib/irc/commands/user_command.rb +105 -0
  22. data/lib/irc/messages/codes.rb +186 -0
  23. data/lib/irc/messages/error_join_message.rb +72 -0
  24. data/lib/irc/messages/error_message.rb +101 -0
  25. data/lib/irc/messages/factory.rb +122 -0
  26. data/lib/irc/messages/invalid_message.rb +35 -0
  27. data/lib/irc/messages/join_message.rb +130 -0
  28. data/lib/irc/messages/message.rb +110 -0
  29. data/lib/irc/messages/nick_message.rb +93 -0
  30. data/lib/irc/messages/notice_message.rb +87 -0
  31. data/lib/irc/messages/part_message.rb +95 -0
  32. data/lib/irc/messages/ping_message.rb +101 -0
  33. data/lib/irc/messages/pong_message.rb +89 -0
  34. data/lib/irc/messages/private_message.rb +121 -0
  35. data/lib/irc/models/bot.rb +159 -0
  36. data/lib/irc/models/channel.rb +158 -0
  37. data/lib/irc/models/network.rb +252 -0
  38. data/lib/irc/models/packet.rb +84 -0
  39. data/lib/irc/models/server.rb +116 -0
  40. data/lib/irc/models/user.rb +112 -0
  41. data/test/functional/irc/client/connection_test.rb +118 -0
  42. data/test/functional/irc/client/context_test.rb +126 -0
  43. data/test/test_helper.rb +32 -0
  44. data/test/unit/irc/client/command_handler_test.rb +137 -0
  45. data/test/unit/irc/commands/join_command_test.rb +72 -0
  46. data/test/unit/irc/commands/nick_command_test.rb +35 -0
  47. data/test/unit/irc/commands/part_command_test.rb +52 -0
  48. data/test/unit/irc/commands/password_command_test.rb +35 -0
  49. data/test/unit/irc/commands/ping_command_test.rb +36 -0
  50. data/test/unit/irc/commands/pong_command_test.rb +36 -0
  51. data/test/unit/irc/commands/quit_command_test.rb +35 -0
  52. data/test/unit/irc/commands/user_command_test.rb +35 -0
  53. data/test/unit/irc/messages/error_join_message_test.rb +45 -0
  54. data/test/unit/irc/messages/factory_test.rb +62 -0
  55. data/test/unit/irc/messages/join_message_test.rb +46 -0
  56. data/test/unit/irc/messages/message_test.rb +56 -0
  57. data/test/unit/irc/messages/nick_message_test.rb +48 -0
  58. data/test/unit/irc/messages/notice_message_test.rb +43 -0
  59. data/test/unit/irc/messages/part_message_test.rb +47 -0
  60. data/test/unit/irc/messages/ping_message_test.rb +57 -0
  61. data/test/unit/irc/messages/pong_message_test.rb +57 -0
  62. data/test/unit/irc/messages/private_message_test.rb +42 -0
  63. data/test/unit/irc/models/bot_test.rb +85 -0
  64. data/test/unit/irc/models/channel_test.rb +90 -0
  65. data/test/unit/irc/models/network_test.rb +210 -0
  66. data/test/unit/irc/models/packet_test.rb +38 -0
  67. data/test/unit/irc/models/server_test.rb +54 -0
  68. data/test/unit/irc/models/user_test.rb +67 -0
  69. metadata +111 -0
@@ -0,0 +1,35 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # Copyright (c) 2006 Roman Scherer | IRC Guerilla | Rapid Packet Movement
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining
6
+ # a copy of this software and associated documentation files (the
7
+ # "Software"), to deal in the Software without restriction, including
8
+ # without limitation the rights to use, copy, modify, merge, publish,
9
+ # distribute, sublicense, and/or sell copies of the Software, and to
10
+ # permit persons to whom the Software is furnished to do so, subject to
11
+ # the following conditions:
12
+ #
13
+ # The above copyright notice and this permission notice shall be
14
+ # included in all copies or substantial portions of the Software.
15
+ #
16
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
+ #
24
+ # $Id: client_error.rb 85 2006-08-13 11:42:07Z roman $
25
+ #
26
+ module IRC
27
+
28
+ module Client
29
+
30
+ class ClientError < Exception
31
+ end
32
+
33
+ end
34
+
35
+ end
@@ -0,0 +1,113 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # Copyright (c) 2006 Roman Scherer | IRC Guerilla | Rapid Packet Movement
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining
6
+ # a copy of this software and associated documentation files (the
7
+ # "Software"), to deal in the Software without restriction, including
8
+ # without limitation the rights to use, copy, modify, merge, publish,
9
+ # distribute, sublicense, and/or sell copies of the Software, and to
10
+ # permit persons to whom the Software is furnished to do so, subject to
11
+ # the following conditions:
12
+ #
13
+ # The above copyright notice and this permission notice shall be
14
+ # included in all copies or substantial portions of the Software.
15
+ #
16
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
+ #
24
+ # $Id: command_handler.rb 85 2006-08-13 11:42:07Z roman $
25
+ #
26
+ require 'irc/commands/command'
27
+ require 'log4r'
28
+
29
+ module IRC
30
+
31
+ module Client
32
+
33
+ class CommandHandlerError < Exception
34
+ end
35
+
36
+ class CommandHandler
37
+
38
+ DELAY_IN_MS = 1000 / 1000
39
+
40
+ # The delay in milliseconds between two commands that are sent to the server.
41
+ attr_accessor :delay
42
+
43
+ def initialize(context)
44
+ @log = Log4r::Logger.new('IRC::Client::CommandHandler')
45
+ @context = context
46
+ @queue = Queue.new
47
+ @delay = DELAY_IN_MS
48
+ end
49
+
50
+ # Sends the command through a queue to the server. Sending commands
51
+ # through a queue prevents from flooding the IRC server.
52
+ def <<(command)
53
+ send_command_via_queue(command)
54
+ end
55
+
56
+ # Sends the command directly to the server bypassing the queue. Sending commands too fast
57
+ # to the server can result in a disconnect from the server.
58
+ def send_command(command)
59
+ @context.socket << "#{command.to_s}\r\n"
60
+ @log.debug("[#{@context.network.to_s.upcase}] >>> #{command.to_s}")
61
+ end
62
+
63
+ # Sends the command through a queue to the server. Sending commands through a queue prevents
64
+ # from flooding the IRC server.
65
+ def send_command_via_queue(command)
66
+ @queue << command
67
+ end
68
+
69
+ def start
70
+
71
+ # Don't start the command handler if already started.
72
+ raise CommandHandlerError.new("Can't start command handler. Already started.") if @thread != nil
73
+
74
+ # Start the command handler thread.
75
+ @thread = Thread.new do
76
+
77
+ loop do
78
+
79
+ # Get the next command from the command queue.
80
+ command = @queue.pop
81
+
82
+ # Send the command to the server.
83
+ send_command(command)
84
+
85
+ # Wait some milliseconds to prevent server flodding.
86
+ sleep(delay)
87
+
88
+ end
89
+
90
+ end
91
+
92
+ @log.debug("[#{@context.network.to_s.upcase}] Command handler successfully started.")
93
+
94
+ end
95
+
96
+ def stop
97
+
98
+ # Don't stop the command handler if already stopped.
99
+ raise CommandHandlerError.new("Can't stop command handler. Already stopped.") if @thread == nil
100
+
101
+ # Kill the thread & clear the command queue.
102
+ @thread.kill
103
+ @queue.clear
104
+
105
+ @log.debug("[#{@context.network.to_s.upcase}] Command handler successfully stopped.")
106
+
107
+ end
108
+
109
+ end
110
+
111
+ end
112
+
113
+ end
@@ -0,0 +1,95 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # Copyright (c) 2006 Roman Scherer | IRC Guerilla | Rapid Packet Movement
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining
6
+ # a copy of this software and associated documentation files (the
7
+ # "Software"), to deal in the Software without restriction, including
8
+ # without limitation the rights to use, copy, modify, merge, publish,
9
+ # distribute, sublicense, and/or sell copies of the Software, and to
10
+ # permit persons to whom the Software is furnished to do so, subject to
11
+ # the following conditions:
12
+ #
13
+ # The above copyright notice and this permission notice shall be
14
+ # included in all copies or substantial portions of the Software.
15
+ #
16
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
+ #
24
+ # $Id: connected_state.rb 88 2006-08-13 12:42:58Z roman $
25
+ #
26
+ require 'irc/client/client_error'
27
+ require 'irc/client/connection_state'
28
+ require 'irc/client/disconnected_state'
29
+ require 'irc/commands/pong_command'
30
+ require 'irc/commands/quit_command'
31
+ require 'log4r'
32
+
33
+ module IRC
34
+
35
+ module Client
36
+
37
+ class ConnectedState < ConnectionState
38
+
39
+ def initialize
40
+ @log = Log4r::Logger.new('IRC::Client::ConnectedState')
41
+ end
42
+
43
+ # Connects to the server.
44
+ def connect(context, server)
45
+ raise ClientError.new("Can't connect to server. Already connected to a server.")
46
+ end
47
+
48
+ # Disconnects from the currently connected server.
49
+ def disconnect(context, message = nil)
50
+
51
+ @log.debug("#{network_name(context)} Disconnecting from server #{context.server.hostname}.")
52
+
53
+ # Send the quit command. If no quit message is given, the nick name will be
54
+ # send as the default quit message.
55
+ send_command(context, IRC::Commands::QuitCommand.new(message || context.nick))
56
+
57
+ # Stop the command & message handlers.
58
+ context.stop_command_handler
59
+ context.stop_message_handler
60
+
61
+ # Set network & server to nil.
62
+ context.network = context.server = nil
63
+
64
+ # Close socket and set socket to nil.
65
+ context.socket.close if context.socket != nil
66
+ context.socket = nil
67
+
68
+ # Change to the disconnected state.
69
+ change_state(context, DisconnectedState.instance)
70
+
71
+ end
72
+
73
+ # Sends the command to the server bypassing the command queue.
74
+ def send_command(context, command)
75
+ context.command_handler.send_command(command)
76
+ end
77
+
78
+ # Sends the command to the server using the command queue.
79
+ def send_command_via_queue(context, command)
80
+ context.command_handler.send_command_via_queue(command)
81
+ end
82
+
83
+ # ConnectionListener
84
+
85
+ # This method gets called when disconnected from a server.
86
+ def on_disconnect(connection, server, reason = nil)
87
+ log.debug("#{network_name(connection)} Successfully disconnected from #{server}. #{reason != nil ? reason + '.' : ''}")
88
+ end
89
+
90
+
91
+ end
92
+
93
+ end
94
+
95
+ end
@@ -0,0 +1,119 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # Copyright (c) 2006 Roman Scherer | IRC Guerilla | Rapid Packet Movement
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining
6
+ # a copy of this software and associated documentation files (the
7
+ # "Software"), to deal in the Software without restriction, including
8
+ # without limitation the rights to use, copy, modify, merge, publish,
9
+ # distribute, sublicense, and/or sell copies of the Software, and to
10
+ # permit persons to whom the Software is furnished to do so, subject to
11
+ # the following conditions:
12
+ #
13
+ # The above copyright notice and this permission notice shall be
14
+ # included in all copies or substantial portions of the Software.
15
+ #
16
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
+ #
24
+ # $Id: connection.rb 85 2006-08-13 11:42:07Z roman $
25
+ #
26
+
27
+ require 'irc/client/context'
28
+
29
+ module IRC
30
+
31
+ module Client
32
+
33
+ class Connection
34
+
35
+ MAX_CONNECTION_TIMEOUT_IN_SEC = 5
36
+
37
+ def initialize(nick = nil, login = nil, realname = nil)
38
+ @context = IRC::Client::Connection::Context.new(nick, login, realname)
39
+ end
40
+
41
+ # Adds a new connection listener. The connection listener gets notified when a
42
+ # message was received from the IRC server.
43
+ def add_connection_listener(connection_listener)
44
+ @context.add_connection_listener(connection_listener)
45
+ end
46
+
47
+ # Sends the command to the server using the command queue.
48
+ def <<(command)
49
+ @context<<(command)
50
+ end
51
+
52
+ # Connects to the server.
53
+ def connect(server)
54
+ @network = server.network
55
+ state.connect(self, server)
56
+ end
57
+
58
+ # Returns true if a connection to an IRC server has been established.
59
+ def connected?
60
+ @context.connected?
61
+ end
62
+
63
+ # Disconnects from the currently connected server.
64
+ def disconnect(message = nil)
65
+ @context.disconnect(message)
66
+ end
67
+
68
+ # Joins the given channels.
69
+ def join(channels)
70
+ @context.join(channels)
71
+ end
72
+
73
+ # Returns true, if the given channel is currently joined.
74
+ def joined?(channel)
75
+ @context.joined?(channel)
76
+ end
77
+
78
+ # Returns an Array of all currently joined channels.
79
+ def joined_channels
80
+ @context.joined_channels
81
+ end
82
+
83
+ # The network to which the context is connected, or nil if no connection has been established.
84
+ def network
85
+ @context.network
86
+ end
87
+
88
+ # Returns true if a connection to an IRC server has been established and the connection
89
+ # has been successfully registered.
90
+ def registered?
91
+ @context.registered?
92
+ end
93
+
94
+ # Leaves the given channels.
95
+ def part(channels)
96
+ @context.part(channels)
97
+ end
98
+
99
+ # Removes a previously added connection listener. The connection listener will not
100
+ # get notified any longer when a message was received from the IRC server.
101
+ def remove_connection_listener(connection_listener)
102
+ @context.remove_connection_listener(connection_listener)
103
+ end
104
+
105
+ # Sends the command to the server bypassing the command queue.
106
+ def send_command(command)
107
+ @context.send_command(command)
108
+ end
109
+
110
+ # Sends the command to the server using the command queue.
111
+ def send_command_via_queue(command)
112
+ @context.send_command_via_queue(command)
113
+ end
114
+
115
+ end
116
+
117
+ end
118
+
119
+ end
@@ -0,0 +1,97 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # Copyright (c) 2006 Roman Scherer | IRC Guerilla | Rapid Packet Movement
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining
6
+ # a copy of this software and associated documentation files (the
7
+ # "Software"), to deal in the Software without restriction, including
8
+ # without limitation the rights to use, copy, modify, merge, publish,
9
+ # distribute, sublicense, and/or sell copies of the Software, and to
10
+ # permit persons to whom the Software is furnished to do so, subject to
11
+ # the following conditions:
12
+ #
13
+ # The above copyright notice and this permission notice shall be
14
+ # included in all copies or substantial portions of the Software.
15
+ #
16
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
+ #
24
+ # $Id: connection_listener.rb 90 2006-08-13 14:45:04Z roman $
25
+ #
26
+ require 'irc/commands/pong_command'
27
+ require 'irc/models/server'
28
+ require 'log4r'
29
+
30
+ module IRC
31
+
32
+ module Client
33
+
34
+ module ConnectionListener
35
+
36
+ # This method gets called when successfully connected to an IRC server.
37
+ def on_connect(connection, server)
38
+ end
39
+
40
+ # This method gets called when disconnected from a server.
41
+ def on_disconnect(connection, server, reason = nil)
42
+ end
43
+
44
+ # This method gets called when a private message was received from a channel or an user.
45
+ def on_private_message(connection, target, message)
46
+ end
47
+
48
+ # This method gets called when a user sends a public message
49
+ # to a channel. If the message was a private message the channel is nil.
50
+ def on_need_more_parameters(connection, nick)
51
+ end
52
+
53
+ # This method gets called when a notice message was received.
54
+ def on_notice(connection, user, message)
55
+ end
56
+
57
+ # This method gets called when a user (possibly us) joins a channel.
58
+ def on_join(connection, channel, user)
59
+ end
60
+
61
+ # This message gets called when a channel join attempt failed.
62
+ def on_join_failure(connection, channel, code, reason)
63
+ end
64
+
65
+ # This method gets called when a user gets kicked from a channel.
66
+ def on_kick(connection, channel, kick_user, kicked_user, reason)
67
+ end
68
+
69
+ # This method gets called when a user (possibly us) leaves a channel.
70
+ def on_part(connection, channel, user)
71
+ end
72
+
73
+ # This method gets called when a channel part attempt failed.
74
+ def on_part_failure(connection, channel)
75
+ end
76
+
77
+ # This method gets called when a ping message was received from the server.
78
+ def on_ping(connection, servers)
79
+ end
80
+
81
+ # This method gets called when a pong message was received from the server.
82
+ def on_pong(connection, daemons)
83
+ end
84
+
85
+ # This method gets called when an error happend during the connection registration.
86
+ def on_registration_failure(connection, message)
87
+ end
88
+
89
+ # This method gets called when a message from the server is received.
90
+ def on_server_response(connection, message)
91
+ end
92
+
93
+ end
94
+
95
+ end
96
+
97
+ end