Browse Source

Begin work on adding telegram driver for Issue #21

Still TODO:
Actually make the driver work, lol. For now we only make the UI and
configuration bits work.
Andy Baugh 5 years ago
parent
commit
8ed5026660

+ 1 - 0
LICENSE-IMAGES.md

@@ -3,3 +3,4 @@ Adding this file here, as some of the imagery used in the Schema modules were no
 * IRC: Released under the [Creative Commons Attribution-Share Alike 3.0 Unported license](https://github.com/fabianalexisinostroza/Antu/blob/master/LICENSE "CC-SA 3.0 Unported"). [Source](https://commons.wikimedia.org/wiki/File:Antu_irc.svg "Wikimedia")
 * Slack: Released under the terms of [Slack's Branding Guidelines](https://slack.com/brand-guidelines "Slack's Branding Guidelines"). [Source](https://brandfolder.com/slack "Slack Icons @ BrandFolder")
 * Discord: Released under the terms of [Discord's Branding Guidelines](https://discordapp.com/branding "Discord's Branding Guidelines"). [Source](https://discordapp.com/assets/f8389ca1a741a115313bede9ac02e2c0.svg)
+* Telegram: Released under the [GNU General Public License, v3](https://github.com/zhukov/webogram/blob/master/LICENSE). [Source](https://github.com/zhukov/webogram/blob/master/app/img/icons/icon.svg)

+ 123 - 0
lib/Cpanel/iContact/Provider/Schema/Telegram.pm

@@ -0,0 +1,123 @@
+package Cpanel::iContact::Provider::Schema::Telegram;
+
+use strict;
+use warnings;
+
+# Name is always uc(MODULE)
+
+=encoding utf-8
+
+=head1 NAME
+
+Cpanel::iContact::Provider::Schema::Telegram - Schema for the Telegram iContact module
+
+=head1 SYNOPSIS
+
+    use Cpanel::iContact::Provider::Schema::Telegram;
+
+    my $settings = Cpanel::iContact::Provider::Schema::Telegram::get_settings();
+
+    my $config = Cpanel::iContact::Provider::Schema::Telegram::get_config();
+
+
+=head1 DESCRIPTION
+
+Provide settings and configuration for the HipChat iContact module.
+
+=cut
+
+=head2 get_settings
+
+Provide config data for TweakSettings that will be saved in
+/etc/wwwacct.conf.shadow
+
+=over 2
+
+=item Input
+
+=over 3
+
+None
+
+=back
+
+=item Output
+
+=over 3
+
+A hashref that can be injected into Whostmgr::TweakSettings::Basic's %Conf
+with the additional help and label keys that are used in the display of the
+tweak settings.
+
+=back
+
+=back
+
+=cut
+
+sub get_settings {
+    my $help = <<HALP;
+Telegram Bot Token: Token created for sending notifications to the bot you configured for sending cPanel & WHM Notifications to users in Telegram, separated by commas.
+<br>In order to create a token for your bot, please message the BotFather in Telegram and it will make one for you.
+HALP
+    return {
+        'CONTACTTELEGRAM' => {
+            'name'     => 'Telegram',
+            'shadow'   => 1,
+            'type'     => 'text',
+            'checkval' => sub {
+                my $value = shift();
+                $value =~ s/^\s+|\s+$//g; # Trim
+
+                return $value if $value eq q{};
+
+                if($value !~ m/^[0-9]+:[A-Z]{3}-[A-Z]{3}[A-Za-z0-9]+-[A-Za-z0-9]+$/ ) {
+                    die "Bogus value, neener neener";
+                }
+                return $value;
+            },
+            'label' => 'Telegram Bot Token',
+            'help'  => $help,
+        }
+    };
+}
+
+=head2 get_config
+
+Provide configuration for the module.
+
+=over 2
+
+=item Input
+
+=over 3
+
+None
+
+=back
+
+=item Output
+
+=over 3
+
+A hash ref containing the following key values:
+
+  default_level:    The iContact default contact level (All)
+  display_name:     The name displayed on the Contact Manager page in WHM.
+
+=back
+
+=back
+
+=cut
+
+sub get_config {
+    return {
+        'default_level' => 'All',
+        'display_name'  => 'Telegram',
+        'icon' =>
+          'data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%22620%22%20height%3D%22620%22%20viewBox%3D%220%200%20620%20620%22%3E%3ClinearGradient%20id%3D%22a%22%20gradientUnits%3D%22userSpaceOnUse%22%20x1%3D%22311.167%22%20y1%3D%22603.333%22%20x2%3D%22311.167%22%20y2%3D%2219%22%3E%3Cstop%20offset%3D%220%22%20stop-color%3D%22%231D93D2%22%2F%3E%3Cstop%20offset%3D%221%22%20stop-color%3D%22%2338B0E3%22%2F%3E%3C%2FlinearGradient%3E%3Ccircle%20fill%3D%22url%28%23a%29%22%20stroke%3D%22%23FFF%22%20stroke-width%3D%2228%22%20stroke-miterlimit%3D%2210%22%20cx%3D%22311.167%22%20cy%3D%22311.167%22%20r%3D%22292.167%22%2F%3E%3Cpath%20fill%3D%22%23C8DAEA%22%20d%3D%22M220.76%20338.848l35.362%2097.88s4.42%209.157%209.157%209.157%2075.146-73.252%2075.146-73.252l78.304-151.24-196.707%2092.197-1.264%2025.258z%22%2F%3E%3Cpath%20fill%3D%22%23A9C6D8%22%20d%3D%22M267.646%20363.95l-6.788%2072.146s-2.842%2022.102%2019.26%200%2043.257-39.152%2043.257-39.152%22%2F%3E%3Cpath%20fill%3D%22%23FFF%22%20d%3D%22M221.398%20342.34l-72.734-23.7s-8.683-3.526-5.894-11.525c.575-1.65%201.736-3.052%205.21-5.473%2016.12-11.234%20298.324-112.667%20298.324-112.667s7.97-2.683%2012.677-.898c2.153.816%203.527%201.737%204.685%205.104.42%201.226.663%203.83.63%206.42-.022%201.87-.252%203.6-.42%206.316-1.72%2027.732-53.145%20234.705-53.145%20234.705s-3.076%2012.113-14.103%2012.525c-4.02.15-8.9-.664-14.734-5.683-21.635-18.612-96.42-68.87-112.944-79.924-.93-.622-1.198-1.433-1.355-2.222-.233-1.165%201.034-2.61%201.034-2.61s130.217-115.75%20133.68-127.895c.268-.94-.74-1.405-2.105-1-8.65%203.183-158.58%2097.858-175.126%20108.313-.968.61-3.682.217-3.682.217z%22%2F%3E%3C%2Fsvg%3E',
+    };
+}
+
+1;

+ 109 - 0
lib/Cpanel/iContact/Provider/Telegram.pm

@@ -0,0 +1,109 @@
+package Cpanel::iContact::Provider::Discord;
+
+use strict;
+use warnings;
+
+use parent 'Cpanel::iContact::Provider';
+
+use Try::Tiny;
+
+=encoding utf-8
+
+=head1 NAME
+
+Cpanel::iContact::Provider::Discord - Backend for the Discord iContact module
+
+=head1 SYNOPSIS
+
+    use Cpanel::iContact::Provider::Discord;
+
+    my $notifier = Cpanel::iContact::Provider::Discord->new();
+    $notifier->send();
+
+
+=head1 DESCRIPTION
+
+Provide backend accessor for the Discord iContact module.
+
+=cut
+
+=head2 send
+
+Sends off the notification over to your Discord channel/user
+
+=over 2
+
+=item Input
+
+=over 3
+
+None
+
+=back
+
+=item Output
+
+=over 3
+
+Truthy value on success, exception on failure.
+
+=back
+
+=back
+
+=cut
+
+sub send {
+    my ($self) = @_;
+
+    my $args_hr    = $self->{'args'};
+    my $contact_hr = $self->{'contact'};
+
+    my @errs;
+
+    require Cpanel::HTTP::Client;
+    my $ua = Cpanel::HTTP::Client->new( 'default_headers' => { 'content-type' => 'application/json' } )->die_on_http_error();
+
+    my $subject = $args_hr->{'subject'};
+    my $message = ${ $args_hr->{'text_body'} };
+
+    require Cpanel::AdminBin::Serializer;
+
+    # GitHub issue #18 -- Discord max message length is 2000 chars.
+    # As such , truncate at 1996, add ellipsis (3 chars).
+    # Why not 1997? I want to avoid fencepost errors.
+    my $message_json = Cpanel::AdminBin::Serializer::Dump(
+        {
+            'content'     => substr( "$subject\n\n$message", 0, 1996 ) . "...",
+        }
+    );
+
+    # Send it
+    foreach my $destination ( @{ $args_hr->{'to'} } ) {
+        try {
+            my $res = $ua->request( 'POST', $destination, { 'content' => $message_json } );
+            die( sprintf "Error %d: %s", $res->status(), $res->reason() ) if !$res->success();
+        }
+        catch {
+            require Cpanel::Exception;
+            push(
+                @errs,
+                Cpanel::Exception::create(
+                    'ConnectionFailed',
+                    'The system failed to send the message to “[_1]” due to an error: [_2]',
+                    [ $destination, $_ ]
+                )
+            );
+        };
+    }
+
+    if (@errs) {
+
+        # Module should already be loaded above
+        die Cpanel::Exception::create( 'Collection', [ exceptions => \@errs ] );
+    }
+
+    return 1;
+}
+
+1;

+ 29 - 0
t/Cpanel-iContact-Provider-Schema-Telegram.t

@@ -0,0 +1,29 @@
+use strict;
+use warnings;
+
+use Cwd qw{abs_path};
+use File::Basename qw{dirname};
+use lib abs_path( dirname(__FILE__) . "/../lib" );
+
+use Test::More;
+use Test::Deep;
+
+use Cpanel::iContact::Provider::Schema::Telegram ();
+
+plan tests => 2;
+
+subtest "Settings getter method performs as expected" => sub {
+    my $model = [ 'CONTACTTELEGRAM' ];
+    my $settings = Cpanel::iContact::Provider::Schema::Telegram::get_settings();
+    is_deeply( [ sort keys( %{$settings} ) ], $model, "Settings returned look OK so far" );
+    foreach my $key (@$model) {
+        is(
+            $settings->{$key}{'checkval'}->('123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11'),
+            '123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11', "Valid values passed to checkval subroutine of '$key' assumed GreatSuccess"
+        ) if ref $settings->{$key}{'checkval'} eq 'CODE';
+    }
+    is( $settings->{'CONTACTTELEGRAM'}{'checkval'}->('    '), '', "Invalid values passed to checkval subroutine of 'CONTACTTELEGRAM' engaged Maximum NO" );
+};
+
+my $config_model = { 'default_level' => 'All', 'display_name' => 'Telegram', 'icon' => ignore() };
+cmp_deeply( Cpanel::iContact::Provider::Schema::Telegram::get_config(), $config_model, "Config getter method return looks OK" );

+ 60 - 0
t/Cpanel-iContact-Provider-Telegram.t

@@ -0,0 +1,60 @@
+use strict;
+use warnings;
+
+use Cwd qw{abs_path};
+use File::Basename qw{dirname};
+use lib abs_path( dirname(__FILE__) . "/../lib" );
+
+use Test::More;
+use Test::Fatal;
+use Test::MockModule ();
+use Config::Simple   ();
+
+use Cpanel::HTTP::Client              ();
+use Cpanel::HTTP::Client::Response    ();
+use Cpanel::iContact::Provider::Telegram ();
+
+plan tests => 2;
+
+# First, let's mock out the parent, and other stuff we wouldn't wanna do in a unit test
+subtest "Provider bits work as expected ('unit' test)" => sub {
+    my $text_scalar = 'lol, jk';
+    my $send_args   = { 'subject' => "[test.host.tld] YOUR COMIC BOOKS ARE DYING!!!1", 'text_body' => \$text_scalar, 'to' => [ 'SalinasPunishmentRoom', '@cPSaurus' ] };
+    my $contact_cfg = {};
+    my $ua_mocker = Test::MockModule->new("Cpanel::HTTP::Client");
+    $ua_mocker->mock( 'request' => sub { return bless {}, "Cpanel::HTTP::Client::Response"; } );
+    my $resp_mocker = Test::MockModule->new("Cpanel::HTTP::Client::Response");
+    $resp_mocker->mock( 'success' => sub { return 1; }, 'status' => sub { return 200; }, 'reason' => sub { return 'OK'; } );
+
+    isa_ok( my $spammer = Cpanel::iContact::Provider::Telegram->new(), "Cpanel::iContact::Provider::Telegram" );
+    is( exception { $spammer->send() }, undef, "send doesn't throw on GreatSuccess" );
+    $resp_mocker->mock( 'success' => sub { return 0; }, 'status' => sub { return 500; }, 'reason' => sub { return 'ENOHUGS'; } );
+    isnt( exception { $spammer->send() }, undef, "send throws whenever anything goes wrong" );
+};
+
+subtest "Can send a message to somewhere (systems level/integration test)" => sub {
+    SKIP: {
+        my $conf_file = abs_path( dirname(__FILE__) . "/../.discordtestrc" );
+        skip "Skipping functional testing, needful not supplied", 1 if !$ENV{'AUTHOR_TESTS'} || !-f $conf_file;
+        my $test_conf = { Config::Simple->import_from($conf_file)->vars() };
+        my $text_body = "This is a test of Cpanel::iContact::Provider::Telegram. Please Ignore";
+        my %args = (
+            'to'        => [ $test_conf->{'CONTACTDISCORD'} ],
+            'subject'   => 'My Super cool test notification',
+            'text_body' => \$text_body,
+        );
+
+        {
+            no warnings qw{redefine once};
+            local *Cpanel::iContact::Provider::Telegram::new = sub {
+                return bless {
+                    'contact' => $test_conf,
+                    'args'    => \%args,
+                }, $_[0];
+            };
+            my $spammer = Cpanel::iContact::Provider::Telegram->new();
+            my $ex;
+            is( $ex = exception { $spammer->send() }, undef, "Didn't fail to send notification using full functional test" ) || diag explain $ex;
+        }
+    }
+};