-
Notifications
You must be signed in to change notification settings - Fork 3
/
showxml.lic
223 lines (194 loc) · 6.61 KB
/
showxml.lic
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
=begin
Adds an XML window to Stormfront that will contain all XML sent by the server and by the client. Client XML will use
monsterbold.
author: LostRanger ([email protected])
game: any
version: 0.3 (2020-03-08)
changelog:
0.3 (2020-03-08):
* Change `puts` to `_respond` for Profanity and other users.
0.2 (2017-05-07):
* avoid whitespace collapse in ShowXML window.
0.1 (2017-05-07):
* initial release
=end
unless defined?(script.want_script_output)
echo 'Your version of Lich is too old for this script.'
exit
end
class ShowXMLScript
VERSION = '0.3 (2020-03-08)'
BOOLEAN_MAP = {
'yes' => true, 'true' => true, 'on' => true, '1' => true,
'no' => false, 'false' => false, 'off' => false, '0' => false,
}
include REXML::StreamListener
def initialize(script)
script.want_script_output = false
script.want_upstream = true
script.want_downstream = false
script.want_downstream_xml = true
@script = script
@window_id = 'showxml_debug_window'
@window_title = 'XML Stream'
@window_location = 'center'
@options = {
:show => true, :bold => true,
:resident => false, :clear => false, :high => false, :time => false, :save => false
}
@output = Queue.new
@thread = nil
@stack = []
end
def help_command
script = "#{$lich_char}#{@script.name}"
spacer = ''.ljust(script.length, ' ')
respond "#{script} version #{VERSION}"
respond "Usage:"
respond
respond " #{script} HELP Shows this help text."
respond
respond " #{script} [<options...>] Begins capturing XML and displaying it in a stream window."
respond
respond 'Options:'
respond ' It is not neccessary to specify all (or any!) options'
respond
respond ' id=showxml_debug_window Stream window ID to use.'
respond
respond ' title="XML Stream" Title for window.'
respond
respond ' location=center Location for window.'
respond
respond ' [no]resident Resident windows persist after being closed. (Default: noresident)'
respond ' If the window is resident, showXML will continue logging after it is closed'
respond
respond ' [no]show (Don\'t) force the window to become visible. (Default: show)'
respond
respond ' [no]clear (Don\'t) clear the window contents on startup. (Default: noclear)'
respond
respond ' [no]bold (Don\'t) use monsterbold for client commands. (Default: bold)'
respond
respond ' [no]high[lights] (Don\'t) process highlight strings. (Default: nohigh)'
respond
respond ' [no]time[stamps] (Don\'t) timestamp messages. (Default: notime)'
respond
respond ' [no]save (Don\'t) save the window configuration. (Default: nosave)'
end
def run(vars)
def commanderror(msg)
echo msg
echo "See #{$lich_char}#{@script.name} HELP"
nil
end
*args = vars[1..-1]
unless args.length > 0
echo "Using default settings. See #{$lich_char}#{@script.name} HELP if you wanted more information."
end
seen = Set.new
args.each{|arg|
next unless arg and arg != ''
if arg.downcase == 'help'
help_command
return
end
setting, value = arg.split('=', 2)
setting.downcase!
setting = 'high' if setting.length > 4 and 'highlights'.start_with?(setting)
setting = 'time' if setting.length > 4 and 'timestamps'.start_with?(setting)
return commanderror("Option '#{setting}' was repeated") unless seen.add?(setting)
if value.nil?
if setting.start_with?('no')
setting = setting[2..-1]
value = false
else
value = true
end
end
case setting
when 'id'
return commanderror('Stream ID must be a string.') unless value.instance_of?(String)
return commanderror('Stream ID must have a nonzero length.') unless value.length > 0
@window_id = value
when 'title'
return commanderror('Window title must be a string.') unless value.instance_of?(String)
return commanderror('Window title must have a nonzero length.') unless value.length > 0
@window_title = value
when 'location'
return commanderror('Window location must be a string.') unless value.instance_of?(String)
return commanderror('Window location must have a nonzero length.') unless value.length > 0
@window_location = value
else
return commanderror("Unknown setting '#{setting}'") unless @options.include?(setting.intern)
if value.instance_of?(String)
valuetext = value
value = BOOLEAN_MAP[value.downcase.strip]
return commanderror("Invalid boolean option '#{valuetext}'") if valuetext.nil?
end
@options[setting.intern] = value
end
}
before_dying{
Thread.kill(@thread)
DownstreamHook.remove('showxml_downstream_hook')
}
# Server hook
DownstreamHook.add('showxml_downstream_hook', proc{|data|
@output.push([:server, data.dup])
data
})
# Client hook
@thread = Thread.new(@output) {|q|
while true
@output.push([:client, upstream_get])
end
}
# Create the window
buffer = []
xml = REXML::Element.new('streamWindow')
xml.attributes['id'] = @window_id
xml.attributes['title'] = @window_title
xml.attributes['location'] = @window_location
# xml.attributes['subtitle'] = " - #{@script.name} #{VERSION}"
xml.attributes['ifClosed'] = ''
xml.attributes['scroll'] = 'auto'
xml.attributes['resident'] = @options[:resident].to_s
xml.attributes['save'] = '' unless @options[:save]
buffer << xml.to_s
if @options[:clear]
xml = REXML::Element.new('clearStream')
xml.attributes['id'] = @window_id
buffer << xml.to_s
end
if @options[:show]
xml = REXML::Element.new('exposeStream')
xml.attributes['id'] = @window_id
buffer << xml.to_s
end
_respond buffer.join('')
# Determine wrapper commands for writing to the window
before = "<pushStream id='#{@window_id}' /><output class='mono'/>"
after = "<popStream id='#{@window_id}' /><output class=''/>\n" # TODO: See if we need to output.. output... or SF is smart enough
if @options[:resident]
exit_command = nil
else
exit_command = "<c>_swclose s#{@window_id}"
end
echo @options.inspect
while true
source, message = @output.pop
output = REXML::Text.new(message, respect_whitespace: true).to_s
if source == :client
if exit_command and exit_command == message
break
end
if @options[:bold]
output = "<b>#{output}</b>\n"
end
end
_respond "#{before}#{output}#{after}"
end
Thread.kill(@thread)
DownstreamHook.remove('showxml_downstream_hook')
end
end
ShowXMLScript.new(script).run(script.vars)