File: Util.pm

package info (click to toggle)
cgiirc 0.5.9-3squeeze1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 896 kB
  • ctags: 443
  • sloc: perl: 9,125; ansic: 163; sh: 99; makefile: 43
file content (77 lines) | stat: -rw-r--r-- 1,526 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# $Id: Util.pm,v 1.5 2006/04/30 12:51:55 dgl Exp $
package IRC::Util;
use Exporter;
@ISA = qw(Exporter);
@EXPORT = qw(is_valid_channel is_valid_nickname is_valid_server make_lowercase check_mode match_mask fullhost2nick fullhost2host add_mode del_mode);

use strict;

sub is_valid_channel {
   return 0 if length $_[0] > 64;
   return 0 if $_[0] =~ /[ ,]/;
   return 1 if $_[0] =~ /^[#&]/;
   return 0;
}

sub is_valid_nickname {
   return 0 if length $_[0] > 32 or length $_[0] < 1;
   return 0 if $_[0] =~ / /;
   return 0 if $_[0] =~ /^[0-9#&]/;
   return 1;
}

sub is_valid_server {
   return 0 if $_[0] !~ /\./;
   return 0 if $_[0] =~ /[!@]/;
   return 0 if $_[0] =~ /[^-A-Za-z0-9\*\._]/;
   return 1;
}

sub make_lowercase{
   my $lc = shift;
   $lc =~ tr/A-Z\[\]\\/a-z\{\}\|/;
   return $lc;
}

sub check_mode {
   my($mode,$bit) = @_;
   return 1 if $mode =~ /\S*\Q$bit\E/;
}

# should really split all the parts up...
sub match_mask {
   my($check,$mask) = @_;
   $mask = quotemeta $mask;
   $mask =~ s/\\\?/./g;
   $mask =~ s/\\\*/.*?/g;
   return 1 if $check =~ /$mask/;
   0;
}

sub fullhost2nick {
   my $host = shift;
   $host =~ s/!.*$//;
   return $host;
}

sub fullhost2host {
   my $host = shift;
   $host =~ s/^.*?!//;
   return $host;
}

sub add_mode{
   my($mode,$bit) = @_;
   return $mode if $mode =~ /^\S*\Q$bit\E/;
   $mode =~ s/^(\S*)/$1$bit/;
   return $mode;
}

sub del_mode{
   my($mode,$bit) = @_;
   return $mode if $mode !~ /^\S*\Q$bit\E/;
   $mode =~ s/^(\S*)$bit/$1/;
   return $mode;
}

1;