forked from elanthia-online/lich-5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lich.rbw
executable file
·5650 lines (4975 loc) · 179 KB
/
lich.rbw
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env ruby
# encoding: US-ASCII
#####
# Copyright (C) 2005-2006 Murray Miron
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#
# Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# Neither the name of the organization nor the names of its contributors
# may be used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#####
# Lich is maintained by Matt Lowe ([email protected])
# Lich version 5 and higher maintained by Elanthia Online and only supports GTK3 Ruby
require 'time'
require 'socket'
require 'rexml/document'
require 'rexml/streamlistener'
require 'stringio'
require 'zlib'
require 'drb'
require 'resolv'
require 'digest/md5'
require 'json'
# TODO: Move all local requires to top of file
require_relative('./lib/constants')
require 'lib/version'
require 'lib/lich'
require 'lib/init'
require 'lib/front-end'
# TODO: Need to split out initiatilzation functions to move require to top of file
require 'lib/gtk'
require 'lib/gui-login'
class NilClass
def dup
nil
end
def method_missing(*args)
nil
end
def split(*val)
Array.new
end
def to_s
""
end
def strip
""
end
def +(val)
val
end
def closed?
true
end
end
class Numeric
def as_time
sprintf("%d:%02d:%02d", (self / 60).truncate, self.truncate % 60, ((self % 1) * 60).truncate)
end
def with_commas
self.to_s.reverse.scan(/(?:\d*\.)?\d{1,3}-?/).join(',').reverse
end
end
class String
@@elevated_untaint = proc { |what| what.orig_untaint }
alias :orig_untaint :untaint
def untaint
@@elevated_untaint.call(self)
end
def to_s
self.dup
end
def stream
@stream
end
def stream=(val)
@stream ||= val
end
end
class StringProc
def initialize(string)
@string = string
@string.untaint
end
def kind_of?(type)
Proc.new {}.kind_of? type
end
def class
Proc
end
def call(*a)
proc { begin; $SAFE = 3; rescue; nil; end; eval(@string) }.call
end
def _dump(d = nil)
@string
end
def inspect
"StringProc.new(#{@string.inspect})"
end
def to_json(*args)
";e #{_dump}".to_json(args)
end
end
class SynchronizedSocket
def initialize(o)
@delegate = o
@mutex = Mutex.new
self
end
def puts(*args, &block)
@mutex.synchronize {
@delegate.puts *args, &block
}
end
def puts_if(*args)
@mutex.synchronize {
if yield
@delegate.puts *args
return true
else
return false
end
}
end
def write(*args, &block)
@mutex.synchronize {
@delegate.write *args, &block
}
end
def method_missing(method, *args, &block)
@delegate.__send__ method, *args, &block
end
end
class LimitedArray < Array
attr_accessor :max_size
def initialize(size = 0, obj = nil)
@max_size = 200
super
end
def push(line)
self.shift while self.length >= @max_size
super
end
def shove(line)
push(line)
end
def history
Array.new
end
end
require_relative("./lib/xmlparser.rb")
class UpstreamHook
@@upstream_hooks ||= Hash.new
def UpstreamHook.add(name, action)
unless action.class == Proc
echo "UpstreamHook: not a Proc (#{action})"
return false
end
@@upstream_hooks[name] = action
end
def UpstreamHook.run(client_string)
for key in @@upstream_hooks.keys
begin
client_string = @@upstream_hooks[key].call(client_string)
rescue
@@upstream_hooks.delete(key)
respond "--- Lich: UpstreamHook: #{$!}"
respond $!.backtrace.first
end
return nil if client_string.nil?
end
return client_string
end
def UpstreamHook.remove(name)
@@upstream_hooks.delete(name)
end
def UpstreamHook.list
@@upstream_hooks.keys.dup
end
end
class DownstreamHook
@@downstream_hooks ||= Hash.new
def DownstreamHook.add(name, action)
unless action.class == Proc
echo "DownstreamHook: not a Proc (#{action})"
return false
end
@@downstream_hooks[name] = action
end
def DownstreamHook.run(server_string)
for key in @@downstream_hooks.keys
return nil if server_string.nil?
begin
server_string = @@downstream_hooks[key].call(server_string.dup) if server_string.is_a?(String)
rescue
@@downstream_hooks.delete(key)
respond "--- Lich: DownstreamHook: #{$!}"
respond $!.backtrace.first
end
end
return server_string
end
def DownstreamHook.remove(name)
@@downstream_hooks.delete(name)
end
def DownstreamHook.list
@@downstream_hooks.keys.dup
end
end
module Setting
@@load = proc { |args|
unless script = Script.current
respond '--- error: Setting.load: calling script is unknown'
respond $!.backtrace[0..2]
next nil
end
if script.class == ExecScript
respond "--- Lich: error: Setting.load: exec scripts can't have settings"
respond $!.backtrace[0..2]
exit
end
if args.empty?
respond '--- error: Setting.load: no setting specified'
respond $!.backtrace[0..2]
exit
end
if args.any? { |a| a.class != String }
respond "--- Lich: error: Setting.load: non-string given as setting name"
respond $!.backtrace[0..2]
exit
end
values = Array.new
for setting in args
begin
v = Lich.db.get_first_value('SELECT value FROM script_setting WHERE script=? AND name=?;', script.name.encode('UTF-8'), setting.encode('UTF-8'))
rescue SQLite3::BusyException
sleep 0.1
retry
end
if v.nil?
values.push(v)
else
begin
values.push(Marshal.load(v))
rescue
respond "--- Lich: error: Setting.load: #{$!}"
respond $!.backtrace[0..2]
exit
end
end
end
if args.length == 1
next values[0]
else
next values
end
}
@@save = proc { |hash|
unless script = Script.current
respond '--- error: Setting.save: calling script is unknown'
respond $!.backtrace[0..2]
next nil
end
if script.class == ExecScript
respond "--- Lich: error: Setting.load: exec scripts can't have settings"
respond $!.backtrace[0..2]
exit
end
if hash.class != Hash
respond "--- Lich: error: Setting.save: invalid arguments: use Setting.save('setting1' => 'value1', 'setting2' => 'value2')"
respond $!.backtrace[0..2]
exit
end
if hash.empty?
next nil
end
if hash.keys.any? { |k| k.class != String }
respond "--- Lich: error: Setting.save: non-string given as a setting name"
respond $!.backtrace[0..2]
exit
end
if hash.length > 1
begin
Lich.db.execute('BEGIN')
rescue SQLite3::BusyException
sleep 0.1
retry
end
end
hash.each { |setting, value|
begin
if value.nil?
begin
Lich.db.execute('DELETE FROM script_setting WHERE script=? AND name=?;', script.name.encode('UTF-8'), setting.encode('UTF-8'))
rescue SQLite3::BusyException
sleep 0.1
retry
end
else
v = SQLite3::Blob.new(Marshal.dump(value))
begin
Lich.db.execute('INSERT OR REPLACE INTO script_setting(script,name,value) VALUES(?,?,?);', script.name.encode('UTF-8'), setting.encode('UTF-8'), v)
rescue SQLite3::BusyException
sleep 0.1
retry
end
end
rescue SQLite3::BusyException
sleep 0.1
retry
end
}
if hash.length > 1
begin
Lich.db.execute('END')
rescue SQLite3::BusyException
sleep 0.1
retry
end
end
true
}
@@list = proc {
unless script = Script.current
respond '--- error: Setting: unknown calling script'
next nil
end
if script.class == ExecScript
respond "--- Lich: error: Setting.load: exec scripts can't have settings"
respond $!.backtrace[0..2]
exit
end
begin
rows = Lich.db.execute('SELECT name FROM script_setting WHERE script=?;', script.name.encode('UTF-8'))
rescue SQLite3::BusyException
sleep 0.1
retry
end
if rows
# fixme
next rows.inspect
else
next nil
end
}
def Setting.load(*args)
@@load.call(args)
end
def Setting.save(hash)
@@save.call(hash)
end
def Setting.list
@@list.call
end
end
module GameSetting
def GameSetting.load(*args)
Setting.load(args.collect { |a| "#{XMLData.game}:#{a}" })
end
def GameSetting.save(hash)
game_hash = Hash.new
hash.each_pair { |k, v| game_hash["#{XMLData.game}:#{k}"] = v }
Setting.save(game_hash)
end
end
module CharSetting
def CharSetting.load(*args)
Setting.load(args.collect { |a| "#{XMLData.game}:#{XMLData.name}:#{a}" })
end
def CharSetting.save(hash)
game_hash = Hash.new
hash.each_pair { |k, v| game_hash["#{XMLData.game}:#{XMLData.name}:#{k}"] = v }
Setting.save(game_hash)
end
end
module Settings
settings = Hash.new
md5_at_load = Hash.new
mutex = Mutex.new
@@settings = proc { |scope|
unless script = Script.current
respond '--- error: Settings: unknown calling script'
next nil
end
unless scope =~ /^#{XMLData.game}\:#{XMLData.name}$|^#{XMLData.game}$|^\:$/
respond '--- error: Settings: invalid scope'
next nil
end
mutex.synchronize {
unless settings[script.name] and settings[script.name][scope]
begin
_hash = Lich.db.get_first_value('SELECT hash FROM script_auto_settings WHERE script=? AND scope=?;', script.name.encode('UTF-8'), scope.encode('UTF-8'))
rescue SQLite3::BusyException
sleep 0.1
retry
end
settings[script.name] ||= Hash.new
if _hash.nil?
settings[script.name][scope] = Hash.new
else
begin
hash = Marshal.load(_hash)
rescue
respond "--- Lich: error: #{$!}"
respond $!.backtrace[0..1]
exit
end
settings[script.name][scope] = hash
end
md5_at_load[script.name] ||= Hash.new
md5_at_load[script.name][scope] = Digest::MD5.hexdigest(settings[script.name][scope].to_s)
end
}
settings[script.name][scope]
}
@@save = proc {
mutex.synchronize {
sql_began = false
settings.each_pair { |script_name, scopedata|
scopedata.each_pair { |scope, data|
if Digest::MD5.hexdigest(data.to_s) != md5_at_load[script_name][scope]
unless sql_began
begin
Lich.db.execute('BEGIN')
rescue SQLite3::BusyException
sleep 0.1
retry
end
sql_began = true
end
blob = SQLite3::Blob.new(Marshal.dump(data))
begin
Lich.db.execute('INSERT OR REPLACE INTO script_auto_settings(script,scope,hash) VALUES(?,?,?);', script_name.encode('UTF-8'), scope.encode('UTF-8'), blob)
rescue SQLite3::BusyException
sleep 0.1
retry
rescue
respond "--- Lich: error: #{$!}"
respond $!.backtrace[0..1]
next
end
end
}
unless Script.running?(script_name)
settings.delete(script_name)
md5_at_load.delete(script_name)
end
}
if sql_began
begin
Lich.db.execute('END')
rescue SQLite3::BusyException
sleep 0.1
retry
end
end
}
}
Thread.new {
loop {
sleep 300
begin
@@save.call
rescue
Lich.log "error: #{$!}\n\t#{$!.backtrace.join("\n\t")}"
respond "--- Lich: error: #{$!}\n\t#{$!.backtrace[0..1].join("\n\t")}"
end
}
}
def Settings.[](name)
@@settings.call(':')[name]
end
def Settings.[]=(name, value)
@@settings.call(':')[name] = value
end
def Settings.to_hash(scope = ':')
@@settings.call(scope)
end
def Settings.char
@@settings.call("#{XMLData.game}:#{XMLData.name}")
end
def Settings.save
@@save.call
end
end
module GameSettings
def GameSettings.[](name)
Settings.to_hash(XMLData.game)[name]
end
def GameSettings.[]=(name, value)
Settings.to_hash(XMLData.game)[name] = value
end
def GameSettings.to_hash
Settings.to_hash(XMLData.game)
end
end
module CharSettings
def CharSettings.[](name)
Settings.to_hash("#{XMLData.game}:#{XMLData.name}")[name]
end
def CharSettings.[]=(name, value)
Settings.to_hash("#{XMLData.game}:#{XMLData.name}")[name] = value
end
def CharSettings.to_hash
Settings.to_hash("#{XMLData.game}:#{XMLData.name}")
end
end
module Vars
@@vars = Hash.new
md5 = nil
mutex = Mutex.new
@@loaded = false
@@load = proc {
mutex.synchronize {
unless @@loaded
begin
h = Lich.db.get_first_value('SELECT hash FROM uservars WHERE scope=?;', "#{XMLData.game}:#{XMLData.name}".encode('UTF-8'))
rescue SQLite3::BusyException
sleep 0.1
retry
end
if h
begin
hash = Marshal.load(h)
hash.each { |k, v| @@vars[k] = v }
md5 = Digest::MD5.hexdigest(hash.to_s)
rescue
respond "--- Lich: error: #{$!}"
respond $!.backtrace[0..2]
end
end
@@loaded = true
end
}
nil
}
@@save = proc {
mutex.synchronize {
if @@loaded
if Digest::MD5.hexdigest(@@vars.to_s) != md5
md5 = Digest::MD5.hexdigest(@@vars.to_s)
blob = SQLite3::Blob.new(Marshal.dump(@@vars))
begin
Lich.db.execute('INSERT OR REPLACE INTO uservars(scope,hash) VALUES(?,?);', "#{XMLData.game}:#{XMLData.name}".encode('UTF-8'), blob)
rescue SQLite3::BusyException
sleep 0.1
retry
end
end
end
}
nil
}
Thread.new {
loop {
sleep 300
begin
@@save.call
rescue
Lich.log "error: #{$!}\n\t#{$!.backtrace.join("\n\t")}"
respond "--- Lich: error: #{$!}\n\t#{$!.backtrace[0..1].join("\n\t")}"
end
}
}
def Vars.[](name)
@@load.call unless @@loaded
@@vars[name]
end
def Vars.[]=(name, val)
@@load.call unless @@loaded
if val.nil?
@@vars.delete(name)
else
@@vars[name] = val
end
end
def Vars.list
@@load.call unless @@loaded
@@vars.dup
end
def Vars.save
@@save.call
end
def Vars.method_missing(arg1, arg2 = '')
@@load.call unless @@loaded
if arg1[-1, 1] == '='
if arg2.nil?
@@vars.delete(arg1.to_s.chop)
else
@@vars[arg1.to_s.chop] = arg2
end
else
@@vars[arg1.to_s]
end
end
end
#
# script bindings are convoluted, but don't change them without testing if:
# class methods such as Script.start and ExecScript.start become accessible without specifying the class name (which is just a syptom of a problem that will break scripts)
# local variables become shared between scripts
# local variable 'file' is shared between scripts, even though other local variables aren't
# defined methods are instantly inaccessible
# also, don't put 'untrusted' in the name of the untrusted binding; it shows up in error messages and makes people think the error is caused by not trusting the script
#
class Scripting
def script
Proc.new {}.binding
end
end
def _script
Proc.new {}.binding
end
TRUSTED_SCRIPT_BINDING = proc { _script }
class Script
@@elevated_script_start = proc { |args|
if args.empty?
# fixme: error
next nil
elsif args[0].class == String
script_name = args[0]
if args[1]
if args[1].class == String
script_args = args[1]
if args[2]
if args[2].class == Hash
options = args[2]
else
# fixme: error
next nil
end
end
elsif args[1].class == Hash
options = args[1]
script_args = (options[:args] || String.new)
else
# fixme: error
next nil
end
else
options = Hash.new
end
elsif args[0].class == Hash
options = args[0]
if options[:name]
script_name = options[:name]
else
# fixme: error
next nil
end
script_args = (options[:args] || String.new)
end
# fixme: look in wizard script directory
# fixme: allow subdirectories?
file_list = Dir.children(File.join(SCRIPT_DIR, "custom")).sort_by{ |fn| fn.sub(/[.](lic|rb|cmd|wiz)$/, '') }.map{ |s| s.prepend("/custom/") } + Dir.children(SCRIPT_DIR).sort_by{|fn| fn.sub(/[.](lic|rb|cmd|wiz)$/, '')}
if file_name = (file_list.find { |val| val =~ /^(?:\/custom\/)?#{Regexp.escape(script_name)}\.(?:lic|rb|cmd|wiz)(?:\.gz|\.Z)?$/ || val =~ /^(?:\/custom\/)?#{Regexp.escape(script_name)}\.(?:lic|rb|cmd|wiz)(?:\.gz|\.Z)?$/i } || file_list.find { |val| val =~ /^(?:\/custom\/)?#{Regexp.escape(script_name)}[^.]+\.(?i:lic|rb|cmd|wiz)(?:\.gz|\.Z)?$/ } || file_list.find { |val| val =~ /^(?:\/custom\/)?#{Regexp.escape(script_name)}[^.]+\.(?:lic|rb|cmd|wiz)(?:\.gz|\.Z)?$/i })
script_name = file_name.sub(/\..{1,3}$/, '')
end
file_list = nil
if file_name.nil?
respond "--- Lich: could not find script '#{script_name}' in directory #{SCRIPT_DIR} or #{SCRIPT_DIR}/custom"
next nil
end
if (options[:force] != true) and (Script.running + Script.hidden).find { |s| s.name =~ /^#{Regexp.escape(script_name.sub('/custom/', ''))}$/i }
respond "--- Lich: #{script_name} is already running (use #{$clean_lich_char}force [scriptname] if desired)."
next nil
end
begin
if file_name =~ /\.(?:cmd|wiz)(?:\.gz)?$/i
trusted = false
script_obj = WizardScript.new("#{SCRIPT_DIR}/#{file_name}", script_args)
else
if script_obj.labels.length > 1
trusted = false
elsif proc { begin; $SAFE = 3; true; rescue; false; end }.call
begin
trusted = Lich.db.get_first_value('SELECT name FROM trusted_scripts WHERE name=?;', script_name.encode('UTF-8'))
rescue SQLite3::BusyException
sleep 0.1
retry
end
else
trusted = true
end
script_obj = Script.new(:file => "#{SCRIPT_DIR}/#{file_name}", :args => script_args, :quiet => options[:quiet])
end
if trusted
script_binding = TRUSTED_SCRIPT_BINDING.call
else
script_binding = Scripting.new.script
end
rescue
respond "--- Lich: error: #{$!}\n\t#{$!.backtrace.join("\n\t")}"
next nil
end
unless script_obj
respond "--- Lich: error: failed to start script (#{script_name})"
next nil
end
script_obj.quiet = true if options[:quiet]
new_thread = Thread.new {
100.times { break if Script.current == script_obj; sleep 0.01 }
if script = Script.current
eval('script = Script.current', script_binding, script.name)
Thread.current.priority = 1
respond("--- Lich: #{script.name} active.") unless script.quiet
if trusted
begin
eval(script.labels[script.current_label].to_s, script_binding, script.name)
rescue SystemExit
nil
rescue SyntaxError
respond "--- Lich: error: #{$!}\n\t#{$!.backtrace[0..1].join("\n\t")}"
Lich.log "error: #{$!}\n\t#{$!.backtrace.join("\n\t")}"
rescue ScriptError
respond "--- Lich: error: #{$!}\n\t#{$!.backtrace[0..1].join("\n\t")}"
Lich.log "error: #{$!}\n\t#{$!.backtrace.join("\n\t")}"
rescue NoMemoryError
respond "--- Lich: error: #{$!}\n\t#{$!.backtrace[0..1].join("\n\t")}"
Lich.log "error: #{$!}\n\t#{$!.backtrace.join("\n\t")}"
rescue LoadError
respond "--- Lich: error: #{$!}\n\t#{$!.backtrace[0..1].join("\n\t")}"
Lich.log "error: #{$!}\n\t#{$!.backtrace.join("\n\t")}"
rescue SecurityError
respond "--- Lich: error: #{$!}\n\t#{$!.backtrace[0..1].join("\n\t")}"
Lich.log "error: #{$!}\n\t#{$!.backtrace.join("\n\t")}"
rescue ThreadError
respond "--- Lich: error: #{$!}\n\t#{$!.backtrace[0..1].join("\n\t")}"
Lich.log "error: #{$!}\n\t#{$!.backtrace.join("\n\t")}"
rescue SystemStackError
respond "--- Lich: error: #{$!}\n\t#{$!.backtrace[0..1].join("\n\t")}"
Lich.log "error: #{$!}\n\t#{$!.backtrace.join("\n\t")}"
rescue Exception
if $! == JUMP
retry if Script.current.get_next_label != JUMP_ERROR
respond "--- label error: `#{Script.current.jump_label}' was not found, and no `LabelError' label was found!"
respond $!.backtrace.first
Lich.log "label error: `#{Script.current.jump_label}' was not found, and no `LabelError' label was found!\n\t#{$!.backtrace.join("\n\t")}"
Script.current.kill
else
respond "--- Lich: error: #{$!}\n\t#{$!.backtrace[0..1].join("\n\t")}"
Lich.log "error: #{$!}\n\t#{$!.backtrace.join("\n\t")}"
end
rescue
respond "--- Lich: error: #{$!}\n\t#{$!.backtrace[0..1].join("\n\t")}"
Lich.log "error: #{$!}\n\t#{$!.backtrace.join("\n\t")}"
ensure
Script.current.kill
end
else
begin
while (script = Script.current) and script.current_label
proc { foo = script.labels[script.current_label]; foo.untaint; begin; $SAFE = 3; rescue; nil; end; eval(foo, script_binding, script.name, 1) }.call
Script.current.get_next_label
end
rescue SystemExit
nil
rescue SyntaxError
respond "--- Lich: error: #{$!}\n\t#{$!.backtrace[0..1].join("\n\t")}"
Lich.log "error: #{$!}\n\t#{$!.backtrace.join("\n\t")}"
rescue ScriptError
respond "--- Lich: error: #{$!}\n\t#{$!.backtrace[0..1].join("\n\t")}"
Lich.log "error: #{$!}\n\t#{$!.backtrace.join("\n\t")}"
rescue NoMemoryError
respond "--- Lich: error: #{$!}\n\t#{$!.backtrace[0..1].join("\n\t")}"
Lich.log "error: #{$!}\n\t#{$!.backtrace.join("\n\t")}"
rescue LoadError
respond "--- Lich: error: #{$!}\n\t#{$!.backtrace[0..1].join("\n\t")}"
Lich.log "error: #{$!}\n\t#{$!.backtrace.join("\n\t")}"
rescue SecurityError
respond "--- Lich: error: #{$!}\n\t#{$!.backtrace[0..1].join("\n\t")}"
if name = Script.current.name
respond "--- Lich: review this script (#{name}) to make sure it isn't malicious, and type #{$clean_lich_char}trust #{name}"
end
Lich.log "error: #{$!}\n\t#{$!.backtrace.join("\n\t")}"
rescue ThreadError
respond "--- Lich: error: #{$!}\n\t#{$!.backtrace[0..1].join("\n\t")}"
Lich.log "error: #{$!}\n\t#{$!.backtrace.join("\n\t")}"
rescue SystemStackError
respond "--- Lich: error: #{$!}\n\t#{$!.backtrace[0..1].join("\n\t")}"
Lich.log "error: #{$!}\n\t#{$!.backtrace.join("\n\t")}"
rescue Exception
if $! == JUMP
retry if Script.current.get_next_label != JUMP_ERROR
respond "--- label error: `#{Script.current.jump_label}' was not found, and no `LabelError' label was found!"
respond $!.backtrace.first
Lich.log "label error: `#{Script.current.jump_label}' was not found, and no `LabelError' label was found!\n\t#{$!.backtrace.join("\n\t")}"
Script.current.kill
else
respond "--- Lich: error: #{$!}\n\t#{$!.backtrace[0..1].join("\n\t")}"
Lich.log "error: #{$!}\n\t#{$!.backtrace.join("\n\t")}"
end
rescue
respond "--- Lich: error: #{$!}\n\t#{$!.backtrace[0..1].join("\n\t")}"
Lich.log "error: #{$!}\n\t#{$!.backtrace.join("\n\t")}"
ensure
Script.current.kill
end
end
else
respond '--- error: out of cheese'
end
}
script_obj.thread_group.add(new_thread)
script_obj
}
@@elevated_exists = proc { |script_name|
if script_name =~ /\\|\//
nil
elsif script_name =~ /\.(?:lic|lich|rb|cmd|wiz)(?:\.gz)?$/i
File.exists?("#{SCRIPT_DIR}/#{script_name}") || File.exists?("#{SCRIPT_DIR}/custom/#{script_name}")
else
File.exists?("#{SCRIPT_DIR}/#{script_name}.lic") || File.exists?("#{SCRIPT_DIR}/custom/#{script_name}.lic") ||
File.exists?("#{SCRIPT_DIR}/#{script_name}.lich") || File.exists?("#{SCRIPT_DIR}/custom/#{script_name}.lich") ||
File.exists?("#{SCRIPT_DIR}/#{script_name}.rb") || File.exists?("#{SCRIPT_DIR}/custom/#{script_name}.rb") ||
File.exists?("#{SCRIPT_DIR}/#{script_name}.cmd") || File.exists?("#{SCRIPT_DIR}/custom/#{script_name}.cmd") ||
File.exists?("#{SCRIPT_DIR}/#{script_name}.wiz") || File.exists?("#{SCRIPT_DIR}/custom/#{script_name}.wiz") ||
File.exists?("#{SCRIPT_DIR}/#{script_name}.lic.gz") || File.exists?("#{SCRIPT_DIR}/custom/#{script_name}.lic.gz") ||
File.exists?("#{SCRIPT_DIR}/#{script_name}.rb.gz") || File.exists?("#{SCRIPT_DIR}/custom/#{script_name}.rb.gz") ||
File.exists?("#{SCRIPT_DIR}/#{script_name}.cmd.gz") || File.exists?("#{SCRIPT_DIR}/custom/#{script_name}.cmd.gz") ||
File.exists?("#{SCRIPT_DIR}/#{script_name}.wiz.gz") || File.exists?("#{SCRIPT_DIR}/custom/#{script_name}.wiz.gz")
end
}
@@elevated_log = proc { |data|
if script = Script.current
if script.name =~ /\\|\//
nil
else
begin
Dir.mkdir("#{LICH_DIR}/logs") unless File.exists?("#{LICH_DIR}/logs")
File.open("#{LICH_DIR}/logs/#{script.name}.log", 'a') { |f| f.puts data }
true
rescue
respond "--- Lich: error: Script.log: #{$!}"
false
end
end
else
respond '--- error: Script.log: unable to identify calling script'
false
end
}
@@elevated_db = proc {
if script = Script.current
if script.name =~ /^lich$/i
respond '--- error: Script.db cannot be used by a script named lich'
nil
elsif script.class == ExecScript
respond '--- error: Script.db cannot be used by exec scripts'
nil
else
SQLite3::Database.new("#{DATA_DIR}/#{script.name.gsub(/\/|\\/, '_')}.db3")
end
else
respond '--- error: Script.db called by an unknown script'
nil
end
}
@@elevated_open_file = proc { |ext, mode, block|
if script = Script.current
if script.name =~ /^lich$/i
respond '--- error: Script.open_file cannot be used by a script named lich'
nil
elsif script.name =~ /^entry$/i
respond '--- error: Script.open_file cannot be used by a script named entry'
nil
elsif script.class == ExecScript
respond '--- error: Script.open_file cannot be used by exec scripts'
nil
elsif ext.downcase == 'db3'
SQLite3::Database.new("#{DATA_DIR}/#{script.name.gsub(/\/|\\/, '_')}.db3")
# fixme: block gets elevated... why?
# elsif block
# File.open("#{DATA_DIR}/#{script.name.gsub(/\/|\\/, '_')}.#{ext.gsub(/\/|\\/, '_')}", mode, &block)
else
File.open("#{DATA_DIR}/#{script.name.gsub(/\/|\\/, '_')}.#{ext.gsub(/\/|\\/, '_')}", mode)
end
else
respond '--- error: Script.open_file called by an unknown script'
nil
end
}
@@running = Array.new
attr_reader :name, :vars, :safe, :file_name, :label_order, :at_exit_procs
attr_accessor :quiet, :no_echo, :jump_label, :current_label, :want_downstream, :want_downstream_xml, :want_upstream, :want_script_output, :hidden, :paused, :silent, :no_pause_all, :no_kill_all, :downstream_buffer, :upstream_buffer, :unique_buffer, :die_with, :match_stack_labels, :match_stack_strings, :watchfor, :command_line, :ignore_pause
def Script.version(script_name, script_version_required = nil)
script_name = script_name.sub(/[.](lic|rb|cmd|wiz)$/, '')
file_list = Dir.children(File.join(SCRIPT_DIR, "custom")).sort_by{ |fn| fn.sub(/[.](lic|rb|cmd|wiz)$/, '') }.map{ |s| s.prepend("/custom/") } + Dir.children(SCRIPT_DIR).sort_by{|fn| fn.sub(/[.](lic|rb|cmd|wiz)$/, '')}
if file_name = (file_list.find { |val| val =~ /^(?:\/custom\/)?#{Regexp.escape(script_name)}\.(?:lic|rb|cmd|wiz)(?:\.gz|\.Z)?$/ || val =~ /^(?:\/custom\/)?#{Regexp.escape(script_name)}\.(?:lic|rb|cmd|wiz)(?:\.gz|\.Z)?$/i } || file_list.find { |val| val =~ /^(?:\/custom\/)?#{Regexp.escape(script_name)}[^.]+\.(?i:lic|rb|cmd|wiz)(?:\.gz|\.Z)?$/ } || file_list.find { |val| val =~ /^(?:\/custom\/)?#{Regexp.escape(script_name)}[^.]+\.(?:lic|rb|cmd|wiz)(?:\.gz|\.Z)?$/i })
script_name = file_name.sub(/\..{1,3}$/, '')
end
file_list = nil
if file_name.nil?
respond "--- Lich: could not find script '#{script_name}' in directory #{SCRIPT_DIR}"
return nil
end
script_version = '0.0.0'
script_data = open("#{SCRIPT_DIR}/#{file_name}", 'r').read
if script_data =~ /^=begin\r?\n?(.+?)^=end/m
comments = $1.split("\n")
else
comments = []
script_data.split("\n").each {|line|
if line =~ /^[\t\s]*#/