messagefactory 0.0.1 → 0.0.2

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.
Files changed (27) hide show
  1. data/Rakefile +5 -4
  2. data/lib/messagefactory.rb +1 -0
  3. data/lib/{Factory.rb → messagefactory/Factory.rb} +10 -4
  4. data/lib/{Handler.rb → messagefactory/Handler.rb} +6 -1
  5. data/lib/{handlers/Welcome.rb → messagefactory/handlers/001.rb} +0 -0
  6. data/lib/{handlers/YourHost.rb → messagefactory/handlers/002.rb} +0 -0
  7. data/lib/{handlers/Created.rb → messagefactory/handlers/003.rb} +0 -0
  8. data/lib/{handlers/Bounce.rb → messagefactory/handlers/005.rb} +0 -0
  9. data/lib/{handlers/LuserChannels.rb → messagefactory/handlers/254.rb} +0 -0
  10. data/lib/messagefactory/handlers/311.rb +140 -0
  11. data/lib/{handlers/Inviting.rb → messagefactory/handlers/341.rb} +0 -0
  12. data/lib/messagefactory/handlers/352.rb +99 -0
  13. data/lib/{handlers/Names.rb → messagefactory/handlers/353.rb} +0 -0
  14. data/lib/{handlers/BadNick.rb → messagefactory/handlers/432.rb} +0 -0
  15. data/lib/{handlers/Invite.rb → messagefactory/handlers/INVITE.rb} +0 -0
  16. data/lib/{handlers/Join.rb → messagefactory/handlers/JOIN.rb} +0 -0
  17. data/lib/{handlers/Kick.rb → messagefactory/handlers/KICK.rb} +0 -0
  18. data/lib/{handlers/Mode.rb → messagefactory/handlers/MODE.rb} +1 -0
  19. data/lib/{handlers/Nick.rb → messagefactory/handlers/NICK.rb} +0 -0
  20. data/lib/{handlers/Notice.rb → messagefactory/handlers/NOTICE.rb} +0 -0
  21. data/lib/{handlers/Part.rb → messagefactory/handlers/PART.rb} +0 -0
  22. data/lib/messagefactory/handlers/PING.rb +41 -0
  23. data/lib/messagefactory/handlers/PONG.rb +37 -0
  24. data/lib/messagefactory/handlers/PRIVMSG.rb +40 -0
  25. data/lib/messagefactory/handlers/QUIT.rb +40 -0
  26. data/lib/messagefactory/handlers/Raw.rb +12 -0
  27. metadata +32 -24
data/Rakefile CHANGED
@@ -13,17 +13,18 @@ require 'spec/rake/spectask'
13
13
 
14
14
  spec = Gem::Specification.new do |s|
15
15
  s.name = 'messagefactory'
16
- s.version = '0.0.1'
16
+ s.version = '0.0.2'
17
17
  s.has_rdoc = true
18
18
  s.extra_rdoc_files = ['README.rdoc', 'LICENSE']
19
- s.summary = 'Your summary here'
19
+ s.summary = 'Objectifies content from an IRC server'
20
20
  s.description = s.summary
21
- s.author = ''
22
- s.email = ''
21
+ s.author = 'spox'
22
+ s.email = 'spox@modspox.com'
23
23
  # s.executables = ['your_executable_here']
24
24
  s.files = %w(LICENSE README.rdoc Rakefile) + Dir.glob("{bin,lib,spec}/**/*")
25
25
  s.require_path = "lib"
26
26
  s.bindir = "bin"
27
+ s.required_ruby_version = '>= 1.8.7'
27
28
  end
28
29
 
29
30
  Rake::GemPackageTask.new(spec) do |p|
@@ -0,0 +1 @@
1
+ require 'messagefactory/Factory'
@@ -21,18 +21,24 @@ module MessageFactory
21
21
  rescue
22
22
  raise 'Failed to determine message type'
23
23
  end
24
- t
24
+ t.to_sym
25
25
  end
26
26
 
27
27
  # string:: String from IRC server to process
28
28
  # Process the given string and return parsed
29
29
  # message or nil
30
- def process(string)
30
+ def process(string, do_require=true)
31
31
  s = nil
32
- if(@handlers[type(string)])
32
+ mtype = type(string)
33
+ if(@handlers[mtype])
33
34
  s = @handlers.process(string)
