-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtlily.PL
executable file
·331 lines (268 loc) · 8.38 KB
/
tlily.PL
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
#!/usr/bin/env perl
# TigerLily: A client for the lily CMC, written in Perl.
# Copyright (C) 1999-2011 The TigerLily Team, <[email protected]>
# http://www.tlily.org/tigerlily/
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 2, as published
# by the Free Software Foundation; see the included file COPYING.
require 5.6.1;
use strict;
use vars qw($EAGAIN $TL_ETCDIR $TL_LIBDIR $TL_TMPDIR $TL_LIBMODDIR);
sub directory_init {
$::TL_ETCDIR = "." unless defined($::TL_ETCDIR);
$::TL_LIBDIR = "lib" unless defined($::TL_LIBDIR);
$::TL_EXTDIR = "extensions" unless defined($::TL_EXTDIR);
$::TL_TMPDIR = "/tmp";
if ($^O =~ /cygwin/) {
$::TL_TMPDIR = $ENV{TEMP} || 'C:\WINDOWS\TEMP';
$::TL_TMPDIR =~ s/^.://g;
$::TL_TMPDIR =~ s/\\/\//g;
}
if ($^O eq 'MSWin32') {
$::TL_TMPDIR = $ENV{TEMP} || 'C:\WINDOWS\TEMP';
}
if (defined($::TL_LIBMODDIR) &&
($::TL_LIBMODDIR ne "//INTERNAL")) {
unshift @INC, $::TL_LIBMODDIR;
}
}
sub platform_init
{
if ($^O eq 'MSWin32')
{
# TODO: "-ap" means "Activestate Perl". No longer a good
# assumption if we're under Windows.
$TLily::Version::VERSION .= "-ap";
}
# Compute $EAGAIN.
# First, try some reasonably standard ways:
eval { require "Errno.pm"; $EAGAIN = Errno::EAGAIN(); 1}
or
eval { require "errno.ph"; $EAGAIN = EAGAIN(); 1}
or do { $EAGAIN = $::fallback_EAGAIN } # Did build_tlily compute a fallback?
or do { # So much for the easy way.
if ($::TL_LIBMODDIR !~ m|^//INTERNAL|) {
if (-f '.tlily-eagain') { # Did we already go down this road?
open(my $fh, '<', '.tlily-eagain') or die "Cannot read .tlily-eagain: $!\n";
$EAGAIN = <$fh>;
close $fh;
if ($EAGAIN =~ /^(\d+)/) {
$EAGAIN = $1;
} else {
die ".tlily-eagain seems to be corrupt; delete it and try again.\n";
}
} else {
$EAGAIN = `$^X tools/find_eagain`;
if ($EAGAIN =~ /^(\d+)/) {
print STDERR "Saving EAGAIN to .tlily-eagain.\n";
$EAGAIN = $1;
open(my $fh, '>', '.tlily-eagain') or die "Cannot write .tlily-eagain: $!\n";
print $fh $EAGAIN, "\n";
close $fh;
} else {
die "Cannot determine value for EAGAIN.\n";
}
}
} else {
die "Cannot determine value for EAGAIN.\n";
}
};
}
BEGIN { directory_init(); }
use lib $TL_LIBDIR;
use TLily::Event;
use TLily::Config;
use TLily::UI;
use TLily::Server;
use TLily::Server::SLCP;
use TLily::User;
use TLily::Extend;
use TLily::Version;
use TLily::Utils;
platform_init();
TLily::Config::init();
TLily::Event::init();
if(defined $config{bot}) {
eval "use TLily::Bot; TLily::Bot::init();";
}
my $nsApp;
my $ui = load_ui();
show_banner();
TLily::User::init;
TLily::Extend::init;
$SIG{__WARN__} = \&sig_warn;
sub sig_warn {
$SIG{__WARN__} = \&sig_warn;
$ui->print("WARNING: ", $_[0]);
}
$SIG{PIPE} = \&sig_pipe;
sub sig_pipe {
$SIG{PIPE} = \&sig_pipe;
my ($package, $filename, $line, $subroutine,
$hasargs, $wantarray, $evaltext, $is_require) = caller(1);
$ui->print("WARNING: Caught SIGPIPE. (from $package::$subroutine)\n");
}
my $sigint = 0;
sub sigint {
exit if ($sigint);
$ui->print("(hit ctrl-c again within 5 seconds to exit)\n");
$sigint = 1;
TLily::Event::time_r(after => 5,
call => sub {
$sigint = 0;
$ui->print("(you waited too long to hit ctrl-c; exit aborted)\n");
});
}
$ui->command_r("interrupt", \&sigint);
$ui->bind("C-c" => "interrupt");
sub output_handler {
my($event, $handler) = @_;
if ($event->{ui_name}) {
my $ui = TLily::UI::name($event->{ui_name});
if ($event->{BELL}) {
$ui->bell();
}
if ($event->{NOTIFY} && $event->{formatter}) {
$event->{formatter}->($ui, $event);
} elsif ($event->{NOTIFY} && $event->{indent}) {
$ui->indent($event->{indent});
$ui->print($event->{text}, "\n");
$ui->indent();
} elsif ($event->{NOTIFY} && $event->{slcp}) {
$ui->style($event->{slcp_fmt} || "slcp");
$ui->print($event->{text}, "\n");
$ui->style("default");
} elsif ($event->{NOTIFY}) {
$ui->print($event->{text}, "\n");
}
}
return 0;
}
TLily::Event::event_r(type => "all",
order => "after",
call => \&output_handler);
sub prompt_handler {
my($event, $handler) = @_;
my $server = $event->{server};
if (defined($event->{value})) {
$server->sendln($event->{value});
return;
}
if ($event->{ui_name}) {
my $ui = TLily::UI::name($event->{ui_name});
$ui->prompt_for(prompt => $event->{text},
password => $event->{password},
call => sub {
TLily::Event::send(type => 'user_input',
ui => $ui,
text => $_[1]);
# or, to bypass the normal handlers (and break
# %commands)
#$server->sendln($_[1]);
});
return;
}
# This should never happen.
warn "Unanswerable prompt: $event->{text}\n";
return;
}
TLily::Event::event_r(type => "prompt",
order => "after",
call => \&prompt_handler);
TLily::Extend::load_extensions($ui);
$ui->print("\n");
TLily::Event::send(type => 'user_input',
ui => $ui,
text => "%connect") unless ($config{noauto});
while (1) {
eval { TLily::Event::loop; };
# Normal exit.
if ($@ eq '') {
$ui->print("*** Exiting.");
exit;
}
# Non-fatal errors.
if ($@ =~ /^Undefined subroutine/) {
$ui->print("ERROR: ", $@);
next;
}
# Oh, well. Guess we have a problem.
die;
}
sub load_ui {
my %available;
my $default_UI = "TextWindow";
$config{UI} ||= $default_UI;
if ($::TL_LIBDIR =~ m|^//INTERNAL|) {
my $ui = eval "TLily::UI::$config{UI}->new(name => 'main');";
if ($@) {
print STDERR "ERROR: Unable to load UI \"$config{UI}\".\n$@\n";
exit;
}
return $ui;
}
opendir(D,"$::TL_LIBDIR/TLily/UI");
map { s/.pm$//; $available{$_}++; } grep /\.pm$/, readdir(D);
closedir D;
# did they specify a substring or case-insensitive match of an available
# UI?
if (! $available{$config{UI}}) {
foreach (keys %available) {
if (/$config{UI}/i) { $config{UI} = $_; }
}
}
# nope? ok, fall back to TextWindow.
if (! $available{$config{UI}}) {
print "UI \"$config{UI}\" was not found.\n";
print "Press ENTER to try the default UI, or Control-C to quit.\n";
<STDIN>;
$config{UI}="TextWindow";
}
# OK, load the UI..
eval "use TLily::UI::$config{UI}; \$ui = TLily::UI::$config{UI}->new(name => 'main');";
if ($@) {
warn "ERROR: Unable to load UI \"$config{UI}\".\n$@\n";
# sometimes they die() in the new(), leaving a half-allocated UI.
TLily::UI::purge("main");
}
die "FATAL: Unable to load a UI module. Exiting.\n" unless $ui;
return $ui;
}
# This screws up cperl something terrible, so put it at the end where we
# don't care.
sub show_banner() {
$ui->prints(yellow => qq[ ("`-/")_.-'"``-._ \n],
yellow => qq[ . . `; -._ )-;-,_`)],
green => qq[ TigerLily $TLily::Version::VERSION\n],
yellow => qq[ (v_,)' _ )`-.\ ``-'],
bwhite => qq[ "Feel Free"\n],
yellow => qq[ _.- _..-_/ / ((.'\n],
yellow => qq[ ((,.-' ((,/ \n],
bwhite => qq[------------------------------------------------------------------------------\n]);
# ` This comment with the backquote is here merely to let cperl work again.
$ui->style("default");
}
=head1 NAME
tlily - TigerLily
=head1 SYNOPSIS
tlily [-h <host>] [-p <port>] [-UI=<UI Name>]
=head1 DESCRIPTION
This is TigerLily. Need we say more? Yes.
=head1 SEE ALSO
F<http://www.tlily.org/tigerlily/>
For hacking on tlily, you may want to see
L<TLily::UI>,
L<TLily::User>,
L<TLily::Event>,
L<TLily::Registrar>,
L<TLily::Daemon>,
L<TLily::Server>,
L<TLily::Daemon::HTTP>,
L<extensions::slcp_parse>,
and
L<extensions::slcp_output>.
=head1 BUGS
Many. We will eventually fix them and cause more.
=cut
1;