root/lang/ruby/net-irc/trunk/lib/net/irc/message.rb @ 29362

Revision 29362, 1.9 kB (checked in by cho45, 4 years ago)

とりあえず 1.9 でテストをパスするように

  • Message#ctcp? を 1.9 対応に
  • 殆どテストの変更
Line 
1class Net::IRC::Message
2        include Net::IRC
3
4        class InvalidMessage < Net::IRC::IRCException; end
5
6        attr_reader :prefix, :command, :params
7
8        # Parse string and return new Message.
9        # If the string is invalid message, this method raises Net::IRC::Message::InvalidMessage.
10        def self.parse(str)
11                _, prefix, command, *rest = *PATTERN::MESSAGE_PATTERN.match(str)
12                raise InvalidMessage, "Invalid message: #{str.dump}" unless _
13
14                case
15                when rest[0] && !rest[0].empty?
16                        middle, trailer, = *rest
17                when rest[2] && !rest[2].empty?
18                        middle, trailer, = *rest[2, 2]
19                when rest[1]
20                        params  = []
21                        trailer = rest[1]
22                when rest[3]
23                        params  = []
24                        trailer = rest[3]
25                else
26                        params  = []
27                end
28
29                params ||= middle.split(/ /)[1..-1]
30                params << trailer if trailer
31
32                new(prefix, command, params)
33        end
34
35        def initialize(prefix, command, params)
36                @prefix  = Prefix.new(prefix.to_s)
37                @command = command
38                @params  = params
39        end
40
41        # Same as @params[n].
42        def [](n)
43                @params[n]
44        end
45
46        # Iterate params.
47        def each(&block)
48                @params.each(&block)
49        end
50
51        # Stringfy message to raw IRC message.
52        def to_s
53                str = ""
54
55                str << ":#{@prefix} " unless @prefix.empty?
56                str << @command
57
58                if @params
59                        f = false
60                        @params.each do |param|
61                                str << " "
62                                if !f && (param.size == 0 || / / =~ param || /^:/ =~ param)
63                                        str << ":#{param}"
64                                        f = true
65                                else
66                                        str << param
67                                end
68                        end
69                end
70
71                str << "\x0D\x0A"
72
73                str
74        end
75        alias to_str to_s
76
77        # Same as params.
78        def to_a
79                @params.dup
80        end
81
82        # If the message is CTCP, return true.
83        def ctcp?
84                message = @params[1]
85                message[0] == "\01"[0] && message[message.length-1] == "\01"[0]
86        end
87
88        def inspect
89                '#<%s:0x%x prefix:%s command:%s params:%s>' % [
90                        self.class,
91                        self.object_id,
92                        @prefix,
93                        @command,
94                        @params.inspect
95                ]
96        end
97
98        autoload :ModeParser, "net/irc/message/modeparser"
99        autoload :ServerConfig, "net/irc/message/serverconfig"
100        autoload :ISupportModeParser, "net/irc/message/isupportmodeparser"
101end # Message
102
Note: See TracBrowser for help on using the browser.