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

Last change on this file since 388 was 379, checked in by nanardon, 13 years ago
  • add set and show bot function, server side
File size: 3.5 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    my $stat = $c->model('Base::ChatStat')->find_or_create({
57        cmd => $cmd,
58        day => 'now()',
59    });
60    $stat->update({ count => ($stat->count || 0) + 1 });
61    $c->model('Base')->storage->dbh->commit;
62}
63
64
65sub message : XMLRPC {
66    my ($self, $c, $contexts, $message, @msgargs) = @_;
67   
68    my $reqspec = {};
69    my @contexts = grep { $_ } (
70        $c->user_exists
71        ? ( 'default' )
72        : (),
73        (ref $contexts
74            ? (@$contexts)
75            : ($contexts)
76        ),
77    );
78
79    foreach my $co (@contexts) {
80        if (ref($co) eq 'HASH') {
81            foreach (keys %$co) {
82                $reqspec->{$_} = $co->{$_};
83            }
84        } else {
85            if (my $coo = $c->forward('/user/fetchdata', [ $co ])) {
86                foreach (keys %$coo) { 
87                    $reqspec->{$_} = $coo->{$_};
88                }
89            }
90        }
91    }
92
93    if (! ref($contexts[-1])) {
94        $reqspec->{from} = $contexts[-1];
95    }
96
97    my ($cmd, @args) = @msgargs
98        ? ($message, @msgargs)
99        : Text::ParseWords::shellwords($message);
100
101    if ($c->get_action( $cmd, '/chat/cmd' )) {
102        return $c->go('/chat/cmd/' . $cmd, [ $reqspec, @args ]);
103    } else {
104        $c->stash->{xmlrpc} = {
105            error => 'No such command',
106        };
107    }
108}
109
110sub paste : XMLRPCLocal {
111    my ($self, $c, $title, $text) = @_;
112
113    if ($c->user_exists) {
114        my @char = ('a' .. 'z', 'A' .. 'Z', 0 .. 9);
115        my $id = join('', map { $char[rand(@char)] } (0..7));
116        $c->model('Base::ChatPaste')->create(
117            {
118                id => $id,
119                user_id => $c->model('Base::Users')->find(
120                    { mail => $c->user->mail })->ukey,
121                title => $title,
122                reply => $text,
123            }
124        );
125        $c->model('Base')->storage->dbh->commit;
126        return $c->stash->{xmlrpc} = $id;
127    } else {
128        return $c->stash->{xmlrpc} = undef;
129    }
130}
131
132sub get_paste : XMLRPCLocal {
133    my ($self, $c, $id) = @_;
134
135    my $paste = $c->model('Base::ChatPaste')->find(
136            { id => $id, },
137            { select => [ qw(whenpaste title reply) ], }
138        );
139    if ($paste) {
140        return $c->stash->{xmlrpc} = { $paste->get_columns };
141    } else {
142        return $c->stash->{xmlrpc} = undef;
143    }
144}
145
146=head1 AUTHOR
147
148Olivier Thauvin
149
150=head1 LICENSE
151
152This library is free software. You can redistribute it and/or modify
153it under the same terms as Perl itself.
154
155=cut
156
157__PACKAGE__->meta->make_immutable;
158
1591;
Note: See TracBrowser for help on using the repository browser.