]> git.donarmstrong.com Git - infobot.git/blob - src/Files.pl
d9d96ede91940a613618b5c4ea2c5bb049fc3519
[infobot.git] / src / Files.pl
1 #
2 # Files.pl: Open and close, read and probably write files.
3 #   Author: xk <xk@leguin.openprojects.net>
4 #  Version: v0.2 (2000502)
5 #  Created: 19991221
6 #
7
8 if (&IsParam("useStrict")) { use strict; }
9
10 # File: Language support.
11 sub loadLang {
12     my ($file) = @_;
13     my $langCount = 0;
14     my $replyName;
15
16     if (!open(FILE, $file)) {
17         &ERROR("FAILED loadLang ($file): $!");
18         exit 0;
19     }
20
21     undef %lang;                # for rehash.
22
23     while (<FILE>) {
24         chop;
25         if ($_ eq "" || /^#/) {
26             undef $replyName;
27             next;
28         }
29
30         if (!/^\s/) {
31             $replyName = $_;
32             next;
33         }
34
35         s/^[\s\t]+//g;
36         if (!$replyName) {
37             &status("loadLang: bad line ('$_')");
38             next;
39         }
40
41         $lang{$replyName}{$_} = 1;
42         $langCount++;
43     }
44     close FILE;
45
46     $file =~ s/^.*\///;
47     &status("Loaded lang $file ($langCount items)");
48 }
49
50 # File: Ignore list.
51 sub loadIgnore {
52     my ($file)  = @_;
53     %ignoreList = ();
54
55     if (!open(FILE, $file)) {
56         &ERROR("FAILED loadIgnore ($file): $!");
57         return;
58     }
59     &status("Loading ignore list...");
60
61     my $count = 0;
62     while (<FILE>) {
63         chomp;
64         next if /^\s*\#/;
65         next unless /\S/;
66
67         if (/^(\S+)[\t\s]+(\S+)([\t\s]+.*)?$/) {
68             &status("  $2");
69             $ignoreList{$2} = 1;
70             $count++;
71         }
72     }
73     close FILE;
74
75     $file =~ s/^.*\///;
76     &status("Loaded ignore $file ($count masks)");
77 }
78
79 # File: Irc Servers list.
80 sub loadIRCServers {
81     my ($file) = @_;
82     @ircServers = ();
83     %ircPort = ();
84
85     if (!open(FILE, $file)) {
86         &ERROR("FAILED loadIRCServers ($file): $!");
87         exit 0;
88     }
89
90     while (<FILE>) {
91         chop;
92         next if /^\s*$/;
93         next if /^[\#\[ ]/;
94
95         if (/^(\S+)(:(\d+))?$/) {
96             push(@ircServers,$1);
97             $ircPort{$1} = ($3 || 6667);
98         } else {
99             &status("loadIRCServers: invalid line => '$_'.");
100         }
101     }
102     close FILE;
103
104     $file =~ s/^.*\///;
105     &status("Loaded ircServers $file (". scalar(@ircServers) ." servers)");
106 }
107
108 # File: User List.
109 sub loadUsers {
110     my ($file) = @_;
111     %userList = ();     # clear it.
112
113     if (!open(FILE, $file)) {
114         &ERROR("FAILED loadUsers ($file): $!");
115         exit 0;
116     }
117
118     my $userName;
119
120     while (<FILE>) {
121         next if /^\s*$/;
122         next if /^#/;
123
124         if (/^UserEntry\s+(.+?)\s/) {
125             $userName = $1;
126             if (/\s*\{\s*/) {
127                 while (<FILE>) {
128                     if (/^\s*(\w+)\s+(.+);$/) {
129                         my ($opt,$val) = ($1,$2);
130
131                         $opt =~ tr/A-Z/a-z/;
132                         $val =~ s/\"//g;
133                         $val =~ s/\+// if ($opt =~ /^flags$/i);
134
135                         if ($opt =~ /^mask$/i) {
136                             $userList{$userName}{$opt}{$val} = 1;
137                         } else {
138                             $userList{$userName}{$opt} = $val;
139                         }
140                     } elsif (/^\s*\}\s*$/) {
141                         last;
142                     }
143                 }
144             } else {
145                 status("parse error: User Entry $userName without right brace");
146             }
147         }
148     }
149     close FILE;
150
151     return unless (&IsParam("VERBOSITY"));
152
153     $file =~ s/^.*\///;
154     &status("Loaded userlist $file (". scalar(keys %userList) ." users)");
155     foreach $userName (keys %userList) {
156         &status("  $userName:");
157         &status("    flags: +$userList{$userName}{'flags'}");
158
159         foreach (keys %{$userList{$userName}{'mask'}}) {
160             status("    hostmask: $_");
161         }
162     }
163 }
164
165 1;