34
35
  else
35
- raise NoMethodError.new("No handler found to process string: #{string}")
36
+ if(do_require)
37
+ require "messagefactory/handlers/#{mtype}"
38
+ s = process(string, false)
39
+ else
40
+ raise NoMethodError.new("No handler found to process string: #{string}")
41
+ end
36
42
  end
37
43
  s
38
44
  end
@@ -8,6 +8,11 @@ unless(OpenStruct.new.type.nil?)
8
8
  end
9
9
  end
10
10
 
11
+ module MessageFactory
12
+ class Message < OpenStruct
13
+ end
14
+ end
15
+
11
16
  module MessageFactory
12
17
  module Handlers
13
18
  class Handler
@@ -28,7 +33,7 @@ module Handlers
28
33
  # orig:: Original message string
29
34
  # Helper to generate the message struct
30
35
  def mk_struct(orig=nil)
31
- m = OpenStruct.new
36
+ m = Message.new
32
37
  m.direction = :incoming
33
38
  m.received = Time.now
34
39
  m.raw = orig.dup
@@ -0,0 +1,140 @@
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 Whois < Handler
7
+ def initialize
8
+ @cache = {}
9
+ end
10
+ def clear_cache
11
+ @cache.clear
12
+ end
13
+ def types_process
14
+ [:'311', :'319', :'312', :'307', :'330', :'317', :'318', :'378']
15
+ end
16
+ # string:: string to process
17
+ # Create a new Mode message
18
+ # OpenStruct will contain:
19
+ # #type #direction #raw #received #source #target #channels #voice #ops
20
+ # #irc_host #idle #signon #nickserv
21
+ def process(string)
22
+ string = string.dup
23
+ orig = string.dup
24
+ m = nil
25
+ begin
26
+ string.slice!(0)
27
+ server = string.slice!(0, string.index(' '))
28
+ string.slice!(0)
29
+ action = string.slice!(0, string.index(' '))
30
+ raise unless types_process.include?(action.to_sym)
31
+ string.slice!(0)
32
+ action = ('process_'+action)
33
+ if(self.methods.include?(action))
34
+ m = self.send(action, string, orig)
35
+ else
36
+ m = store_raw(string, orig)
37
+ end
38
+ m.source = server if m
39
+ rescue => boom
40
+ raise "Failed to parse Whois message: #{orig}"
41
+ end
42
+ m
43
+ end
44
+
45
+ def store_raw(string, orig)
46
+ parts = string.split
47
+ m = @cache[parts[1]]
48
+ raise unless m
49
+ m.raw << orig
50
+ nil
51
+ end
52
+
53
+ #:swiftco.wa.us.dal.net 311 spox spox ~spox pool-96-225-201-176.ptldor.dsl-w.verizon.net * :spox
54
+ def process_311(string, orig)
55
+ m = mk_struct(orig)
56
+ m.raw = [orig]
57
+ m.type = :whois
58
+ parts = string.split
59
+ m.target = parts[1]
60
+ m.username = parts[2]
61
+ m.host = parts[3]
62
+ parts[5].slice!(0)
63
+ m.real_name = parts[5]
64
+ @cache[m.target] = m
65
+ nil
66
+ end
67
+ #:swiftco.wa.us.dal.net 319 spox spox :+#php #ruby #mysql @#mod_spox
68
+ def process_319(string, orig)
69
+ parts = string.split
70
+ parts.shift
71
+ m = @cache[parts.shift]
72
+ raise unless m
73
+ m.raw << orig
74
+ parts[0].slice!(0)
75
+ parts.each do |chan|
76
+ start = chan[0,1]
77
+ case start
78
+ when '+'
79
+ chan.slice!(0)
80
+ m.voice ||= []
81
+ m.voice << chan
82
+ when '@'
83
+ chan.slice!(0)
84
+ m.ops ||= []
85
+ m.ops << chan
86
+ end
87
+ m.channels ||= []
88
+ m.channels << chan
89
+ end
90
+ nil
91
+ end
92
+ #:swiftco.wa.us.dal.net 317 spox spox 529 1265393189 :seconds idle, signon time
93
+ def process_317(string, orig)
94
+ parts = string.split
95
+ parts.shift
96
+ m = @cache[parts.shift]
97
+ raise unless m
98
+ m.raw << orig
99
+ m.idle = parts.shift.to_i
100
+ m.signon = parts.shift.to_i
101
+ nil
102
+ end
103
+ #:swiftco.wa.us.dal.net 312 spox spox swiftco.wa.us.dal.net :www.swiftco.net - Swift Communications
104
+ def process_312(string, orig)
105
+ parts = string.split
106
+ parts.shift
107
+ m = @cache[parts.shift]
108
+ raise unless m
109
+ m.raw << orig
110
+ m.irc_host = parts.shift
111
+ nil
112
+ end
113
+ #:swiftco.wa.us.dal.net 307 spox spox :has identified for this nick
114
+ def process_307(string, orig)
115
+ parts = string.split
116
+ parts.shift
117
+ m = @cache[parts.shift]
118
+ raise unless m
119
+ m.nickserv = :identified
120
+ m.raw << orig
121
+ nil
122
+ end
123
+ #:wolfe.freenode.net 330 spox spox spox :is logged in as
124
+ def process_330(string, orig)
125
+ process_307(string, orig)
126
+ end
127
+ #:wolfe.freenode.net 318 spox spox :End of /WHOIS list.
128
+ def process_318(string, orig)
129
+ parts = string.split
130
+ parts.shift
131
+ target = parts.shift
132
+ m = @cache[target]
133
+ raise unless m
134
+ m.raw << orig
135
+ @cache.delete(target)
136
+ m
137
+ end
138
+ end
139
+ end
140
+ end
@@ -0,0 +1,99 @@
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 Who < Handler
7
+ def initialize
8
+ @cache = {}
9
+ end
10
+ def types_process
11
+ [:'352', :'315']
12
+ end
13
+ # string:: string to process
14
+ # Create a new Who message
15
+ # OpenStruct will contain:
16
+ # #type #direction #raw #received #source #channel #message
17
+ # :nodoc: Nick style:
18
+ # :swiftco.wa.us.dal.net 352 spox * ~metallic codemunkey.net punch.va.us.dal.net metallic H :2 Sean Grimes
19
+ # :swiftco.wa.us.dal.net 315 spox metallic :End of /WHO list.
20
+ # Channel style:
21
+ # :swiftco.wa.us.dal.net 352 spox #php ~chendo 24.47.233.220.static.exetel.com.au punch.va.us.dal.net chendo H@ :2 chendo
22
+ # :swiftco.wa.us.dal.net 315 spox #php :End of /WHO list.
23
+ def process(string)
24
+ string = string.dup
25
+ orig = string.dup
26
+ m = nil
27
+ begin
28
+ server = string.slice!(0, string.index(' '))
29
+ string.slice!(0)
30
+ action = string.slice!(0, string.index(' ')).to_sym
31
+ string.slice!(0)
32
+ string.slice!(0, string.index(' ')+1)
33
+ if(action == :'352')
34
+ m = who_content(string, orig)
35
+ elsif(action == :'315')
36
+ m = who_end(string, orig)
37
+ else
38
+ raise
39
+ end
40
+ rescue
41
+ raise "Failed to parse Who message: #{orig}"
42
+ end
43
+ m
44
+ end
45
+
46
+ def who_content(string, orig)
47
+ name = string.slice!(0, string.index(' '))
48
+ string.slice!(0)
49
+ nick = OpenStruct.new
50
+ nick.username = string.slice!(0, string.index(' '))
51
+ string.slice!(0)
52
+ nick.host = string.slice!(0, string.index(' '))
53
+ string.slice!(0)
54
+ nick.irc_host = string.slice!(0, string.index(' '))
55
+ string.slice!(0)
56
+ nick.nick = string.slice!(0, string.index(' '))
57
+ string.slice!(0)
58
+ status = string.slice!(0, string.index(' '))
59
+ string.slice!(0, string.index(':')+1)
60
+ nick.hops = string.slice!(0, string.index(' ')).to_i
61
+ string.slice!(0)
62
+ nick.real_name = string
63
+ target = name == '*' ? nick.nick : name
64
+ m = fetch_target(target, orig, name != '*')
65
+ m.nicks << nick
66
+ m.ops.push nick if status.index('@')
67
+ m.voice.push nick if status.index('+')
68
+ m.raw << orig
69
+ nil
70
+ end
71
+
72
+ def fetch_target(target, orig, channel=true)
73
+ unless(@cache[target])
74
+ m = mk_struct(orig)
75
+ m.type = :who
76
+ m.raw = []
77
+ m.nicks = []
78
+ if(channel)
79
+ m.ops = []
80
+ m.voice = []
81
+ m.channel = target
82
+ end
83
+ @cache[target] = m
84
+ end
85
+ @cache[target]
86
+ end
87
+
88
+ def who_end(string, orig)
89
+ name = string.slice!(0, string.index(' '))
90
+ m = @cache[name]
91
+ if(m)
92
+ @cache.delete(name)
93
+ m.raw << orig
94
+ end
95
+ m
96
+ end
97
+ end
98
+ end
99
+ end
@@ -41,6 +41,7 @@ module Handlers
41
41
  m.modes = string.slice!(0, string.index(' '))
