10 use lib Xchat::get_info('xchatdir');
11 use Xchat::XPI::Utils qw<called_from_script>;
13 our $VERSION = '0.05';
15 use constant { COLOR_TAG => "\00307" };
19 @attributes = qw<tag name version desc author email url banner>;
26 $class = ref($class) || $class;
29 my $caller = (caller 0)[0];
31 unless (defined $opts{tag}) {
32 $opts{tag} = ($caller =~ /([^:]*):*$/) ? $1 : 'UNKNOWN';
34 unless (defined $opts{name}) {
35 $opts{name} = $opts{tag};
37 unless (defined $opts{desc}) {
38 $opts{desc} = $opts{name};
40 unless (defined $opts{version}) {
41 my $version = do { no strict 'refs'; ${$caller.'::VERSION'} };
42 $version = '0.01' unless defined $version;
43 $opts{version} = $version;
45 unless (defined $opts{banner}) {
46 my $author = defined $opts{author} ? $opts{author} : 'unknown hacker';
47 my $email = defined $opts{email} ? "<$opts{email}>" : undef;
48 my $who = join ', ', grep defined, $email, $opts{url};
49 $who = " ($who)" if length $who;
50 $opts{banner} = $opts{desc} . " by $author$who";
53 my $ph = bless \do { local *PH }, $class;
56 *$ph->{$_} = $opts{$_} for @attributes;
60 Xchat::register(@opts{qw<name version banner>}, sub {
62 if (*$ph->{atexit_id}) {
63 my @callbacks = sort { *$ph->{atexit}{$b} <=> *$ph->{atexit}{$a} }
64 keys %{*$ph->{atexit}};
65 $_->($ph) for @callbacks;
70 $ph->add_atexit($opts{unload}) if $opts{unload};
72 $ph->print("\002", $ph->name, ' v', $ph->version,
73 " loaded\002 (", $ph->banner, ")\n");
80 eval "sub $_ { my \$ph = \$_[0]; *\$ph->{$_} }; 1" or die $@ for @attributes;
89 my $data = join '', *$ph->{buf}, @_;
91 while ((my $i = index $data, "\n") >= 0) {
92 Xchat::print(COLOR_TAG . $ph->tag . "\t" . (substr $data, 0, ($i + 1), ''));
101 sub printf { shift->print(sprintf @_) }
106 if (length $ph->{buf}) {
114 my ($ph, $callback) = @_;
116 if (!*$ph->{atexit_id}) {
117 tie my %h, 'Tie::RefHash';
118 *$ph->{atexit} = \%h;
121 return *$ph->{atexit}{$callback} = ++*$ph->{atexit_id};
125 my ($ph, $callback) = @_;
127 if (exists *$ph->{atexit}{$callback}) {
128 delete *$ph->{atexit}{$callback};
137 (defined $ph && $ph->isa(__PACKAGE__)) ? $ph : shift->new(@_);
140 sub PRINT { shift->print(@_) }
142 sub PRINTF { shift->printf(@_) }