| 1 |
#!/usr/bin/perl |
| 2 |
# vim: ts=4:sw=4:et:ai:sts=4 |
| 3 |
# |
| 4 |
# KGB - an IRC bot helping collaboration |
| 5 |
# Copyright © 2012 Damyan Ivanov |
| 6 |
# |
| 7 |
# This program is free software; you can redistribute it and/or modify it under |
| 8 |
# the terms of the GNU General Public License as published by the Free Software |
| 9 |
# Foundation; either version 2 of the License, or (at your option) any later |
| 10 |
# version. |
| 11 |
# |
| 12 |
# This program is distributed in the hope that it will be useful, but WITHOUT |
| 13 |
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 14 |
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
| 15 |
# details. |
| 16 |
# |
| 17 |
# You should have received a copy of the GNU General Public License along with |
| 18 |
# this program; if not, write to the Free Software Foundation, Inc., 51 |
| 19 |
# Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 20 |
|
| 21 |
use strict; |
| 22 |
use warnings; |
| 23 |
use autodie; |
| 24 |
|
| 25 |
use Getopt::Long; |
| 26 |
use Pod::Usage; |
| 27 |
use YAML qw(LoadFile DumpFile Bless); |
| 28 |
|
| 29 |
my $file = '/etc/kgb-bot/kgb.conf'; |
| 30 |
my $dir = '/etc/kgb-bot/kgb.conf.d'; |
| 31 |
my $net = 'oftc'; |
| 32 |
my $save = 0; |
| 33 |
|
| 34 |
|
| 35 |
GetOptions( |
| 36 |
'file=s' => \$file, |
| 37 |
'dir=s' => \$dir, |
| 38 |
'net|network=s' => \$net, |
| 39 |
'save|write!' => \$save, |
| 40 |
) or pod2usage( { -verbose => 1 } ); |
| 41 |
|
| 42 |
@ARGV == 3 or pod2usage(); |
| 43 |
|
| 44 |
my $yaml = LoadFile($file); |
| 45 |
my $networks = $yaml->{networks}; |
| 46 |
|
| 47 |
if ( $dir and -d $dir ) { |
| 48 |
$yaml = {}; |
| 49 |
} |
| 50 |
|
| 51 |
my ( $prj, $chan, $pwd ) = @ARGV; |
| 52 |
|
| 53 |
$yaml->{repositories}{$prj}{password} = $pwd; |
| 54 |
|
| 55 |
unless ( $chan =~ /^[#&+]/ ) { |
| 56 |
warn "W: Prepending channel name with a hash sign.\n"; |
| 57 |
$chan = "#$chan"; |
| 58 |
} |
| 59 |
|
| 60 |
unless ( exists $networks->{$net} ) { |
| 61 |
warn "W: Network '$net' missing on configuration file.\n"; |
| 62 |
} |
| 63 |
|
| 64 |
push @{ $yaml->{channels} }, { name => $chan, network => $net, repos => [ $prj ] }; |
| 65 |
|
| 66 |
my $i; |
| 67 |
my %key_order = map( ( $_ => $i++ ), |
| 68 |
qw(soap queue_limit log_file pid_dir min_protocol_ver |
| 69 |
debug admins |
| 70 |
repositories networks channels |
| 71 |
smart_answers smart_answers_polygen colors) ); |
| 72 |
|
| 73 |
Bless($yaml)->keys( |
| 74 |
[ sort { ( $key_order{$a} // 999 ) <=> ( $key_order{$b} // 999 ) } |
| 75 |
keys %$yaml |
| 76 |
] |
| 77 |
); |
| 78 |
|
| 79 |
foreach ( @{ $yaml->{channels} } ) { |
| 80 |
Bless($_)->keys( |
| 81 |
[ sort { $a ne 'name' } keys %$_ ] |
| 82 |
); |
| 83 |
} |
| 84 |
|
| 85 |
my $outfh = \*STDOUT; |
| 86 |
my ( $uid, $gid, $mode ); |
| 87 |
|
| 88 |
if ($save) { |
| 89 |
$uid = ( stat($file) )[4]; |
| 90 |
$gid = ( stat($file) )[5]; |
| 91 |
$mode = ( stat($file) )[2] & 07777; |
| 92 |
if ( $dir and -d $dir ) { |
| 93 |
$file = File::Spec->catdir($dir, "$prj.conf"); |
| 94 |
} |
| 95 |
umask(~$mode & 0777); |
| 96 |
open( $outfh, '>', $file ); |
| 97 |
} |
| 98 |
|
| 99 |
print $outfh YAML::Dump($yaml); |
| 100 |
|
| 101 |
if ($save) { |
| 102 |
chown $uid, $gid, $file; |
| 103 |
} |
| 104 |
|
| 105 |
__END__ |
| 106 |
|
| 107 |
=head1 NAME |
| 108 |
|
| 109 |
kgb-add-project - helper script for adding new projects to kgb-bot configuration file |
| 110 |
|
| 111 |
=head1 SYNOPSIS |
| 112 |
|
| 113 |
B<kgb-add-project> [--file configfile] [--dir configdir] [--net|--networks ircnetwork] [--save|--write] project-id ircchannel password |
| 114 |
|
| 115 |
=head1 DESCRIPTION |
| 116 |
|
| 117 |
B<kgb-add-project> allows one to add new projects to B<kgb-bot>'s configuration |
| 118 |
file from the command line. It writes the new config to stdout unless I<--save|--write> is given. |
| 119 |
|
| 120 |
=head1 ARGUMENTS |
| 121 |
|
| 122 |
=over |
| 123 |
|
| 124 |
=item B<--file> configfile |
| 125 |
|
| 126 |
Optional. Defaults to F</etc/kgb-bot/kgb.conf>. |
| 127 |
|
| 128 |
=item B<--dir> configdir |
| 129 |
|
| 130 |
Optional. Defaults to F</etc/kgb-bot/kgb.conf.d/>. |
| 131 |
Used for writing config snippets for projects with B<--save|--write>. |
| 132 |
|
| 133 |
=item B<--net|network> ircnetwork |
| 134 |
|
| 135 |
Optional. Defaults to B<oftc>. Needs to be in the I<networks> section of the configuration file. |
| 136 |
|
| 137 |
=item B<--save|--write> |
| 138 |
|
| 139 |
Optional. Write new config back to file instead of stdout. |
| 140 |
|
| 141 |
=item B<project-id> |
| 142 |
|
| 143 |
B<project-id> to be added. |
| 144 |
|
| 145 |
=item B<irchannel> |
| 146 |
|
| 147 |
IRC channel where the new project sends its messages. If the channel doesn't |
| 148 |
start with a character denoting IRC channel (C<#&+>), then a hash sign is |
| 149 |
prepended. |
| 150 |
|
| 151 |
=item B<password> |
| 152 |
|
| 153 |
Password for the new project. |
| 154 |
|
| 155 |
=back |
| 156 |
|
| 157 |
=head1 EXAMPLE |
| 158 |
|
| 159 |
B<kgb-add-project> my-project \#projectchannel RudFiarced0 |
| 160 |
|
| 161 |
=head1 CAVEATS |
| 162 |
|
| 163 |
=over |
| 164 |
|
| 165 |
=item * |
| 166 |
|
| 167 |
B<kgb-add-project> sorts the top-level sections of the configuration file in |
| 168 |
a fixed order and re-orders the subsections alphabetically. |
| 169 |
|
| 170 |
=item * |
| 171 |
|
| 172 |
The output of B<kgb-add-project> does not contain the comments that were |
| 173 |
present in the source configuration file. |
| 174 |
|
| 175 |
=back |
| 176 |
|
| 177 |
=head1 SEE ALSO |
| 178 |
|
| 179 |
=over |
| 180 |
|
| 181 |
=item L<App::KGB> |
| 182 |
|
| 183 |
=item L<kgb-bot(1)> |
| 184 |
|
| 185 |
=back |
| 186 |
|
| 187 |
=head1 COPYRIGHT AND LICENSE |
| 188 |
|
| 189 |
Copyright (C) 2012 Damyan Ivanov |
| 190 |
|
| 191 |
This program is free software; you can redistribute it and/or modify it under |
| 192 |
the terms of the GNU General Public License as published by the Free Software |
| 193 |
Foundation; either version 2 of the License, or (at your option) any later |
| 194 |
version. |
| 195 |
|
| 196 |
This program is distributed in the hope that it will be useful, but WITHOUT |
| 197 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 198 |
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
| 199 |
details. |
| 200 |
|
| 201 |
You should have received a copy of the GNU General Public License along with |
| 202 |
this program; if not, write to the Free Software Foundation, Inc., 51 |
| 203 |
Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 204 |
|
| 205 |
=cut |