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