Pulse 1.2.8 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -9,28 +9,25 @@ module Pulse
9
9
  attr_accessor :scripts
10
10
 
11
11
  def initialize options
12
- @scripts = []
13
- @channels = {}
14
- @settings = Settings.new options
15
- @callbacks = {}
16
- @connection = Connection.new self, @settings
17
- @conversations = {}
12
+ @conversations, @callbacks, @channels, @scripts = {}, {}, {}, []
18
13
 
19
- load_scripts
14
+ @settings = Settings.new options
15
+ @connection = Connection.new self, @settings
20
16
 
21
- trap 2 do
22
- unload_scripts
23
- transmit :QUIT, "I was interrupted"
24
- end
17
+ load_scripts
18
+ trap 2, &method(:quit)
25
19
  end
26
20
 
27
21
  def connect
28
- @connection.establish
22
+ if not @connection.established?
23
+ @connection.establish
24
+ else
25
+ raise ConnectionError, 'Connection has already been established'
26
+ end
29
27
  end
30
28
 
31
29
  def got_command command
32
30
  name = :"got_#{command.name.downcase}"
33
- puts "<< #{command}"
34
31
 
35
32
  if respond_to? name
36
33
  __send__ name, command
@@ -38,18 +35,11 @@ module Pulse
38
35
  end
39
36
 
40
37
  def connection_established connection
41
- puts "Connection has been established."
42
-
43
38
  transmit :NICK, @settings.nickname
44
39
  transmit :USER, @settings.username, ?*, ?*, @settings.realname
45
40
  end
46
41
 
47
42
  def connection_terminated connection
48
- puts "The connection was terminated."
49
- end
50
-
51
- def say recipient, line
52
- transmit :PRIVMSG, recipient.to_s, line
53
43
  end
54
44
 
55
45
  def load_scripts
@@ -64,25 +54,30 @@ module Pulse
64
54
  end.clear
65
55
  end
66
56
 
67
- def each_user name
57
+ def instances_of name
68
58
  @channels.values.select { |c| c.user? name }.each do |channel|
69
59
  yield channel.user name
70
60
  end
71
61
  end
72
62
 
73
- def send_file path, recipient
74
- Thread.new path, recipient do
75
- DCC.new(path).listen do |addr|
76
- # FIXME: find the right local ip address and convert it to an integer
77
- transmit :PRIVMSG, recipient, "\x01DCC SEND #{File.basename path} 1489377357 #{addr[1]} #{File.size path}\x01"
78
- end
79
- end
80
- end
81
-
82
63
  def transmit name, *args
83
64
  @connection.transmit name, *args
84
65
  end
85
66
 
67
+ def say recipient, line
68
+ transmit :PRIVMSG, recipient.to_s, line
69
+ end
70
+
71
+ def join channel
72
+ transmit :JOIN, channel.to_s
73
+ end
74
+
75
+ def quit signal
76
+ unload_scripts
77
+
78
+ transmit :QUIT, "Received kill signal (#{signal})"
79
+ end
80
+
86
81
  private
87
82
 
88
83
  def emit name, *args
@@ -90,12 +85,9 @@ module Pulse
90
85
  callback.call *args
91
86
  end if @callbacks[name]
92
87
 
93
-
94
88
  @scripts.each do |script|
95
89
  begin
96
- if script.respond_to? name
97
- script.__send__ name, *args
98
- end
90
+ script.__send__ name, *args if script.respond_to? name
99
91
  rescue => exception
100
92
  puts "Script error: #{exception.message} on line #{exception.line + 1} in #{script.path}"
101
93
  end
@@ -23,12 +23,22 @@ module Pulse
23
23
  @delegate.connection_terminated self
24
24
  end
25
25
 
26
- def close
27
- @socket.close
26
+ def transmit name, *arguments
27
+ if established?
28
+ Command.new(name, arguments).tap { |this| @queue << this }
29
+ else
30
+ raise ConnectionError, 'Connection has not been established'
31
+ end
28
32
  end
29
33
 
