4
0

Common.pm 6.4 KB

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