/usr/share/perl5/Pisg/Common.pm is in pisg 0.73-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | package Pisg::Common;
# pisg - Perl IRC Statistics Generator
#
# Copyright (C) 2001-2005 <Morten Brix Pedersen> - morten@wtf.dk
# Copyright (C) 2003-2005 Christoph Berg <cb@df7cb.de>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
=head1 NAME
Pisg::Common - some common functions of pisg.
=cut
use Exporter;
@ISA = ('Exporter');
@EXPORT = qw(add_alias add_aliaswild add_ignore add_url_ignore is_ignored url_is_ignored find_alias store_aliases restore_aliases match_urls match_email htmlentities urlencode is_nick randomglob wordlist_regexp);
use strict;
$^W = 1;
my (%aliases, %aliaswilds, %ignored, %aliasseen, %ignored_urls, %url_seen);
my (%aliases2, %aliaswilds2, %ignored2, %aliasseen2, %ignored_urls2, %url_seen2);
# add_alias assumes that the first argument is the true nick and the second is
# the alias, but will accomidate other arrangements if necessary.
sub add_alias
{
my ($nick, $alias) = @_;
my $lcnick = lc($nick);
my $lcalias = lc($alias);
if (not defined $aliases{$lcnick}) {
if (not defined $aliases{$lcalias}) {
$aliases{$lcnick} = $nick;
$aliases{$lcalias} = $nick;
} else {
$aliases{$lcnick} = $aliases{$lcalias};
}
} elsif (not defined $aliases{$lcalias}) {
$aliases{$lcalias} = $aliases{$lcnick};
}
}
sub add_aliaswild
{
my ($nick, $alias) = @_;
my $lcnick = lc($nick);
my $lcalias = lc($alias);
if (not defined $aliases{$lcnick}) {
$aliases{$lcnick} = $nick;
}
$aliaswilds{$lcalias} = $nick;
}
sub add_ignore
{
my $nick = shift;
$ignored{$nick} = 1;
}
sub is_ignored
{
my $nick = shift;
if ($ignored{$nick}) {
return 1;
} elsif ($ignored{is_nick($nick)}) {
$ignored{$nick} = 1;
} else {
$ignored{$nick} = 0;
}
}
sub url_is_ignored
{
my $url = shift;
if ($ignored_urls{$url}) {
return 1;
}
}
sub add_url_ignore
{
my $url = shift;
$ignored_urls{$url} = 1;
}
# Sub to do a -cheap- check on wether or not a word is a nick
# This will only match if it has seen it used as a nick
sub is_nick
{
my ($nick, $wilds) = @_;
my $lcnick = lc($nick);
if ($aliases{$lcnick}) {
return $aliases{$lcnick};
} elsif ($aliasseen{$lcnick}) {
return $aliasseen{$lcnick}
}
# check aliaswilds if were are in _mostusedword()
if (defined $wilds) {
foreach (keys %aliaswilds) {
if ($lcnick =~ /^$_$/i) {
add_alias($aliaswilds{$_}, $lcnick);
return $aliaswilds{$_};
}
}
}
return 0;
}
# For efficiency reasons, find_alias() caches aliases when it finds them,
# because the regexp search through %aliaswilds is *really* expensive.
# %aliasseen is used to mark nicks for which nothing matches--we can't add
# such nicks to an actual alias, though, because they might be aliased (e.g.
# by a nick change) later.
sub find_alias
{
my ($nick) = @_;
my $lcnick = lc($nick);
if ($aliases{$lcnick}) {
return $aliases{$lcnick};
} elsif ($aliasseen{$lcnick}) {
return $aliasseen{$lcnick};
} else {
foreach (keys %aliaswilds) {
if ($nick =~ /^$_$/i) {
add_alias($aliaswilds{$_}, $lcnick);
return $aliaswilds{$_};
}
}
}
$aliasseen{$lcnick} = $nick;
return $nick;
}
sub store_aliases
{
%aliases2 = %aliases;
%aliaswilds2 = %aliaswilds;
%ignored2 = %ignored;
%aliasseen2 = %aliasseen;
%ignored_urls2 = %ignored_urls;
%url_seen2 = %url_seen;
}
sub restore_aliases
{
%aliases = %aliases2;
%aliaswilds = %aliaswilds2;
%ignored = %ignored2;
%aliasseen = %aliasseen2;
%ignored_urls = %ignored_urls2;
%url_seen = %url_seen2;
}
sub match_urls
{
my $str = shift;
my @urls;
# we don't treat mailto: as URL here
while ($str =~ /((?:(?:https?|ftp|telnet|news):\/\/|(?:(?:(www)|(ftp))[\w-]*\.))[-\w\/~\@:]+\.\S+[\w\/])/gio) {
my $url = $2 ? "http://$1" : ($3 ? "ftp://$1" : $1);
my $url_strip = $url;
$url_strip =~ s/\/$//;
$url_seen{$url_strip} ||= $url; # normalize URL to first seen form
push (@urls, $url_seen{$url_strip});
}
return @urls;
}
sub htmlentities
{
my $str = shift;
my $charset = shift;
return $str unless $str;
$str =~ s/\&/\&/go;
$str =~ s/\</\</go;
$str =~ s/\>/\>/go;
if ($charset and $charset =~ /iso-8859-1/i) { # this is for people without Text::Iconv
$str =~ s/ü/ü/go;
$str =~ s/ö/ö/go;
$str =~ s/ä/ä/go;
$str =~ s/ß/ß/go;
$str =~ s/å/å/go;
$str =~ s/æ/æ/go;
$str =~ s/ø/ø/go;
$str =~ s/Å/Å/go;
$str =~ s/Æ/Æ/go;
$str =~ s/Ø/Ø/go;
$str =~ s/\x95/\•/go;
}
return $str;
}
sub urlencode
{
my $str = shift;
$str =~ s/([^\w_.\/?=:+-])/sprintf "%%%02X", ord($1)/ge;
return $str;
}
sub randomglob
{
my ($pattern, $globpath, $nick) = @_;
return $pattern unless $pattern =~ /[*?]/;
my @globs = glob $globpath . $pattern;
my $return = $globs[int(rand(@globs))];
unless($return) {
print STDERR "Warning: picture $globpath$pattern for $nick not found\n";
return $pattern;
}
$return =~ s/^$globpath//;
return $return;
}
sub wordlist_regexp
{
my $list = shift;
$list =~ s/^\s+//; # split ignores trailing empty fields
my @words = split(/\s+/, $list);
my $regexpaliases = shift;
unless($regexpaliases) {
map {
$_ = quotemeta; # quote everything
s/\\\*/\\S*/g; # replace \*
s/^\\S\*// or $_ = "\\b$_"; # ... but remote it at beginning/end of word
s/\\S\*$// or $_ = "$_\\b";
} @words;
}
return join '|', @words;
}
1;
|