File: build_ldif.pl

package info (click to toggle)
libmail-imapclient-perl 2.1.4-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,236 kB
  • ctags: 231
  • sloc: perl: 11,273; makefile: 56
file content (195 lines) | stat: -rwxr-xr-x 5,283 bytes parent folder | download
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
#!/usr/local/bin/perl
#$Id: build_ldif.pl,v 19991216.9 2000/12/11 21:58:51 dkernen Exp $
use Mail::IMAPClient;
use MIME::Lite;
use Data::Dumper;

=head1 DESCRIPTION

B<build_ldif.pl> accepts the name of a target folder as an argument. It
then opens that folder and rummages through all the mail files in it, looking
for "Reply-to:" headers (or "From:" headers, where there is no "Reply-to:").
It then prints to STDOUT a file in ldif format containing entries for all of 
the addresses that it finds. It also appends a message into the specified folder containing
all of the addresses in both the B<To:> field of the message header and in an 
LDIF-format attachment.

B<build_ldif.pl> requires B<MIME::Lite>.

=head1 SYNTAX

B<build_ldif.pl> I<-h>

B<build_ldif.pl> I<-s servername -u username -p password -f folder [ -d ]>

=over 4

=item -f The folder name to process.

=item -s The servername of the IMAP server 

=item -t Include "To" and "Cc" fields as well as "From"

=item -u The user to log in as

=item -p The password for the user specified in the I<-u> option

=item -d Tells the IMAP client to turn on debugging info

=item -n Suppress delivering message to folder

=item -h Prints out this document

=back

B<NOTE:> You can supply defaults for the above options by updating the script.

=cut

use Getopt::Std;

getopts('hs:u:p:f:dtn');

# Update the following to supply defaults:

$opt_f ||= "default folder";
$opt_s ||= "default server";
$opt_u ||= "default user";
$opt_p ||= "default password";	# security risk: use with caution!

# Let the compiler know we're serious about these variables:
$opt_0 = ( $opt_h or $opt_d or $opt_t or $opt_n or $opt_0); 

exec "perldoc $0" if $opt_h;

my $imap = Mail::IMAPClient->new( 
		Server 	=> $opt_s ,
		User 	=> $opt_u ,
		Password=> $opt_p ,
		Debug	=> $opt_d||0 ,
) or die "can't connect to server\n";

$imap->select($opt_f); $imap->expunge;

my @msgs = $imap->search("NOT SUBJECT",qq("buid_ldif.pl $opt_f Output"));
my %list;
foreach my $m (@msgs) {

	my $ref = $imap->parse_headers($m,"Reply-to","From");
	
	warn "Couldn't get recipient address from msg#$m\n" 
		unless 	scalar(@{$ref->{'Reply-to'}})   ||
			scalar(@{$ref->{'From'}})	;	

	my $from = scalar(@{$ref->{'Reply-to'}}) 	? 
			  $ref->{'Reply-to'}[0]		: 
			  $ref->{'From'}[0] 		;
	my $name = $from				;

	$name =~ s/<.*//				;
	if ($name =~ /\@/) {
		$name = $from				;
		$name =~ s/\@.*//;			;
	}
	$name =~ s/\"//g				;
	$name =~ s/^\s+|\s+$//g				;
	my $addr = $from				; 
	$addr =~ s/.*<//				;
	$addr =~ s/[\<\>]//g				;
	$list{lc($addr)} = [ $addr, $name ]
		unless exists $list{lc($addr)} 		;
	if ($opt_t) {					# Do "To" and "Cc", too
		my $ref = $imap->parse_headers($m,"To","Cc")		;
		my @array = ( 	@{$ref->{To}}	, @{$ref->{Cc}}	)	;
		my @members = ()					;
		foreach my $text (@array) 				{
		  while ( $text =~ /	"([^"\\]*(\\.[^"\\]*)*"[^,]*),?	|
					([^",]+),?			|
					,
			/gx 
		  ) {
			push @members, defined($1)?$1:$3 		;
		  }
		}
		foreach my $to (@members) 				{ 

			my $name = $to					;

			$name =~ s/<.*//				;
			if ($name =~ /\@/) {
				$name = $to				;
				$name =~ s/\@.*//;			;
			}
			$name =~ s/\"//g				;
			$name =~ s/^\s+|\s+$//g				;
			my $addr = $to					; 
			$addr =~ s/.*<//				;
			$addr =~ s/[\<\>]//g				;
			$list{lc($addr)} = [ $addr, $name ]
				unless exists $list{lc($addr)} 		;
		}
	
	}
}

my $text = join "",map {
	qq{dn: cn="} . $list{$_}[1] . 
	qq{", mail=$list{$_}[0]\n} .
	qq{cn: } . $list{$_}[1] . qq{\n} .
	qq{mail: $list{$_}[0]\n} .
	qq{objectclass: top\nobjectclass: person\n\n};
} keys %list ;

# Create a new multipart message:
my $msg = MIME::Lite->new(
        From    => $opt_u,
        map({ ("To" => $list{$_}[0]) } keys %list),
        Subject => "LDIF file from $opt_f",
        Type    =>'TEXT',
        Data    =>"Attached is the LDIF file of addresses from folder $opt_f."
);
$msg->attach(	Type     =>'text/ldif',
		Filename => "$opt_f.ldif",
                Data 	 => $text ,
);
print $text;
$imap->append($opt_f, $msg->as_string) unless $opt_n;
print Dumper($imap) if $opt_d;
$imap->logout;

# $Id: build_ldif.pl,v 19991216.9 2000/12/11 21:58:51 dkernen Exp $
# $Log: build_ldif.pl,v $
# Revision 19991216.9  2000/12/11 21:58:51  dkernen
#
# Modified Files:
# 	build_dist.pl build_ldif.pl copy_folder.pl find_dup_msgs.pl
# 	imap_to_mbox.pl populate_mailbox.pl
# to add CVS data
#
# Revision 19991216.8  2000/03/02 19:57:13  dkernen
#
# Modified Files: build_ldif.pl -- to support new option to all "To:" and "Cc:" to be included in ldif file
#
# Revision 19991216.7  2000/02/21 16:16:10  dkernen
#
# Modified Files: build_ldif.pl  -- to allow for "To:" and "Cc:" header handling and
# to handle quoted names in headers
#
# Revision 19991216.6  1999/12/28 13:56:59  dkernen
# Fixed -h option (help).
#
# Revision 19991216.5  1999/12/16 17:19:10  dkernen
# Bring up to same level
#
# Revision 19991124.3  1999/12/16 17:14:24  dkernen
# Incorporate changes for exists method performance enhancement
#
# Revision 19991124.02  1999/11/24 17:46:18  dkernen
# More fixes to t/basic.t
#
# Revision 19991124.01  1999/11/24 16:51:48  dkernen
# Changed t/basic.t to test for UIDPLUS before trying UID cmds
#
# Revision 1.8  1999/11/23 17:51:05  dkernen
# Committing version 1.06 distribution copy
#