blur 1.4 → 1.4.1

Sign up to get free protection for your applications and to get access to all the features.
data/library/blur.rb CHANGED
@@ -1,6 +1,5 @@
1
1
  # encoding: utf-8
2
2
 
3
- require 'pp'
4
3
  require 'yaml'
5
4
  require 'socket'
6
5
  require 'ostruct'
@@ -8,7 +7,7 @@ require 'ostruct'
8
7
  Dir.glob("#{File.dirname __FILE__}/blur/**/*.rb").each &method(:require)
9
8
 
10
9
  module Blur
11
- class << Version = [1,4]
10
+ class << Version = [1,4,1]
12
11
  def to_s; join '.' end
13
12
  end
14
13
 
@@ -10,6 +10,7 @@ module Blur
10
10
 
11
11
  def initialize options
12
12
  @options = options
13
+ @scripts = []
13
14
  @networks = []
14
15
  @callbacks = {}
15
16
  @connected = true
@@ -18,6 +19,7 @@ module Blur
18
19
  @networks.<< Network.new options
19
20
  end
20
21
 
22
+ load_scripts
21
23
  trap 2, &method(:quit)
22
24
  end
23
25
 
@@ -41,6 +43,23 @@ module Blur
41
43
  end
42
44
  end
43
45
 
46
+ def load_scripts
47
+ script_path = File.dirname $0
48
+
49
+ Dir.glob("#{script_path}/scripts/*.rb").each do |path|
50
+ script = Script.new path
51
+ script.client = self
52
+
53
+ @scripts << script
54
+ end
55
+ end
56
+
57
+ def unload_scripts
58
+ @scripts.each do |script|
59
+ script.unload!
60
+ end.clear
61
+ end
62
+
44
63
  def quit signal
45
64
  @networks.each do |network|
46
65
  network.transmit :QUIT, "Got SIGINT?"
@@ -64,7 +83,7 @@ module Blur
64
83
  network.transcieve
65
84
  sleep 0.05
66
85
  rescue StandardError => exception
67
- puts "#{network} threw an exception: #{exception.class.name} #{exception.message} #{exception.backtrace.to_s}"
86
+ puts "\e[1m\e[31merror:\e[39m #{exception.message}\e[0m"
68
87
 
69
88
  network.disconnect if network.connected?
70
89
  @networks.delete network
@@ -79,6 +98,14 @@ module Blur
79
98
  @callbacks[name].each do |callback|
80
99
  callback.call *args
81
100
  end if @callbacks[name]
101
+
102
+ @scripts.each do |script|
103
+ begin
104
+ script.__send__ name, *args if script.respond_to? name
105
+ rescue Exception => exception
106
+ puts "\e[1m#{File.basename script.path}:#{exception.line}: \e[31merror:\e[39m #{exception.message}\e[0m"
107
+ end
108
+ end
82
109
  end
83
110
 
84
111
  def catch name, &block
@@ -1,7 +1,11 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  class Exception
4
- def line; backtrace.first.match(/^.*?:(\d+):/)[1].to_i end
4
+ Pattern = /^.*?:(\d+):/
5
+
6
+ def line
7
+ backtrace[0].match(Pattern)[1].to_i + 1
8
+ end
5
9
  end
6
10
 
7
11
  class String
@@ -52,11 +52,13 @@ module Blur
52
52
  # Someone send a message
53
53
  def got_privmsg network, command
54
54
  return if command.sender.is_a? String # Ignore all server privmsgs
55
-
56
55
  name, message = command.params
57
56
 
58
57
  if channel = network.channel_by_name(name)
59
58
  if user = channel.user_by_nick(command.sender.nickname)
59
+ user.name = command.sender.username
60
+ user.host = command.sender.hostname
61
+
60
62
  emit :message, user, channel, message
61
63
  else
62
64
  # Odd… this shouldn't happen
@@ -3,7 +3,7 @@
3
3
  module Blur
4
4
  class Network
5
5
  class Channel
6
- attr_accessor :name, :users
6
+ attr_accessor :name, :users, :network
7
7
 
8
8
  def initialize name, network = nil, users = []
9
9
  @name = name
@@ -21,6 +21,8 @@ module Blur
21
21
  def terminate
22
22
  @socket.close
23
23
  @socket = nil
