| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253 |
- package Pisg::Common;
- # pisg - Perl IRC Statistics Generator
- #
- # Copyright (C) 2001-2012 The pisg project
- #
- # 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;
|