ACL for hardware class config, RT#85057
[freeside.git] / FS / FS / acct_snarf.pm
1 package FS::acct_snarf;
2 use base qw(FS::Record);
3
4 use strict;
5 use Tie::IxHash;
6 use FS::Record qw( qsearchs );
7 use FS::cust_svc;
8
9 =head1 NAME
10
11 FS::acct_snarf - Object methods for acct_snarf records
12
13 =head1 SYNOPSIS
14
15   use FS::acct_snarf;
16
17   $record = new FS::acct_snarf \%hash;
18   $record = new FS::acct_snarf { 'column' => 'value' };
19
20   $error = $record->insert;
21
22   $error = $new_record->replace($old_record);
23
24   $error = $record->delete;
25
26   $error = $record->check;
27
28 =head1 DESCRIPTION
29
30 An FS::svc_acct object represents an external mail account, typically for
31 download of mail.  FS::acct_snarf inherits from FS::Record.  The following
32 fields are currently supported:
33
34 =over 4
35
36 =item snarfnum - primary key
37
38 =item snarfname - Label
39
40 =item svcnum - Account (see L<FS::svc_acct>)
41
42 =item machine - external machine to download mail from
43
44 =item protocol - protocol (pop3, imap, etc.)
45
46 =item username - external login username
47
48 =item _password - external login password
49
50 =back
51
52 =head1 METHODS
53
54 =over 4
55
56 =item new HASHREF
57
58 Creates a new record.  To add the record to the database, see L<"insert">.
59
60 Note that this stores the hash reference, not a distinct copy of the hash it
61 points to.  You can ask the object for a copy with the I<hash> method.
62
63 =cut
64
65 sub table { 'acct_snarf'; }
66
67 =item insert
68
69 Adds this record to the database.  If there is an error, returns the error,
70 otherwise returns false.
71
72 =cut
73
74 # the insert method can be inherited from FS::Record
75
76 =item delete
77
78 Delete this record from the database.
79
80 =cut
81
82 # the delete method can be inherited from FS::Record
83
84 =item replace OLD_RECORD
85
86 Replaces the OLD_RECORD with this one in the database.  If there is an error,
87 returns the error, otherwise returns false.
88
89 =cut
90
91 # the replace method can be inherited from FS::Record
92
93 =item cust_svc
94
95 =cut
96
97 sub cust_svc {
98   my $self = shift;
99   qsearchs('cust_svc', { 'svcnum' => $self->svcnum } );
100 }
101
102
103 =item svc_export
104
105 Calls the replace export for any communigate exports attached to this rule's
106 service.
107
108 =cut
109
110 sub svc_export {
111   my $self = shift;
112
113   my $cust_svc = $self->cust_svc;
114   my $svc_x = $cust_svc->svc_x;
115   
116   #_singledomain too
117   my @exports = $cust_svc->part_svc->part_export('communigate_pro');
118   my @errors = map $_->export_replace($svc_x, $svc_x), @exports;
119
120   @errors ? join(' / ', @errors) : '';
121
122 }
123
124 =item check
125
126 Checks all fields to make sure this is a valid external mail account.  If
127 there is an error, returns the error, otherwise returns false.  Called by the
128 insert and replace methods.
129
130 =cut
131
132 sub check {
133   my $self = shift;
134   my $error =
135        $self->ut_numbern('snarfnum')
136     || $self->ut_textn('snarfname') #alphasn?
137     || $self->ut_number('svcnum')
138     || $self->ut_foreign_key('svcnum', 'svc_acct', 'svcnum')
139     || $self->ut_domain('machine')
140     || $self->ut_alphan('protocol')
141     || $self->ut_textn('username')
142     || $self->ut_numbern('check_freq')
143     || $self->ut_enum('leavemail', [ '', 'Y' ])
144     || $self->ut_enum('apop', [ '', 'Y' ])
145     || $self->ut_enum('tls', [ '', 'Y' ])
146     || $self->ut_alphan('mailbox')
147   ;
148   return $error if $error;
149
150   $self->_password =~ /^[^\t\n]*$/ or return "illegal password";
151   $self->_password($1);
152
153   ''; #no error
154 }
155
156 sub check_freq_labels {
157
158   tie my %hash, 'Tie::IxHash',
159     0 => 'Never',
160     60 => 'minute',
161     120 => '2 minutes',
162     180 => '3 minutes',
163     300 => '5 minutes',
164     600 => '10 minutes',
165     900 => '15 minutes',
166     1800 => '30 minutes',
167     3600 => 'hour',
168     7200 => '2 hours',
169     10800 => '3 hours',
170     21600 => '6 hours',
171     43200 => '12 hours',
172     86400 => 'day',
173     172800 => '2 days',
174     259200 => '3 days',
175     604800 => 'week',
176     1000000000 => 'Disabled',
177   ;
178
179   \%hash;
180 }
181
182 =item cgp_hashref
183
184 Returns a hashref representing this external mail account, suitable for
185 Communigate Pro API commands:
186
187 =cut
188
189 sub cgp_hashref {
190   my $self = shift;
191   {
192     'authName' => $self->username,
193     'domain'   => $self->machine,
194     'password' => $self->_password,
195     'period'   => $self->check_freq.'s',
196     'APOP'     => ( $self->apop      eq 'Y' ? 'YES' : 'NO' ),
197     'TLS'      => ( $self->tls       eq 'Y' ? 'YES' : 'NO' ),
198     'Leave'    => ( $self->leavemail eq 'Y' ? 'YES' : 'NO' ), #XXX leave??
199   };
200 }
201
202 =back
203
204 =head1 BUGS
205
206 =head1 SEE ALSO
207
208 L<FS::Record>, schema.html from the base documentation.
209
210 =cut
211
212 1;
213