42
42
  string.slice!(0)
43
43
  m.target = string
44
+ m.nick_mode = Hash[m.target.split.zip(m.modes.split(''))]
44
45
  end
45
46
  rescue
46
47
  raise "Failed to parse Mode message: #{orig}"
@@ -0,0 +1,41 @@
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 Ping < Handler
7
+ def types_process
8
+ :PING
9
+ end
10
+ # string:: string to process
11
+ # Create a new Ping message
12
+ # OpenStruct will contain:
13
+ # #type #direction #raw #received #server #message
14
+ # :nodoc: PING :not.configured
15
+ # :nodoc: :not.configured PING :test
16
+ def process(string)
17
+ string = string.dup
18
+ m = mk_struct(string)
19
+ begin
20
+ m.type = :ping
21
+ if(string.slice(0).chr == ':')
22
+ string.slice!(0)
23
+ m.server = string.slice!(0, string.index(' '))
24
+ string.slice!(0)
25
+ raise 'error' unless string.slice!(0, string.index(' ')).to_sym == :PING
26
+ string.slice!(0, string.index(':')+1)
27
+ m.message = string
28
+ else
29
+ raise 'error' unless string.slice!(0, string.index(' ')).to_sym == :PING
30
+ string.slice!(0, string.index(':')+1)
31
+ m.server = string
32
+ m.message = string
33
+ end
34
+ rescue
35
+ raise "Failed to parse Ping message: #{m.raw}"
36
+ end
37
+ m
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,37 @@
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 Pong < Handler
7
+ def types_process
8
+ :PONG
9
+ end
10
+ # string:: string to process
11
+ # Create a new Ping message
12
+ # OpenStruct will contain:
13
+ # #type #direction #raw #received #server #message
14
+ # :nodoc: :swiftco.wa.us.dal.net PONG swiftco.wa.us.dal.net :FOO
15
+ def process(string)
16
+ string = string.dup
17
+ m = mk_struct(string)
18
+ begin
19
+ m.type = :pong
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 == :PONG
24
+ if(string.index(':'))
25
+ string.slice!(0, string.index(':')+1)
26
+ m.message = string
27
+ else
28
+ m.message = ''
29
+ end
30
+ rescue
31
+ raise "Failed to parse Pong message: #{m.raw}"
32
+ end
33
+ m
34
+ end
35
+ end
36
+ end
37
+ 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 Privmsg < Handler
7
+ def types_process
8
+ :PRIVMSG
9
+ end
10
+ # string:: string to process
11
+ # Create a new Privmsg message
12
+ # OpenStruct will contain:
13
+ # #type #direction #raw #received #source #target #message
14
+ # :nodoc: I thoguht about adding in #public and #private methods and
15
+ # something for the nick a message is addressing, but we want
16
+ # to keep this stuff simple, so we will let the implementation program
17
+ # worry about extras like that.
18
+ # :nodoc: :spox!~spox@host PRIVMSG #m :foobar
19
+ # :nodoc: :spox!~spox@host PRIVMSG mod_spox :foobar
20
+ def process(string)
21
+ string = string.dup
22
+ m = mk_struct(string)
23
+ begin
24
+ m.type = :privmsg
25
+ string.slice!(0)
26
+ m.source = string.slice!(0, string.index(' '))
27
+ string.slice!(0)
28
+ raise 'error' unless string.slice!(0, string.index(' ')).to_sym == :PRIVMSG
29
+ string.slice!(0)
30
+ m.target = string.slice!(0, string.index(' '))
31
+ string.slice!(0, string.index(':')+1)
32
+ m.message = string
33
+ rescue
34
+ raise "Failed to parse Privmsg message: #{m.raw}"
35
+ end
36
+ m
37
+ end
38
+ end
39
+ end
40
+ 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 Quit < Handler
7
+ def types_process
8
+ :QUIT
9
+ end
10
+ # string:: string to process
11
+ # Create a new Quit message
12
+ # OpenStruct will contain:
13
+ # #type #direction #raw #received #source #message
14
+ # :nodoc: :spox!~spox@host QUIT :Ping timeout
15
+ # :spox!~spox@host QUIT :
16
+ def process(string)
17
+ string = string.dup
18
+ m = mk_struct(string)
19
+ begin
20
+ m.type = :quit
21
+ string.slice!(0)
22
+ m.source = string.slice!(0, string.index(' '))
23
+ string.slice!(0)
24
+ idx = string.index(' ')
25
+ idx ||= string.length
26
+ raise unless string.slice!(0, idx).to_sym == :QUIT
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 Quit message: #{m.raw}"
35
+ end
36
+ m
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,12 @@
1
+ module Handlers
2
+ class Raw < Handler
3
+ def types_process
4
+ :UNKNOWN
5
+ end
6
+ def process(string)
7
+ m = mk_struct(string)
8
+ m.type = :raw
9
+ m
10
+ end
11
+ end
12
+ end
metadata CHANGED
@@ -1,20 +1,20 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: messagefactory
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
- - ""
7
+ - spox
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-02-05 00:00:00 Z
12
+ date: 2010-04-18 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
16
- description: Your summary here
17
- email: ""
16
+ description: Objectifies content from an IRC server
17
+ email: spox@modspox.com
18
18
  executables: []
