[35] | 1 | package Sophie::Controller::Chat; |
---|
| 2 | use Moose; |
---|
| 3 | use namespace::autoclean; |
---|
[38] | 4 | use Getopt::Long; |
---|
| 5 | use Text::ParseWords; |
---|
[35] | 6 | |
---|
| 7 | BEGIN {extends 'Catalyst::Controller'; } |
---|
| 8 | |
---|
| 9 | =head1 NAME |
---|
| 10 | |
---|
| 11 | Sophie::Controller::Chat - Catalyst Controller |
---|
| 12 | |
---|
| 13 | =head1 DESCRIPTION |
---|
| 14 | |
---|
| 15 | Catalyst Controller. |
---|
| 16 | |
---|
| 17 | =head1 METHODS |
---|
| 18 | |
---|
| 19 | =cut |
---|
| 20 | |
---|
| 21 | |
---|
| 22 | =head2 index |
---|
| 23 | |
---|
| 24 | =cut |
---|
| 25 | |
---|
| 26 | sub index :Path :Args(0) { |
---|
| 27 | my ( $self, $c ) = @_; |
---|
| 28 | |
---|
[189] | 29 | $c->stash->{template} = 'chat/index.html'; |
---|
| 30 | if (my $cmd = $c->req->param('cmd')) { |
---|
| 31 | $c->forward('message', [ |
---|
| 32 | [{ |
---|
| 33 | distribution => $c->req->param('distribution') || undef, |
---|
| 34 | release => $c->req->param('release') || undef, |
---|
| 35 | arch => $c->req->param('arch') || undef, |
---|
| 36 | max_line => 30, |
---|
| 37 | no_paste => 1, |
---|
| 38 | }], $cmd ]); |
---|
| 39 | } |
---|
[36] | 40 | |
---|
[35] | 41 | } |
---|
| 42 | |
---|
[100] | 43 | sub viewpaste :Path :Args(1) { |
---|
| 44 | my ($self, $c, $pasteid) = @_; |
---|
| 45 | |
---|
| 46 | $c->forward('get_paste', [ $pasteid ]); |
---|
| 47 | if (! $c->stash->{xmlrpc}) { |
---|
| 48 | $c->go('/404/index'); |
---|
| 49 | } |
---|
| 50 | } |
---|
| 51 | |
---|
[238] | 52 | |
---|
| 53 | sub update_statistic : Private { |
---|
| 54 | my ($self, $c, $cmd) = @_; |
---|
| 55 | |
---|
| 56 | $c->model('Base::ChatStat')->search( |
---|
| 57 | { |
---|
| 58 | -nest => \[ |
---|
| 59 | "day < now() - ?::interval", |
---|
| 60 | [ plain_text => "365 days" ], |
---|
| 61 | ], |
---|
| 62 | } |
---|
| 63 | )->delete; |
---|
| 64 | my $stat = $c->model('Base::ChatStat')->find_or_create({ |
---|
| 65 | cmd => $cmd, |
---|
| 66 | day => 'now()', |
---|
| 67 | }); |
---|
| 68 | $stat->update({ count => ($stat->count || 0) + 1 }); |
---|
| 69 | $c->model('Base')->storage->dbh->commit; |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | |
---|
[35] | 73 | sub message : XMLRPC { |
---|
[66] | 74 | my ($self, $c, $contexts, $message, @msgargs) = @_; |
---|
[35] | 75 | |
---|
| 76 | my $reqspec = {}; |
---|
[72] | 77 | my @contexts = grep { $_ } ( |
---|
| 78 | $c->user_exists |
---|
[189] | 79 | ? ( 'default' ) |
---|
| 80 | : (), |
---|
| 81 | (ref $contexts |
---|
| 82 | ? (@$contexts) |
---|
| 83 | : ($contexts) |
---|
| 84 | ), |
---|
[72] | 85 | ); |
---|
[35] | 86 | |
---|
[72] | 87 | foreach my $co (@contexts) { |
---|
[36] | 88 | if (ref($co) eq 'HASH') { |
---|
[35] | 89 | foreach (keys %$co) { |
---|
| 90 | $reqspec->{$_} = $co->{$_}; |
---|
| 91 | } |
---|
| 92 | } else { |
---|
[36] | 93 | if (my $coo = $c->forward('/user/fetchdata', [ $co ])) { |
---|
[35] | 94 | foreach (keys %$coo) { |
---|
| 95 | $reqspec->{$_} = $coo->{$_}; |
---|
| 96 | } |
---|
| 97 | } |
---|
| 98 | } |
---|
| 99 | } |
---|
| 100 | |
---|
[66] | 101 | my ($cmd, @args) = @msgargs |
---|
| 102 | ? ($message, @msgargs) |
---|
| 103 | : Text::ParseWords::shellwords($message); |
---|
[38] | 104 | |
---|
| 105 | if ($c->get_action( $cmd, '/chat/cmd' )) { |
---|
[66] | 106 | return $c->go('/chat/cmd/' . $cmd, [ $reqspec, @args ]); |
---|
[64] | 107 | } else { |
---|
| 108 | $c->stash->{xmlrpc} = { |
---|
| 109 | error => 'No such command', |
---|
| 110 | }; |
---|
[38] | 111 | } |
---|
[35] | 112 | } |
---|
| 113 | |
---|
[100] | 114 | sub paste : XMLRPCLocal { |
---|
| 115 | my ($self, $c, $title, $text) = @_; |
---|
| 116 | |
---|
[156] | 117 | if ($c->user_exists) { |
---|
| 118 | my @char = ('a' .. 'z', 'A' .. 'Z', 0 .. 9); |
---|
| 119 | my $id = join('', map { $char[rand(@char)] } (0..7)); |
---|
| 120 | $c->model('Base::ChatPaste')->create( |
---|
| 121 | { |
---|
| 122 | id => $id, |
---|
| 123 | user_id => $c->model('Base::Users')->find( |
---|
| 124 | { mail => $c->user->mail })->ukey, |
---|
| 125 | title => $title, |
---|
| 126 | reply => $text, |
---|
| 127 | } |
---|
| 128 | ); |
---|
| 129 | $c->model('Base')->storage->dbh->commit; |
---|
| 130 | return $c->stash->{xmlrpc} = $id; |
---|
| 131 | } else { |
---|
| 132 | return $c->stash->{xmlrpc} = undef; |
---|
| 133 | } |
---|
[100] | 134 | } |
---|
| 135 | |
---|
| 136 | sub get_paste : XMLRPCLocal { |
---|
| 137 | my ($self, $c, $id) = @_; |
---|
| 138 | |
---|
| 139 | my $paste = $c->model('Base::ChatPaste')->find( |
---|
| 140 | { id => $id, }, |
---|
| 141 | { select => [ qw(whenpaste title reply) ], } |
---|
| 142 | ); |
---|
| 143 | if ($paste) { |
---|
| 144 | return $c->stash->{xmlrpc} = { $paste->get_columns }; |
---|
| 145 | } else { |
---|
| 146 | return $c->stash->{xmlrpc} = undef; |
---|
| 147 | } |
---|
| 148 | } |
---|
| 149 | |
---|
[35] | 150 | =head1 AUTHOR |
---|
| 151 | |
---|
| 152 | Olivier Thauvin |
---|
| 153 | |
---|
| 154 | =head1 LICENSE |
---|
| 155 | |
---|
| 156 | This library is free software. You can redistribute it and/or modify |
---|
| 157 | it under the same terms as Perl itself. |
---|
| 158 | |
---|
| 159 | =cut |
---|
| 160 | |
---|
| 161 | __PACKAGE__->meta->make_immutable; |
---|
| 162 | |
---|
| 163 | 1; |
---|