ircp 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format d
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ircp.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 mashiro
2
+
3
+ MIT License
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.
data/README.md ADDED
@@ -0,0 +1,32 @@
1
+ # Ircp
2
+
3
+ Ircp is a IRC parser for ruby.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'ircp'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install ircp
18
+
19
+ ## Usage
20
+
21
+ ```ruby
22
+ require 'ircp'
23
+ msg = Ircp.parse 'PRIVMSG ircp :hello ircp!'
24
+ ```
25
+
26
+ ## Contributing
27
+
28
+ 1. Fork it
29
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
30
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
31
+ 4. Push to the branch (`git push origin my-new-feature`)
32
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new(:spec)
6
+ task :default => :spec
data/ircp.gemspec ADDED
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/ircp/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["mashiro"]
6
+ gem.email = ["mail@mashiro.org"]
7
+ gem.description = %q{irc parser}
8
+ gem.summary = %q{irc parser for ruby}
9
+ gem.homepage = ""
10
+
11
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
12
+ gem.files = `git ls-files`.split("\n")
13
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
14
+ gem.name = "ircp"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Ircp::VERSION
17
+
18
+ gem.add_dependency 'treetop'
19
+ gem.add_development_dependency 'rake'
20
+ gem.add_development_dependency 'rspec'
21
+ end
@@ -0,0 +1,15 @@
1
+ require 'ircp/prefix'
2
+
3
+ module Ircp
4
+ class Message
5
+ attr_accessor :raw, :prefix, :command, :params
6
+
7
+ def initialize(*args)
8
+ options = args.last.is_a?(Hash) ? args.pop : {}
9
+ @raw = options[:raw]
10
+ @prefix = options[:prefix].is_a?(Hash) ? Prefix.new(options[:prefix]) : nil
11
+ @command = options[:command]
12
+ @params = args
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,74 @@
1
+ # vim: ts=2 sw=2 et
2
+ # -*- coding: ascii-8bit -*-
3
+ require 'ircp/parser/message_node_classes'
4
+
5
+ module Ircp
6
+ module Parser
7
+ # RFC1459 based
8
+ grammar Message
9
+ rule message
10
+ prefix:( ':' v:prefix space )? command params:params? crlf? <MessageNode>
11
+ end
12
+
13
+ rule prefix
14
+ '' servername &space <ShortPrefixNode> /
15
+ nick user:( '!' v:user )? host:( '@' v:host )? <LongPrefixNode>
16
+ end
17
+
18
+ rule command
19
+ letter+ <CommandNode> /
20
+ number 3..3 <NumericReplyNode>
21
+ end
22
+
23
+ rule params
24
+ middles:( space v:middle )* trailing:( space ':' v:trailing )? <ParamsNode>
25
+ end
26
+
27
+ rule middle
28
+ [^ :] [^ ]*
29
+ end
30
+
31
+ rule trailing
32
+ .*
33
+ end
34
+
35
+
36
+ rule space
37
+ [ ]+
38
+ end
39
+
40
+ rule nospace
41
+ [^ ]
42
+ end
43
+
44
+ rule crlf
45
+ [\x0D\x0A]
46
+ end
47
+
48
+ rule letter
49
+ [a-zA-Z]
50
+ end
51
+
52
+ rule number
53
+ [0-9]
54
+ end
55
+
56
+
57
+ rule servername
58
+ '' host <ServerNameNode>
59
+ end
60
+
61
+ rule host
62
+ [^ !@]+ <HostNode>
63
+ end
64
+
65
+ rule nick
66
+ [^ !@]+ <NickNode>
67
+ end
68
+
69
+ rule user
70
+ [^ !@]+ <UserNode>
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,98 @@
1
+ module Ircp
2
+ module Parser
3
+ class MessageNode < Treetop::Runtime::SyntaxNode
4
+ def eval(env = {})
5
+ env[:raw] = text_value
6
+ prefix.v.eval (env[:prefix] ||= {}) unless prefix.empty?
7
+ command.eval env
8
+ params.eval env unless params.empty?
9
+ env
10
+ end
11
+ end
12
+
13
+ class ShortPrefixNode < Treetop::Runtime::SyntaxNode
14
+ def eval(env)
15
+ env[:raw] = text_value
16
+ servername.eval env
17
+ env
18
+ end
19
+ end
20
+
21
+ class LongPrefixNode < Treetop::Runtime::SyntaxNode
22
+ def eval(env)
23
+ env[:raw] = text_value
24
+ nick.eval env
25
+ user.v.eval env unless user.empty?
26
+ host.v.eval env unless host.empty?
27
+ env
28
+ end
29
+ end
30
+
31
+ class NumericReplyNode < Treetop::Runtime::SyntaxNode
32
+ def eval(env)
33
+ env[:command] = text_value
34
+ env[:numeric_reply] = text_value.to_i
35
+ env
36
+ end
37
+ end
38
+
39
+ class CommandWithParamsNode < Treetop::Runtime::SyntaxNode
40
+ def eval(env)
41
+ command.eval env
42
+ params.eval env unless params.empty?
43
+ env
44
+ end
45
+ end
46
+
47
+ class CommandNode < Treetop::Runtime::SyntaxNode
48
+ def eval(env)
49
+ env[:command] = text_value
50
+ env
51
+ end
52
+ end
53
+
54
+ class ParamsNode < Treetop::Runtime::SyntaxNode
55
+ def eval(env)
56
+ env[:options] = env[:params] = [middle_values, trailing_value].flatten.compact
57
+ env
58
+ end
59
+
60
+ def middle_values
61
+ middles.elements.map { |e| e.v.text_value }
62
+ end
63
+
64
+ def trailing_value
65
+ trailing.v.text_value unless trailing.empty?
66
+ end
67
+ end
68
+
69
+ class ServerNameNode < Treetop::Runtime::SyntaxNode
70
+ def eval(env)
71
+ env[:servername] = text_value
72
+ host.eval env
73
+ env
74
+ end
75
+ end
76
+
77
+ class HostNode < Treetop::Runtime::SyntaxNode
78
+ def eval(env)
79
+ env[:host] = text_value
80
+ env
81
+ end
82
+ end
83
+
84
+ class NickNode < Treetop::Runtime::SyntaxNode
85
+ def eval(env)
86
+ env[:nick] = text_value
87
+ env
88
+ end
89
+ end
90
+
91
+ class UserNode < Treetop::Runtime::SyntaxNode
92
+ def eval(env)
93
+ env[:user] = text_value
94
+ env
95
+ end
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,2 @@
1
+ require 'treetop'
2
+ require 'ircp/parser/message'
@@ -0,0 +1,13 @@
1
+ module Ircp
2
+ class Prefix
3
+ attr_accessor :raw, :servername, :nick, :user, :host
4
+
5
+ def initialize(options = {})
6
+ @raw = options[:raw]
7
+ @servername = options[:servername]
8
+ @nick = options[:nick]
9
+ @user = options[:user]
10
+ @host = options[:host]
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,3 @@
1
+ module Ircp
2
+ VERSION = "0.0.1"
3
+ end
data/lib/ircp.rb ADDED
@@ -0,0 +1,21 @@
1
+ require 'ircp/version'
2
+ require 'ircp/parser'
3
+ require 'ircp/message'
4
+
5
+ module Ircp
6
+ class ParseError < StandardError
7
+ end
8
+
9
+ def self.message_parser
10
+ @message_parser ||= Ircp::Parser::MessageParser.new
11
+ end
12
+
13
+ def self.parse(text)
14
+ node = message_parser.parse(text)
15
+ raise ParseError.new(message_parser.failure_reason) if node.nil?
16
+
17
+ env = node.eval
18
+ params = env.delete(:params)
19
+ Message.new *params, env
20
+ end
21
+ end
@@ -0,0 +1,71 @@
1
+ require 'spec_helper'
2
+
3
+ shared_context 'parse text' do |text|
4
+ before { @result = Ircp.parse(text) }
5
+ subject { @result }
6
+ let(:result) { @result }
7
+ end
8
+
9
+ shared_examples_for 'prefix for' do |options|
10
+ subject { result.prefix }
11
+ if options.nil?
12
+ it { should be_nil }
13
+ else
14
+ options.each do |key, value|
15
+ its(key) { should eq value }
16
+ end
17
+ end
18
+ end
19
+
20
+ shared_examples_for 'params for' do |*args|
21
+ subject { result.params }
22
+ args.each.with_index do |arg, index|
23
+ its([index]) { should eq arg }
24
+ end
25
+ end
26
+
27
+ describe Ircp::Parser do
28
+ describe '#parse' do
29
+ context 'PASS secretpasswordhere' do
30
+ include_context 'parse text', description
31
+ its(:command) { should eq 'PASS' }
32
+ it_should_behave_like 'prefix for', nil
33
+ it_should_behave_like 'params for', 'secretpasswordhere'
34
+ end
35
+
36
+ context ':testnick USER guest tolmoon tolsun :Ronnie Reagan' do
37
+ include_context 'parse text', description
38
+ its(:command) { should eq 'USER' }
39
+ it_should_behave_like 'prefix for', :servername => 'testnick'
40
+ it_should_behave_like 'params for', 'guest', 'tolmoon', 'tolsun', 'Ronnie Reagan'
41
+ end
42
+
43
+ context 'JOIN #foo,#bar fubar,foobar' do
44
+ include_context 'parse text', description
45
+ its(:command) { should eq 'JOIN' }
46
+ it_should_behave_like 'prefix for', nil
47
+ it_should_behave_like 'params for', '#foo,#bar', 'fubar,foobar'
48
+ end
49
+
50
+ context 'MODE &oulu +b *!*@*.edu' do
51
+ include_context 'parse text', description
52
+ its(:command) { should eq 'MODE' }
53
+ it_should_behave_like 'prefix for', nil
54
+ it_should_behave_like 'params for', '&oulu', '+b', '*!*@*.edu'
55
+ end
56
+
57
+ context ':Angel PRIVMSG Wiz :Hello are you receiving this message ?' do
58
+ include_context 'parse text', description
59
+ its(:command) { should eq 'PRIVMSG' }
60
+ it_should_behave_like 'prefix for', :servername => 'Angel'
61
+ it_should_behave_like 'params for', 'Wiz', 'Hello are you receiving this message ?'
62
+ end
63
+
64
+ context 'PRIVMSG #*.edu :NSFNet is undergoing work, expect interruptions' do
65
+ include_context 'parse text', description
66
+ its(:command) { should eq 'PRIVMSG' }
67
+ it_should_behave_like 'prefix for', nil
68
+ it_should_behave_like 'params for', '#*.edu', 'NSFNet is undergoing work, expect interruptions'
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,11 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'ircp'
5
+
6
+ # Requires supporting files with custom matchers and macros, etc,
7
+ # in ./support/ and its subdirectories.
8
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
9
+
10
+ RSpec.configure do |config|
11
+ end
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ircp
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - mashiro
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-01-26 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: treetop
16
+ requirement: &18199220 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *18199220
25
+ - !ruby/object:Gem::Dependency
26
+ name: rake
27
+ requirement: &18198800 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *18198800
36
+ - !ruby/object:Gem::Dependency
37
+ name: rspec
38
+ requirement: &18198380 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *18198380
47
+ description: irc parser
48
+ email:
49
+ - mail@mashiro.org
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - .gitignore
55
+ - .rspec
56
+ - Gemfile
57
+ - LICENSE
58
+ - README.md
59
+ - Rakefile
60
+ - ircp.gemspec
61
+ - lib/ircp.rb
62
+ - lib/ircp/message.rb
63
+ - lib/ircp/parser.rb
64
+ - lib/ircp/parser/message.treetop
65
+ - lib/ircp/parser/message_node_classes.rb
66
+ - lib/ircp/prefix.rb
67
+ - lib/ircp/version.rb
68
+ - spec/ircp/parser_spec.rb
69
+ - spec/spec_helper.rb
70
+ homepage: ''
71
+ licenses: []
72
+ post_install_message:
73
+ rdoc_options: []
74
+ require_paths:
75
+ - lib
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ none: false
78
+ requirements:
79
+ - - ! '>='
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ requirements: []
89
+ rubyforge_project:
90
+ rubygems_version: 1.8.10
91
+ signing_key:
92
+ specification_version: 3
93
+ summary: irc parser for ruby
94
+ test_files: []