All pastes #830561 Raw Edit

Meins

public text v1 · immutable
#830561 ·published 2007-12-24 01:29 UTC
rendered paste body
sub validate_hostmask {
  my ($hostmask) = (@_);

  $hostmask or return 0;

#  if ($hostmask !~ /^[^!@]+![^!@]+@[^!@]+$/) { #dancer ircd
  if ($hostmask !~ /^[^!@]+![i|n]=[^!@]+@[^!@]+$/) {
    debug("'$hostmask' has bad syntax");
    return undef;
  }
#  if ($hostmask =~ /!\*@/ or $hostmask =~ /@[\*\.]*$/) { #dancer ircd
  if ($hostmask =~ /!\*@/ or $hostmask =~ /@[\*\.]*$/) {
    debug("'$hostmask' is an open hostmask (insecure)");
    return undef;
  }
  return 1;
}


# Converts an RFC 1459 IRC hostmask (with * as a glob wildchar) into a
# regular expression.
# params:
#   $hostmask : hostmask to convert
# returns:
#   A regular expression representation of $hostmask
sub hostmask_to_regexp {
  my ($hostmask) = @_;

  # split hostmask into nick and userhost, since the rules differ
  my ($nick, $userhost) = $hostmask =~ /^(.*)!(.*)$/;

  # print "# $nick --- $userhost\n";

  # Substitutions to take a standard IRC hostmask and convert it to
  #   a regexp.  I thought this was pretty clever...  :)

  # {}| are really the lowercase equivalents of []\  (Don't ask me... read RFC1459)
  # The result of this is that { is equivalent to [ in a nick, etc.
  # I couldn't do a direct substitution for each of these.  There would
  #   be problems with the inserted ] and \ chars (used to define the
  #   character classes on the right side of the s///) being picked up
  #   by the second and third s/// expressions.  The solution was to
  #   substitute a character that would never be found in a real hostmask
  #   in a first pass, and convert those characters to the correct
  #   regexp in a second pass.
  # First pass: convert each instance of a char (upper or lower) to some
  #   'impossible' character.  (ascii 01, 02, and 03)
  $nick =~ s/[{[]/\01/g;
  $nick =~ s/[}\]]/\02/g;
  $nick =~ s/[|\\]/\03/g;
  # Second pass: convert each impossible char to the appropriate regexp
  $nick =~ s/\01/[{[]/g;
  $nick =~ s/\02/[}\\]]/g;
  $nick =~ s/\03/[|\\\\]/g;

  $userhost =~ s/([[\]{}|\\])/\\$1/g; # escape \[]{} only in userhost

  $hostmask = "$nick!$userhost";      # recombine
  $hostmask =~ s/([().?^\$])/\\$1/g;  # escape ().?^$
  $hostmask =~ s/\*/.*/g;             # translate wildchar "*" to regexp equivalent ".*"

  return $hostmask;
}