File Coverage

blib/lib/Hubot/Scripts/sayhttpd.pm
Criterion Covered Total %
statement 18 60 30.0
branch 0 18 0.0
condition 0 6 0.0
subroutine 6 12 50.0
pod 0 1 0.0
total 24 97 24.7


line stmt bran cond sub pod time code
1             package Hubot::Scripts::sayhttpd;
2             $Hubot::Scripts::sayhttpd::VERSION = '0.1.9';
3 1     1   1125 use strict;
  1         2  
  1         28  
4 1     1   6 use warnings;
  1         1  
  1         26  
5 1     1   6 use Encode qw/decode_utf8/;
  1         1  
  1         51  
6 1     1   1143 use JSON;
  1         6586  
  1         6  
7              
8             sub load {
9 0     0 0   my ( $class, $robot ) = @_;
10             $robot->httpd->reg_cb(
11             '/hubot/say' => sub {
12 0     0     my ( $httpd, $req ) = @_;
13 0           my $json = undef;
14              
15 0           eval { $json = decode_json( $req->{content} ); };
  0            
16 0 0         if ($@) {
17 0           $req->respond(
18             [
19             400,
20             'Bad Request',
21             { content => 'text/json' },
22             "{ 'status': 'error', 'error': 'could not parse json' }"
23             ]
24             );
25 0           return;
26             }
27 0           my $helper = Hubot::Scripts::sayhttpd::helper->new();
28              
29 0 0         if ( !$helper->checkRoom( $json->{'room'} ) ) {
30 0           $req->respond(
31             [
32             400, 'Bad Request',
33             { content => 'text/json' },
34             "{ 'status': 'error', 'error': 'missing room' }"
35             ]
36             );
37 0           return;
38             }
39 0 0         if ( !$helper->checkSecret( $json->{'secret'} ) ) {
40 0           $req->respond(
41             [
42             401,
43             'Unauthorized',
44             { content => 'text/json' },
45             "{ 'status': 'error', 'error': 'Secret missing/wrong/not set in ENV' }"
46             ]
47             );
48 0           return;
49             }
50 0 0         if ( !$helper->checkMessage( $json->{'message'} ) ) {
51 0           $req->respond(
52             [
53             400, 'Bad Request',
54             { content => 'text/json' },
55             "{ 'status': 'error', 'error': 'missing message' }"
56             ]
57             );
58 0           return;
59             }
60 0           my $user = Hubot::User->new( { 'room' => $json->{'room'} } );
61 0           $robot->adapter->send( $user, decode_utf8( $json->{'message'} ) );
62 0           $req->respond(
63             { content => ['text/json', "{ 'status': 'OK' }"] } );
64             }
65 0           );
66             }
67              
68              
69             package Hubot::Scripts::sayhttpd::helper;
70             $Hubot::Scripts::sayhttpd::helper::VERSION = '0.1.9';
71 1     1   507 use strict;
  1         2  
  1         31  
72 1     1   6 use warnings;
  1         3  
  1         265  
73              
74             sub new {
75 0     0     my $class = shift;
76 0           my $self = { _secret => $ENV{HUBOT_SAY_HTTP_SECRET} };
77 0           bless $self, $class;
78 0           return $self;
79             }
80              
81             sub checkSecret {
82 0     0     my ( $class, $secret ) = @_;
83 0 0         unless ($secret) {
84 0           return undef;
85             }
86 0 0         unless ( $class->{_secret} ) {
87 0           return undef;
88             }
89 0 0         if ( $secret eq $class->{_secret} ) {
90 0           return 1;
91             }
92 0           return undef;
93             }
94              
95             sub checkRoom {
96 0     0     my ( $class, $room ) = @_;
97 0 0 0       if ( $room && $room =~ m /../ ) {
98 0           return 1;
99             }
100 0           return undef;
101             }
102              
103             sub checkMessage {
104 0     0     my ( $class, $message ) = @_;
105 0 0 0       if ( $message && $message =~ m /../ ) {
106 0           return 1;
107             }
108 0           return undef;
109             }
110              
111             1;
112              
113             =pod
114              
115             =encoding utf-8
116              
117             =head1 NAME
118              
119             Hubot::Scripts::sayhttpd
120              
121             =head1 VERSION
122              
123             version 0.1.9
124              
125             =head1 SYNOPSIS
126              
127             =head1 CONFIGURATION
128              
129             =over
130              
131             =item HUBOT_SAY_HTTP_SECRET
132              
133             =back
134              
135             =head1 DESCRIPTION
136              
137             HTTP Say Interface with SERECT file.
138              
139             =head1 JSON API
140              
141             Please ensure that the http client sending Content-Type "application/json".
142              
143             curl -H 'Content-Type: application/json' -d '{"room": "#test-channel", "secret": "foobar", "message": "Hello from JSON" }' http://localhost:8080/hubot/say
144              
145             =head1 AUTHOR
146              
147             Jonas Genannt <jonas@capi2name.de>
148              
149             =head1 COPYRIGHT AND LICENSE
150              
151             This software is copyright (c) 2013 by Jonas Genannt.
152              
153             This is free software; you can redistribute it and/or modify it under
154             the same terms as the Perl 5 programming language system itself.
155              
156             =cut