10 our $VERSION = '0.03';
12 use constant { COLOR_TAG => "\00307" };
16 $class = ref($class) || $class || return;
20 my $caller = (caller 0)[0];
21 $opts{tag} ||= ($caller =~ /([^:]*):*$/) ? $1 : 'UNKNOWN';
22 $opts{name} ||= $opts{tag};
23 $opts{desc} ||= $opts{name};
24 $opts{version} ||= do { no strict 'refs'; ${$caller.'::VERSION'} }
27 my $email = $opts{email};
28 $email = '<' . $email . '>' if defined $email;
29 my $desc = join ', ', grep defined, $opts{author}, $email, $opts{url};
30 $desc = ' (' . $desc . ')' if length $desc;
31 $opts{banner} = $opts{desc} . $desc;
33 my $ph = bless \do { local *PH }, $class;
36 *$ph->{$_} = $opts{$_} for qw/tag name version desc author email url banner/;
39 Xchat::register(@opts{qw/name version banner/}, sub {
41 if (*$ph->{atexit_id}) {
42 my @callbacks = sort { *$ph->{atexit}{$b} <=> *$ph->{atexit}{$a} }
43 keys %{*$ph->{atexit}};
44 $_->($ph) for @callbacks;
48 $ph->add_atexit($opts{unload}) if $opts{unload};
50 $ph->print("\002", $ph->name, ' v', $ph->version,
51 " loaded\002 (", $ph->banner, ")\n");
56 eval "sub $_ { *\$_[0]->{$_} }"
57 for qw/tag name version desc author email url banner/;
63 my $data = join '', *$ph->{buf},
65 (@_) ? @_ : ((defined) ? $_ : '');
66 while ((my $i = index $data, "\n") >= 0) {
67 Xchat::print(COLOR_TAG . $ph->tag . "\t" . (substr $data, 0, ($i + 1), ''));
74 sub printf { shift->print(sprintf @_) }
79 $ph->print("\n") if length $ph->{buf};
83 my ($ph, $callback) = @_;
84 return unless $ph && $callback && ref $callback eq 'CODE';
85 if (!*$ph->{atexit_id}) {
86 tie my %h, 'Tie::RefHash';
89 return *$ph->{atexit}{$callback} = ++*$ph->{atexit_id};
93 my ($ph, $callback) = @_;
94 return unless $ph && $callback;
95 if (exists *$ph->{atexit}{$callback}) {
96 delete *$ph->{atexit}{$callback};
104 ((defined $ph && $ph->isa(__PACKAGE__))
109 sub PRINT { shift->print(@_) }
111 sub PRINTF { shift->printf(@_) }
113 my $ph = __PACKAGE__->new(
114 name => 'Extended Xchat Perl Interface',
115 desc => 'Adds extended support for Perl scripts',
116 author => 'Vincent Pit (VPIT)',
117 email => 'perl@profvince.com',
118 url => 'http://www.profvince.com',