Common.pm 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. package Pisg::Common;
  2. # pisg - Perl IRC Statistics Generator
  3. #
  4. # Copyright (C) 2001-2005 <Morten Brix Pedersen> - morten@wtf.dk
  5. # Copyright (C) 2003-2005 Christoph Berg <cb@df7cb.de>
  6. #
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 2 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program; if not, write to the Free Software
  19. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. =head1 NAME
  21. Pisg::Common - some common functions of pisg.
  22. =cut
  23. use Exporter;
  24. @ISA = ('Exporter');
  25. @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);
  26. use strict;
  27. $^W = 1;
  28. my (%aliases, %aliaswilds, %ignored, %aliasseen, %ignored_urls, %url_seen);
  29. my (%aliases2, %aliaswilds2, %ignored2, %aliasseen2, %ignored_urls2, %url_seen2);
  30. # add_alias assumes that the first argument is the true nick and the second is
  31. # the alias, but will accomidate other arrangements if necessary.
  32. sub add_alias
  33. {
  34. my ($nick, $alias) = @_;
  35. my $lcnick = lc($nick);
  36. my $lcalias = lc($alias);
  37. if (not defined $aliases{$lcnick}) {
  38. if (not defined $aliases{$lcalias}) {
  39. $aliases{$lcnick} = $nick;
  40. $aliases{$lcalias} = $nick;
  41. } else {
  42. $aliases{$lcnick} = $aliases{$lcalias};
  43. }
  44. } elsif (not defined $aliases{$lcalias}) {
  45. $aliases{$lcalias} = $aliases{$lcnick};
  46. }
  47. }
  48. sub add_aliaswild
  49. {
  50. my ($nick, $alias) = @_;
  51. my $lcnick = lc($nick);
  52. my $lcalias = lc($alias);
  53. if (not defined $aliases{$lcnick}) {
  54. $aliases{$lcnick} = $nick;
  55. }
  56. $aliaswilds{$lcalias} = $nick;
  57. }
  58. sub add_ignore
  59. {
  60. my $nick = shift;
  61. $ignored{$nick} = 1;
  62. }
  63. sub is_ignored
  64. {
  65. my $nick = shift;
  66. if ($ignored{$nick}) {
  67. return 1;
  68. } elsif ($ignored{is_nick($nick)}) {
  69. $ignored{$nick} = 1;
  70. } else {
  71. $ignored{$nick} = 0;
  72. }
  73. }
  74. sub url_is_ignored
  75. {
  76. my $url = shift;
  77. if ($ignored_urls{$url}) {
  78. return 1;
  79. }
  80. }
  81. sub add_url_ignore
  82. {
  83. my $url = shift;
  84. $ignored_urls{$url} = 1;
  85. }
  86. # Sub to do a -cheap- check on wether or not a word is a nick
  87. # This will only match if it has seen it used as a nick
  88. sub is_nick
  89. {
  90. my ($nick) = @_;
  91. my $lcnick = lc($nick);
  92. if ($aliases{$lcnick}) {
  93. return $aliases{$lcnick};
  94. } elsif ($aliasseen{$lcnick}) {
  95. return $aliasseen{$lcnick}
  96. }
  97. return 0;
  98. }
  99. # For efficiency reasons, find_alias() caches aliases when it finds them,
  100. # because the regexp search through %aliaswilds is *really* expensive.
  101. # %aliasseen is used to mark nicks for which nothing matches--we can't add
  102. # such nicks to an actual alias, though, because they might be aliased (e.g.
  103. # by a nick change) later.
  104. sub find_alias
  105. {
  106. my ($nick) = @_;
  107. my $lcnick = lc($nick);
  108. if ($aliases{$lcnick}) {
  109. return $aliases{$lcnick};
  110. } elsif ($aliasseen{$lcnick}) {
  111. return $aliasseen{$lcnick};
  112. } else {
  113. foreach (keys %aliaswilds) {
  114. if ($nick =~ /^$_$/i) {
  115. add_alias($aliaswilds{$_}, $lcnick);
  116. return $aliaswilds{$_};
  117. }
  118. }
  119. }
  120. $aliasseen{$lcnick} = $nick;
  121. return $nick;
  122. }
  123. sub store_aliases
  124. {
  125. %aliases2 = %aliases;
  126. %aliaswilds2 = %aliaswilds;
  127. %ignored2 = %ignored;
  128. %aliasseen2 = %aliasseen;
  129. %ignored_urls2 = %ignored_urls;
  130. %url_seen2 = %url_seen;
  131. }
  132. sub restore_aliases
  133. {
  134. %aliases = %aliases2;
  135. %aliaswilds = %aliaswilds2;
  136. %ignored = %ignored2;
  137. %aliasseen = %aliasseen2;
  138. %ignored_urls = %ignored_urls2;
  139. %url_seen = %url_seen2;
  140. }
  141. sub match_urls
  142. {
  143. my $str = shift;
  144. my @urls;
  145. # we don't treat mailto: as URL here
  146. while ($str =~ /((?:(?:https?|ftp|telnet|news):\/\/|(?:(?:(www)|(ftp))[\w-]*\.))[-\w\/~\@:]+\.\S+[\w\/])/gio) {
  147. my $url = $2 ? "http://$1" : ($3 ? "ftp://$1" : $1);
  148. my $url_strip = $url;
  149. $url_strip =~ s/\/$//;
  150. $url_seen{$url_strip} ||= $url; # normalize URL to first seen form
  151. push (@urls, $url_seen{$url_strip});
  152. }
  153. return @urls;
  154. }
  155. sub htmlentities
  156. {
  157. my $str = shift;
  158. my $charset = shift;
  159. $str =~ s/\&/\&amp;/go;
  160. $str =~ s/\</\&lt;/go;
  161. $str =~ s/\>/\&gt;/go;
  162. if ($charset =~ /iso-8859-1/i) { # this is for people without Text::Iconv
  163. $str =~ s/ü/&uuml;/go;
  164. $str =~ s/ö/&ouml;/go;
  165. $str =~ s/ä/&auml;/go;
  166. $str =~ s/ß/&szlig;/go;
  167. $str =~ s/å/&aring;/go;
  168. $str =~ s/æ/&aelig;/go;
  169. $str =~ s/ø/&oslash;/go;
  170. $str =~ s/Å/&Aring;/go;
  171. $str =~ s/Æ/&AElig;/go;
  172. $str =~ s/Ø/&Oslash;/go;
  173. $str =~ s/\x95/\&bull;/go;
  174. }
  175. return $str;
  176. }
  177. sub urlencode
  178. {
  179. my $str = shift;
  180. $str =~ s/([^\w_.\/?=:+-])/sprintf "%%%02X", ord($1)/ge;
  181. return $str;
  182. }
  183. sub randomglob
  184. {
  185. my ($pattern, $globpath, $nick) = @_;
  186. return $pattern unless $pattern =~ /[*?]/;
  187. my @globs = glob $globpath . $pattern;
  188. my $return = $globs[int(rand(@globs))];
  189. unless($return) {
  190. print STDERR "Warning: picture $globpath$pattern for $nick not found\n";
  191. return $pattern;
  192. }
  193. $return =~ s/^$globpath//;
  194. return $return;
  195. }
  196. sub wordlist_regexp
  197. {
  198. my $list = shift;
  199. $list =~ s/^\s+//; # split ignores trailing empty fields
  200. my @words = split(/\s+/, $list);
  201. my $regexpaliases = shift;
  202. unless($regexpaliases) {
  203. map {
  204. $_ = quotemeta; # quote everything
  205. s/\\\*/\\S*/g; # replace \*
  206. s/^\\S\*// or $_ = "\\b$_"; # ... but remote it at beginning/end of word
  207. s/\\S\*$// or $_ = "$_\\b";
  208. } @words;
  209. }
  210. return join '|', @words;
  211. }
  212. 1;