30
- def transmit name, *arguments
31
- Command.new(name, arguments).tap { |this| @queue << this }
34
+ def established?; not @socket.nil? and not @socket.closed? end
35
+
36
+ def close
37
+ if established?
38
+ @socket.close
39
+ else
40
+ raise ConnectionError, 'Connection is already closed'
41
+ end
32
42
  end
33
43
  end
34
44
  end
@@ -20,7 +20,6 @@ module Pulse
20
20
  else
21
21
  @channels[command[2]] ||= Channel.new(command[2], self, users)
22
22
  end
23
- p users, command[3]
24
23
  end
25
24
 
26
25
  # The IRCd is checking whether or not we're still alive
@@ -56,11 +55,7 @@ module Pulse
56
55
  (@conversations[command.sender.nickname] ||= Conversation.new user, self).tap do |conversation|
57
56
  user.channel = conversation
58
57
 
59
- if message =~ /^\x01DCC (.*?)\x01$/
60
- emit :dcc, user, conversation, $1.split
61
- else
62
- emit :conversation, user, conversation, message
63
- end
58
+ emit :conversation, user, conversation, message
64
59
  end
65
60
  end
66
61
  end
@@ -73,7 +68,7 @@ module Pulse
73
68
  user.channel = channel
74
69
  user.synchronize command.sender
75
70
  channel.users << user
76
- emit :user_joined, user, channel
71
+ emit :user_entered, user, channel
77
72
  end
78
73
  end
79
74
 
@@ -2,7 +2,6 @@
2
2
 
3
3
  module Pulse
4
4
  class Queue
5
-
6
5
  SleepDuration = 2
7
6
  Timelapse = 4
8
7
  Amount = 3
@@ -29,8 +28,6 @@ module Pulse
29
28
  end
30
29
 
31
30
  @socket.write "#{command}\n"
32
-
33
- puts ">> #{command}"
34
31
  else
35
32
  Thread.stop
36
33
  end
@@ -15,16 +15,13 @@ module Pulse
15
15
  YAML.dump @containers, file
16
16
  end
17
17
 
18
- puts "Dumping cache for script #{@script.inspect} to #{path} …"
18
+ puts "Dumping cache for script #{@script.name} to #{path} …"
19
19
  end
20
20
 
21
21
  def load
22
- if yaml = YAML.load_file(path)
23
- @containers = yaml
24
-
25
- puts "Imported cache from #{path} …"
22
+ if data = YAML.load_file(path)
23
+ puts "Imported cache from #{path} …" if @containers = data
26
24
  end
27
-
28
25
  rescue
29
26
  puts "The cache is corrupted. Removing."
30
27
  File.unlink path
@@ -2,29 +2,31 @@
2
2
 
3
3
  module Pulse
4
4
  class Script < Module
5
-
6
5
  attr_accessor :name, :author, :version, :path
7
6
 
8
7
  def initialize path, client
9
- @path, @client = path, client
10
- @loaded = false
8
+ @path, @client, @loaded = path, client, false
11
9
 
12
- evaluate_script
13
10
  cache.load if has_cache?
14
- __send__ :loaded if respond_to? :loaded
11
+ emit :loaded if evaluate_script
15
12
  end
16
13
 
17
- def Script name, version = [1, 0, 0], author = nil, &block
14
+ def Script name, version = [1,0,0], author = nil, &block
18
15
  @name, @version, @author = name, version, author
16
+
19
17
  instance_eval &block
20
18
  end
21
19
 
20
+ def cache_for name; script = @client.scripts.find { |script| script.name == name } ? script.cache : nil end
21
+
22
+ def emit method, *args; __send__ method, *args if respond_to? method end
23
+ def script name; @client.scripts.find { |script| script.name == name } end
24
+ def cache; @cache ||= Cache.new self end
25
+
26
+ def has_cache?; File.exists? "#{File.expand_path File.dirname $0}/cache/#@name.yml" end
22
27
  def loaded?; @loaded == true end
23
28
 
24
- def unload!
25
- cache.save if @cache
26
- __send__ :unloaded if respond_to? :unloaded
27
- end
29
+ def unload!; cache.save if @cache; emit :unloaded end
28
30
 
29
31
  def to_yaml opts = {}; @name.to_yaml opts end