19
19
 
20
20
  extensions: []
@@ -26,23 +26,31 @@ files:
26
26
  - LICENSE
27
27
  - README.rdoc
28
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
29
+ - lib/messagefactory.rb
30
+ - lib/messagefactory/Factory.rb
31
+ - lib/messagefactory/Handler.rb
32
+ - lib/messagefactory/handlers/001.rb
33
+ - lib/messagefactory/handlers/002.rb
34
+ - lib/messagefactory/handlers/003.rb
35
+ - lib/messagefactory/handlers/005.rb
36
+ - lib/messagefactory/handlers/254.rb
37
+ - lib/messagefactory/handlers/311.rb
38
+ - lib/messagefactory/handlers/341.rb
39
+ - lib/messagefactory/handlers/352.rb
40
+ - lib/messagefactory/handlers/353.rb
41
+ - lib/messagefactory/handlers/432.rb
42
+ - lib/messagefactory/handlers/INVITE.rb
43
+ - lib/messagefactory/handlers/JOIN.rb
44
+ - lib/messagefactory/handlers/KICK.rb
45
+ - lib/messagefactory/handlers/MODE.rb
46
+ - lib/messagefactory/handlers/NICK.rb
47
+ - lib/messagefactory/handlers/NOTICE.rb
48
+ - lib/messagefactory/handlers/PART.rb
49
+ - lib/messagefactory/handlers/PING.rb
50
+ - lib/messagefactory/handlers/PONG.rb
51
+ - lib/messagefactory/handlers/PRIVMSG.rb
52
+ - lib/messagefactory/handlers/QUIT.rb
53
+ - lib/messagefactory/handlers/Raw.rb
46
54
  - spec/dummy.rb
47
55
  has_rdoc: true
48
56
  homepage:
@@ -57,7 +65,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
57
65
  requirements:
58
66
  - - ">="
59
67
  - !ruby/object:Gem::Version
60
- version: "0"
68
+ version: 1.8.7
61
69
  version:
62
70
  required_rubygems_version: !ruby/object:Gem::Requirement
63
71
  requirements:
@@ -71,6 +79,6 @@ rubyforge_project:
71
79
  rubygems_version: 1.3.5
72
80
  signing_key:
73
81
  specification_version: 3
74
- summary: Your summary here
82
+ summary: Objectifies content from an IRC server
75
83
  test_files: []
76
84