10 our $VERSION = '0.05';
12 use constant { COLOR_TAG => "\00307" };
16 @attributes = qw<tag name version desc author email url banner>;
23 $class = ref($class) || $class;
26 my $caller = (caller 0)[0];
27 unless (defined $opts{tag}) {
28 $opts{tag} = ($caller =~ /([^:]*):*$/) ? $1 : 'UNKNOWN';
30 unless (defined $opts{name}) {
31 $opts{name} = $opts{tag};
33 unless (defined $opts{desc}) {
34 $opts{desc} = $opts{name};
36 unless (defined $opts{version}) {
37 my $version = do { no strict 'refs'; ${$caller.'::VERSION'} };
38 $version = '0.01' unless defined $version;
39 $opts{version} = $version;
41 unless (defined $opts{banner}) {
42 my $email = $opts{email};
43 $email = "<$email>" if defined $email;
44 my $who = join ', ', grep defined, $opts{author}, $email, $opts{url};
45 $who = " ($who)" if length $who;
46 $opts{banner} = $opts{desc} . $who;
49 my $ph = bless \do { local *PH }, $class;
52 *$ph->{$_} = $opts{$_} for @attributes;
55 Xchat::register(@opts{qw<name version banner>}, sub {
57 if (*$ph->{atexit_id}) {
58 my @callbacks = sort { *$ph->{atexit}{$b} <=> *$ph->{atexit}{$a} }
59 keys %{*$ph->{atexit}};
60 $_->($ph) for @callbacks;
64 $ph->add_atexit($opts{unload}) if $opts{unload};
66 $ph->print("\002", $ph->name, ' v', $ph->version,
67 " loaded\002 (", $ph->banner, ")\n");
74 eval "sub $_ { my \$ph = \$_[0]; *\$ph->{$_} }; 1" or die $@ for @attributes;
83 my $data = join '', *$ph->{buf}, @_;
85 while ((my $i = index $data, "\n") >= 0) {
86 Xchat::print(COLOR_TAG . $ph->tag . "\t" . (substr $data, 0, ($i + 1), ''));
95 sub printf { shift->print(sprintf @_) }
100 if (length $ph->{buf}) {
108 my ($ph, $callback) = @_;
110 if (!*$ph->{atexit_id}) {
111 tie my %h, 'Tie::RefHash';
112 *$ph->{atexit} = \%h;
115 return *$ph->{atexit}{$callback} = ++*$ph->{atexit_id};
119 my ($ph, $callback) = @_;
121 if (exists *$ph->{atexit}{$callback}) {
122 delete *$ph->{atexit}{$callback};
131 (defined $ph && $ph->isa(__PACKAGE__)) ? $ph : shift->new(@_);
134 sub PRINT { shift->print(@_) }
136 sub PRINTF { shift->printf(@_) }