]> git.donarmstrong.com Git - infobot.git/blob - src/Files.pl
* Add vim formatting comments ( # vim:ts=4:sw=4:expandtab:tw=80 )
[infobot.git] / src / Files.pl
1 #
2 # Files.pl: Open and close, read and probably write files.
3 #   Author: dms
4 #  Version: v0.3 (20010120)
5 #  Created: 19991221
6 #
7
8 use strict;
9
10 use vars qw(%lang %ircPort);
11 use vars qw(@ircServers);
12 use vars qw($bot_config_dir);
13
14 # File: Language support.
15 sub loadLang {
16     my ($file) = @_;
17     my $langCount = 0;
18     my $replyName;
19
20     if (!open(FILE, $file)) {
21         &ERROR("Failed reading lang file ($file): $!");
22         exit 0;
23     }
24
25     undef %lang;                # for rehash.
26
27     while (<FILE>) {
28         chop;
29         if ($_ eq '' || /^#/) {
30             undef $replyName;
31             next;
32         }
33
34         if (!/^\s/) {
35             $replyName = $_;
36             next;
37         }
38
39         s/^[\s\t]+//g;
40         if (!$replyName) {
41             &status("loadLang: bad line ('$_')");
42             next;
43         }
44
45         $lang{$replyName}{$_} = 1;
46         $langCount++;
47     }
48     close FILE;
49
50     $file =~ s/^.*\///;
51     &status("Loaded $file ($langCount items)");
52 }
53
54 # File: Irc Servers list.
55 sub loadIRCServers {
56     my ($file)  = $bot_config_dir."/infobot.servers";
57     @ircServers = ();
58     %ircPort = ();
59
60     if (!open(FILE, $file)) {
61         &ERROR("Failed reading server list ($file): $!");
62         exit 0;
63     }
64
65     while (<FILE>) {
66         chop;
67         next if /^\s*$/;
68         next if /^[\#\[ ]/;
69
70         if (/^\s*(\S+?)(:(\d+))?\s*$/) {
71             push(@ircServers,$1);
72             $ircPort{$1} = ($3 || 6667);
73         } else {
74             &status("loadIRCServers: invalid line => '$_'.");
75         }
76     }
77     close FILE;
78
79     $file =~ s/^.*\///;
80     &status("Loaded $file (". scalar(@ircServers) ." servers)");
81 }
82
83 1;
84
85 # vim:ts=4:sw=4:expandtab:tw=80