File.pm 2.94 KB
Newer Older
1
2
3
4
5
# -*- indent-tabs-mode: nil; -*-
# vim:ft=perl:et:sw=4

# Sympa - SYsteme de Multi-Postage Automatique
#
6
7
# Copyright 2019 The Sympa Community. See the AUTHORS.md file at
# the top-level directory of this distribution and at
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
# <https://github.com/sympa-community/sympa.git>.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

package Sympa::DataSource::File;

use strict;
use warnings;

use Sympa::Log;
use Sympa::Regexps;

use base qw(Sympa::DataSource);

my $log = Sympa::Log->instance;

sub _open {
    my $self = shift;

    my $fh;
    unless (open $fh, '<', $self->{path}) {
        $log->syslog('err', 'Unable to open file "%s": %m', $self->{path});
        return undef;
    }

    return $fh;
}

# Old name: (part of) Sympa::List::_include_users_file().
sub _next {
    my $self = shift;

51
    my $email_re = Sympa::Regexps::addrspec();
52
53
54
55
56
57

    my $lines = 0;
    my $found = 0;

    my $ifh = $self->__dsh;
    while (my $line = <$ifh>) {
58
        $line =~ s/\s+\z//;    # allow any styles of newline
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

        if (++$lines > 49 and not $found) {
            $log->syslog(
                'err',
                'Too much errors in file %s. Source file probably corrupted. Cancelling',
                $self->{path}
            );
            return undef;
        }

        # Empty lines are skipped
        next if $line =~ /^\s*$/;
        next if $line =~ /^\s*\#/;

        # Skip badly formed emails.
        unless ($line =~ /\A\s*($email_re)(?:\s+(\S.*))?\z/) {
            $log->syslog('err', 'Skip badly formed line: "%s"', $line);
            next;
        }
        my ($email, $gecos) = ($1, $2);
        $gecos =~ s/\s+\z// if defined $gecos;
        $found++;

        return [$email, $gecos];
    }

    return;
}

sub _close {
    my $self = shift;

    my $fh = $self->__dsh;
    return unless ref $fh;

    unless (close $fh) {
        $log->syslog('info', 'Can\'t close data source %s: %m', $self);
        return undef;
    }

    return 1;
}

1;
__END__

=encoding utf-8

=head1 NAME

Sympa::DataSource::file - Data source based on local file

=head1 DESCRIPTION

TBD.

Each line is expected to start with a valid email address and
an optional display name.

=head2 Attributes

=over

=item {name}

Short description of this data source.

=item {path}

Full path to local file.

=back

=head1 SEE ALSO

L<Sympa::DataSource>.

=head1 HISTORY

138
L<Sympa::DataSource::File> appeared on Sympa 6.2.45b.
139
140

=cut