Common.pm 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package Pisg::Common;
  2. use Exporter;
  3. @ISA = ('Exporter');
  4. @EXPORT = qw(add_alias add_aliaswild add_ignore is_ignored find_alias match_url match_email htmlentities);
  5. use strict;
  6. $^W = 1;
  7. my ($conf, $debug);
  8. my (%aliases, %aliaswilds, %ignored, %aliasseen);
  9. sub init_common
  10. {
  11. $debug = shift;
  12. }
  13. # add_alias assumes that the first argument is the true nick and the second is
  14. # the alias, but will accomidate other arrangements if necessary.
  15. sub add_alias
  16. {
  17. my ($nick, $alias) = @_;
  18. my $lcnick = lc($nick);
  19. my $lcalias = lc($alias);
  20. if (not defined $aliases{$lcnick}) {
  21. if (not defined $aliases{$lcalias}) {
  22. $aliases{$lcnick} = $nick;
  23. $aliases{$lcalias} = $nick;
  24. } else {
  25. $aliases{$lcnick} = $aliases{$lcalias};
  26. }
  27. } elsif (not defined $aliases{$lcalias}) {
  28. $aliases{$lcalias} = $aliases{$lcnick};
  29. } elsif ($aliases{$lcnick} ne $aliases{$lcalias}) {
  30. $debug->("Alias collision: alias $alias -> $aliases{$lcalias} but nick $nick -> $aliases{$lcnick}");
  31. }
  32. #$debug->("Alias added: $alias -> $aliases{$lcalias}");
  33. }
  34. sub add_aliaswild
  35. {
  36. my ($nick, $alias) = @_;
  37. my $lcnick = lc($nick);
  38. my $lcalias = lc($alias);
  39. if (not defined $aliases{$lcnick}) {
  40. $aliases{$lcnick} = $nick;
  41. }
  42. $aliaswilds{$lcalias} = $nick;
  43. $debug->("Aliaswild added: $alias -> $aliaswilds{$lcalias}");
  44. }
  45. sub add_ignore
  46. {
  47. my $nick = shift;
  48. $ignored{$nick} = 1;
  49. }
  50. sub is_ignored
  51. {
  52. my $nick = shift;
  53. if ($ignored{$nick} || $ignored{find_alias($nick)}) {
  54. return 1;
  55. }
  56. }
  57. # For efficiency reasons, find_alias() caches aliases when it finds them,
  58. # because the regexp search through %aliaswilds is *really* expensive.
  59. # %aliasseen is used to mark nicks for which nothing matches--we can't add
  60. # such nicks to an actual alias, though, because they might be aliased (e.g.
  61. # by a nick change) later.
  62. sub find_alias
  63. {
  64. my ($nick) = @_;
  65. my $lcnick = lc($nick);
  66. if ($aliases{$lcnick}) {
  67. return $aliases{$lcnick};
  68. } elsif ($aliasseen{$lcnick}) {
  69. return $aliasseen{$lcnick};
  70. } else {
  71. foreach (keys %aliaswilds) {
  72. if ($nick =~ /^$_$/i) {
  73. add_alias($aliaswilds{$_}, $lcnick);
  74. return $aliaswilds{$_};
  75. }
  76. }
  77. }
  78. $aliasseen{$lcnick} = $nick;
  79. return $nick;
  80. }
  81. sub match_url
  82. {
  83. my ($str) = @_;
  84. if ($str =~ /(http|https|ftp|telnet|news)(:\/\/[-a-zA-Z0-9_]+\.[-a-zA-Z0-9.,_~=:;&@%?#\/+]+)/) {
  85. return "$1$2";
  86. }
  87. return undef;
  88. }
  89. sub match_email
  90. {
  91. my ($str) = @_;
  92. if ($str =~ /([-a-zA-Z0-9._]+@[-a-zA-Z0-9_]+\.[-a-zA-Z0-9._]+)/) {
  93. return $1;
  94. }
  95. return undef;
  96. }
  97. sub htmlentities
  98. {
  99. my $str = shift;
  100. $str =~ s/\&/\&/go;
  101. $str =~ s/\</\&lt;/go;
  102. $str =~ s/\>/\&gt;/go;
  103. $str =~ s/ü/&uuml;/go;
  104. $str =~ s/ö/&ouml;/go;
  105. $str =~ s/ä/&auml;/go;
  106. $str =~ s/ß/&szlig;/go;
  107. $str =~ s/å/&aring;/go;
  108. $str =~ s/æ/&aelig;/go;
  109. $str =~ s/ø/&oslash;/go;
  110. return $str;
  111. }
  112. 1;