1
0

ngircd-conf-convert 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. #!/usr/bin/perl
  2. # Copyright (C) 2011, Christoph Biedl, <debian.axhn@manchmal.in-ulm.de>
  3. #%# license-gplv2+
  4. =head1 NAME
  5. ngircd-conf-convert - convert ngircd.conf from pre18 version
  6. =head1 VERSION
  7. Version 2011.10.30
  8. =cut
  9. our $VERSION = '2011.10.30';
  10. =head1 SYNOPSIS
  11. convert ngircd.conf from pre18 version
  12. Usage:
  13. ngircd-conf-convert [options] <old ngircd.conf> <new ngircd.conf>
  14. =cut
  15. use 5.010;
  16. use strict;
  17. use warnings;
  18. use Getopt::Long;
  19. use Pod::Usage;
  20. my $ignorecomments;
  21. =head1 OPTIONS
  22. =over
  23. =cut
  24. {
  25. my $help;
  26. my $man;
  27. my %GetOptions = (
  28. 'help|?' => \$help,
  29. 'man' => \$man
  30. );
  31. =item B<--ignorecomments>
  32. By default, this program will rewrite configuration variables even if
  33. they are commented out, e.g.
  34. ;NoDNS = no
  35. to
  36. ;DNS = yes
  37. Use this option to disable that feature.
  38. =cut
  39. $GetOptions{'ignorecomments'} = \$ignorecomments;
  40. =item F<old-ngircd.conf> F<new-ngircd.conf>
  41. The old configuration file, and where to write the converted one to.
  42. The first one must exist, the content of the latter will be
  43. overwritten. If C<-> is given, content is read from stdin or written
  44. to stdout, respectively.
  45. =back
  46. =head1 DESCRIPTION
  47. The C<ngircd-conf-convert> converts ngIRCd configuration files according
  48. to the new section and variable names introduced in version 18.
  49. The changes done are small: Just renamings and adding the appropriate
  50. section name. No blocks are moved around, you might want to do that
  51. manually.
  52. Suggested workflow:
  53. ngircd-conf-convert ngircd.conf ngircd.conf.new
  54. # compare dumps created by --configtest
  55. </dev/null ngircd --configtest --config ngircd.conf |
  56. sed -n '/GLOBAL/,$p' >dump.old
  57. </dev/null ngircd --configtest --config ngircd.conf.new |
  58. sed -n '/GLOBAL/,$p' >dump.new
  59. cmp -s dump.old dump.new || echo "ALERT: configuration was changed!"
  60. Remember: Your C<ngircd.conf> may contain sensitive data, so don't
  61. do that in a world-readable directory.
  62. =cut
  63. GetOptions (%GetOptions) or pod2usage (2);
  64. pod2usage (1) if ($help);
  65. pod2usage (-exitstatus => 0, -ignorecomments => 2) if ($man);
  66. (scalar (@ARGV) == 2) or
  67. die ("Need two file parameters.\n");
  68. }
  69. my %convert_logic = (
  70. # variable name => [ <new section>, <new name>, <invert> ]
  71. 'allowremoteoper' => [ 'Options' ],
  72. 'chrootdir' => [ 'Options' ],
  73. 'connectipv4' => [ 'Options' ],
  74. 'connectipv6' => [ 'Options' ],
  75. 'connectretry' => [ 'Limits' ],
  76. 'maxconnections' => [ 'Limits' ],
  77. 'maxconnectionsip' => [ 'Limits' ],
  78. 'maxjoins' => [ 'Limits' ],
  79. 'maxnicklength' => [ 'Limits' ],
  80. 'nodns' => [ 'Options', 'DNS', 1 ],
  81. 'noident' => [ 'Options', 'Ident', 1 ],
  82. 'nopam' => [ 'Options', 'PAM', 1 ],
  83. 'opercanusemode' => [ 'Options' ],
  84. 'operservermode' => [ 'Options' ],
  85. 'pingtimeout' => [ 'Limits' ],
  86. 'pongtimeout' => [ 'Limits' ],
  87. 'predefchannelsonly' => [ 'Options' ],
  88. 'sslcertfile' => [ 'SSL', 'CertFile' ],
  89. 'ssldhfile' => [ 'SSL', 'DHFile' ],
  90. 'sslkeyfile' => [ 'SSL', 'KeyFile' ],
  91. 'sslkeyfilepassword' => [ 'SSL', 'KeyFilePassword' ],
  92. 'sslports' => [ 'SSL', 'Ports' ],
  93. 'syslogfacility' => [ 'Options' ],
  94. 'webircpassword' => [ 'Options' ],
  95. );
  96. my $oldvars = join ('|', keys %convert_logic);
  97. my ($old_file, $new_file) = @ARGV;
  98. my ($old_fh, $new_fh);
  99. if ($old_file eq '-') {
  100. $old_fh = *STDIN;
  101. } else {
  102. (-f $old_file) or
  103. die ("Not a file: '$old_file'.\n");
  104. open ($old_fh, '<', $old_file) or
  105. die ("Canno read '$old_file': $!.\n");
  106. }
  107. my $section = '';
  108. # last section read from input
  109. my $set_section = '';
  110. # section we're currently in
  111. # slurp content
  112. my @content = <$old_fh>;
  113. ($old_file eq '-') or close ($old_fh);
  114. sub insert_line ($$) {
  115. # insert content before the given line. If this is a comment, seek
  116. # further backwards.
  117. my ($before, $content) = @_;
  118. while ($before > 0 && $content[--$before] =~ /^\s*#/) {
  119. }
  120. splice @content, $before+1, 0, $content;
  121. }
  122. for (my $i = 0; $i < scalar (@content); $i++) {
  123. my $line = $content[$i];
  124. if ($line =~ /^\s*\[([a-z]+)\]/i) {
  125. $set_section = $section = lc $1;
  126. } elsif ($line =~ /^\s*(,|#|$)/) {
  127. # nothing to do
  128. } elsif (
  129. $section eq 'global' &&
  130. $line =~ /^(\s*)(;\s*)?($oldvars)(\s*=\s*)(.*?)$/i
  131. ) {
  132. my ($left, $comment, $var, $middle, $value) =
  133. ($1 // '', $2 // '', $3, $4, $5 // '');
  134. my ($new_section, $new_var, $invert) = @{$convert_logic{lc $var}};
  135. $new_var //= $var;
  136. ($comment && $ignorecomments) and
  137. next;
  138. if ($set_section ne $new_section) {
  139. # switch section
  140. insert_line ($i, "[$new_section]\n"); $i++;
  141. $set_section = $new_section;
  142. }
  143. $invert and
  144. ($value = ($value =~ /^(yes|true|[1-9]\d*)\s*$/) ? 'no' : 'yes');
  145. $content[$i] = "$left$comment$new_var$middle$value\n";
  146. } elsif ($set_section ne $section) {
  147. # other content, switch back to global if necessary
  148. die if ($section ne 'global');
  149. insert_line ($i, "[Global]\n"); $i++;
  150. $set_section = $section;
  151. }
  152. }
  153. if ($new_file eq '-') {
  154. $new_fh = *STDOUT;
  155. } else {
  156. open ($new_fh, '>', $new_file) or
  157. die ("Canno write '$new_file': $!.\n");
  158. print $new_fh @content;
  159. }
  160. ($new_file eq '-') or close ($new_fh);
  161. exit 0;
  162. __END__
  163. =head1 AUTHOR
  164. Christoph Biedl, C<E<lt>debian.axhn@manchmal.in-ulm.deE<gt>>
  165. =head1 COPYRIGHT & LICENSE
  166. #%# license-gplv2+
  167. =cut
  168. # -- ETYKACLGPAK