package II0e2073608046581d7be737ae6b5074c0::mod_imnica; use strict; use Data::Dumper; use LWP::UserAgent; sub checkSettings { my $class = shift; my $this = shift; my $settings = $this->unpackSettings; return ['error.missing_config'] if grep { ! $settings->{$_} } qw/username password list_id/; my $login_error = $class->_login_api($this); return $login_error if $login_error; my $content = $class->_call_api($this, { Command => 'List.Get', ListID => $settings->{list_id} }); my ($success) = $content =~ m|<Success><\!\[CDATA\[(.+?)\]\]></Success>|si; return ['error.invalid_list'] if $success != 1; return; } sub subscribe { my $class = shift; my ($this, $user) = @_; my $settings = $this->unpackSettings; my %options = ( Command => 'Subscriber.Subscribe', ListID => $settings->{list_id}, EmailAddress => $user->{email}, IPAddress => $ENV{REMOTE_ADDR}, ); $class->_login_api($this); $class->_call_api($this, \%options); return; } sub _login_api { my $class = shift; my $this = shift; return if $this->{session_id}; my $settings = $this->unpackSettings; $this->{ua} = LWP::UserAgent->new; $this->{ua}->timeout(20); $this->{ua}->env_proxy; my $loginURL = "http://www.imnicamail.com/v4/api.php?Command=User.Login&ResponseFormat=XML&Username=$settings->{username}&Password=$settings->{password}"; my $response = $this->{ua}->post($loginURL); warn "Imnica USER LOGIN";
warn "RESPONSE status: " . $response->status_line;
return ['error.connect', $response->status_line] if $response->message !~ /OK/; my ($success) = $response->content =~ m|<Success><\!\[CDATA\[(.+?)\]\]></Success>|si; return ['error.auth'] if $success != 1; ($this->{session_id}) = $response->content =~ m|<SessionID><\!\[CDATA\[(.+?)\]\]></SessionID>|si; return; } sub _call_api { my $class = shift; my ($this, $options) = @_; die 'Imnica: no UserAgent and/or Session ID' unless $this->{ua} && $this->{session_id}; my $apiURL = "http://www.imnicamail.com/v4/api.php?SessionID=$this->{session_id}&ResponseFormat=XML&" . join('&', map { $_ . '=' . _url_encode($options->{$_}) } keys %$options); my $response = $this->{ua}->post($apiURL); my $content = $response->content; warn "Imnica API URL: " . $apiURL;
warn "OPTIONS: " . Dumper($options);
warn "RESPONSE status: " . $response->status_line;
warn "RESPONSE content: " . $content;
$content; } sub _url_encode { my $v = shift; $v =~ s/([\x00-\x19"=\+\&#%;:\/<>\?{}|\\\\^~`\[\]\x7F-\xFF])/sprintf('%%%02X',ord($1))/eg; $v =~ s/\x20/+/g; $v; } 1; 