[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 | |
---|
[36] | 29 | |
---|
[35] | 30 | } |
---|
| 31 | |
---|
[100] | 32 | sub viewpaste :Path :Args(1) { |
---|
| 33 | my ($self, $c, $pasteid) = @_; |
---|
| 34 | |
---|
| 35 | $c->forward('get_paste', [ $pasteid ]); |
---|
| 36 | if (! $c->stash->{xmlrpc}) { |
---|
| 37 | $c->go('/404/index'); |
---|
| 38 | } |
---|
| 39 | } |
---|
| 40 | |
---|
[35] | 41 | sub message : XMLRPC { |
---|
[66] | 42 | my ($self, $c, $contexts, $message, @msgargs) = @_; |
---|
[35] | 43 | |
---|
| 44 | my $reqspec = {}; |
---|
[72] | 45 | my @contexts = grep { $_ } ( |
---|
| 46 | $c->user_exists |
---|
| 47 | ? ( 'default', |
---|
| 48 | (ref $contexts |
---|
| 49 | ? (@$contexts) |
---|
| 50 | : ($contexts) |
---|
| 51 | ), |
---|
| 52 | ) |
---|
| 53 | : () |
---|
| 54 | ); |
---|
[35] | 55 | |
---|
[72] | 56 | foreach my $co (@contexts) { |
---|
[36] | 57 | if (ref($co) eq 'HASH') { |
---|
[35] | 58 | foreach (keys %$co) { |
---|
| 59 | $reqspec->{$_} = $co->{$_}; |
---|
| 60 | } |
---|
| 61 | } else { |
---|
[36] | 62 | if (my $coo = $c->forward('/user/fetchdata', [ $co ])) { |
---|
[35] | 63 | foreach (keys %$coo) { |
---|
| 64 | $reqspec->{$_} = $coo->{$_}; |
---|
| 65 | } |
---|
| 66 | } |
---|
| 67 | } |
---|
| 68 | } |
---|
| 69 | |
---|
[66] | 70 | my ($cmd, @args) = @msgargs |
---|
| 71 | ? ($message, @msgargs) |
---|
| 72 | : Text::ParseWords::shellwords($message); |
---|
[38] | 73 | |
---|
| 74 | if ($c->get_action( $cmd, '/chat/cmd' )) { |
---|
[66] | 75 | return $c->go('/chat/cmd/' . $cmd, [ $reqspec, @args ]); |
---|
[64] | 76 | } else { |
---|
| 77 | $c->stash->{xmlrpc} = { |
---|
| 78 | error => 'No such command', |
---|
| 79 | }; |
---|
[38] | 80 | } |
---|
[35] | 81 | } |
---|
| 82 | |
---|
[100] | 83 | sub paste : XMLRPCLocal { |
---|
| 84 | my ($self, $c, $title, $text) = @_; |
---|
| 85 | |
---|
| 86 | my @char = ('a' .. 'z', 'A' .. 'Z', 0 .. 9); |
---|
| 87 | my $id = join('', map { $char[rand(@char)] } (0..7)); |
---|
| 88 | $c->model('Base::ChatPaste')->create( |
---|
| 89 | { |
---|
| 90 | id => $id, |
---|
| 91 | user_id => $c->model('Base::Users')->find( |
---|
| 92 | { mail => $c->user->mail })->ukey, |
---|
| 93 | title => $title, |
---|
| 94 | reply => $text, |
---|
| 95 | } |
---|
| 96 | ); |
---|
| 97 | $c->model('Base')->storage->dbh->commit; |
---|
| 98 | $c->stash->{xmlrpc} = $id; |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | sub get_paste : XMLRPCLocal { |
---|
| 102 | my ($self, $c, $id) = @_; |
---|
| 103 | |
---|
| 104 | my $paste = $c->model('Base::ChatPaste')->find( |
---|
| 105 | { id => $id, }, |
---|
| 106 | { select => [ qw(whenpaste title reply) ], } |
---|
| 107 | ); |
---|
| 108 | if ($paste) { |
---|
| 109 | return $c->stash->{xmlrpc} = { $paste->get_columns }; |
---|
| 110 | } else { |
---|
| 111 | return $c->stash->{xmlrpc} = undef; |
---|
| 112 | } |
---|
| 113 | } |
---|
| 114 | |
---|
[35] | 115 | =head1 AUTHOR |
---|
| 116 | |
---|
| 117 | Olivier Thauvin |
---|
| 118 | |
---|
| 119 | =head1 LICENSE |
---|
| 120 | |
---|
| 121 | This library is free software. You can redistribute it and/or modify |
---|
| 122 | it under the same terms as Perl itself. |
---|
| 123 | |
---|
| 124 | =cut |
---|
| 125 | |
---|
| 126 | __PACKAGE__->meta->make_immutable; |
---|
| 127 | |
---|
| 128 | 1; |
---|