source: server/trunk/web/lib/Sophie/Controller/Chat.pm @ 319

Last change on this file since 319 was 319, checked in by nanardon, 13 years ago
  • delete old paste in maintenance task
File size: 3.6 KB
Line 
1package Sophie::Controller::Chat;
2use Moose;
3use namespace::autoclean;
4use Getopt::Long;
5use Text::ParseWords;
6
7BEGIN {extends 'Catalyst::Controller'; }
8
9=head1 NAME
10
11Sophie::Controller::Chat - Catalyst Controller
12
13=head1 DESCRIPTION
14
15Catalyst Controller.
16
17=head1 METHODS
18
19=cut
20
21
22=head2 index
23
24=cut
25
26sub 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
43sub 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
53sub 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
73sub 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
114sub 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')->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    }
134}
135
136sub 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
150=head1 AUTHOR
151
152Olivier Thauvin
153
154=head1 LICENSE
155
156This library is free software. You can redistribute it and/or modify
157it under the same terms as Perl itself.
158
159=cut
160
161__PACKAGE__->meta->make_immutable;
162
1631;
Note: See TracBrowser for help on using the repository browser.