Handle hostnames with upper-case letters
[webmin.git] / adsl-client / adsl-client-lib.pl
1 # adsl-client-lib.pl
2 # Common functions for parsing the rp-pppoe config file
3
4 BEGIN { push(@INC, ".."); };
5 use WebminCore;
6 &init_config();
7 do 'secrets-lib.pl';
8
9 # get_config()
10 # Parse the PPPOE configuration file
11 sub get_config
12 {
13 local @rv;
14 local $lnum = 0;
15 open(FILE, $config{'pppoe_conf'}) || return undef;
16 while(<FILE>) {
17         s/\r|\n//g;
18         s/^\s*#.*$//;
19         if (/^\s*(\S+)\s*=\s*"([^"]*)"/ ||
20             /^\s*(\S+)\s*=\s*'([^']*)'/ ||
21             /^\s*(\S+)\s*=\s*(\S+)/) {
22                 push(@rv, { 'name' => $1,
23                             'value' => $2,
24                             'line' => $lnum });
25                 }
26         $lnum++;
27         }
28 close(FILE);
29 return \@rv;
30 }
31
32 # find(name, &config)
33 # Looks up an entry in the config file
34 sub find
35 {
36 local $c;
37 foreach $c (@{$_[1]}) {
38         if (lc($c->{'name'}) eq lc($_[0])) {
39                 return $c->{'value'};
40                 }
41         }
42 return undef;
43 }
44
45 # save_directive(&config, name, value)
46 sub save_directive
47 {
48 local ($old) = grep { lc($_->{'name'}) eq lc($_[1]) } @{$_[0]};
49 local $lref = &read_file_lines($config{'pppoe_conf'});
50 local $nl = "$_[1]=".($_[2] =~ /^\S+$/ ? $_[2] : "\"$_[2]\"");
51 if ($old) {
52         $lref->[$old->{'line'}] = $nl;
53         }
54 else {
55         push(@$lref, $nl);
56         }
57 }
58
59 # get_adsl_ip()
60 # Returns the device name and IP address of the ADSL connection (if up),
61 # or nothing if down
62 sub get_adsl_ip
63 {
64 local $out = `$config{'status_cmd'} 2>&1`;
65 if ($out =~ /link is up/i &&
66     $out =~ /on\s+interface\s+ppp(\d+)[\000-\377]+inet addr:\s*(\S+)/i) {
67         return ($1, $2);
68         }
69 elsif ($out =~ /attached\s+to\s+(ppp\d+)/i) {
70         return ($1, undef);
71         }
72 elsif ($out =~ /could\s+not\s+find\s+interface\s+corresponding\s+to/i) { 
73         return ("unknown", undef) 
74         } 
75 elsif ($out =~ /demand-connection/) {
76         return ("demand", undef);
77         }
78 else {
79         return ( );
80         }
81 }
82
83 # get_pppoe_version(&out)
84 sub get_pppoe_version
85 {
86 local $out = `$config{'pppoe_cmd'} -V 2>&1`;
87 ${$_[0]} = $out;
88 return $out =~ /version\s+(\S+)/i ? $1 : undef;
89 }
90
91 1;
92