#!/usr/bin/perl
# address_chooser.cgi
# Display a list of entries from the address book
use strict;
use warnings;
no warnings 'redefine';
no warnings 'uninitialized';
our (%text, %in, %uconfig);
our $trust_unknown_referers = 1;
require './mailbox-lib.pl';
&ReadParse();
&popup_header($text{'address_choose'}, undef,
"onLoad='document.forms[0].search.focus()'");
print <
function makeaddress(a, n)
{
if (n == "") {
// An address without a name
return "<"+a+">";
}
else if (n.indexOf(",") != -1) {
// A name with a comma in it
return "\\""+n+"\\" <"+a+">";
}
else {
// A name without a comma
return n+" <"+a+">";
}
}
// Add an address to the list
function newaddress(a, n)
{
av = makeaddress(a, n);
if (top.opener.ifield.value == "") {
top.opener.ifield.value = av;
}
else {
top.opener.ifield.value += ","+av;
}
}
// Remove an address from the list
function oldaddress(a, n)
{
av = makeaddress(a, n);
curr = top.opener.ifield.value;
idx1 = curr.indexOf(av+",");
idx2 = curr.indexOf(","+av);
if (curr == av) {
// Address only!
curr = "";
}
else if (idx1 != -1) {
// Found the address, in string
curr = curr.substring(0, idx1)+curr.substring(idx1+av.length+1);
}
else if (idx2 != -1) {
// Found the ,address in string
curr = curr.substring(0, idx2)+curr.substring(idx2+av.length+1);
}
else {
// Look for address only
sp = curr.split(",");
curr = "";
for(j=0; j 0)
a = a.substr(0, at);
}
if (top.opener.rfield != null) {
// Put real name in separate field
top.opener.ifield.value = a;
top.opener.rfield.value = n;
}
else {
// Combine to single field
if (n == "") {
av = "<"+a+">";
}
else {
av = n+" <"+a+">";
}
top.opener.ifield.value = av;
}
window.close();
}
EOF
# Find addresses and groups
my @addrs = &list_addresses();
my $addrs_count;
my @agroups;
my %mems;
if ($in{'search'}) {
my $s = $in{'search'};
@addrs = grep { $_->[0] =~ /\Q$s\E/i || $_->[1] =~ /\Q$s\E/i } @addrs;
}
if ($in{'mode'}) {
@addrs = grep { $_->[3] } @addrs;
$addrs_count = scalar(@addrs);
}
else {
if (!$uconfig{'from_in_to'}) {
@addrs = grep { !$_->[3] } @addrs;
}
$addrs_count = scalar(@addrs);
@agroups = &list_address_groups();
if ($in{'search'}) {
my $s = $in{'search'};
@agroups = grep { $_->[0] =~ /\Q$s\E/i ||
$_->[1] =~ /\Q$s\E/i } @agroups;
}
foreach my $a (@agroups) {
push(@addrs, [ $a->[0] ]);
$mems{$a->[0]} = [ &split_addresses($a->[1]) ];
}
}
# Show search form
if (@addrs || $in{'search'}) {
print &ui_form_start("address_chooser.cgi", "post");
print "$text{'address_search'}\n";
print &ui_textbox("search", $in{'search'}, 20);
print &ui_hidden("mode", $in{'mode'});
print &ui_hidden("addr", $in{'addr'});
print &ui_submit($text{'address_ok'});
print &ui_form_end();
}
# Show list of addresses
my %infield;
my $href;
if (@addrs) {
my @sp = &split_addresses(&decode_mimewords($in{'addr'}));
for(my $i=0; $i<@sp; $i++) {
$infield{$sp[$i]->[0]} = $i;
}
print "\n";
}
elsif ($in{'search'}) {
print "$text{'address_none2'}