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

Last change on this file since 114 was 100, checked in by nanardon, 14 years ago
  • add web page to view paste from the bot
  • add first function
File size: 2.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
30}
31
32sub 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
41sub 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
83sub 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
101sub 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
115=head1 AUTHOR
116
117Olivier Thauvin
118
119=head1 LICENSE
120
121This library is free software. You can redistribute it and/or modify
122it under the same terms as Perl itself.
123
124=cut
125
126__PACKAGE__->meta->make_immutable;
127
1281;
Note: See TracBrowser for help on using the repository browser.