Prechádzať zdrojové kódy

<include > added, by Hanno Hecker (most of the patch includes reindentations)

Morten Brix Pedersen 24 rokov pred
rodič
commit
24f6a96696
3 zmenil súbory, kde vykonal 144 pridanie a 106 odobranie
  1. 2 0
      docs/Changelog
  2. 22 0
      docs/pisg-doc.sgml
  3. 120 106
      modules/Pisg.pm

+ 2 - 0
docs/Changelog

@@ -16,6 +16,8 @@ pisg (x.xx)
    * A problem with multiple mode changes (+vv, +oo) in Trillian format has
      been fixed.
    * README has been polished/modified
+   * It's now possible to include different configuration files using a new
+     <include ... > syntax (thanks, Hanno Hecker <hanno-pisg@bee.de>)
    * Changes thanks to Matthew "Cheetah" Gabeler-Lee (<msg2@po.cwru.edu>):
     * A ShowWordTime option now exists that works the same as ShowLineTime,
       but uses word stats

+ 22 - 0
docs/pisg-doc.sgml

@@ -230,6 +230,28 @@
     </para>
 
    </sect1>
+   <sect1 id="including-config">
+    <title>Including common settings for various channels</title>
+    
+    <para>
+    If you have, for example, more than one channel, where the users are the
+    same, or you don't want to maintain more than one user file, you can use
+    the "include" setting in the main config file:
+
+    <programlisting>
+    <![CDATA[
+     <include="/home/vetinari/pisg/users.cfg">
+    ]]>
+    </programlisting>
+
+    This will include the file /home/vetinari/pisg/users.cfg in the config
+    at the place where the include statement is set.
+    </para>
+
+    <para>
+    Note, that you can NOT include a file from an included file!
+    </para>
+   </sect1>
    <sect1 id="using-css">
     <title>Changing the layout of your stats page</title>
 

+ 120 - 106
modules/Pisg.pm

@@ -55,8 +55,13 @@ sub run
         unless ($self->{cfg}->{silent});
 
     # Init the configuration file (aliases, ignores, channels, etc)
-    my $r = $self->init_config()
-        if ($self->{use_configfile});
+    my $r;
+    if ($self->{use_configfile}) {
+        if ((open(CONFIG, $self->{cfg}->{configfile}) 
+            or open(CONFIG, $self->{search_path} . "/$self->{cfg}->{configfile}"))) {
+            $r = $self->init_config(\*CONFIG)
+        }
+    }
 
     print "Using config file: $self->{cfg}->{configfile}\n\n"
         if ($r && !$self->{cfg}->{silent});
@@ -274,137 +279,146 @@ sub init_words
 sub init_config
 {
     my $self = shift;
+    my $fh   = shift;
+    while (my $line = <$fh>)
+    {
+        next if ($line =~ /^#/);
+        chomp $line;
 
-    if ((open(CONFIG, $self->{cfg}->{configfile}) or open(CONFIG, $self->{search_path} . "/$self->{cfg}->{configfile}"))) {
-
-        while (my $line = <CONFIG>)
-        {
-            next if ($line =~ /^#/);
-            chomp $line;
-
-            if ($line =~ /<user.*>/) {
-                my $nick;
+        if ($line =~ /<user.*>/) {
+            my $nick;
 
-                if ($line =~ /nick="([^"]+)"/) {
-                    $nick = $1;
-                    add_alias($nick, $nick);
-                } else {
-                    print STDERR "Warning: $self->{cfg}->{configfile}, line $.: No nick specified\n";
-                    next;
-                }
+            if ($line =~ /nick="([^"]+)"/) {
+                $nick = $1;
+                add_alias($nick, $nick);
+            } else {
+                print STDERR "Warning: $self->{cfg}->{configfile}, line $.: No nick specified\n";
+                next;
+            }
 
-                if ($line =~ /alias="([^"]+)"/) {
-                    my @thisalias = split(/\s+/, lc($1));
-                    foreach (@thisalias) {
-                        if ($self->{cfg}->{regexpaliases} and /[\|\[\]\{\}\(\)\?\+\.\*\^\\]/) {
-                            add_aliaswild($nick, $_);
-                        } elsif (not $self->{cfg}->{regexpaliases} and s/\*/\.\*/g) {
-                            # quote it if it is a wildcard
-                            s/([\|\[\]\{\}\(\)\?\+\^\\])/\\$1/g;
-                            add_aliaswild($nick, $_);
-                        } else {
-                            add_alias($nick, $_);
-                        }
+            if ($line =~ /alias="([^"]+)"/) {
+                my @thisalias = split(/\s+/, lc($1));
+                foreach (@thisalias) {
+                    if ($self->{cfg}->{regexpaliases} and /[\|\[\]\{\}\(\)\?\+\.\*\^\\]/) {
+                        add_aliaswild($nick, $_);
+                    } elsif (not $self->{cfg}->{regexpaliases} and s/\*/\.\*/g) {
+                        # quote it if it is a wildcard
+                        s/([\|\[\]\{\}\(\)\?\+\^\\])/\\$1/g;
+                        add_aliaswild($nick, $_);
+                    } else {
+                        add_alias($nick, $_);
                     }
                 }
+            }
 
-                if ($line =~ /pic="([^"]+)"/) {
-                    $self->{users}->{userpics}{$nick} = $1;
-                }
+            if ($line =~ /pic="([^"]+)"/) {
+                $self->{users}->{userpics}{$nick} = $1;
+            }
 
