@@ -36,31 +36,10 @@ def send(text, notice = false)
36
36
split_start = @bot . config . message_split_start || ""
37
37
split_end = @bot . config . message_split_end || ""
38
38
command = notice ? "NOTICE" : "PRIVMSG"
39
- max_bytesize = 510 - ":#{ @bot . mask } #{ command } #{ @name } :" . bytesize
40
- max_bytesize_without_end = max_bytesize - split_end . bytesize
39
+ prefix = ":#{ @bot . mask } #{ command } #{ @name } :"
41
40
42
41
text . lines . map ( &:chomp ) . each do |line |
43
- if line . bytesize <= max_bytesize
44
- @bot . irc . send ( "#{ command } #@name :#{ line } " )
45
- next
46
- end
47
-
48
- splitted = [ ]
49
- acc = 0
50
- acc_rune_sizes = line . each_char . map { |ch |
51
- acc += ch . bytesize
52
- }
53
- while line . bytesize > max_bytesize_without_end
54
- max_rune = acc_rune_sizes . rindex { |bs | bs <= max_bytesize_without_end }
55
- max_rune ||= 0
56
- r = line . rindex ( /\s / , max_rune ) || max_rune
57
- if r == 0
58
- r = 1
59
- end
60
- splitted << ( line [ 0 ...r ] + split_end . tr ( " " , "\u00A0 " ) )
61
- line = split_start . tr ( " " , "\u00A0 " ) + line [ r ..-1 ] . lstrip
62
- end
63
- splitted << line
42
+ splitted = split_message ( line , prefix , split_start , split_end )
64
43
65
44
splitted [ 0 , ( @bot . config . max_messages || splitted . size ) ] . each do |string |
66
45
string . tr! ( "\u00A0 " , " " ) # clean string from any non-breaking spaces
@@ -188,5 +167,28 @@ def <=>(other)
188
167
nil
189
168
end
190
169
end
170
+
171
+ private
172
+ def split_message ( msg , prefix , split_start , split_end )
173
+ max_bytesize = 510 - prefix . bytesize
174
+ max_bytesize_without_end = max_bytesize - split_end . bytesize
175
+
176
+ if msg . bytesize <= max_bytesize
177
+ return [ msg ]
178
+ end
179
+
180
+ splitted = [ ]
181
+ acc = 0
182
+ acc_rune_sizes = msg . each_char . map { |ch |
183
+ acc += ch . bytesize
184
+ }
185
+ while msg . bytesize > max_bytesize_without_end
186
+ max_rune = acc_rune_sizes . rindex { |bs | bs <= max_bytesize_without_end } || 0
187
+ r = [ msg . rindex ( /\s / , max_rune ) || max_rune , 1 ] . max
188
+ splitted << ( msg [ 0 ...r ] + split_end . tr ( " " , "\u00A0 " ) )
189
+ msg = split_start . tr ( " " , "\u00A0 " ) + msg [ r ..-1 ] . lstrip
190
+ end
191
+ splitted << msg
192
+ end
191
193
end
192
194
end
0 commit comments