-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlich.rb
More file actions
executable file
·1411 lines (1339 loc) · 55 KB
/
lich.rb
File metadata and controls
executable file
·1411 lines (1339 loc) · 55 KB
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
#####
# 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.
#####
##
# ...... Jesus this file is a mess.......
##
# Get the debug-mode STDERR redirection in place so there's a record of any errors (in Windows, the program has no STDOUT or STDERR)
if ARGV.find { |arg| arg =~ /^--debug$/ }
$stderr = File.open('lich_debug.txt','a')
$stdout = $stderr
$stderr.sync = true
$stdout.sync = true
ARGV.delete_if { |arg| arg =~ /^--debug$/ }
elsif $LICHCONFIG['OS'] =~ /win/i
$stderr = File.open('lich_debug.txt','w')
$stdout = $stderr
$stderr.sync = true
$stdout.sync = true
end
$: << '.' << 'gui'
$:.uniq!
begin
require "common.rb"
rescue
$stderr.puts $!
rescue LoadError
$stderr.puts $!
end
class NilClass
def +(val)
val
end
def closed?
true
end
def method_missing(*args)
nil
end
end
if ARGV.find { |arg| arg =~ /^-h$|^--help$/ } then puts <<CLIHELP
Usage: lich [OPTION]
Options are:
-h, --help Display this list.
-V, --version Display the program version number and credits.
-d, --directory Set the main Lich program directory (this run only).
--script-dir Define the directory Lich will search for script files in (this run only). See example below.
-s, --stormfront Run in StormFront mode (this run only). ip:port used by SF is storm.gs4.game.play.net:10024, stream encoding is XML-based
-w, --wizard Run in Wizard mode (this run only). ip:port used by Wizard is gs3.simutronics.net:4900, stream encoding is GSL
--dragonrealms Run in Wizard mode for the DragonRealms game (this run only).
--platinum Platinum players connect to a different address than Prime players. This tells Lich to catch the Platinum connection.
-g, --game Set the IP address and port of the game (this run only). See example below.
-c, --compressed Do compression/decompression of the I/O data using Zlib (this is for MCCP, Mud Client Compression Protocol).
--bare Perform no data-scanning, just pass all game lines directly to scripts. For maximizing efficiency w/ non-Simu MUDs.
--debug Mainly of use in Windows; redirects the program's STDERR & STDOUT to the '/lich_err.txt' file.
--uninstall Restore the hosts backup (and in Windows also launch the uninstall application).
The majority of Lich's built-in functionality was designed and implemented with Simutronics MUDs in mind (primarily Gemstone IV): as such, many options/features provided by Lich may not be applicable when it is used with a non-Simutronics MUD. In nearly every aspect of the program, users who are not playing a Simutronics game should be aware that if the description of a feature/option does not sound applicable and/or compatible with the current game, it should be assumed that the feature/option is not. This particularly applies to in-script methods (commands) that depend heavily on the data received from the game conforming to specific patterns (for instance, it's extremely unlikely Lich will know how much "health" your character has left in a non-Simutronics game, and so the "health" script command will most likely return a value of 0).
The level of increase in efficiency when Lich is run in "bare-bones mode" (i.e. started with the --bare argument) depends on the data stream received from a given game, but on average results in a moderate improvement and it's recommended that Lich be run this way for any game that does not send "status information" in a format consistent with Simutronics' GSL or XML encoding schemas.
Examples:
lich -w -d /usr/bin/lich/ (run Lich in Wizard mode using the dir '/usr/bin/lich/' as the program's home)
lich -g gs3.simutronics.net 4000 (run Lich using the IP address 'gs3.simutronics.net' and the port number '4000')
lich --script-dir /mydir/scripts (run Lich with its script directory set to '/mydir/scripts')
lich --bare -g skotos.net 5555 (run in bare-bones mode with the IP address and port of the game set to 'skotos.net:5555')
CLIHELP
exit; end
if ARGV.find { |arg| arg =~ /^-(?i:V)$|^--version$/ } then $stdout.puts <<CLIVERSION
The Lich, version #{$version}
(an implementation of the Ruby interpreter by Yukihiro Matsumoto designed to be a `script engine' for text-based MUDs)
- The Lich program and all material collectively referred to as "The Lich project" is copyright (C) 2005-2006 Murray Miron.
- The Gemstone IV and DragonRealms games are copyright (C) Simutronics Corporation.
- The Wizard front-end and the StormFront front-end are also copyrighted by the Simutronics Corporation.
- Ruby is (C) Yukihiro `Matz' Matsumoto.
- Inno Setup Compiler 5 is (C) 1997-2005 Jordan Russell (used for the Windows installation package).
Thanks to all those who've reported bugs and helped me track down problems on both Windows and Linux.
CLIVERSION
exit; end
if ARGV[1] and ARGV[1] !~ /^-/
sal_fname = ARGV[1]
else
sal_fname = nil
end
sock_keepalive_proc = proc { |sock|
err_msg = proc { |err|
err ||= $!
$stderr.puts Time.now
$stderr.puts err
$stderr.puts err.backtrace
}
begin
sock.setsockopt(Socket::SOL_SOCKET, Socket::SO_KEEPALIVE, true)
rescue
err_msg.call($!)
rescue Exception
err_msg.call($!)
end
}
trace_var(:$_CLIENT_, sock_keepalive_proc)
trace_var(:$_SERVER_, sock_keepalive_proc)
$LICHCONFIG.each_pair { |key, val| $LICHCONFIG[key] = File.expand_registry_str(val) }
Lich.reload_settings
# A few pages of repetitive checks to set things up for the current environment (operating system, program directories, front-end being used, etc.)
lich_dir = $lich_dir
if File.exists?(lich_dir + 'Scripts')
script_dir = lich_dir + 'Scripts/'
elsif File.exists?(lich_dir + 'scripts')
script_dir = lich_dir + 'scripts/'
else
script_dir = lich_dir.dup
end
if tmplich_dir = ARGV.find { |val| val =~ /^-d$|^--directory$/ }
lich_dir = ARGV[ARGV.index(tmplich_dir).succ]
unless lich_dir[-1..-1] =~ /\\|\// then lich_dir += lich_dir.slice(/\\|\//) end
if File.exists?(lich_dir + 'scripts') then script_dir = "#{lich_dir}scripts#{lich_dir.slice(/\\|\//)}" elsif File.exists?(lich_dir + 'Scripts') then script_dir = "#{lich_dir}Scripts#{lich_dir.slice(/\\|\//)}" else $stdout.puts("WARNING, no script directory found! The 'scripts' directory must be inside the Lich directory for things to function properly."); script_dir = "#{lich_dir}scripts#{lich_dir.slice(/\\|\//)}" end
$stdout.puts("Lich directory set to #{lich_dir}")
end
$nix = false
if tmp = ARGV.find { |arg| arg =~ /^--launcher$/ }
tmpidx = ARGV.index tmp
ARGV.delete_at(tmpidx)
estr = "$SAFE = 3\n" + ARGV[tmpidx..-1].join(" ")
tmp = tmpidx = nil
estr.taint
simu_port = simu_ip = nil
eval(estr, nil, "LauncherCode")
end
unless sal_fname
if ARGV.find { |arg| arg =~ /^-w$|^--wizard$/ }
simu_port = 4900; simu_ip = 'gs3.simutronics.net'; $stormfront = false
elsif ARGV.find { |arg| arg =~ /^-s$|^--stormfront$/ }
simu_port = 10024; simu_ip = "storm.gs4.game.play.net"; $stormfront = true
elsif gameinfoargv = ARGV.find { |arg| arg =~ /^-g$|^--game$/ }
simu_ip = ARGV[ARGV.index(gameinfoargv) + 1]
simu_port = ARGV[ARGV.index(gameinfoargv) + 2].to_i
$stdout.puts("Game information being used: #{simu_ip}:#{simu_port}"); $stdout.flush; $stormfront = false
elsif gameinfoargv = ARGV.find { |arg| arg =~ /^--platinum$/ }
simu_ip = "gs-plat.simutronics.net"
simu_port = 10121
elsif ARGV.find { |arg| arg =~ /^--dragonrealms$/i }
simu_ip = 'dr.simutronics.net'; simu_port = 4901
elsif File.exists?("#{lich_dir}game.txt")
file = File.open("#{lich_dir}game.txt"); data = file.readlines; file.close; file = nil
data.delete_if { |line| line =~ /^\#/ }
unless data.nil?
simu_ip = data.shift.strip
simu_port = data.shift.strip
if data.first =~ /BARE/
$BARE_BONES = true
elsif data.first =~ /XML/
$stormfront = true
elsif data.first =~ /GSL/
nil
end
end
elsif (File.exists?("#{lich_dir}stormfront-mode.txt") or File.exists?("#{lich_dir}stormfront-mode.txt.txt"))
simu_port = 10024
simu_ip = "storm.gs4.game.play.net"
$stormfront = true
elsif File.exists?("#{lich_dir}dr-mode.txt")
simu_port = 4901
simu_ip = "dr.simutronics.net"
$stormfront = false
elsif File.exists?(lich_dir + 'wizard-mode.txt')
simu_port = 4900
simu_ip = 'gs3.simutronics.net'
$stormfront = false
elsif File.exists?('/Gse.~xt') or File.exists?(ENV['HOME'] + '/.wine/drive_c/Gse.~xt')
begin
print("No game/front-end input found, auto-detecting...")
if File.exists?('/Gse.~xt')
file = File.open('/Gse.~xt')
else
file = File.open(ENV['HOME'] + '/.wine/drive_c/Gse.~xt')
end
guessdata = file.readlines.collect { |line| line.strip }
file.close
file = nil
simu_ip = guessdata.find { |line| line =~ /^GAMEHOST/ }.split('=').last.strip
if simu_ip == '127.0.0.1'
simu_ip = 'gs3.simutronics.net'
simu_port = 4900
print(" ...PsiNet alteration of file detected; configuring for Wizard.\n")
$stormfront = false
else
simu_port = guessdata.find { |line| line =~ /^GAMEPORT/ }.split('=').last.strip.to_i
fe = guessdata.find { |line| line =~ /^GAMEFILE/ }.split('=').last.strip
if fe == 'WIZARD.EXE'
print(" ...configuring for Wizard.\n")
$stormfront = false
else
$stormfront = true
print(" ...configuring for StormFront.\n")
end
end
rescue
$stderr.puts("Unrecoverable error during read of 'Gse.~xt' file! Falling back on defaults...")
$stderr.puts($!)
simu_port = 4900
simu_ip = 'gs3.simutronics.net'
$stormfront = false
end
else
simu_port = 4900
simu_ip = "gs3.simutronics.net"
$stormfront = false
end
else
simu_port = nil
simu_ip = nil
end
if ARGV.find { |arg| arg =~ /^--bare$/ }
puts "Running in bare-bones mode."
$BARE_BONES = true
end
puts(sprintf("IP:port = %s:%d", simu_ip, simu_port)) unless sal_fname
if set_sdir = ARGV.find { |arg| arg =~ /--script-dir/i }
script_dir = ARGV[ARGV.index(set_sdir) + 1]
script_dir[-1..-1] !~ /\/|\\/ ? script_dir = File.join(script_dir, '') : nil
$stdout.puts("Script dir has been set to '#{script_dir}'."); $stdout.flush
end
if ARGV.find { |arg| arg =~ /^--?c(?:ompressed)$/i }
$ZLIB_STREAM = true
trace_var :$_SERVER_, proc { |server_socket|
$_SERVER_ = ZlibStream.wrap(server_socket) if $ZLIB_STREAM
}
trace_var :$_CLIENT_, proc { |client_socket|
$_CLIENT_ = ZlibStream.wrap(client_socket) if $ZLIB_STREAM
}
else
$ZLIB_STREAM = false
end
# Defining of various variables, GC.enable ensures that the garbage collector prevents us from needlessly using up system resources, etc..
log_dir = lich_dir
_TAGREGEXP_ = '^GSj|^GSg|^GSr|^GSm|^GSl|^GSZ|^GSY|^GSX|^GSn|^GSJ|^GSK|^GSq|^GSQ|^GSP|^GSa|^GSb'
_SFPARSEREGEXP_ = '<[^>]+>'
$SEND_CHARACTER = '>'
$_FAKE_STORMFRONT = false
$_SFPARSER_ = LichXML.new
$_SFPARSER_.extend(SFMetadata)
$lich_dir ||= lich_dir
$script_dir ||= script_dir
$data_dir ||= $lich_dir + "data" + File::SEPARATOR
$right_hand = String.new
$left_hand = String.new
$npcs = Array.new
$pcs = String.new
$roomarea = String.new
$roomtitle = String.new
$room_count = 0
$last_dir = String.new
$familiar_directions = String.new
$familiar_area = String.new
$familiar_room = String.new
$familiar_npcs = Array.new
$familiar_pcs = String.new
JUMP = Exception.exception('JUMP')
JUMP_ERROR = Exception.exception('JUMP_ERROR')
$_LICHERRCNT_ = 0
trace_var :$_LICHERRCNT_, proc { |n|
if n >= 5
begin
$_SERVER_.close unless $_SERVER_.closed?
$_CLIENT_.close unless $_CLIENT_.closed?
rescue Exception
rescue
ensure
exit
end
end
}
Socket.do_not_reverse_lookup = true
$_TA_BUFFER_ = []
$_SERVERBUFFER_ = CachedArray.new
$_CLIENTBUFFER_ = CachedArray.new
Dir.entries(CachedArray.dir).find_all { |f| f =~ /^\.cache[.\d]+/ }.each { |f|
if (Time.now - File.mtime(File.join(CachedArray.dir, f))) > 3600
File.delete(File.join(CachedArray.dir, f)) rescue()
end
}
# Since there are so damn many places, this locates and returns the user's hosts file location
if $hosts_dir
hosts_dir = File.expand_registry_str($hosts_dir)
$hosts_dir = nil
else
if ENV['windir']
hosts_dir = find_hosts_file(ENV['windir'])
elsif ENV['SYSTEMROOT']
hosts_dir = find_hosts_file(ENV['SYSTEMROOT'])
else
hosts_dir = find_hosts_file
end
end
if ARGV.find { |arg| arg =~ /--uninstall/ }
$stdout.puts("Uninstalling...")
uninsfile = Dir.entries(lich_dir).find_all { |file| file =~ /unins[0-9]+\.exe/i }.sort.last
if File.exists?($lich_dir + "launcher-uninstall.dat")
system("\"#{$lich_dir}" + "lichlauncher.exe\" -u")
end
if File.exists?(hosts_dir + 'hosts') and File.exists?($lich_dir + 'hosts.sav')
if File.mtime(hosts_dir + "hosts") <= File.mtime($lich_dir + "hosts.sav")
File.open($lich_dir + 'hosts.sav') { |savf|
File.open(hosts_dir + 'hosts', 'w') { |hostf|
hostf.write savf.read
}
}
end
exec(lich_dir + uninsfile) rescue()
exit
elsif File.exists?(hosts_dir + 'hosts')
$stdout.puts("Restoration is unnecessary, launching uninstall application.")
exec(lich_dir + uninsfile) rescue()
exit
else
$stderr.puts("Cannot properly locate your hosts file or hosts backup file! If they can't be found to restore them, it's nearly impossible that they were found to change in the first place. Launching uninstall application.")
exec(lich_dir + uninsfile) rescue()
exit
end
end
if hosts_dir.nil? and sal_fname.nil? then $stderr.puts("hosts_dir is nil (#{Time.now})"); exit(1) end
unless sal_fname
simu_quad_ip = IPSocket.getaddress(simu_ip)
begin
listener = TCPServer.new("localhost", simu_port)
begin
listener.setsockopt(Socket::SOL_SOCKET,Socket::SO_REUSEADDR,1)
rescue
$stderr.puts("Error during setsockopt, aborting setting of SO_REUSEADDR: #{$!}")
end
rescue
$temp_error ||= 0
$temp_error += 1
sleep 1
retry unless $temp_error >= 30
$stderr.puts("Lich cannot bind to the proper port, aborting execution.")
exit!
end
$temp_error = nil
hack_hosts(hosts_dir, simu_ip)
if File.exists?("#{lich_dir}nosge.txt")
nil
elsif File.exists?("#{lich_dir}launch.txt")
launch_sge_user(lich_dir)
elsif ($LICHCONFIG['SGE Directory'] and File.exists? File.join($LICHCONFIG['SGE Directory'], 'Sge.exe')) or File.exists?("/Program Files/SIMU/SGE/Sge.exe")
launch_sge_win(listener)
elsif File.exists?("/home/fallen/.wine/drive_c/Program Files/SIMU/SGE/Sge.exe")
launch_sge_nix(listener)
elsif File.exists?("#{ENV['HOME']} + '/.wine/drive_c/Program Files/SIMU/SGE/Sge.exe")
launch_sge_nix(listener)
end
timeout_thread = Thread.new { sleep 120 ; $stderr.puts("Timeout, restoring backup and exiting.") ; heal_hosts(hosts_dir); exit 1 }
open_client(listener)
timeout_thread.kill
timeout_thread = nil
Process.wait rescue()
heal_hosts(hosts_dir)
else
$stderr.puts "sal_fname == #{sal_fname}"
begin
sal_data = File.open(sal_fname) { |file| file.readlines }.collect { |line| line.chomp }
rescue
$stderr.puts "Error opening .sal (Simutronics Auto Launch) file: #{$!}"
exit(1)
end
unless gameport = sal_data.find { |line| line =~ /GAMEPORT=/ }
$stderr.puts ".sal file contains no GAMEPORT info"
exit(1)
end
unless gamehost = sal_data.find { |opt| opt =~ /GAMEHOST=/ }
$stderr.puts ".sal file contains no GAMEHOST info"
exit(1)
end
unless game = sal_data.find { |opt| opt =~ /GAME=/ }
$stderr.puts ".sal file contains no GAME info"
exit(1)
end
gameport = gameport.split('=').last
gamehost = gamehost.split('=').last
game = game.split('=').last
$stderr.puts sprintf("gamehost: %s gameport: %s game: %s", gamehost, gameport, game)
begin
listener = TCPServer.new("localhost", nil)
rescue
$stderr.puts "Cannot bind listening socket to local port: #{$!}"
$stderr.puts sprintf("HOST: %s PORT: %s GAME: %s", gamehost, gameport, game)
$stderr.puts sal_fname
exit(1)
end
begin
listener.setsockopt(Socket::SOL_SOCKET, Socket::SO_REUSEADDR, true)
rescue
$stderr.puts "Cannot set SO_REUSEADDR sockopt"
end
localport = listener.addr[1]
mod_data = []
sal_data.each { |sal_line| mod_data.push sal_line.sub(/GAMEPORT=.+/, "GAMEPORT=#{localport}").sub(/GAMEHOST=.+/, "GAMEHOST=localhost") }
File.open($lich_dir + "lich.sal", "w") { |f| f.puts mod_data }
if File.exists?($lich_dir + "autolaunch.txt")
launch = File.open($lich_dir + 'autolaunch.txt') { |f| f.read.gsub(/[\r\n]/, '').strip }
if launch.empty?
Thread.new { system(File.join($LICHCONFIG['Launcher Directory'], "Launcher.exe #{$lich_dir + 'lich.sal'}")) }
else
Thread.new { system(launch + ' ' + File.join($lich_dir, "lich.sal")) }
end
elsif File.exists?($lich_dir + "launcher-uninstall.dat")
launch = File.open($lich_dir + "launcher-uninstall.dat")
prog = launch.readlines.first.chomp
launch.close
Thread.new { system(prog.sub("%1", File.join($lich_dir.gsub("/", "\\"), "lich.sal"))) }
else
Thread.new { system(File.join($LICHCONFIG['Launcher Directory'], "Launcher.exe " + $lich_dir.gsub("/", "\\") + "lich.sal")) }
end
timeout_thr = Thread.new {
sleep 15
$stderr.puts "timeout waiting for connection."
exit(1)
}
$_CLIENT_ = listener.accept
begin
timeout_thr.kill
listener.close
rescue
$stderr.puts $!
end
$_SERVER_ = TCPSocket.open(gamehost, gameport)
if game =~ /STORM/i
$stormfront = true
end
end
unless ENV['OS'] =~ /win/i
begin
Process.uid = `id -ru`.strip.to_i
Process.gid = `id -rg`.strip.to_i
Process.egid = `id -rg`.strip.to_i
Process.euid = `id -ru`.strip.to_i
rescue SecurityError
$stderr.puts "Error dropping superuser privileges: #{$!}"
rescue SystemCallError
$stderr.puts "Error dropping superuser privileges: #{$!}"
rescue
$stderr.puts "Error dropping superuser privileges: #{$!}"
end
end
Dir.mkdir($data_dir) rescue()
errtimeout = 1
# We've connected with the game client... so shutdown the listening socket (open it up for use by other progs, etc.)
begin
# Somehow... for some ridiculous reason... Windows doesn't let us close the socket if we shut it down first...
# listener.shutdown
listener.close unless listener.closed?
rescue
$stderr.puts("error closing listener socket: #{$!}")
errtimeout += 1
if errtimeout > 20 then $stderr.puts("error appears unrecoverable, aborting") end
sleep 0.05
retry unless errtimeout > 20
end
if ARGV.find { |arg| arg =~ /^--test|^-t/ }
$_SERVER_ = $stdin
$_CLIENT_.puts "Running in test mode: host socket set to stdin."
elsif !$_SERVER_
open_gs(simu_quad_ip, simu_port)
end
errtimeout = nil
listener = timeout_thr = nil
def show_favs(who=nil)
begin
file = File.open("#{$lich_dir}favorites.txt"); fav_data = file.readlines; file.close; file = nil
if who.nil?
favs_all = fav_data.find_all { |line| line =~ /^ALL:/ } ; favs_all.each_with_index { |scr,idx| favs_all[idx] = scr.sub('ALL:','') }
favs_char = fav_data.find_all { |line| line =~ /^#{checkname}:/ } ; favs_char.each_with_index { |scr,idx| favs_char[idx] = scr.sub((checkname + ':'),'') }
elsif who == 'all'
fav_list = fav_data.sort_by { |name| name.split(':').first }
respond("Favorites for All Characters:")
respond(fav_list)
return
end
rescue
fav_data = [ "none" ]
favs_all = [ 'none' ]
favs_char = [ 'none' ]
end
if fav_data.empty? or fav_data.nil?
fav_list = "none"
else
fav_list_all = favs_all.join(', ').gsub(/\r|\n/,'')
fav_list_char = favs_char.join(', ').gsub(/\r|\n/,'')
end
respond("Global Favorites (apply to all chars): #{fav_list_all}.") unless favs_all.empty?
respond("Favorites for Character #{checkname}: #{fav_list_char}.") unless favs_char.empty?
end
if File.exists?("#{lich_dir}lich-char.txt")
file = File.open("#{lich_dir}lich-char.txt"); arr = file.readlines; arr = arr.find_all { |line| line !~ /^#/ }; $clean_lich_char = arr.last.strip; file.close; file = nil
else
$clean_lich_char = ';'
end
lich_char = Regexp.escape("#{$clean_lich_char}")
undef :exit!
client_thread = Thread.new {
$login_time = Time.now
alias_failed = false
aliasarray = Array.new
if File.exists?(lich_dir + 'aliases.txt')
file = File.open(lich_dir + 'aliases.txt', 'rb')
newfile = File.open($data_dir + 'aliases.dat', 'wb')
newfile.write(file.read)
file.close
newfile.close
file = nil
newfile = nil
File.delete(lich_dir + 'aliases.txt')
GC.start
end
begin
if File.exists?(lich_dir + 'aliases.dat')
File.rename(lich_dir + 'aliases.dat', $data_dir + 'aliases.dat')
end
file = File.open($data_dir + 'aliases.dat', 'rb'); alias_list_hash = Marshal.load(file.read); file.close; file = nil
alias_list = alias_list_hash.keys.dup
rescue
alias_list_hash = Hash.new
alias_list = Array.new
file = File.open($data_dir + 'aliases.dat','wb'); file.write(Marshal.dump(alias_list_hash)); file.close; file = nil
alias_failed = true unless alias_failed
retry unless alias_failed
end
aliasregexp = alias_list.join('|^(?:<c>)?')
running_alias = false
if File.exists?(lich_dir + "spellshorts.txt")
dospellshorts = true
else
dospellshorts = false
end
if !($stormfront or $_FAKE_STORMFRONT) and File.exists?(lich_dir + "ta_limit.txt")
file = File.open(lich_dir + "ta_limit.txt")
$TALIMIT = file.readlines.find_all { |line| line !~ /^#/ }.join.slice(/\d+/).to_i
file.close
file = nil
else
$TALIMIT = 0
end
2.times {
client_string = $_CLIENT_.gets
$_CLIENTBUFFER_.push(client_string.dup)
$_SERVER_.write(client_string)
}
until $_CLIENT_.closed? or $_SERVER_.closed?
#init_hooks
#undef :init_hooks
begin
if defined?(Hook) and not defined?(UpstreamHook)
UpstreamHook = Hook
else
class UpstreamHook
def UpstreamHook.method_missing(*args)
false
end
end
end
while client_string = $_CLIENT_.gets
$_IDLETIMESTAMP_ = Time.now
if dospellshorts and (spellshort = /^\s*(\d\d\d\d?)(.*)?$/.match(client_string.chomp))
if !spellshort.captures[1].empty?
$stormfront ? $_SERVER_.send("<c>prep #{spellshort.captures[0]}\n", 0) : $_SERVER_.send("prep #{spellshort.captures[0]}\n", 0)
$stormfront ? $_SERVER_.send("<c>cast at #{spellshort.captures[1].strip}\n", 0) : $_SERVER_.send("cast at #{spellshort.captures[1].strip}\n", 0)
else
$stormfront ? $_SERVER_.send("<c>incant #{spellshort.captures[0]}\n", 0) : $_SERVER_.send("incant #{spellshort.captures[0]}\n", 0)
end
next
end
if client_string =~ /^(?:<c>)?#{lich_char}/o and (client_string !~ /^(?:<c>)?(#{aliasregexp})\b([^\r\n]+)?$/ or client_string =~ /^(?:<c>)?#{lich_char}alias\b/i)
client_string = client_string.sub(/#{lich_char}/o, ';')
cmd = $'.chomp.downcase
if cmd =~ /^\!/
cleaned_psi_string = client_string.sub(/^;\!/, ';').chomp
$_SERVER_.puts(cleaned_psi_string)
$_CLIENTBUFFER_.push(cleaned_psi_string)
respond("--- Lich: saw the '!' (ignore this) signal. Sent: '#{cleaned_psi_string}'.")
elsif UpstreamHook.call(cmd)
next
elsif cmd =~ /^spellshorts?$/i
dospellshorts = !dospellshorts
respond "--- Lich: spellshorts are now %s." % (dospellshorts ? "on" : "off")
if dospellshorts
File.open($lich_dir + "spellshorts.txt", "w") { |f| f.puts } rescue()
else
File.unlink($lich_dir + "spellshorts.txt") rescue()
end
next
elsif cmd.empty?
respond("--- Lich: saw no data in the Lich-command string -- ignoring!")
elsif cmd =~ /^list$|^l$/
unless Script.index.empty? then respond("--- Lich: #{Script.index.join(", ")}.") else respond("--- Lich: no active scripts.") end
respond("--- Lich: 'Watchfors' active are #{Watchfor.list}.") if Watchfor.any?
elsif cmd =~ /^debug$/
$LICH_DEBUG = !$LICH_DEBUG
if $LICH_DEBUG
respond("--- Lich: debugging information will now be shown for scripts.")
else
respond("--- Lich: debugging information will no longer be shown for scripts.")
end
elsif eobj = /^(?:exec|e)(q)? .+/.match(cmd)
Thread.new {
cmd_data = "execscript.set_as_good\n#{client_string.sub(/^(?:<c>;|;)(?:exec|execq|e|eq) /i, '')}"
Thread.current.priority = 0
begin
if eobj.captures.first.nil? then execscript = Script.new('exec') else execscript = Script.new('exec',['quiet']) end
eval(cmd_data, nil, execscript.name.to_s, -1)
respond("--- Lich: #{execscript.name} has finished.") unless execscript.quiet
execscript.kill
rescue SyntaxError
respond("--- Lich SyntaxError: #{$!}")
respond("--- Lich: #{execscript.name} has exited.")
execscript.kill
rescue SystemExit
respond("--- Lich: #{execscript.name} has exited.") unless execscript.quiet
execscript.kill
rescue SecurityError
respond("--- Lich SecurityError: #{$!}")
respond("--- Lich: #{execscript.name} has exited.")
execscript.kill
rescue ThreadError
respond("--- Lich ThreadError: #{$!}")
respond("--- Lich: #{execscript.name} has exited.")
execscript.kill
rescue Exception
respond("--- Exception: #{$!}")
respond("--- Lich: #{execscript.name} has exited.")
execscript.kill
rescue ScriptError
respond("--- ScriptError: #{$!}")
respond("--- Lich: #{execscript.name} has exited.")
execscript.kill
rescue
respond("--- Lich Error: #{$!}")
respond("--- Lich: #{execscript.name} has exited.")
execscript.kill
end
Script.thread_hash.delete(Thread.current.group)
}
elsif cmd =~ /^kill$|^stop$|^k$/
if Script.index.empty?
respond("--- Lich: no scripts running!")
else
respond("--- Lich: #{Script.index.last.kill} stopped.")
end
elsif cmd =~ /^kill all$|^stop all$|^ka$/
wf = Watchfor.any?
if Script.index.empty?
unless wf
respond("--- Lich: no scripts running!")
else
Watchfor.untrace_all(true)
respond("--- Lich: all 'Watchfors' have been killed.")
end
else
if (Script.index.find_all { |val| !val.no_ka }.empty?)
respond("--- Lich: all currently running scripts have set the 'no kill all' option! Kill them individually.")
next
else
Watchfor.untrace_all(true)
Script.index.find_all { |ikeelu| !ikeelu.no_ka }.each { |ikeelu| ikeelu.kill }
end
list = Script.index.find_all { |scr| scr.no_ka }.collect { |scr| scr.name }
if wf
if list.empty?
respond("--- Lich: all 'Watchfors' and all running scripts have been stopped.")
else
respond("--- Lich: all 'Watchfors' and all scripts except #{list.join(', ')} have been stopped.")
end
else
if list.empty?
respond("--- Lich: all scripts have been stopped.")
else
respond("--- Lich: all scripts except #{list.join(', ')} have been stopped.")
end
end
end
list.clear
list = nil
GC.start
elsif cmd =~ /^k\s.+|^stop\s.+|^kill\s.+/
target_name = client_string.sub('<c>', '').sub(/^;k\s|^;s\s|^;stop\s|^;kill\s/i, '').chomp
if (condemned = Script.index.find { |s_sock| s_sock.name == target_name }).nil?
condemned = Script.index.find { |s_sock| s_sock.name =~ /^#{target_name}/i }
end
if condemned.nil?
respond("--- Lich: #{target_name}.lic does not appear to be running! Use ';list' to see what's active.")
else
name = condemned.kill
respond("--- Lich: #{name} stopped.")
end
GC.start
elsif cmd == "log"
dump_to_log(log_dir)
elsif cmd =~ /^chat .+|^,.+|^who(?: \w+)?$|^(\w+):.+|^(admin) .+|^locate \w+$|^info \w+$|^skills \w+$|^spells \w+$|^health \w+$/
namen = $1.dup
if $2 == "admin"
adminaction = $'.dup
if (tgt = Script.index.find { |scr| scr.name =~ /lichnet/i })
tgt.unique_puts(client_string.sub(/^(?:<c>)?;/,''))
else
respond("--- Lich: the LichNet client script must be running in order to talk to the LichNet server!")
end
tgt = nil
elsif (tgt = Script.index.find { |scr| scr.name =~ /lichnet/i })
if !namen.nil?
tgt.unique_puts(client_string.sub(/^(?:<c>;|;)[^:]+:/,"to #{namen} "))
else
tgt.unique_puts(client_string.sub(/^(?:<c>;|;)(?:chat|,)?/i,'').sub(/^\s*(locate|info|skills|spells|health) (\w+)$/i, '::\1::\2'))
end
else
respond("--- Lich: the LichNet client script must be running in order to talk to the LichNet server!")
end
tgt = nil
elsif cmd =~ /^(?:tunein|tuneout)$/i
if tgt = Script.index.find { |scr| scr.name =~ /lichnet/i }
tgt.unique_puts "::#{cmd}::"
else
respond("--- Lich: the LichNet client script must be running in order to talk to the LichNet server!")
end
tgt = nil
elsif cmd =~ /^send |^s /
$_CLIENTBUFFER_.pop
if cmd.split[1] == "to"
tgt = Script.index.find { |fscr| fscr.name == cmd.split[2].chomp.strip }
if tgt.nil?
tgt = Script.index.find { |fscr| fscr.name =~ /^#{cmd.split[2].chomp.strip}/i }
end
if tgt.nil? then respond("--- Lich: '#{cmd.split[2].chomp.strip}' does not match any active script!") ; next end
msg = client_string.sub(/^(?:<c>;|;)(?:[Ss]|[Ss][Ee][Nn][Dd])\s[Tt][Oo]\s#{cmd.split[2]}\s/, '').chomp
if tgt.unique
tgt.unique_puts(msg)
respond("--- Lich: sent to '#{tgt}' ('unique' stack): #{msg}")
else
tgt.puts(msg)
respond("--- Lich: sent to '#{tgt}': #{msg}")
end
tgt = nil
else
message = client_string.sub(/(?:<c>;|;)(?:[Ss]|[Ss][Ee][Nn][Dd]) /, '')
if Script.index.empty? then respond("--- No active scripts to send to!") ; next end
respond("--- Sent: #{message}")
Script.namescript_incoming(message)
end
elsif cmd == "reload"
Lich.reload_settings
begin
file = File.open($data_dir + 'aliases.dat', 'rb'); alias_list_hash = Marshal.load(file.read); file.close; file = nil
alias_list = alias_list_hash.keys.dup
rescue
file = File.open($data_dir + 'aliases.dat', 'wb'); file.write(Marshal.dump(Hash.new)); file.close; file = nil
retry
end
respond("--- Lich: settings have been reloaded from your 'settings.txt' and 'aliases.dat' files.")
elsif cmd =~ /^fave?s add\s.+/
$_CLIENTBUFFER_.pop
if client_string.split[2] =~ /all/i
add = client_string.sub('<c>', '').sub(/\;fave?s add all /i, '').chomp
tgt = 'ALL:'
else
add = client_string.sub('<c>', '').sub(/\;fave?s add /i, '').chomp
tgt = "#{checkname}:"
end
begin
file = File.open("#{lich_dir}favorites.txt"); fav_data = file.readlines; file.close; file = nil
fav_data.compact!
list = Dir.entries($script_dir)
unless list.find { |sname| sname =~ /^#{add}\.lic/i }
add = list.find { |sname| sname =~ /^#{add}.*\.lic/i }
unless add.nil? then add.sub!(/\.lic$/i,'') end
end
fav_data.push(tgt + add) unless add.nil?
file = File.open("#{lich_dir}favorites.txt", "w"); file.puts(fav_data); file.close; file = nil
rescue SystemCallError
file = File.open("#{lich_dir}favorites.txt", "w"); file.puts(tgt + add); file.close; file = nil
rescue
respond("--- Lich: unrecoverable error encountered, aborting preferences update: #{$!}")
end
show_favs
tgt = nil
elsif cmd =~ /^fave?s del(?:ete)?\s.+/
$_CLIENTBUFFER_.pop
if client_string.split[2] =~ /all/i
del = client_string.sub('<c>', '').sub(/^\;fave?s del(?:ete)? all /i, "").chomp
tgt = 'all'
else
del = client_string.sub('<c>','').sub(/^\;fave?s del(?:ete)? /i,'').chomp
tgt = "#{checkname}"
end
begin
file = File.open("#{lich_dir}favorites.txt"); fav_data = file.readlines; file.close; file = nil
fav_data.delete_if { |line| line =~ /^#{tgt}:#{del}/i }
fav_data.compact!
file = File.open("#{lich_dir}favorites.txt", "w"); file.puts(fav_data); file.close; file = nil
rescue
respond("--- Lich: unrecoverable error encountered, aborting preferences update: #{$!}")
end
show_favs
tgt = nil
elsif cmd =~ /^(?:fave?s|fave?s all)$/
$_CLIENTBUFFER_.pop
if cmd.split.length == 1 then show_favs else show_favs('all') end
elsif cmd =~ /^fave?s load$/
$_CLIENTBUFFER_.pop
begin
respond("\r\n--- Lich: started #{(load_favs(lich_dir,script_dir,true).join(', '))}.\r\n")
rescue
respond; respond("Fatal error loading favs list: #{$!}"); respond
end
elsif cmd == "stats"
begin
t_struct = Process.times
ramsz = 0
ObjectSpace.each_object(Script) { |scr| ramsz += (scr.io.length + scr.unique_io.length + scr.upstream_io.length + 12) }
if File.exists? $_SERVERBUFFER_.getfd
sbufsz = File.size($_SERVERBUFFER_.getfd)
else
sbufsz = 0
end
if File.exists? $_CLIENTBUFFER_.getfd
pbufsz = File.size($_CLIENTBUFFER_.getfd)
else
pbufsz = 0
end
fary = []
ary = []
ObjectSpace.each_object(File) { |f| fary.push f.path if !f.closed? }
ObjectSpace.each_object(Script) { |s| ary.push s.name unless Script.find(s.name) }
respond
respond sprintf("Time passed since login: %s", ((Time.now.to_f - $login_time.to_f) / 60.00).as_time)
respond sprintf("System CPU time used (secs): %f", t_struct.stime)
respond sprintf("User CPU time used (secs): %f", t_struct.utime)
respond sprintf("Lich's process ID: %d", Process.pid)
echo
respond sprintf("Lich's `home' directory is: %s", $lich_dir)
respond sprintf("Lich's `saved data' dir is: %s", $data_dir)
respond sprintf("Lich's script directory is: %s", $script_dir)
respond sprintf("Wizard script directory is: %s", File.join($LICHCONFIG['Wizard Directory'], "Scripts")) if $LICHCONFIG['Wizard Directory']
respond sprintf("# of room transitions counted: %d%s", $room_count - 1, ($stormfront ? " (not accurate when using SF)" : ""))
echo
respond sprintf("Size of temporary disk caches: %.3fKB", (sbufsz + pbufsz) / 1024.0)
respond sprintf("RAM in use by script IO buffers: %.3fKB", ramsz / 1024.0)
respond sprintf("# of dead scripts still on heap: %d%s", ary.length, (ary.empty? ? "" : " (#{ary.join(', ')})"))
respond sprintf("# of open file descriptors: %d%s", fary.length, (fary.empty? ? "" : " (#{fary.join(', ')})"))
echo
respond sprintf("# of scripts in scheduling pool: %d", Script.index.length)
ary = Script.index.reject { |s| Script.wakelist.include? s }
respond sprintf("# of scripts queued to execute: %d%s", ary.length, (ary.empty? ? "" : " (#{ary.join(', ')})"))
respond sprintf("# of scripts blocked on IO: %d%s", Script.wakeme.length, (Script.wakeme.empty? ? "" : " (#{Script.wakeme.collect { |thr| Script.index.find { |s| s.threads.list.include? thr } }.compact.join(', ')})"))
ary = fary = nil
rescue
respond("--- Lich: an error while checking settings has occurred: #{$!}")
end
elsif cmd == "repeat notice"
show_notice
elsif cmd =~ /^alias(?:es)?( add | add| set | set| help| del )?([^=]+)?(?:\=)?(.+)?$/
begin
if $1.nil?
if alias_list_hash.empty?
respond("")
respond("--- You currently have no Lich aliases.")
respond("")
else
respond("--- Your current Lich aliases are:\r\n")
tn = '0'
alias_list.sort.each { |akakey| respond(tn.succ! + ') ' + akakey.upcase + " => " + alias_list_hash[akakey])}
end
elsif ($1 == " add ") || ($1 == " add") || ($1 == " set ") || ($1 == " set") || ($1 == " help")
if ($2.nil? || $3.nil?)
respond("- Syntax for alias setting is `;alias set [what you will type]=[what will be sent]'.")
respond("- Syntax for alias deleting is `;alias del [which alias to delete]'.")
respond("- Separate lines with `\\r'; ex: ;alias set telloff='I hate you!\\r'Go away!")
respond("- You can also use `\\?', and Lich will replace it with whatever came after the alias.")
respond("- `add' is a synonym for 'set'. Also, Lich will properly recognize aliased commands; e.g.:")
respond(" `;alias set startup=;test\\r;echo\\r;calcredux' would start scripts test, echo, & calcredux.")
next
end
hkey = $2.to_s.chomp
hval = client_string.sub(/[^=]+=\s*/, '')
alias_list_hash[hkey] = hval
file = File.open($data_dir + 'aliases.dat','wb'); file.write(Marshal.dump(alias_list_hash)); file.close; file = nil
alias_list = alias_list_hash.keys.dup
aliasregexp = alias_list.join('|^(?:<c>)?')
respond("--- Alias `#{hkey.chomp}' => `#{hval.chomp}' added.")
next
elsif $1 == " del "
if $2.nil?
respond("- Syntax for alias setting is `;alias set [what you will type]=[what will be sent]'.")
respond("- Syntax for alias deleting is `;alias del [which alias to delete]'.")
respond("- Separate lines with `\\r'; ex: ;alias set telloff='I hate you!\\r'Go away!")
respond("- You can also use `\\?', and Lich will replace it with whatever came after the alias.")
respond("- `add' is a synonym for `set'. Also, Lich will properly recognize aliased commands; e.g.:")
respond(" `;alias set startup=;test\\r;echo\\r;calcredux' would start scripts test, echo, & calcredux.")
next
end
delalias = $2.dup
if (agone = alias_list_hash.keys.find { |akey| akey =~ /^#{Regexp.escape(delalias)}$/})
vgone = alias_list_hash.delete(agone)
elsif (agone = alias_list_hash.keys.find { |akey| akey =~ /^#{Regexp.escape(delalias)}/i })
vgone = alias_list_hash.delete(agone)
else
respond("--- Alias `#{delalias}' was not found!")
next
end
file = File.open($data_dir + 'aliases.dat','wb'); file.write(Marshal.dump(alias_list_hash)); file.close; file = nil
alias_list = alias_list_hash.keys.dup
aliasregexp = alias_list.join('|^(?:<c>)?')
respond("--- Alias `#{agone.chomp}' => '#{vgone.chomp}' removed.")
end
rescue
respond "--- Lich: unrecoverable error modifying aliases: #{$!}"
end
elsif cmd =~ /^magic\s?(?:clear|set|set .+|reset|help|details|enable|disable)?\s?(?:[^\s]+)?\s?(?:[0-9]+)?$/