-                if ($line =~ /bigpic="([^"]+)"/) {
-                    $self->{users}->{biguserpics}{$nick} = $1;
-                }
+            if ($line =~ /bigpic="([^"]+)"/) {
+                $self->{users}->{biguserpics}{$nick} = $1;
+            }
 
-                if ($line =~ /link="([^"]+)"/) {
-                    $self->{users}->{userlinks}{$nick} = $1;
-                }
+            if ($line =~ /link="([^"]+)"/) {
+                $self->{users}->{userlinks}{$nick} = $1;
+            }
 
-                if ($line =~ /ignore="Y"/i) {
-                    add_ignore($nick);
-                }
+            if ($line =~ /ignore="Y"/i) {
+                add_ignore($nick);
+            }
 
-                if ($line =~ /sex="([MmFf])"/i) {
-                    $self->{users}->{sex}{$nick} = lc($1);
-                }
-            } elsif ($line =~ /<link(.*)>/) {
+            if ($line =~ /sex="([MmFf])"/i) {
+                $self->{users}->{sex}{$nick} = lc($1);
+            }
+        } elsif ($line =~ /<link(.*)>/) {
 
-                if ($line =~ /url="([^"]+)"/) {
-                    my $url = $1;
-                    if ($line =~ /ignore="Y"/i) {
-                        add_url_ignore($url);
-                    }
-                } else {
-                    print STDERR "Warning: $self->{cfg}->{configfile}, line $.: No URL specified\n";
+            if ($line =~ /url="([^"]+)"/) {
+                my $url = $1;
+                if ($line =~ /ignore="Y"/i) {
+                    add_url_ignore($url);
                 }
