package N0i::Xchat::Colors; use strict; use warnings; use base 'N0i::Xchat::Base'; sub version { '2005040501' } sub new { my ($class, %o) = @_; my $self = { %o, __hooks => [], pevents => {}, switch_pevents => {} }; bless $self, $class; $self->_initialize_pevents; return $self; } sub _initialize_pevents { my $self = shift; my $file = File::Spec->catfile(Xchat::get_info('xchatdir'),'pevents.conf'); my $pevents = ''; { local $/; open PEVENTS, $file or die "cannot open $file: $!\n"; $pevents = ; close PEVENTS; } foreach ($self->handlers) { my ($name, $key, $has, $switch) = @$_; $self->{switch_pevents}{$key} = 1 if $switch; next unless $pevents =~ m{event_name=$name\nevent_text=(.+?)\n}s; $self->{pevents}{$key} = $1; for ($self->{pevents}{$key}) { # fuckin uuuuuuugly! s/\$t/\t/g; s/\%C/\cC/g; s/\%B/\cB/g; s/\%U/\cU/g; s/\%R/\cR/g; s/\%O/\cO/g; } } } sub color_of { my ($self, $nick) = @_; foreach (@{ $self->{colors} }) { if ($nick =~ /^$_->[1]$/) { return wantarray ? @$_ : $_->[0]; } } return 0; } sub Ncolor { my ($self, $nick, $highlight) = @_; my $color = $self->color_of($nick); if ($highlight) { return "\cO\cB\cC$color$nick\cO"; } else { return "\cO\cC$color$nick\cO"; } } sub colorize { my ($self, $message) = @_; my @patterns = @{ $self->{colors} }; # drop default pattern pop @patterns; # drop IRC-Services (may match any 1 letter word) :( shift @patterns; foreach (@patterns) { my ($color, $pattern) = @$_; $message =~ s{(?<=\b)($pattern)(?=\b)}{\cC$color$1\cC0}g; } # underline URLs :)) # $message =~ s{((?i:ht|f)tps?://\S+|www\d?\.\S+)}{\cU$1\cU}ig; return $message; } sub handlers { return ( [ 'Add Notify', 'notify_add', 0, 1 ], [ 'Banned', 'you_banned'], [ 'Change Nick', 'nick_change'], [ 'Channel Action', 'chan_act'], [ 'Channel Action Hilight', 'chan_act_hi'], [ 'Channel Ban', 'mode_ban'], [ 'Channel Creation', 'chan_create'], [ 'Channel DeHalfOp', 'mode_dehalfop'], [ 'Channel DeOp', 'mode_deop'], [ 'Channel DeVoice', 'mode_devoice'], [ 'Channel Exempt', 'mode_exempt'], [ 'Channel Half-Operator', 'mode_halfop'], [ 'Channel INVITE', 'chan_invite'], [ 'Channel Message', 'chan_msg'], [ 'Channel Mode Generic', 'mode_generic', 1 ], [ 'Channel Modes', 'chan_modes'], [ 'Channel Msg Hilight', 'chan_msg_hi'], [ 'Channel Notice', 'notice_chan', 1 ], [ 'Channel Operator', 'mode_chanop'], [ 'Channel Remove Exempt', 'mode_rm_exempt'], [ 'Channel Remove Invite', 'mode_rm_invite'], [ 'Channel Remove Keyword', 'mode_rm_key'], [ 'Channel Remove Limit', 'mode_rm_limit'], [ 'Channel Set Key', 'mode_set_key'], [ 'Channel Set Limit', 'mode_set_limit'], [ 'Channel UnBan', 'mode_unban'], [ 'Channel Voice', 'mode_voice'], [ 'CTCP Generic', 'ctcp_gen', 1 ], [ 'CTCP Generic to Channel', 'ctcp_gen_chan', 1 ], [ 'CTCP Send', 'ctcp_send', 0, 1 ], [ 'Delete Notify', 'notify_delete', 0, 1 ], [ 'Ignore Add', 'ignore_add', 0, 1 ], [ 'Ignore Changed', 'ignore_changed', 0, 1 ], [ 'Ignore Remove', 'ignore_remove', 0, 1 ], [ 'Invited', 'invited', 1 ], [ 'Invite', 'mode_invite'], [ 'Join', 'chan_join', 1, 1 ], [ 'Keyword', 'mode_key'], [ 'Kick', 'mode_kick', 1 ], [ 'Killed', 'you_killed'], [ 'Message Send', 'msg_send'], [ 'Nick Clash', 'nick_clash'], [ 'Notice', 'notice_a'], [ 'Notice Send', 'notice_send'], [ 'Notify Offline', 'notify_offline', 0, 1 ], [ 'Notify Online', 'notify_online', 0, 1 ], [ 'Part', 'chan_part', 1, 1 ], [ 'Part with Reason', 'part_w_reason', 1, 1 ], [ 'Ping Reply', 'ping_reply', 0, 1 ], [ 'Private Message', 'prv_msg'], [ 'Private Message to Dialog', 'prv_msg_dlg'], [ 'Quit', 'irc_quit', 1, 1 ], [ 'Raw Modes', 'modes_raw'], [ 'Resolving User', 'resolving'], [ 'Topic Change', 'topic_change', 1 ], [ 'Topic', 'chan_topic'], [ 'Topic Creation', 'topic_create', 1 ], [ 'User Limit', 'user_limit'], [ 'You Kicked', 'you_kicked', 1 ], [ 'Your Message', 'ur_msg'], ); } sub pevent { my ($self, $pevent) = @_; return ( $self->{pevents}{$pevent} || '', $self->switch_context($pevent) ); } sub hilite { return $_[0]->{hilite} } sub hilite_off { return $_[0]->{hilite_off} || $_[0]->{hilite} } sub lolite { return $_[0]->{lolite} } sub lolite_off { return $_[0]->{lolite_off} || $_[0]->{lolite} } sub switch { return $_[0]->{switch} } sub switch_context { my ($self, $pevent) = @_; # switching context disabled return unless Xchat::get_info('channel') =~ $self->switch; # switching not enabled for the current print event return unless $self->{switch_pevents}{$pevent}; return [ Xchat::get_info('network'), Xchat::get_info('network') ]; } sub capab { my ($self, $feat) = @_; return $feat && exists $self->{capab}{$feat} ? $self->{capab}{$feat} : 0 ; } 1;