package Journalizer;
use strict;
use warnings;

use Digest::MD5 qw( md5_hex );
use HTTP::Cookies;
use IO::All;
use SOAP::Lite;

########################################################################
# CONFIGURATION
#########################################################################

use constant CONFIG => "$ENV{HOME}/.journalizerrc";

my ( $userid, $password ) = @{ io(CONFIG) }[ 0 .. 1 ];
chmod 0600, CONFIG;
unless ( $userid and $password )
{
    die "No userid and/or password. Did you create a .journalizerrc ?";
    return;
}

########################################################################
# POSTING
#########################################################################

sub add_entry
{
    my ( $self, $configref ) = @_;
    my %config = %$configref;

    unless ( $userid and $password )
    {
        die "No userid and/or password. Did you create a .journalizerrc ?";
        return;
    }
    unless ( $config{title} )
    {
        die "No title!";
        return;
    }
    unless ( $config{body} )
    {
        die "No body!";
        return;
    }

    my $host = 'use.perl.org';

    my $cookie_jar = HTTP::Cookies->new;
    $cookie_jar->set_cookie(
        0,
        user => _make_cookie( $userid, $password ),
        '/', $host
    );

    my $journal =
        SOAP::Lite->uri("http://$host/Slash/Journal/SOAP")
        ->proxy( "http://$host/journal.pl", cookie_jar => $cookie_jar );

    my $result = $journal->add_entry(
        subject  => $config{title},
        body     => $config{body},
        discuss  => 1,
        posttype => 2,
    );
    if ( $result->fault )
    {
        die $result->faultstring . "\n";
    }
    else
    {
        return $result->result;
    }
}

# Taken from http://www.perlmonks.org/?node=157135
sub _make_cookie
{
    my ( $uid, $passwd ) = @_;
    my $cookie = $uid . '::' . md5_hex($passwd);
    $cookie =~ s/(.)/sprintf("%%%02x", ord($1))/ge;
    $cookie =~ s/%/%25/g;
    $cookie;
}

########################################################################
# FORMATTER
#########################################################################

package Journalizer::Formatter;
use base 'Text::KwikiFormatish';
sub format { __PACKAGE__->new->process(@_) }

sub process_order
{
    qw(
        function
        header_1 header_2 header_3 header_4 header_5 header_6
        horizontal_line lists
        code paragraph
        named_http_link no_http_link http_link
        no_mailto_link mailto_link
        cpan_link
        inline negation
        bold italic underscore
        mdash
        )
}

sub code_postformat
{
    my ( $self, $text ) = @_;
    $text =~ s/\s+$//s;
    return "<ecode>$text</ecode>\n\n";
}

sub paragraph_format
{
    my ( $self, $text ) = @_;
    return ''    if $text =~ /^[\s\n]*$/;
    return $text if $text =~ /^<(o|u)l>/i;
    return "<p>$text</p>";
}

sub cpan_link
{
    my ( $self, $text ) = @_;
    $self->split_method( $text, qr{ ( % [:[:alpha:]]+ ) }x,
        'cpan_link_format', );
}

sub cpan_link_format
{
    my ( $self, $text ) = @_;
    $text =~ s/^%//;
    my $module    = $self->escape($text);
    my $cpan_link =
        qq{<a href="http://search.cpan.org/perldoc?$module">$text</a>};
    return $cpan_link;
}

