| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package App::TeleGramma::Config; | 
| 2 |  |  |  |  |  |  | $App::TeleGramma::Config::VERSION = '0.14'; | 
| 3 |  |  |  |  |  |  | # ABSTRACT: TeleGramma and TeleGramma plugin configuration | 
| 4 |  |  |  |  |  |  |  | 
| 5 | 3 |  |  | 3 |  | 462 | use Mojo::Base -base; | 
|  | 3 |  |  |  |  | 5 |  | 
|  | 3 |  |  |  |  | 17 |  | 
| 6 | 3 |  |  | 3 |  | 400 | use File::Spec::Functions qw/catdir/; | 
|  | 3 |  |  |  |  | 6 |  | 
|  | 3 |  |  |  |  | 171 |  | 
| 7 | 3 |  |  | 3 |  | 1248 | use Config::INI::Writer 0.025; | 
|  | 3 |  |  |  |  | 35110 |  | 
|  | 3 |  |  |  |  | 96 |  | 
| 8 | 3 |  |  | 3 |  | 1291 | use Config::INI::Reader 0.025; | 
|  | 3 |  |  |  |  | 11285 |  | 
|  | 3 |  |  |  |  | 1511 |  | 
| 9 |  |  |  |  |  |  |  | 
| 10 |  |  |  |  |  |  | has config    => sub { {} }; | 
| 11 |  |  |  |  |  |  | has path_base => sub { catdir($ENV{HOME}, '.telegramma') }; | 
| 12 |  |  |  |  |  |  |  | 
| 13 | 45 |  |  | 45 | 0 | 96 | sub path_config      { catdir(shift->path_base, 'telegramma.ini') } | 
| 14 | 4 |  |  | 4 | 0 | 31 | sub path_plugins     { catdir(shift->path_base, 'plugins') } | 
| 15 | 1 |  |  | 1 | 0 | 7 | sub path_logs        { catdir(shift->path_base, 'logs') } | 
| 16 | 1 |  |  | 1 | 0 | 17 | sub path_plugin_data { catdir(shift->path_base, 'plugindata') } | 
| 17 | 0 |  |  | 0 | 0 | 0 | sub path_pid         { catdir(shift->path_base, 'telegramma.pid') } | 
| 18 |  |  |  |  |  |  |  | 
| 19 |  |  |  |  |  |  | sub read { | 
| 20 | 36 |  |  | 36 | 0 | 155 | my $self = shift; | 
| 21 | 36 |  |  |  |  | 71 | $self->config( Config::INI::Reader->read_file( $self->path_config ) ); | 
| 22 |  |  |  |  |  |  | } | 
| 23 |  |  |  |  |  |  |  | 
| 24 |  |  |  |  |  |  | sub write { | 
| 25 | 7 |  |  | 7 | 0 | 803 | my $self = shift; | 
| 26 | 7 |  |  |  |  | 13 | Config::INI::Writer->write_file($self->config, $self->path_config); | 
| 27 |  |  |  |  |  |  | } | 
| 28 |  |  |  |  |  |  |  | 
| 29 |  |  |  |  |  |  | sub default_config { | 
| 30 |  |  |  |  |  |  | [ | 
| 31 | 1 |  |  | 1 | 0 | 11 | '_' => [ bot_token => 'please change me, see: https://telegram.me/BotFather' ], | 
| 32 |  |  |  |  |  |  | ], | 
| 33 |  |  |  |  |  |  | } | 
| 34 |  |  |  |  |  |  |  | 
| 35 |  |  |  |  |  |  | sub create_if_necessary { | 
| 36 | 1 |  |  | 1 | 0 | 967 | my $self = shift; | 
| 37 |  |  |  |  |  |  |  | 
| 38 | 1 |  |  |  |  | 5 | foreach my $path ( | 
| 39 |  |  |  |  |  |  | $self->path_base, | 
| 40 |  |  |  |  |  |  | $self->path_plugins, | 
| 41 |  |  |  |  |  |  | $self->path_plugin_data, | 
| 42 |  |  |  |  |  |  | catdir($self->path_plugins, 'App'), | 
| 43 |  |  |  |  |  |  | catdir($self->path_plugins, 'App/TeleGramma'), | 
| 44 |  |  |  |  |  |  | catdir($self->path_plugins, 'App/TeleGramma/Plugin'), | 
| 45 |  |  |  |  |  |  | $self->path_logs ) { | 
| 46 |  |  |  |  |  |  |  | 
| 47 | 7 | 100 |  |  |  | 120 | if (! -e $path) { | 
|  |  | 50 |  |  |  |  |  | 
| 48 | 6 |  |  |  |  | 173 | mkdir $path, 0700 || die "cannot create $path: $!\n"; | 
| 49 |  |  |  |  |  |  | } | 
| 50 |  |  |  |  |  |  | elsif (! -d $path) { | 
| 51 | 0 |  |  |  |  | 0 | die "$path is not a directory?\n"; | 
| 52 |  |  |  |  |  |  | } | 
| 53 |  |  |  |  |  |  | } | 
| 54 |  |  |  |  |  |  |  | 
| 55 | 1 |  |  |  |  | 14 | my $config_path = $self->path_config; | 
| 56 | 1 | 50 |  |  |  | 22 | if (! -e $config_path) { | 
| 57 | 1 |  |  |  |  | 4 | $self->create_default_config; | 
| 58 | 1 |  |  |  |  | 3 | return 1; | 
| 59 |  |  |  |  |  |  | } | 
| 60 |  |  |  |  |  |  |  | 
| 61 | 0 |  |  |  |  | 0 | return 0; | 
| 62 |  |  |  |  |  |  | } | 
| 63 |  |  |  |  |  |  |  | 
| 64 |  |  |  |  |  |  | sub create_default_config { | 
| 65 | 1 |  |  | 1 | 0 | 2 | my $self = shift; | 
| 66 | 1 |  |  |  |  | 3 | my $path = $self->path_config; | 
| 67 |  |  |  |  |  |  |  | 
| 68 | 1 |  |  |  |  | 8 | Config::INI::Writer->write_file($self->default_config, $path); | 
| 69 | 1 |  |  |  |  | 1314 | chmod 0600, $path; | 
| 70 |  |  |  |  |  |  | } | 
| 71 |  |  |  |  |  |  |  | 
| 72 |  |  |  |  |  |  | sub config_created_message { | 
| 73 | 0 |  |  | 0 | 0 |  | my $self = shift; | 
| 74 | 0 |  |  |  |  |  | my $path = $self->path_config; | 
| 75 |  |  |  |  |  |  |  | 
| 76 |  |  |  |  |  |  | return <<EOF | 
| 77 |  |  |  |  |  |  | Your new config has been created in $path | 
| 78 |  |  |  |  |  |  |  | 
| 79 |  |  |  |  |  |  | Please edit it now and update the Telegram Bot token, then | 
| 80 |  |  |  |  |  |  | re-run $0. | 
| 81 |  |  |  |  |  |  |  | 
| 82 |  |  |  |  |  |  | The configuration will have an entry for each plugin currently available on | 
| 83 |  |  |  |  |  |  | your system, but disabled. | 
| 84 |  |  |  |  |  |  | EOF | 
| 85 | 0 |  |  |  |  |  | } | 
| 86 |  |  |  |  |  |  |  | 
| 87 |  |  |  |  |  |  |  | 
| 88 |  |  |  |  |  |  |  | 
| 89 |  |  |  |  |  |  | 1; | 
| 90 |  |  |  |  |  |  |  | 
| 91 |  |  |  |  |  |  | __END__ | 
| 92 |  |  |  |  |  |  |  | 
| 93 |  |  |  |  |  |  | =pod | 
| 94 |  |  |  |  |  |  |  | 
| 95 |  |  |  |  |  |  | =encoding UTF-8 | 
| 96 |  |  |  |  |  |  |  | 
| 97 |  |  |  |  |  |  | =head1 NAME | 
| 98 |  |  |  |  |  |  |  | 
| 99 |  |  |  |  |  |  | App::TeleGramma::Config - TeleGramma and TeleGramma plugin configuration | 
| 100 |  |  |  |  |  |  |  | 
| 101 |  |  |  |  |  |  | =head1 VERSION | 
| 102 |  |  |  |  |  |  |  | 
| 103 |  |  |  |  |  |  | version 0.14 | 
| 104 |  |  |  |  |  |  |  | 
| 105 |  |  |  |  |  |  | =head1 AUTHOR | 
| 106 |  |  |  |  |  |  |  | 
| 107 |  |  |  |  |  |  | Justin Hawkins <justin@hawkins.id.au> | 
| 108 |  |  |  |  |  |  |  | 
| 109 |  |  |  |  |  |  | =head1 COPYRIGHT AND LICENSE | 
| 110 |  |  |  |  |  |  |  | 
| 111 |  |  |  |  |  |  | This software is copyright (c) 2019 by Justin Hawkins <justin@eatmorecode.com>. | 
| 112 |  |  |  |  |  |  |  | 
| 113 |  |  |  |  |  |  | This is free software; you can redistribute it and/or modify it under | 
| 114 |  |  |  |  |  |  | the same terms as the Perl 5 programming language system itself. | 
| 115 |  |  |  |  |  |  |  | 
| 116 |  |  |  |  |  |  | =cut |