+            } else {
+                print STDERR "Warning: $self->{cfg}->{configfile}, line $.: No URL specified\n";
+            }
 
 
-            } elsif ($line =~ /<set(.*)>/) {
-
-                my $settings = $1;
-                if ($settings !~ /=["'](.*)["']/ || $settings =~ /(\w)>/ ) {
-                    print STDERR "Warning: $self->{cfg}->{configfile}, line $.: Missing or wrong quotes near $1\n";
-                }
+        } elsif ($line =~ /<set(.*)>/) {
 
-                while ($settings =~ s/[ \t]([^=]+)=["']([^"']*)["']//) {
-                    my $var = lc($1);
-                    $var =~ s/ //; # Remove whitespace
-                    if (!defined($self->{cfg}->{$var})) {
-                        if (defined($self->{bwd}->{$var})) {
-                            print "Using backwards compatibility option '$var'; you should change it to '$self->{bwd}->{$var}'\n";
-                            unless (($self->{cfg}->{lc($self->{bwd}->{$var})} eq $2) || $self->{override_cfg}->{lc($self->{bwd}->{$var})}) {
-                                $self->{cfg}->{$self->{bwd}->{$var}} = $2;
-                            }
-                            next;
-                        } else {
-                            print STDERR "Warning: $self->{cfg}->{configfile}, line $.: No such configuration option: '$var'\n";
-                            next;
-                        }
-                    }
-                    unless (($self->{cfg}->{$var} eq $2) || $self->{override_cfg}->{$var}) {
-                        $self->{cfg}->{$var} = $2;
-                    }
-                }
+            my $settings = $1;
+            if ($settings !~ /=["'](.*)["']/ || $settings =~ /(\w)>/ ) {
+                print STDERR "Warning: $self->{cfg}->{configfile}, line $.: Missing or wrong quotes near $1\n";
+            }
 
-            } elsif ($line =~ /<channel=['"]([^'"]+)['"](.*)>/i) {
-                my ($channel, $settings) = ($1, $2);
-                $self->{chans}->{$channel}->{channel} = $channel;
-                $self->{cfg}->{chan_done}{$self->{cfg}->{channel}} = 1; # don't parse channel in $self->{cfg}->{channel} if a channel statement is present
-                while ($settings =~ s/\s([^=]+)=["']([^"']*)["']//) {
-                    my $var = lc($1);
+            while ($settings =~ s/[ \t]([^=]+)=["']([^"']*)["']//) {
+                my $var = lc($1);
+                $var =~ s/ //; # Remove whitespace
+                if (!defined($self->{cfg}->{$var})) {
                     if (defined($self->{bwd}->{$var})) {
                         print "Using backwards compatibility option '$var'; you should change it to '$self->{bwd}->{$var}'\n";
-                        $self->{chans}->{$channel}{lc($self->{bwd}->{$var})} = $2;
+                        unless (($self->{cfg}->{lc($self->{bwd}->{$var})} eq $2) || $self->{override_cfg}->{lc($self->{bwd}->{$var})}) {
+                            $self->{cfg}->{$self->{bwd}->{$var}} = $2;
+                        }
+                        next;
                     } else {
-                        $self->{chans}->{$channel}{$var} = $2;
+                        print STDERR "Warning: $self->{cfg}->{configfile}, line $.: No such configuration option: '$var'\n";
+                        next;
                     }
                 }
-                while (<CONFIG>) {
-                    last if ($_ =~ /<\/*channel>/i);
-                    if ($_ =~ /^\s*(\w+)\s*=\s*["']([^"']*)["']/) {
-                        my $var = lc($1);
-                        unless ($self->{override_cfg}->{$var}) {
-                            if (defined($self->{bwd}->{$var})) {
-                                print "Using backwards compatibility option '$var'; you should change it to '$self->{bwd}->{$var}'\n";
-                                $self->{chans}->{$channel}{lc($self->{bwd}->{$var})} = $2;
-                            } else {
-                                $self->{chans}->{$channel}{$var} = $2;
-                            }
+                unless (($self->{cfg}->{$var} eq $2) || $self->{override_cfg}->{$var}) {
+                    $self->{cfg}->{$var} = $2;
+                }
+            }
+
+        } elsif ($line =~ /<channel=['"]([^'"]+)['"](.*)>/i) {
+            my ($channel, $settings) = ($1, $2);
+            $self->{chans}->{$channel}->{channel} = $channel;
+            $self->{cfg}->{chan_done}{$self->{cfg}->{channel}} = 1; # don't parse channel in $self->{cfg}->{channel} if a channel statement is present
+            while ($settings =~ s/\s([^=]+)=["']([^"']*)["']//) {
+                my $var = lc($1);
+                if (defined($self->{bwd}->{$var})) {
+                    print "Using backwards compatibility option '$var'; you should change it to '$self->{bwd}->{$var}'\n";
+                    $self->{chans}->{$channel}{lc($self->{bwd}->{$var})} = $2;
+                } else {
+                    $self->{chans}->{$channel}{$var} = $2;
+                }
+            }
+            while (<$fh>) {
+                last if ($_ =~ /<\/*channel>/i);
+                if ($_ =~ /^\s*(\w+)\s*=\s*["']([^"']*)["']/) {
+                    my $var = lc($1);
+                    unless ($self->{override_cfg}->{$var}) {
+                        if (defined($self->{bwd}->{$var})) {
+                            print "Using backwards compatibility option '$var'; you should change it to '$self->{bwd}->{$var}'\n";
+                            $self->{chans}->{$channel}{lc($self->{bwd}->{$var})} = $2;
+                        } else {
+                            $self->{chans}->{$channel}{$var} = $2;
                         }
-                    } elsif ($_ !~ /^$/) {
-                        print STDERR "Warning: $self->{cfg}->{configfile}, line $.: Unrecognized line\n";
                     }
+                } elsif ($_ !~ /^$/) {
+                    print STDERR "Warning: $self->{cfg}->{configfile}, line $.: Unrecognized line\n";
                 }
-            } elsif ($line =~ /<(\w+)?.*[^>]$/) {
-                print STDERR "Warning: $self->{cfg}->{configfile}, line $.: Missing end on element <$1 (probably multi-line?)\n";
-            } elsif ($line =~ /\S/) {
-                $line =~ s/\n//;
-                print "Warning: $self->{cfg}->{configfile}, line $.: Unrecognized line\n";
             }
+        } elsif ($line =~ /<include\s*=\s*(["'])(.*)\1\s*>/) {
+            my $include_cfg = $2;
+            my $backup_cfg = $self->{cfg}->{configfile};
+            $self->{cfg}->{configfile} = $include_cfg;
+            my $r;
+            if ((open(INCLUDE, $self->{cfg}->{configfile}) 
+                or open(INCLUDE, $self->{search_path} . "/$self->{cfg}->{configfile}"))) {
+                $r = $self->init_config(\*INCLUDE);
+            }
+            print "Included config file: $self->{cfg}->{configfile}\n\n"
+                if ($r && !$self->{cfg}->{silent});
+            $self->{cfg}->{configfile} = $backup_cfg;
+        } elsif ($line =~ /<(\w+)?.*[^>]$/) {
+            print STDERR "Warning: $self->{cfg}->{configfile}, line $.: Missing end on element <$1 (probably multi-line?)\n";
+        } elsif ($line =~ /\S/) {
+            $line =~ s/\n//;
+            print "Warning: $self->{cfg}->{configfile}, line $.: Unrecognized line\n";
         }
-
-        close(CONFIG);
     }
+
+    close($fh);
 }
 
 sub init_pisg