« lib/Monitoring/Spooler/Cmd/Command/bootstrap.pm
↑ Summary
lib/Monitoring/Spooler/Transport/SmsSend.pm »
| 1 | package Monitoring::Spooler::Transport::Sipgate; | ||
| 2 | $Monitoring::Spooler::Transport::Sipgate::VERSION = '0.05'; | ||
| 3 | BEGIN { | ||
| 4 | $Monitoring::Spooler::Transport::Sipgate::AUTHORITY = 'cpan:TEX'; | ||
| 5 | } | ||
| 6 | # ABSTRACT: a monitoring spooler transport for the sipgate service | ||
| 7 | |||
| 8 | use 5.010_000; | ||
| 9 | use mro 'c3'; | ||
| 10 | use feature ':5.10'; | ||
| 11 | |||
| 12 | use Moose; | ||
| 13 | use namespace::autoclean; | ||
| 14 | |||
| 15 | # use IO::Handle; | ||
| 16 | # use autodie; | ||
| 17 | # use MooseX::Params::Validate; | ||
| 18 | # use Carp; | ||
| 19 | # use English qw( -no_match_vars ); | ||
| 20 | # use Try::Tiny; | ||
| 21 | use HTTP::Cookies; | ||
| 22 | use XMLRPC::Lite; | ||
| 23 | |||
| 24 | # extends ... | ||
| 25 | extends 'Monitoring::Spooler::Transport'; | ||
| 26 | # has ... | ||
| 27 | has 'cookies' => ( | ||
| 28 | 'is' => 'ro', | ||
| 29 | 'isa' => 'HTTP::Cookies', | ||
| 30 | 'lazy' => 1, | ||
| 31 | 'builder' => '_init_cookies', | ||
| 32 | ); | ||
| 33 | has 'client' => ( | ||
| 34 | 'is' => 'ro', | ||
| 35 | 'isa' => 'XMLRPC::Lite', | ||
| 36 | 'lazy' => 1, | ||
| 37 | 'builder' => '_init_client', | ||
| 38 | ); | ||
| 39 | has 'url' => ( | ||
| 40 | 'is' => 'rw', | ||
| 41 | 'isa' => 'Str', | ||
| 42 | 'lazy' => '1', | ||
| 43 | 'builder' => '_init_url', | ||
| 44 | ); | ||
| 45 | has 'username' => ( | ||
| 46 | 'is' => 'rw', | ||
| 47 | 'isa' => 'Str', | ||
| 48 | 'required' => 1, | ||
| 49 | ); | ||
| 50 | has 'password' => ( | ||
| 51 | 'is' => 'rw', | ||
| 52 | 'isa' => 'Str', | ||
| 53 | 'required' => 1, | ||
| 54 | ); | ||
| 55 | has 'responses' => ( | ||
| 56 | 'is' => 'ro', | ||
| 57 | 'isa' => 'HashRef', | ||
| 58 | 'lazy' => 1, | ||
| 59 | 'builder' => '_init_responses', | ||
| 60 | ); | ||
| 61 | # with ... | ||
| 62 | # initializers ... | ||
| 63 | sub _init_url { | ||
| 64 | my $self = shift; | ||
| 65 | |||
| 66 | my $url = 'https://'.$self->username().':'.$self->password().'@samurai.sipgate.net/RPC2'; | ||
| 67 | |||
| 68 | return $url; | ||
| 69 | } | ||
| 70 | |||
| 71 | sub _init_cookies { | ||
| 72 | my $self = shift; | ||
| 73 | |||
| 74 | my $Cookies = HTTP::Cookies::->new( ignore_discard => 1, ); | ||
| 75 | |||
| 76 | return $Cookies; | ||
| 77 | } | ||
| 78 | |||
| 79 | sub _init_client { | ||
| 80 | my $self = shift; | ||
| 81 | |||
| 82 | my $Client = XMLRPC::Lite::->proxy( $self->url() ); | ||
| 83 | $Client->transport()->cookie_jar( $self->cookies() ); | ||
| 84 | if ( $Client->transport()->can('ssl_opts') ) { | ||
| 85 | $Client->transport()->ssl_opts( verify_hostname => 0, ); | ||
| 86 | } | ||
| 87 | |||
| 88 | my $resp = $Client->call( | ||
| 89 | 'samurai.ClientIdenfity', | ||
| 90 | { | ||
| 91 | 'ClientName' => 'Monitoring::Spooler::Transport::Sipgate', | ||
| 92 | 'ClientVersion' => '0.1', | ||
| 93 | 'ClientVendor' => 'RandomCompany', | ||
| 94 | } | ||
| 95 | ); | ||
| 96 | # ignore the result of this call since it seems not to be essential | ||
| 97 | |||
| 98 | return $Client; | ||
| 99 | } | ||
| 100 | |||
| 101 | sub _init_responses { | ||
| 102 | my $self = shift; | ||
| 103 | |||
| 104 | # see http://www.sipgate.de/beta/public/static/downloads/basic/api/sipgate_api_documentation.pdf, page 30ff. | ||
| 105 | my $resp_ref = { | ||
| 106 | '200' => 'Method success', | ||
| 107 | '400' => 'Method not supported', | ||
| 108 | '401' => 'Request denied (no reason specified)', | ||
| 109 | '402' => 'Internal error', | ||
| 110 | '403' => 'Invalid arguments', | ||
| 111 | '404' => 'Resources exceeded', | ||
| 112 | '405' => 'Invalid parameter name', | ||
| 113 | '406' => 'Invalid parameter type', | ||
| 114 | '407' => 'Invalid parameter value', | ||
| 115 | '408' => 'Attempt to set a non-writable parameter', | ||
| 116 | '409' => 'Notification request denied', | ||
| 117 | '410' => 'Parameter exceeds maximum size', | ||
| 118 | '411' => 'Missig parameter', | ||
| 119 | '412' => 'Too many requests', | ||
| 120 | '500' => 'Date out of range', | ||
| 121 | '501' => 'URI does not belong to user', | ||
| 122 | '502' => 'Unknown type of service', | ||
| 123 | '503' => 'Selected payment method failed', | ||
| 124 | '504' => 'Selected currecy not supported', | ||
| 125 | '505' => 'Amount exceeds limit', | ||
| 126 | '506' => 'Malformed SIP URI', | ||
| 127 | '507' => 'URI not in list', | ||
| 128 | '508' => 'Format is not valid E.164', | ||
| 129 | '509' => 'Unknown status', | ||
| 130 | '510' => 'Unknown ID', | ||
| 131 | '511' => 'Invalid timevalue', | ||
| 132 | '512' => 'Referenced session not found', | ||
| 133 | '513' => 'Only single value per TOS allowed', | ||
| 134 | '514' => 'Malformed VCARD format', | ||
| 135 | '515' => 'Malformed PID format', | ||
| 136 | '516' => 'Presence information not available', | ||
| 137 | '517' => 'Invalid label name', | ||
| 138 | '518' => 'Label not assigned', | ||
| 139 | '519' => "Label doesn't exist", | ||
| 140 | '520' => 'Parameter includes invalid characters', | ||
| 141 | '521' => 'Bad password. (Rejected due to security concerns)', | ||
| 142 | '522' => 'Malformed timezone format', | ||
| 143 | '523' => 'Delay exceeds limit', | ||
| 144 | '524' => 'Requested VPN type not available', | ||
| 145 | '525' => 'Requested TOS not available', | ||
| 146 | '526' => 'Unified messaging not available', | ||
| 147 | '527' => 'URI not available for registration', | ||
| 148 | }; | ||
| 149 | for my $i (900 .. 999) { | ||
| 150 | $resp_ref->{$i} = 'Vendor defined status code'; | ||
| 151 | } | ||
| 152 | |||
| 153 | return $resp_ref; | ||
| 154 | } | ||
| 155 | |||
| 156 | # your code here ... | ||
| 157 | sub provides { | ||
| 158 | my $self = shift; | ||
| 159 | my $type = shift; | ||
| 160 | |||
| 161 | if($type =~ m/^text$/i) { | ||
| 162 | return 1; | ||
| 163 | } | ||
| 164 | |||
| 165 | return; | ||
| 166 | } | ||
| 167 | |||
| 168 | sub run { | ||
| 169 | my $self = shift; | ||
| 170 | my $destination = shift; | ||
| 171 | my $message = shift; | ||
| 172 | |||
| 173 | $destination = $self->_clean_number($destination); | ||
| 174 | $message = substr($message,0,159); | ||
| 175 | |||
| 176 | my $resp = $self->client()->call( | ||
| 177 | 'samurai.SessionInitiate', | ||
| 178 | { | ||
| 179 | 'RemoteUri' => 'sip:'.$destination.'@sipgate.net', | ||
| 180 | 'TOS' => 'text', | ||
| 181 | 'Content' => $message, | ||
| 182 | } | ||
| 183 | ); | ||
| 184 | my $result = $resp->result(); | ||
| 185 | |||
| 186 | if($result && $result->{'StatusCode'} == 200) { | ||
| 187 | $self->logger()->log( message => 'Sent '.$message.' to '.$destination, level => 'debug', ); | ||
| 188 | return 1; | ||
| 189 | } else { | ||
| 190 | my $errstr = $result->{'StatusCode'}; | ||
| 191 | if($self->responses()->{$result->{'StatusCode'}}) { | ||
| 192 | $errstr .= ' ('.$result->responses()->{$result->{'StatusCode'}}.')'; | ||
| 193 | } | ||
| 194 | $errstr .= ' - '.$result->{'StatusString'}; | ||
| 195 | $self->logger()->log( message => 'Failed to send '.$message.' to '.$destination.'. Error: '.$errstr, level => 'debug', ); | ||
| 196 | return; | ||
| 197 | } | ||
| 198 | } | ||
| 199 | |||
| 200 | no Moose; | ||
| 201 | __PACKAGE__->meta->make_immutable; | ||
| 202 | |||
| 203 | 1; | ||
| 204 | |||
| 205 | __END__ |