File: Addressbook.pm

package info (click to toggle)
ciderwebmail 1.05%2B20240702-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,392 kB
  • sloc: perl: 3,201; xml: 782; javascript: 675; sh: 42; makefile: 29
file content (202 lines) | stat: -rw-r--r-- 4,743 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package CiderWebmail::Controller::Addressbook;
use Moose;

use Carp qw/ croak /;

use CiderWebmail::Util;
use Email::Valid;

BEGIN {extends 'Catalyst::Controller'; }

=head1 NAME

CiderWebmail::Controller::Addressbook - Catalyst Controller

=head1 DESCRIPTION

Catalyst Controller.

=head1 METHODS

=cut

=head2 index

redirect to addressbook/list

=cut

sub index : Private {
    my ($self, $c) = @_;

    $c->res->redirect($c->uri_for('/addressbook/list'));

    return;
}

=head2 setup

common function used to setup addressbook

=cut

sub setup : Chained('/') PathPart('addressbook') CaptureArgs(0) {
    my ($self, $c) = @_;

    CiderWebmail::Util::add_foldertree_to_stash($c);

    return;
}

=head2 list

list addressbook contents

=cut

sub list : Chained('/addressbook/setup') PathPart('list') Args(0) {
    my ($self, $c) = @_;

    #todo move compose to /compose instead of /mailbox/FOO/compose or figure out the mailbox here
    $c->stash->{uri_compose} = $c->uri_for("/mailbox/INBOX/compose");
    $c->stash->{uri_addressbook} = $c->uri_for("/addressbook");

    my @addresses = $c->model('DB::Addressbook')->search({ user => $c->user->id })->all;
    $c->stash->{addresses} = \@addresses;

    $c->stash->{template} = 'addressbook/list.xml';

    $c->detach();

    return;
}

=head2 modify

common setup function for addressbook modify operations (add, delete, edit)

=cut

sub modify : Chained('/addressbook/setup') Path('modify') CaptureArgs(0) {
    my ($self, $c) = @_;

    if ($c->req->param('update')) {
        $c->stash->{error} = "All fields need to be filled out" unless ($c->req->param('firstname') =~ m/\w+/xm);
        $c->stash->{error} = "All fields need to be filled out" unless ($c->req->param('surname') =~ m/\w+/xm);
        $c->stash->{error} = "All fields need to be filled out" unless ($c->req->param('email') && Email::Valid->address($c->req->param('email')));

        return if $c->stash->{error};

        my $addressbook = $c->model('DB::Addressbook');

        my $entry;
        if (defined $c->req->param('id') and ($c->req->param('id') =~ m/^\d+$/xm)) {
            $entry = $addressbook->find($c->req->param('id'));
        }

        if (defined $entry) {
            croak("entry does not belong to user") unless $entry->user eq $c->user->id;
            $entry->update({
                firstname => $c->req->param('firstname'),
                surname => $c->req->param('surname'),
                email => $c->req->param('email'),
                user => $c->user->id });

        }
        else {
            $c->model('DB::Addressbook')->create({
                firstname => $c->req->param('firstname'),
                surname => $c->req->param('surname'),
                email => $c->req->param('email'),
                user => $c->user->id });
        }

        $c->forward('list');
    }

    return;
}

=head2 edit

edit addressbook entry

=cut

sub edit : Chained('/addressbook/modify') PathPart('edit') Args(1) {
    my ($self, $c, $id ) = @_;

    my $entry  = $c->model('DB::Addressbook')->search({ user => $c->user->id, id => $id })->first;
    croak("entry not found in addressbook") unless $entry;

    $c->stash->{id} = $entry->id;
    $c->stash->{firstname} = $entry->firstname;
    $c->stash->{surname} = $entry->surname;
    $c->stash->{email} = $entry->email;

    $c->stash->{uri_modify} = $c->uri_for('/addressbook/modify/edit', $id);

    $c->stash->{template} = 'addressbook/edit.xml';

    return;
}

=head2 add

add addressbook entry

=cut

sub add : Chained('/addressbook/modify') PathPart('add') Args(0) {
    my ($self, $c ) = @_;

    $c->stash->{firstname} = $c->req->param('firstname');
    $c->stash->{surname} = $c->req->param('surname');
    $c->stash->{email} = $c->req->param('email');

    #we only have a name (for example from an email "From" header), attemt a best-effort to split it into first and surname
    if ($c->req->param('name')) {
        if ($c->req->param('name') =~ m/^(\w+)/mx) {
            $c->stash->{firstname} = $1;
        }

        if ($c->req->param('name') =~ m/\w+\s*(.*)/mx) {
            $c->stash->{surname} = $1;
        }
    }

    $c->stash->{uri_modify} = $c->uri_for('/addressbook/modify/add');

    $c->stash->{template} = 'addressbook/edit.xml';

    return;
}

=head2 delete

delete addressbook entry

=cut

sub delete : Chained('/addressbook/setup') Path('delete') Args(1) {
    my ($self, $c, $id) = @_;

    $c->model('DB::Addressbook')->search({ user => $c->user->id, id => $id })->delete;

    $c->forward('list');

    return;
}

=head1 AUTHOR

Mathias Reitinger,,,

=head1 LICENSE

This library is free software. You can redistribute it and/or modify
it under the same terms as Perl itself.

=cut

1;