30
32
 
@@ -32,19 +34,6 @@ module Pulse
32
34
  %{#<#{self.class.name} @name=#{@name.inspect} @version=#{@version.inspect} @author=#{@author.inspect}}
33
35
  end
34
36
 
35
- def cache_for name
36
- script = @client.scripts.find { |script| script.name == name }
37
- script ? script.cache : nil
38
- end
39
-
40
- def script name; @client.scripts.find { |script| script.name == name } end
41
-
42
- def has_cache?
43
- File.exists? "#{File.expand_path File.dirname $0}/cache/#@name.yml"
44
- end
45
-
46
- def cache; @cache ||= Cache.new self end
47
-
48
37
  private
49
38
 
50
39
  def evaluate_script
@@ -52,6 +41,5 @@ module Pulse
52
41
  rescue
53
42
  puts "Parse error: #{$!.message}"
54
43
  end
55
-
56
44
  end
57
45
  end
data/library/pulse.rb CHANGED
@@ -2,33 +2,21 @@
2
2
 
3
3
  require 'yaml'
4
4
  require 'socket'
5
- require 'pulse/dcc'
6
- require 'pulse/user'
7
- require 'pulse/queue'
8
- require 'pulse/client'
9
- require 'pulse/script'
10
- require 'pulse/channel'
11
- require 'pulse/command'
12
- require 'pulse/settings'
13
- require 'pulse/connection'
14
- require 'pulse/enhancements'
15
- require 'pulse/conversation'
16
- require 'pulse/script/cache'
17
5
 
18
6
  Thread.abort_on_exception = true
19
7
 
8
+ Dir.glob("#{File.dirname __FILE__}/pulse/**/*.rb").each &method(:require)
9
+
20
10
  module Pulse
21
- class << Version = [1,2]
11
+ class ConnectionError < StandardError; end
12
+
13
+ class << Version = [1,3]
22
14
  def to_s; join ?. end
23
15
  end
24
16
 
25
- class << self
26
- def connect options, &block
27
- puts "=> Pulse #{Version}"
28
-
29
- Client.new(options).tap do |client|
30
- client.instance_eval &block
31
- end.connect
32
- end
17
+ def self.connect options, &block
18
+ Client.new(options).tap do |client|
19
+ client.instance_eval &block
20
+ end.connect
33
21
  end
34
22
  end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 1
7
- - 2
8
- - 8
9
- version: 1.2.8
7
+ - 3
8
+ - 0
9
+ version: 1.3.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Mikkel Kroman
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: executables
15
15
  cert_chain: []
16
16
 
17
- date: 2010-12-11 00:00:00 +01:00
17
+ date: 2010-12-13 00:00:00 +01:00
18
18
  default_executable:
19
19
  dependencies: []
20
20
 
@@ -38,7 +38,6 @@ files:
38
38
  - library/pulse/user.rb
39
39
  - library/pulse/conversation.rb
40
40
  - library/pulse/channel.rb
41
- - library/pulse/dcc.rb
42
41
  - library/pulse/enhancements.rb
43
42
  - library/pulse.rb
44
43
  has_rdoc: true
data/library/pulse/dcc.rb DELETED
@@ -1,38 +0,0 @@
1
- # encoding: utf-8
2
-
3
- require 'timeout'
4
-
5
- module Pulse
6
- class DCC
7
- Duration = 150
8
-
9
- def initialize path, position = 0
10
- @path, @position = path, 0
11
- end
12
-
13
- def listen
14
- client = nil
15
-
16
- TCPServer.open '0.0.0.0', 0 do |server|
17
- yield server.addr if block_given?
18
-
19
- begin
20
- timeout(Duration) { client = server.accept }
21
- rescue Timeout::Error
22
- server.close
23
- end
24
-
25
- each_chunk { |chunk| client.write chunk } if client
26
- client.close if client
27
- end
28
- end
29
-
30
- def each_chunk
31
- File.open @path, ?r do |file|
32
- file.readpartial @position if @position > 0
33
- yield file.readpartial 1024 until file.eof?
34
- end
35
- end
36
-
37
- end
38
- end