iiplc/iiplc.app

240 lines
5.6 KiB
Plaintext
Raw Permalink Normal View History

2014-06-24 11:31:20 +04:00
#!/usr/bin/perl
2015-08-27 09:45:40 +03:00
# Copyright © 2014-2015 Difrex <difrex.punk@gmail.com>
2014-07-31 00:52:57 +04:00
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
2014-07-31 00:57:58 +04:00
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2014-06-16 12:11:34 +04:00
2014-06-11 15:55:00 +04:00
use strict;
use warnings;
use Plack::Builder;
use Plack::Request;
use Plack::Response;
2015-08-11 09:49:27 +03:00
2014-08-04 13:35:42 +04:00
# Static files
use Plack::App::File;
2014-06-11 15:55:00 +04:00
use II::Config;
use II::Get;
use II::Send;
use II::Render;
use II::DB;
use II::Enc;
my $c = II::Config->new();
my $config = $c->load();
my $render = II::Render->new();
my $echo = sub {
my $env = shift;
my $req = Plack::Request->new($env);
2014-06-16 10:55:47 +04:00
my $echo = $req->param('echo');
my $view = $req->param('view');
2014-06-11 15:55:00 +04:00
2014-06-24 11:31:20 +04:00
my $echo_messages = $render->echo_mes( $echo, $view );
2014-06-11 15:55:00 +04:00
return [ 200, [ 'Content-type' => 'text/html' ], ["$echo_messages"], ];
};
my $thread = sub {
my $env = shift;
my $req = Plack::Request->new($env);
my $subg = $req->param('subg');
my $echo = $req->param('echo');
my $thread = $render->thread( $subg, $echo );
return [ 200, [ 'Content-type' => 'text/html' ], ["$thread"], ];
};
2014-11-06 11:24:03 +03:00
# Get new messages
2014-06-11 15:55:00 +04:00
my $get = sub {
2014-08-08 10:41:09 +04:00
$config = $c->reload();
2014-11-06 11:24:03 +03:00
my $msgs;
if ( $config->{host} =~ m/.+\,.+/ ) {
my @hosts = split( /,/, $config->{host} );
foreach my $host (@hosts) {
$config->{host} = $host;
my $GET = II::Get->new($config);
$msgs .= $GET->get_echo();
}
2016-04-19 12:21:56 +03:00
} else {
2014-11-06 11:24:03 +03:00
my $GET = II::Get->new($config);
$msgs .= $GET->get_echo();
}
2014-06-11 15:55:00 +04:00
my $new_mes = $render->new_mes($msgs);
return [ 200, [ 'Content-type' => 'text/html' ], ["$new_mes"], ];
};
my $root = sub {
2014-08-07 22:24:19 +04:00
$config = $c->reload();
2014-06-11 15:55:00 +04:00
my $index = $render->index($config);
return [ 200, [ 'Content-type' => 'text/html' ], [$index], ];
};
my $me = sub {
2014-08-07 22:24:19 +04:00
$config = $c->reload();
2014-06-11 15:55:00 +04:00
my $messages = $render->to_me($config);
return [ 200, [ 'Content-type' => 'text/html' ], [$messages], ];
};
my $tree = sub {
2014-08-07 22:24:19 +04:00
$config = $c->reload();
2014-06-11 15:55:00 +04:00
my $subges = $render->tree($config);
return [ 200, [ 'Content-type' => 'text/html' ], ['Дерево'], ];
};
my $new = sub {
my $env = shift;
my $req = Plack::Request->new($env);
my $echo = $req->param('echo');
my $send = $render->send_new($echo);
return [ 200, [ 'Content-type' => 'text/html' ], [$send], ];
};
my $send = sub {
my $env = shift;
my $req = Plack::Request->new($env);
my $hash = $req->param('hash');
my $send = $render->send($hash);
return [ 200, [ 'Content-type' => 'text/html' ], [$send], ];
};
my $enc = sub {
my $env = shift;
my $req = Plack::Request->new($env);
# Get parameters
my $echo = $req->param('echo');
my $to = $req->param('to');
my $post = $req->param('post');
my $subg = $req->param('subg');
my $hash = $req->param('hash');
my $time = time();
2014-08-07 22:24:19 +04:00
$config = $c->reload();
2014-06-11 15:55:00 +04:00
my $data = {
echo => $echo,
to => $to,
from => $config->{nick},
subg => $subg,
post => $post,
time => $time,
hash => $hash,
};
my $enc = II::Enc->new( $config, $data );
$enc->encode() == 0 or die "$!\n";
return [ 302, [ 'Location' => '/out' ], [], ];
};
my $out = sub {
my $out = $render->out();
return [ 200, [ 'Content-type' => 'text/html' ], [$out], ];
};
# Push message to server
my $push = sub {
my $env = shift;
my $req = Plack::Request->new($env);
my $echo = $req->param('echo');
my $base64 = $req->param('base64');
my $hash = $req->param('hash');
2014-08-07 22:24:19 +04:00
$config = $c->reload();
2014-11-06 12:45:38 +03:00
if ( $config->{host} =~ m/.+\,.+/ ) {
my @hosts = split( /,/, $config->{host} );
$config->{host} = $hosts[0];
}
2014-06-11 15:55:00 +04:00
my $s = II::Send->new( $config, $echo, $base64 );
$s->send($hash);
my $db = II::DB->new();
$db->update_out($hash);
return [ 302, [ 'Location' => "/e?echo=$echo" ], [], ];
};
2014-06-24 11:31:20 +04:00
# Messages from user
my $user = sub {
my $env = shift;
my $req = Plack::Request->new($env);
my $user = $req->param('user');
my $mes_from = $render->user($user);
return [ 200, [ 'Content-type' => 'text/html' ], [$mes_from], ];
};
2014-08-12 10:42:27 +04:00
# Search
########
my $search = sub {
my $env = shift;
my $req = Plack::Request->new($env);
my $query = $req->param('q');
my $db = II::DB->new();
my @post = $db->do_search($query);
my $result = $render->search(@post);
2014-08-12 10:42:27 +04:00
return [ 200, [ 'Content-type' => 'text/html' ], [$result], ];
};
2016-04-19 12:21:56 +03:00
# Delete out message
####################
my $del = sub {
my $env = shift;
my $req = Plack::Request->new($env);
my $hash = $req->param('hash');
my $db = II::DB->new();
$db->del_out($hash);
return [301, ['Location' => '/out'], [], ];
};
2014-08-04 13:35:42 +04:00
# Mountpoints
2014-06-11 15:55:00 +04:00
builder {
2015-08-11 09:49:27 +03:00
mount "/static" => Plack::App::File->new( root => './s/' )->to_app;
2014-08-12 10:42:27 +04:00
mount "/search" => $search;
2015-08-11 09:49:27 +03:00
mount '/' => $root;
mount '/e' => $echo;
mount '/s' => $thread;
mount '/u' => $user;
mount '/me' => $me;
mount '/tree' => $tree;
mount '/get/' => $get;
mount '/send' => $send;
mount '/enc' => $enc;
mount '/out' => $out;
mount '/push' => $push;
mount '/new' => $new;
2016-04-19 12:21:56 +03:00
mount '/del' => $del;
2014-06-11 15:55:00 +04:00
};