24
+ @buffer = ""
25
+ @queue.clear
24
26
  end
25
27
 
26
28
  def transmit command
@@ -9,8 +9,8 @@ module Blur
9
9
  @nick = nick.sub /^[@|~|\+|%|&]/, ''
10
10
  end
11
11
 
12
- def synchronize sender
13
- @nick, @name, @host = sender.nickname, sender.username, sender.hostname
12
+ def say message
13
+ @channel.network.say self, message
14
14
  end
15
15
 
16
16
  def inspect
@@ -0,0 +1,54 @@
1
+ # encoding: utf-8
2
+
3
+ module Blur
4
+ class Script < Module
5
+ attr_accessor :name, :author, :version, :path, :client
6
+
7
+ def evaluated?; @evaluated end
8
+
9
+ def initialize path
10
+ @path = path
11
+ @evaluated = false
12
+
13
+ if evaluate and @evaluated
14
+ cache.load if Cache.exists? @name
15
+
16
+ __send__ :loaded if respond_to? :loaded
17
+ end
18
+ end
19
+
20
+ def Script name, version = [1,0], author = nil, &block
21
+ @name = name
22
+ @author = author
23
+ @version = version
24
+
25
+ instance_eval &block
26
+
27
+ true
28
+ end
29
+
30
+ def unload!
31
+ @cache.save if @cache
32
+
33
+ __send__ :unloaded if respond_to? :unloaded
34
+ end
35
+
36
+ def cache
37
+ @cache ||= Cache.new self
38
+ end
39
+
40
+ def inspect
41
+ %{#<#{self.class.name} @name=#{@name.inspect} @version=#{@version.inspect} @author=#{@author.inspect}>}
42
+ end
43
+
44
+ private
45
+
46
+ def evaluate
47
+ if module_eval File.read(@path), File.basename(@path), 0
48
+ @evaluated = true
49
+ end
50
+ rescue Exception => exception
51
+ puts "\e[1m#{File.basename @path}:#{exception.line}: \e[31merror:\e[39m #{exception.message}\e[0m"
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,52 @@
1
+ # encoding: utf-8
2
+
3
+ module Blur
4
+ class Script < Module
5
+ class Cache
6
+
7
+ def self.path
8
+ %{#{File.dirname File.expand_path $0}/cache}
9
+ end
10
+
11
+ def self.exists? name
12
+ File.exists? "#{path}/#{name}.yml"
13
+ end
14
+
15
+ def [] key; @hash[key] ||= {} end
16
+ def []= key, value; @hash[key] = value end
17
+
18
+ def initialize script
19
+ @hash = {}
20
+ @script = script
21
+ end
22
+
23
+ def save
24
+ directory = File.dirname path
25
+
26
+ unless File.directory? directory
27
+ Dir.mkdir directory
28
+ end
29
+
30
+ File.open path, ?w do |file|
31
+ YAML.dump @hash, file
32
+ end
33
+ end
34
+
35
+ def load
36
+ if yaml = YAML.load_file(path)
37
+ @hash = yaml
38
+ end
39
+ rescue
40
+ File.unlink path
41
+ end
42
+
43
+ def to_s; @hash end
44
+
45
+ private
46
+
47
+ def path
48
+ %{#{Cache.path}/#{@script.name}.yml}
49
+ end
50
+ end
51
+ end
52
+ end
metadata CHANGED
@@ -5,7 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 1
7
7
  - 4
8
- version: "1.4"
8
+ - 1
9
+ version: 1.4.1
9
10
  platform: ruby
10
11
  authors:
11
12
  - Mikkel Kroman
@@ -13,7 +14,7 @@ autorequire:
13
14
  bindir: executables
14
15
  cert_chain: []
15
16
 
16
- date: 2011-01-17 00:00:00 +01:00
17
+ date: 2011-01-18 00:00:00 +01:00
17
18
  default_executable:
18
19
  dependencies: []
19
20
 
@@ -34,6 +35,8 @@ files:
34
35
  - library/blur/network/connection.rb
35
36
  - library/blur/network/user.rb
36
37
  - library/blur/network.rb
38
+ - library/blur/script/cache.rb
39
+ - library/blur/script.rb
37
40
  - library/blur.rb
38
41
  has_rdoc: true
39
42
  homepage: