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