messagefactory 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,3 @@
1
+ == messagefactory
2
+
3
+ Put appropriate LICENSE for your project here.
data/README.rdoc ADDED
@@ -0,0 +1,53 @@
1
+ == MessageFactory
2
+
3
+ MessageFactory is a simple little library for parsing messages from an IRC
4
+ server and generating easy to use objects from them. This library is intended
5
+ to be used within an IRC type application, and does nothing of value by itself.
6
+
7
+ === install (easy):
8
+
9
+ gem install messagefactory
10
+
11
+ === install (less easy):
12
+
13
+ git clone http://github.com/spox/messagefactory.git
14
+ cd messagefactory
15
+ gem build *.gemspec
16
+ gem install ./
17
+
18
+ === install (less easy that's a little easier)
19
+
20
+ {rip}[http://hellorip.com/about.html] makes it easy to install directly from a github repository.
21
+
22
+ === Testing
23
+
24
+ MessageFactory is currently tested on:
25
+
26
+ * Ruby 1.8.6-p383
27
+ * Ruby 1.8.7-p248
28
+ * Ruby 1.9.1-p376
29
+ * JRuby 1.4.0
30
+
31
+ == Example
32
+
33
+ === Code:
34
+
35
+ require 'messagefactory'
36
+
37
+ factory = MessageFactory::Factory.new
38
+ message = factory.process(':mod_spox!~mod_spox@host JOIN :#m')
39
+ p message
40
+
41
+ === Result:
42
+
43
+ #<OpenStruct direction=:incoming, received=Fri Feb 05 19:58:42 UTC 2010, raw=":mod_spox!~mod_spox@host JOIN :#m", type=:join, source="mod_spox!~mod_spox@host", channel="#m">
44
+
45
+ == Last remarks
46
+
47
+ If you find any bugs, please report them through {github}[http://github.com/spox/messagefactory/issues].
48
+ If you are in need of any help, you can generally find me on DALnet and Freenode.
49
+
50
+ == License
51
+
52
+ MessageFactory is licensed under the LGPLv3
53
+ Copyright (c) 2009 spox <spox@modspox.com>
data/Rakefile ADDED
@@ -0,0 +1,51 @@
1
+ #
2
+ # To change this template, choose Tools | Templates
3
+ # and open the template in the editor.
4
+
5
+
6
+ require 'rubygems'
7
+ require 'rake'
8
+ require 'rake/clean'
9
+ require 'rake/gempackagetask'
10
+ require 'rake/rdoctask'
11
+ require 'rake/testtask'
12
+ require 'spec/rake/spectask'
13
+
14
+ spec = Gem::Specification.new do |s|
15
+ s.name = 'messagefactory'
16
+ s.version = '0.0.1'
17
+ s.has_rdoc = true
18
+ s.extra_rdoc_files = ['README.rdoc', 'LICENSE']
19
+ s.summary = 'Your summary here'
20
+ s.description = s.summary
21
+ s.author = ''
22
+ s.email = ''
23
+ # s.executables = ['your_executable_here']
24
+ s.files = %w(LICENSE README.rdoc Rakefile) + Dir.glob("{bin,lib,spec}/**/*")
25
+ s.require_path = "lib"
26
+ s.bindir = "bin"
27
+ end
28
+
29
+ Rake::GemPackageTask.new(spec) do |p|
30
+ p.gem_spec = spec
31
+ p.need_tar = true
32
+ p.need_zip = true
33
+ end
34
+
35
+ Rake::RDocTask.new do |rdoc|
36
+ files =['README.rdoc', 'LICENSE', 'lib/**/*.rb']
37
+ rdoc.rdoc_files.add(files)
38
+ rdoc.main = "README.rdoc" # page to start on
39
+ rdoc.title = "messagefactory Docs"
40
+ rdoc.rdoc_dir = 'doc/rdoc' # rdoc output folder
41
+ rdoc.options << '--line-numbers'
42
+ end
43
+
44
+ Rake::TestTask.new do |t|
45
+ t.test_files = FileList['test/**/*.rb']
46
+ end
47
+
48
+ Spec::Rake::SpecTask.new do |t|
49
+ t.spec_files = FileList['spec/**/*.rb']
50
+ t.libs << Dir["lib"]
51
+ end
data/lib/Factory.rb ADDED
@@ -0,0 +1,41 @@
1
+ module MessageFactory
2
+ class Factory
3
+ # Create a new Factory
4
+ def initialize
5
+ @handers = {}
6
+ end
7
+
8
+ # s:: string from server
9
+ # Determine type of message
10
+ def type(s)
11
+ s = s.dup
12
+ t = nil
13
+ begin
14
+ if(s.slice(0,1).chr == ':')
15
+ s.slice!(0..s.index(' '))
16
+ t = s.slice!(0..s.index(' ')-1)
17
+ else
18
+ t = s.slice(0..s.index(' ')-1)
19
+ end
20
+ t.strip!
21
+ rescue
22
+ raise 'Failed to determine message type'
23
+ end
24
+ t
25
+ end
26
+
27
+ # string:: String from IRC server to process
28
+ # Process the given string and return parsed
29
+ # message or nil
30
+ def process(string)
31
+ s = nil
32
+ if(@handlers[type(string)])
33
+ s = @handlers.process(string)
34
+ else
35
+ raise NoMethodError.new("No handler found to process string: #{string}")
36
+ end
37
+ s
38
+ end
39
+
40
+ end
41
+ end
data/lib/Handler.rb ADDED
@@ -0,0 +1,39 @@
1
+ require 'ostruct'
2
+
3
+ unless(OpenStruct.new.type.nil?)
4
+ class OpenStruct
5
+ def type
6
+ @table[:type]
7
+ end
8
+ end
9
+ end
10
+
11
+ module MessageFactory
12
+ module Handlers
13
+ class Handler
14
+
15
+ # Returns symbol or array of symbols of allowed message types
16
+ def types_process
17
+ raise NotImplementedError.new
18
+ end
19
+
20
+ # data:: string of data
21
+ # Process string and create proper message
22
+ def process(data)
23
+ raise NotImplementedError.new
24
+ end
25
+
26
+ protected
27
+
28
+ # orig:: Original message string
29
+ # Helper to generate the message struct
30
+ def mk_struct(orig=nil)
31
+ m = OpenStruct.new
32
+ m.direction = :incoming
33
+ m.received = Time.now
34
+ m.raw = orig.dup
35
+ m
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,34 @@
1
+
2
+ module MessageFactory
3
+ module Handlers
4
+ class BadNick < Handler
5
+ # Returns type(s) supported
6
+ def types_process
7
+ :'432'
8
+ end
9
+ # string:: string to process
10
+ # Create a new BadNick message
11
+ # OpenStruct will contain:
12
+ # #type #direction #raw #bad_nick #received
13
+ # :nodoc: ':the.server 432 spox 999 :Erroneous Nickname'
14
+ def process(string)
15
+ m = nil
16
+ string = string.dup
17
+ orig = string.dup
18
+ begin
19
+ m = mk_struct(string)
20
+ m.type = :badnick
21
+ string.slice!(0)
22
+ m.server = string.slice!(0, string.index(' '))
23
+ string.slice!(0)
24
+ raise unless string.slice!(0, string.index(' ')).to_sym == :'432'
25
+ 2.times{string.slice!(0, string.index(' ')+1)}
26
+ m.bad_nick = string.slice!(0, string.index(' '))
27
+ rescue
28
+ raise "Failed to parse BadNick string: #{orig}"
29
+ end
30
+ m
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,33 @@
1
+ module MessageFactory
2
+ module Handlers
3
+ class Bounce < Handler
4
+
5
+ # Returns type(s) supported
6
+ def types_process
7
+ :'005'
8
+ end
9
+
10
+ # string:: string to process
11
+ # Create a new BadNick message
12
+ # OpenStruct will contain:
13
+ # #type #direction #raw #received #server #port
14
+ # :nodoc:
15
+ def process(string)
16
+ m = nil
17
+ begin
18
+ m = mk_struct(string)
19
+ m.type = :bounce
20
+ orig = string.dup
21
+ 2.times{string.slice!(0..string.index(' '))}
22
+ server = string.slice!(0..string.index(',')-1)
23
+ string.slice!(0..string.index(' ',4))
24
+ m.server = server
25
+ m.port = string
26
+ rescue Object => boom
27
+ raise "Failed to parse Bounce string: #{orig}"
28
+ end
29
+ m
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,39 @@
1
+ require 'time'
2
+
3
+ module MessageFactory
4
+ module Handlers
5
+ class Created < Handler
6
+
7
+ # Returns type(s) supported
8
+ def types_process
9
+ :'003'
10
+ end
11
+
12
+ # string:: string to process
13
+ # Create a new Created message
14
+ # OpenStruct will contain:
15
+ # #type #direction #raw #server #received #created
16
+ # :nodoc: :not.configured 003 spox :This server was created Tue Mar 24 2009 at 15:42:36 PDT'
17
+ def process(string)
18
+ string = string.dup
19
+ m = nil
20
+ begin
21
+ m = mk_struct(string)
22
+ m.type = :created
23
+ orig = string.dup
24
+ string.downcase!
25
+ string.slice!(0)
26
+ m.server = string.slice!(0, string.index(' '))
27
+ string.slice!(0)
28
+ raise 'Bad message type' unless string.slice!(0, string.index(' ')).to_sym == :'003'
29
+ string.slice!(0, string.index('d')+2)
30
+ time = Time.parse(string)
31
+ m.created = time
32
+ rescue
33
+ raise "Failed to parse Created message: #{orig}"
34
+ end
35
+ m
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,36 @@
1
+ module MessageFactory
2
+ module Handlers
3
+ class Invite < Handler
4
+ # Returns type(s) supported
5
+ def types_process
6
+ :INVITE
7
+ end
8
+ # string:: string to process
9
+ # Create a new Invite message
10
+ # OpenStruct will contain:
11
+ # #type #direction #raw #received #source #target #channel
12
+ # :nodoc: :spox!~spox@192.168.0.107 INVITE spox_ :#a
13
+ def process(string)
14
+ string = string.dup
15
+ orig = string.dup
16
+ m = nil
17
+ begin
18
+ m = mk_struct(string)
19
+ m.type = :invite
20
+ string.slice!(0)
21
+ m.source = string.slice!(0, string.index(' '))
22
+ string.slice!(0)
23
+ raise 'error' unless string.slice!(0, string.index(' ')).to_sym == :INVITE
24
+ string.slice!(0)
25
+ m.target = string.slice!(0, string.index(' '))
26
+ string.slice!(0)
27
+ string.slice!(0, string.index(':')+1)
28
+ m.channel = string
29
+ rescue
30
+ raise "Failed to parse Invite message: #{orig}"
31
+ end
32
+ m
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,37 @@
1
+
2
+ module MessageFactory
3
+ module Handlers
4
+ class Inviting < Handler
5
+ def types_process
6
+ :'341'
7
+ end
8
+ # string:: string to process
9
+ # Create a new Inviting message
10
+ # OpenStruct will contain:
11
+ # #type #direction #raw #received #server #source #target #channel
12
+ # :nodoc: :not.configured 341 spox spox_ #a
13
+ def process(string)
14
+ string = string.dup
15
+ orig = string.dup
16
+ m = nil
17
+ begin
18
+ m = mk_struct(string)
19
+ m.type = :inviting
20
+ string.slice!(0)
21
+ m.server = string.slice!(0, string.index(' '))
22
+ string.slice!(0)
23
+ raise 'error' unless string.slice!(0, string.index(' ')).to_sym == :'341'
24
+ string.slice!(0)
25
+ m.source = string.slice!(0, string.index(' '))
26
+ string.slice!(0)
27
+ m.target = string.slice!(0, string.index(' '))
28
+ string.slice!(0)
29
+ m.channel = string
30
+ rescue
31
+ raise "Failed to parse Inviting message: #{orig}"
32
+ end
33
+ m
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,33 @@
1
+
2
+ module MessageFactory
3
+ module Handlers
4
+ class Join < Handler
5
+ def types_process
6
+ :JOIN
7
+ end
8
+ # string:: string to process
9
+ # Create a new Join message
10
+ # OpenStruct will contain:
11
+ # #type #direction #raw #received #source #channel
12
+ # :nodoc: :mod_spox!~mod_spox@host JOIN :#m
13
+ def process(string)
14
+ string = string.dup
15
+ orig = string.dup
16
+ m = nil
17
+ begin
18
+ m = mk_struct(string)
19
+ m.type = :join
20
+ string.slice!(0)
21
+ m.source = string.slice!(0, string.index(' '))
22
+ string.slice!(0)
23
+ raise 'error' unless string.slice!(0, string.index(' ')).to_sym == :JOIN
24
+ string.slice!(0, string.index(':')+1)
25
+ m.channel = string
26
+ rescue
27
+ raise "Failed to parse Join message: #{orig}"
28
+ end
29
+ m
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,36 @@
1
+ module MessageFactory
2
+ module Handlers
3
+ class Kick < Handler
4
+ def types_process
5
+ :KICK
6
+ end
7
+ # string:: string to process
8
+ # Create a new Kick message
9
+ # OpenStruct will contain:
10
+ # #type #direction #raw #received #source #target #channel #message
11
+ # :nodoc: :spax!~spox@host KICK #m spox :foo
12
+ def process(string)
13
+ string = string.dup
14
+ orig = string.dup
15
+ m = nil
16
+ begin
17
+ m = mk_struct(string)
18
+ m.type = :kick
19
+ string.slice!(0)
20
+ m.source = string.slice!(0, string.index(' '))
21
+ string.slice!(0)
22
+ raise 'error' unless string.slice!(0, string.index(' ')).to_sym == :KICK
23
+ string.slice!(0)
24
+ m.channel = string.slice!(0, string.index(' '))
25
+ string.slice!(0)
26
+ m.target = string.slice!(0, string.index(' '))
27
+ string.slice!(0, 2)
28
+ m.message = string
29
+ rescue
30
+ raise "Failed to parse Kick message: #{orig}"
31
+ end
32
+ m
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,37 @@
1
+
2
+ module MessageFactory
3
+ module Handlers
4
+ class LuserChannels < Handler
5
+ def types_process
6
+ :'254'
7
+ end
8
+ # string:: string to process
9
+ # Create a new LuserChannels message
10
+ # OpenStruct will contain:
11
+ # #type #direction #raw #received #server #target #num_channels #message
12
+ # :nodoc: :crichton.freenode.net 254 spox 24466 :channels formed
13
+ def process(string)
14
+ string = string.dup
15
+ orig = string.dup
16
+ m = nil
17
+ begin
18
+ m = mk_struct(string)
19
+ m.type = :luserchannels
20
+ string.slice!(0)
21
+ m.server = string.slice!(0, string.index(' '))
22
+ string.slice!(' ')
23
+ raise 'error' unless string.slice!(0, string.index(' ')).to_sym == :'254'
24
+ string.slice!(0)
25
+ m.target = string.slice!(0, string.index(' '))
26
+ string.slice!(0)
27
+ m.num_channels = string.slice!(0, string.index(' '))
28
+ string.slice!(0, string.index(':')+1)
29
+ m.message = string
30
+ rescue
31
+ raise "Failed to parse LuserChannels message: #{orig}"
32
+ end
33
+ m
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,52 @@
1
+ # To change this template, choose Tools | Templates
2
+ # and open the template in the editor.
3
+ module MessageFactory
4
+ module Handlers
5
+ class Mode < Handler
6
+ def types_process
7
+ :MODE
8
+ end
9
+ # string:: string to process
10
+ # Create a new Mode message
11
+ # OpenStruct will contain:
12
+ # #type #direction #raw #received #source #target #channel #modes #set #unset
13
+ # :nodoc: :spax!~spox@host MODE #m +o spax
14
+ # :nodoc: :spax MODE spax :+iw
15
+ def process(string)
16
+ string = string.dup
17
+ orig = string.dup
18
+ m = nil
19
+ self_mode = string.index('!').nil?
20
+ begin
21
+ m = mk_struct(string)
22
+ m.type = :mode
23
+ string.slice!(0)
24
+ m.source = string.slice!(0, string.index(' '))
25
+ string.slice!(0)
26
+ raise 'error' unless string.slice!(0, string.index(' ')).to_sym == :MODE
27
+ string.slice!(0)
28
+ if(self_mode)
29
+ m.target = string.slice!(0, string.index(' '))
30
+ string.slice!(0, string.index(':')+1)
31
+ action = string.slice!(0).chr
32
+ m.set = action == '+'
33
+ m.unset = action == '-'
34
+ m.modes = string
35
+ else
36
+ m.channel = string.slice!(0, string.index(' '))
37
+ string.slice!(0)
38
+ action = string.slice!(0).chr
39
+ m.set = action == '+'
40
+ m.unset = action == '-'
41
+ m.modes = string.slice!(0, string.index(' '))
42
+ string.slice!(0)
43
+ m.target = string
44
+ end
45
+ rescue
46
+ raise "Failed to parse Mode message: #{orig}"
47
+ end
48
+ m
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,82 @@
1
+ # To change this template, choose Tools | Templates
2
+ # and open the template in the editor.
3
+
4
+ #RPL_NAMREPLY => 353
5
+ #RPL_ENDOFNAMES => 366
6
+
7
+ module MessageFactory
8
+ module Handlers
9
+ class Names < Handler
10
+ def initialize
11
+ @cache = {}
12
+ end
13
+ def types_process
14
+ [:'353', :'366']
15
+ end
16
+ # string:: string to process
17
+ # Create a new Names message
18
+ # OpenStruct will contain:
19
+ # #type #direction #raw #received #target #channel #server #nicks
20
+ # :nodoc: :swiftco.wa.us.dal.net 353 spox = #mod_spox :mod_spox spox
21
+ # :nodoc: :swiftco.wa.us.dal.net 366 spox #mod_spox :End of /NAMES list.
22
+ def process(string)
23
+ string = string.dup
24
+ orig = string.dup
25
+ m = nil
26
+ begin
27
+ string.slice!(0)
28
+ server = string.slice!(0, string.index(' '))
29
+ string.slice!(0)
30
+ case string.slice!(0, string.index(' ')).to_sym
31
+ when :'353'
32
+ m = names(string, orig)
33
+ when :'366'
34
+ m = end_names(string, orig)
35
+ else
36
+ raise 'error'
37
+ end
38
+ if(m)
39
+ m.server = server
40
+ end
41
+ rescue
42
+ raise "Failed to parse Name message: #{orig}"
43
+ end
44
+ m
45
+ end
46
+
47
+ def names(string, orig)
48
+ string.slice!(0)
49
+ target = string.slice!(0, string.index(' '))
50
+ string.slice!(0, 3)
51
+ channel = string.slice!(0, string.index(' '))
52
+ m = @cache[channel] ? @cache[channel] : mk_struct(orig)
53
+ m.type = :names
54
+ m.target = target
55
+ m.channel = channel
56
+ if(m.raw.is_a?(Array))
57
+ m.raw.push(orig)
58
+ else
59
+ m.raw = [m.raw]
60
+ end
61
+ string.slice!(0, string.index(':')+1)
62
+ m.nicks ||= []
63
+ m.nicks = m.nicks + string.split
64
+ @cache[channel] = m
65
+ nil
66
+ end
67
+
68
+ def end_names(string, orig)
69
+ string.slice!(0)
70
+ string.slice!(0, string.index(' ')+1)
71
+ channel = string.slice!(0, string.index(' '))
72
+ m = nil
73
+ if(@cache[channel])
74
+ m = @cache[channel]
75
+ m.raw.push(orig)
76
+ @cache.delete(channel)
77
+ end
78
+ m
79
+ end
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,33 @@
1
+ # To change this template, choose Tools | Templates
2
+ # and open the template in the editor.
3
+
4
+ module MessageFactory
5
+ module Handlers
6
+ class Nick < Handler
7
+ def types_process
8
+ :NICK
9
+ end
10
+ # string:: string to process
11
+ # Create a new Nick message
12
+ # OpenStruct will contain:
13
+ # #type #direction #raw #received #source #new_nick
14
+ # :nodoc: :spox!~spox@some.random.host NICK :flock_of_deer
15
+ def process(string)
16
+ string = string.dup
17
+ m = mk_struct(string)
18
+ begin
19
+ m.type = :nick
20
+ string.slice!(0)
21
+ m.source = string.slice!(0, string.index(' '))
22
+ string.slice!(0)
23
+ raise 'error' unless string.slice!(0, string.index(' ')).to_sym == :NICK
24
+ string.slice!(0, string.index(' ')+2)
25
+ m.new_nick = string
26
+ rescue
27
+ raise "Failed to parse Nick message: #{m.raw}"
28
+ end
29
+ m
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,36 @@
1
+ # To change this template, choose Tools | Templates
2
+ # and open the template in the editor.
3
+
4
+ module MessageFactory
5
+ module Handlers
6
+ class Notice < Handler
7
+ def types_process
8
+ :NOTICE
9
+ end
10
+ # string:: string to process
11
+ # Create a new Notice message
12
+ # OpenStruct will contain:
13
+ # #type #direction #raw #received #source #target #message
14
+ # :nodoc: :spox!~spox@some.host NOTICE spax :test
15
+ # :nodoc: :spox!~spox@some.host NOTICE #mod_spox :test
16
+ def process(string)
17
+ string = string.dup
18
+ m = mk_struct(string)
19
+ begin
20
+ m.type = :notice
21
+ string.slice!(0)
22
+ m.source = string.slice!(0, string.index(' '))
23
+ string.slice!(0)
24
+ raise 'error' unless string.slice!(0, string.index(' ')).to_sym == :NOTICE
25
+ string.slice!(0)
26
+ m.target = string.slice!(0, string.index(' '))
27
+ string.slice!(0, string.index(':')+1)
28
+ m.message = string
29
+ rescue
30
+ raise "Failed to parse Notice message: #{m.raw}"
31
+ end
32
+ m
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,40 @@
1
+ # To change this template, choose Tools | Templates
2
+ # and open the template in the editor.
3
+
4
+ module MessageFactory
5
+ module Handlers
6
+ class Part < Handler
7
+ def types_process
8
+ :PART
9
+ end
10
+ # string:: string to process
11
+ # Create a new Part message
12
+ # OpenStruct will contain:
13
+ # #type #direction #raw #received #source #channel #message
14
+ # :nodoc: :mod_spox!~mod_spox@host PART #m :
15
+ # :nodoc: :foobar!~foobar@some.host PART #php
16
+ def process(string)
17
+ string = string.dup
18
+ m = mk_struct(string)
19
+ begin
20
+ m.type = :part
21
+ string.slice!(0)
22
+ m.source = string.slice!(0, string.index(' '))
23
+ string.slice!(0)
24
+ raise 'error' unless string.slice!(0, string.index(' ')).to_sym == :PART
25
+ string.slice!(0)
26
+ m.channel = string.slice!(0, string.index(' '))
27
+ if(string.index(':'))
28
+ string.slice!(0, string.index(':')+1)
29
+ m.message = string
30
+ else
31
+ m.message = ''
32
+ end
33
+ rescue
34
+ raise "Failed to parse Part message: #{m.raw}"
35
+ end
36
+ m
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,38 @@
1
+
2
+ module MessageFactory
3
+ module Handlers
4
+ class Welcome < Handler
5
+ def types_process
6
+ :'001'
7
+ end
8
+ # string:: string to process
9
+ # Create a new Welcome message
10
+ # OpenStruct will contain:
11
+ # #type #direction #raw #received #message #target #user #server
12
+ # :nodoc: :not.configured 001 spox :Welcome to the unconfigured IRC Network spox!~spox@192.168.0.107
13
+ def process(string)
14
+ string = string.dup
15
+ orig = string.dup
16
+ m = nil
17
+ begin
18
+ m = mk_struct(string)
19
+ m.type = :welcome
20
+ string.slice!(0)
21
+ m.server = string.slice!(0, string.index(' '))
22
+ string.slice!(0)
23
+ raise 'error' unless string.slice!(0, string.index(' ')).to_sym == :'001'
24
+ string.slice!(0)
25
+ m.target = string.slice!(0, string.index(' '))
26
+ string.slice!(0, string.index(':')+1)
27
+ m.message = string.slice!(0, string.rindex(' '))
28
+ string.slice!(0)
29
+ m.user = string
30
+ rescue
31
+ raise "Failed to parse Welcome message: #{orig}"
32
+ end
33
+ m
34
+ end
35
+
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,35 @@
1
+
2
+ module MessageFactory
3
+ module Handlers
4
+ class YourHost < Handler
5
+ def types_process
6
+ :'002'
7
+ end
8
+ # string:: string to process
9
+ # Create a new YourHost message
10
+ # OpenStruct will contain:
11
+ # #type #direction #raw #received #target #server #version
12
+ # :nodoc: :not.configured 002 spox :Your host is not.configured, running version bahamut-1.8(04)
13
+ def process(string)
14
+ string = string.dup
15
+ orig = string.dup
16
+ m = nil
17
+ begin
18
+ m = mk_struct(string)
19
+ m.type = :yourhost
20
+ string.slice!(0)
21
+ m.server = string.slice!(0, string.index(' '))
22
+ string.slice!(0)
23
+ raise 'error' unless string.slice!(0, string.index(' ')).to_sym == :'002'
24
+ string.slice!(0)
25
+ m.target = string.slice!(0, string.index(' '))
26
+ string.slice!(0, string.rindex(' ')+1)
27
+ m.version = string
28
+ rescue
29
+ raise "Failed to parse YourHost message: #{orig}"
30
+ end
31
+ m
32
+ end
33
+ end
34
+ end
35
+ end
data/spec/dummy.rb ADDED
File without changes
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: messagefactory
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - ""
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-02-05 00:00:00 Z
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Your summary here
17
+ email: ""
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.rdoc
24
+ - LICENSE
25
+ files:
26
+ - LICENSE
27
+ - README.rdoc
28
+ - Rakefile
29
+ - lib/Factory.rb
30
+ - lib/Handler.rb
31
+ - lib/handlers/Inviting.rb
32
+ - lib/handlers/Created.rb
33
+ - lib/handlers/Kick.rb
34
+ - lib/handlers/Nick.rb
35
+ - lib/handlers/Mode.rb
36
+ - lib/handlers/Names.rb
37
+ - lib/handlers/YourHost.rb
38
+ - lib/handlers/Join.rb
39
+ - lib/handlers/Invite.rb
40
+ - lib/handlers/Welcome.rb
41
+ - lib/handlers/Bounce.rb
42
+ - lib/handlers/BadNick.rb
43
+ - lib/handlers/LuserChannels.rb
44
+ - lib/handlers/Part.rb
45
+ - lib/handlers/Notice.rb
46
+ - spec/dummy.rb
47
+ has_rdoc: true
48
+ homepage:
49
+ licenses: []
50
+
51
+ post_install_message:
52
+ rdoc_options: []
53
+
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: "0"
61
+ version:
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: "0"
67
+ version:
68
+ requirements: []
69
+
70
+ rubyforge_project:
71
+ rubygems_version: 1.3.5
72
+ signing_key:
73
+ specification_version: 3
74
+ summary: Your summary here
75
+ test_files: []
76
+