Skip to content

Commit 4604f8c

Browse files
committed
Move cowsay to Rex::Text so that everyone can enjoy it ;)
1 parent 15cfa92 commit 4604f8c

File tree

2 files changed

+51
-35
lines changed

2 files changed

+51
-35
lines changed

lib/rex/text.rb

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1126,6 +1126,56 @@ def self.hexify(str, col = DefaultWrap, line_start = '', line_end = '', buf_star
11261126
return output
11271127
end
11281128

1129+
#
1130+
# Converts a string to one similar to what would be used by cowsay(1), a UNIX utility for
1131+
# displaying text as if it was coming from an ASCII-cow's mouth:
1132+
#
1133+
# __________________
1134+
# < the cow says moo >
1135+
# ------------------
1136+
# \ ^__^
1137+
# \ (oo)\_______
1138+
# (__)\ )\/\
1139+
# ||----w |
1140+
# || ||
1141+
#
1142+
# @param text [String] The string to cowsay
1143+
# @param width [Fixnum] Width of the cow's cloud. Default's to cowsay(1)'s default, 39.
1144+
def self.cowsay(text, width=39)
1145+
# cowsay(1) chunks a message up into 39-byte chunks and wraps it in '| ' and ' |'
1146+
# Rex::Text.wordwrap(text, 0, 39, ' |', '| ') almost does this, but won't
1147+
# split a word that has > 39 characters in it which results in oddly formed
1148+
# text in the cowsay banner, so just do it by hand. This big mess wraps
1149+
# the provided text in an ASCII-cloud and then makes it look like the cloud
1150+
# is a thought/word coming from the ASCII-cow. Each line in the
1151+
# ASCII-cloud is no more than the specified number-characters long, and the cloud corners are
1152+
# made to look rounded
1153+
text_lines = text.scan(Regexp.new(".{1,#{width}}"))
1154+
max_length = text_lines.map(&:size).sort.last
1155+
cloud_parts = []
1156+
cloud_parts << " #{'_' * (max_length + 2)} "
1157+
if text_lines.size == 1
1158+
cloud_parts << "< #{text} >"
1159+
else
1160+
cloud_parts << "/ #{text_lines.first.ljust(max_length, ' ')} \\"
1161+
if text_lines.size > 2
1162+
text_lines[1, text_lines.length - 2].each do |line|
1163+
cloud_parts << "| #{line.ljust(max_length, ' ')} |"
1164+
end
1165+
end
1166+
cloud_parts << "\\ #{text_lines.last.ljust(max_length, ' ')} /"
1167+
end
1168+
cloud_parts << " #{'-' * (max_length + 2)} "
1169+
cloud_parts << <<EOS
1170+
\\ ,__,
1171+
\\ (oo)____
1172+
(__) )\\
1173+
||--|| *
1174+
EOS
1175+
cloud_parts.join("\n")
1176+
end
1177+
1178+
11291179
##
11301180
#
11311181
# Transforms

modules/post/multi/general/wall.rb

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -33,40 +33,6 @@ def initialize(info = {})
3333
], self.class)
3434
end
3535

36-
def cowsay(text)
37-
# cowsay(1) chunks a message up into 39-byte chunks and wraps it in '| ' and ' |'
38-
# Rex::Text.wordwrap(text, 0, 39, ' |', '| ') almost does this, but won't
39-
# split a word that has > 39 characters in it which results in oddly formed
40-
# text in the cowsay banner, so just do it by hand. This big mess wraps
41-
# the provided text in an ASCII-cloud and then makes it look like the cloud
42-
# is a thought/word coming from the ASCII-cow. Each line in the
43-
# ASCII-cloud is no more than 34-characters long, and the cloud corners are
44-
# made to look rounded
45-
text_lines = text.scan(/.{1,34}/)
46-
max_length = text_lines.map(&:size).sort.last
47-
cloud_parts = []
48-
cloud_parts << " #{'_' * (max_length + 2)} "
49-
if text_lines.size == 1
50-
cloud_parts << "< #{text} >"
51-
else
52-
cloud_parts << "/ #{text_lines.first.ljust(max_length, ' ')} \\"
53-
if text_lines.size > 2
54-
text_lines[1, text_lines.length - 2].each do |line|
55-
cloud_parts << "| #{line.ljust(max_length, ' ')} |"
56-
end
57-
end
58-
cloud_parts << "\\ #{text_lines.last.ljust(max_length, ' ')} /"
59-
end
60-
cloud_parts << " #{'-' * (max_length + 2)} "
61-
cloud_parts << <<EOS
62-
\\ ,__,
63-
\\ (oo)____
64-
(__) )\\
65-
||--|| *
66-
EOS
67-
cloud_parts.join("\n")
68-
end
69-
7036
def users
7137
datastore['USERS'] ? datastore['USERS'].split(/\s*,\s*/) : nil
7238
end
@@ -78,7 +44,7 @@ def message
7844
text = datastore['MESSAGE']
7945
end
8046

81-
datastore['COWSAY'] ? cowsay(text) : text
47+
datastore['COWSAY'] ? Rex::Text.cowsay(text, 100) : text
8248
end
8349

8450
def run

0 commit comments

Comments
 (0)