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 | $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 | } |
---|
40 | |
---|
41 | } |
---|
42 | |
---|
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 | |
---|
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 | |
---|
73 | sub message : XMLRPC { |
---|
74 | my ($self, $c, $contexts, $message, @msgargs) = @_; |
---|
75 | |
---|
76 | my $reqspec = {}; |
---|
77 | my @contexts = grep { $_ } ( |
---|
78 | $c->user_exists |
---|
79 | ? ( 'default' ) |
---|
80 | : (), |
---|
81 | (ref $contexts |
---|
82 | ? (@$contexts) |
---|
83 | : ($contexts) |
---|
84 | ), |
---|
85 | ); |
---|
86 | |
---|
87 | foreach my $co (@contexts) { |
---|
88 | if (ref($co) eq 'HASH') { |
---|
89 | foreach (keys %$co) { |
---|
90 | $reqspec->{$_} = $co->{$_}; |
---|
91 | } |
---|
92 | } else { |
---|
93 | if (my $coo = $c->forward('/user/fetchdata', [ $co ])) { |
---|
94 | foreach (keys %$coo) { |
---|
95 | $reqspec->{$_} = $coo->{$_}; |
---|
96 | } |
---|
97 | } |
---|
98 | } |
---|
99 | } |
---|
100 | |
---|
101 | my ($cmd, @args) = @msgargs |
---|
102 | ? ($message, @msgargs) |
---|
103 | : Text::ParseWords::shellwords($message); |
---|
104 | |
---|
105 | if ($c->get_action( $cmd, '/chat/cmd' )) { |
---|
106 | return $c->go('/chat/cmd/' . $cmd, [ $reqspec, @args ]); |
---|
107 | } else { |
---|
108 | $c->stash->{xmlrpc} = { |
---|
109 | error => 'No such command', |
---|
110 | }; |
---|
111 | } |
---|
112 | } |
---|
113 | |
---|
114 | sub paste : XMLRPCLocal { |
---|
115 | my ($self, $c, $title, $text) = @_; |
---|
116 | |
---|
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')->search({ |
---|
121 | -nest => \[ |
---|
122 | "whenpaste < now() - ?::interval", |
---|
123 | [ plain_text => "30 days" ], |
---|
124 | ] |
---|
125 | })->delete; |
---|
126 | $c->model('Base::ChatPaste')->create( |
---|
127 | { |
---|
128 | id => $id, |
---|
129 | user_id => $c->model('Base::Users')->find( |
---|
130 | { mail => $c->user->mail })->ukey, |
---|
131 | title => $title, |
---|
132 | reply => $text, |
---|
133 | } |
---|
134 | ); |
---|
135 | $c->model('Base')->storage->dbh->commit; |
---|
136 | return $c->stash->{xmlrpc} = $id; |
---|
137 | } else { |
---|
138 | return $c->stash->{xmlrpc} = undef; |
---|
139 | } |
---|
140 | } |
---|
141 | |
---|
142 | sub get_paste : XMLRPCLocal { |
---|
143 | my ($self, $c, $id) = @_; |
---|
144 | |
---|
145 | my $paste = $c->model('Base::ChatPaste')->find( |
---|
146 | { id => $id, }, |
---|
147 | { select => [ qw(whenpaste title reply) ], } |
---|
148 | ); |
---|
149 | if ($paste) { |
---|
150 | return $c->stash->{xmlrpc} = { $paste->get_columns }; |
---|
151 | } else { |
---|
152 | return $c->stash->{xmlrpc} = undef; |
---|
153 | } |
---|
154 | } |
---|
155 | |
---|
156 | =head1 AUTHOR |
---|
157 | |
---|
158 | Olivier Thauvin |
---|
159 | |
---|
160 | =head1 LICENSE |
---|
161 | |
---|
162 | This library is free software. You can redistribute it and/or modify |
---|
163 | it under the same terms as Perl itself. |
---|
164 | |
---|
165 | =cut |
---|
166 | |
---|
167 | __PACKAGE__->meta->make_immutable; |
---|
168 | |
---|
169 | 1; |
---|