aboutsummaryrefslogtreecommitdiff
path: root/config_file.pm
blob: b3bd38f9bd6f11faf25e0dee3d73d7773416bd77 (plain)
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
78
79
80
81
82
83
84
package config_file;

use strict;
use warnings;
use Config::Tiny;

sub parse_config
{
	my @scalar_configs = (
		'nick',
		'username',
		'ircname',
		'server',
		'port',
		'usessl',
		'sslcert',
		'sslkey',
		'password',
		'must_id',
		'quit_msg',
		'user',
		'group',
		'url_on',
		'url_len',
		'prefix_nick',
		'prefix',
		'antiflood_on');
	my @list_configs = (
		'channels',
		'ignore',
		'admins');
	my @optional_configs = (
		'password');
	my $file = $_[0];
	my %built_config;
	my $config = Config::Tiny->read($file);

	# FIXME catch undefined/missing config options
	foreach my $option (@scalar_configs) {
		my $value = $config->{_}->{$option};
		if (! defined $value && ! grep {$_ eq $option} @optional_configs) {
			die "Option \"$option\" must be set in $file\n";
		}
		$built_config{$option} = $config->{_}->{$option};
	}

	foreach my $option (@list_configs) {
		my $vals = $config->{_}->{$option};
		$vals =~ s/^\s+|\s+$//g;
		@built_config{$option} = [split /\s*,\s*/, $vals];
	}

	# special case: triggers hash
	my %triggers;
	foreach (split ',', $config->{_}->{triggers}) {
		my ($match, $response) = split /=>/;
		# strip outer quotes
		$match =~ s/^[^']*'|'[^']*$//g;
		$response =~ s/^[^']*'|'[^']*$//g;
		$triggers{$match} = $response;
	}

	# special case: timezones hash
	my %timezone;
	foreach (split ',', $config->{_}->{timezone}) {
		my ($who, $tz) = split /=>/;
		# strip outer quotes
		$who =~ s/^[^']*'|'[^']*$//g;
		$tz =~ s/^[^']*'|'[^']*$//g;
		$timezone{$who} = $tz;
	}

	$built_config{uid} = getpwnam($built_config{user})
		or die "Cannot get uid of $built_config{user}: $!\n";
	$built_config{gid} = getgrnam($built_config{group})
		or die "Cannot get gid of $built_config{group}: $!\n";


	$built_config{triggers} = \%triggers;
	$built_config{timezone} = \%timezone;

	return %built_config;
}
1;