PageRenderTime 73ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

/mailboxes/folders-lib.pl

https://bitbucket.org/gencer/webmin
Perl | 3608 lines | 2953 code | 170 blank | 485 comment | 764 complexity | 01d8c9b3b1d14d869a04deec4830094c MD5 | raw file
Possible License(s): BSD-3-Clause, CC-BY-SA-3.0

Large files files are truncated, but you can click here to view the full file

  1. # folders-lib.pl
  2. # Functions for dealing with mail folders in various formats
  3. $pop3_port = 110;
  4. $imap_port = 143;
  5. $cache_directory = $user_module_config_directory || $module_config_directory;
  6. @index_fields = ( "subject", "from", "to", "date", "size",
  7. "x-spam-status", "message-id" );
  8. $create_cid_count = 0;
  9. # mailbox_list_mails(start, end, &folder, [headersonly], [&error])
  10. # Returns an array whose size is that of the entire folder, with messages
  11. # in the specified range filled in.
  12. sub mailbox_list_mails
  13. {
  14. if ($_[2]->{'type'} == 0) {
  15. # List a single mbox formatted file
  16. return &list_mails($_[2]->{'file'}, $_[0], $_[1]);
  17. }
  18. elsif ($_[2]->{'type'} == 1) {
  19. # List a qmail maildir
  20. local $md = $_[2]->{'file'};
  21. return &list_maildir($md, $_[0], $_[1], $_[3]);
  22. }
  23. elsif ($_[2]->{'type'} == 2) {
  24. # Get mail headers/body from a remote POP3 server
  25. # Login first
  26. local @rv = &pop3_login($_[2]);
  27. if ($rv[0] != 1) {
  28. # Failed to connect or login
  29. if ($_[4]) {
  30. @{$_[4]} = @rv;
  31. return ();
  32. }
  33. elsif ($rv[0] == 0) { &error($rv[1]); }
  34. else { &error(&text('save_elogin', $rv[1])); }
  35. }
  36. local $h = $rv[1];
  37. local @uidl = &pop3_uidl($h);
  38. local %onserver = map { &safe_uidl($_), 1 } @uidl;
  39. # Work out what range we want
  40. local ($start, $end) = &compute_start_end($_[0], $_[1], scalar(@uidl));
  41. local @rv = map { undef } @uidl;
  42. # For each message in the range, get the headers or body
  43. local ($i, $f, %cached, %sizeneed);
  44. local $cd = "$cache_directory/$_[2]->{'id'}.cache";
  45. if (opendir(CACHE, $cd)) {
  46. while($f = readdir(CACHE)) {
  47. if ($f =~ /^(\S+)\.body$/) {
  48. $cached{$1} = 2;
  49. }
  50. elsif ($f =~ /^(\S+)\.headers$/) {
  51. $cached{$1} = 1;
  52. }
  53. }
  54. closedir(CACHE);
  55. }
  56. else {
  57. mkdir($cd, 0700);
  58. }
  59. for($i=$start; $i<=$end; $i++) {
  60. local $u = &safe_uidl($uidl[$i]);
  61. if ($cached{$u} == 2 || $cached{$u} == 1 && $_[3]) {
  62. # We already have everything that we need
  63. }
  64. elsif ($cached{$u} == 1 || !$_[3]) {
  65. # We need to get the entire mail
  66. &pop3_command($h, "retr ".($i+1));
  67. open(CACHE, ">$cd/$u.body");
  68. while(<$h>) {
  69. s/\r//g;
  70. last if ($_ eq ".\n");
  71. print CACHE $_;
  72. }
  73. close(CACHE);
  74. unlink("$cd/$u.headers");
  75. $cached{$u} = 2;
  76. }
  77. else {
  78. # We just need the headers
  79. &pop3_command($h, "top ".($i+1)." 0");
  80. open(CACHE, ">$cd/$u.headers");
  81. while(<$h>) {
  82. s/\r//g;
  83. last if ($_ eq ".\n");
  84. print CACHE $_;
  85. }
  86. close(CACHE);
  87. $cached{$u} = 1;
  88. }
  89. local $mail = &read_mail_file($cached{$u} == 2 ?
  90. "$cd/$u.body" : "$cd/$u.headers");
  91. if ($cached{$u} == 1) {
  92. if ($mail->{'body'} ne "") {
  93. $mail->{'size'} = int($mail->{'body'});
  94. }
  95. else {
  96. $sizeneed{$i} = 1;
  97. }
  98. }
  99. $mail->{'idx'} = $i;
  100. $mail->{'id'} = $uidl[$i];
  101. $rv[$i] = $mail;
  102. }
  103. # Get sizes for mails if needed
  104. if (%sizeneed) {
  105. &pop3_command($h, "list");
  106. while(<$h>) {
  107. s/\r//g;
  108. last if ($_ eq ".\n");
  109. if (/^(\d+)\s+(\d+)/ && $sizeneed{$1-1}) {
  110. # Add size to the mail cache
  111. $rv[$1-1]->{'size'} = $2;
  112. local $u = &safe_uidl($uidl[$1-1]);
  113. open(CACHE, ">>$cd/$u.headers");
  114. print CACHE $2,"\n";
  115. close(CACHE);
  116. }
  117. }
  118. }
  119. # Clean up any cached mails that no longer exist on the server
  120. foreach $f (keys %cached) {
  121. if (!$onserver{$f}) {
  122. unlink($cached{$f} == 1 ? "$cd/$f.headers"
  123. : "$cd/$f.body");
  124. }
  125. }
  126. return @rv;
  127. }
  128. elsif ($_[2]->{'type'} == 3) {
  129. # List an MH directory
  130. local $md = $_[2]->{'file'};
  131. return &list_mhdir($md, $_[0], $_[1], $_[3]);
  132. }
  133. elsif ($_[2]->{'type'} == 4) {
  134. # Get headers and possibly bodies from an IMAP server
  135. # Login and select the specified mailbox
  136. local @rv = &imap_login($_[2]);
  137. if ($rv[0] != 1) {
  138. # Something went wrong
  139. if ($_[4]) {
  140. @{$_[4]} = @rv;
  141. return ();
  142. }
  143. elsif ($rv[0] == 0) { &error($rv[1]); }
  144. elsif ($rv[0] == 3) { &error(&text('save_emailbox', $rv[1])); }
  145. elsif ($rv[0] == 2) { &error(&text('save_elogin2', $rv[1])); }
  146. }
  147. local $h = $rv[1];
  148. local $count = $rv[2];
  149. return () if (!$count);
  150. $_[2]->{'lastchange'} = $rv[3] if ($rv[3]);
  151. # Work out what range we want
  152. local ($start, $end) = &compute_start_end($_[0], $_[1], $count);
  153. local @mail = map { undef } (0 .. $count-1);
  154. # Get the headers or body of messages in the specified range
  155. local @rv;
  156. if ($_[3]) {
  157. # Just the headers
  158. @rv = &imap_command($h,
  159. sprintf "FETCH %d:%d (RFC822.SIZE UID FLAGS RFC822.HEADER)",
  160. $start+1, $end+1);
  161. }
  162. else {
  163. # Whole messages
  164. @rv = &imap_command($h,
  165. sprintf "FETCH %d:%d (UID FLAGS BODY.PEEK[])", $start+1, $end+1);
  166. }
  167. # Parse the headers or whole messages that came back
  168. local $i;
  169. for($i=0; $i<@{$rv[1]}; $i++) {
  170. # Extract the actual mail part
  171. local $mail = &parse_imap_mail($rv[1]->[$i]);
  172. if ($mail) {
  173. $mail->{'idx'} = $start+$i;
  174. $mail[$start+$i] = $mail;
  175. }
  176. }
  177. return @mail;
  178. }
  179. elsif ($_[2]->{'type'} == 5) {
  180. # A composite folder, which combined two or more others.
  181. local @mail;
  182. # Work out exactly how big the total is
  183. local ($sf, %len, $count);
  184. foreach $sf (@{$_[2]->{'subfolders'}}) {
  185. print DEBUG "working out size of ",&folder_name($sf),"\n";
  186. $len{$sf} = &mailbox_folder_size($sf);
  187. $count += $len{$sf};
  188. }
  189. # Work out what range we need
  190. local ($start, $end) = &compute_start_end($_[0], $_[1], $count);
  191. # Fetch the needed part of each sub-folder
  192. local $pos = 0;
  193. foreach $sf (@{$_[2]->{'subfolders'}}) {
  194. local ($sfstart, $sfend);
  195. local $sfn = &folder_name($sf);
  196. $sfstart = $start - $pos;
  197. $sfend = $end - $pos;
  198. $sfstart = $sfstart < 0 ? 0 :
  199. $sfstart >= $len{$sf} ? $len{$sf}-1 : $sfstart;
  200. $sfend = $sfend < 0 ? 0 :
  201. $sfend >= $len{$sf} ? $len{$sf}-1 : $sfend;
  202. print DEBUG "getting mail from $sfstart to $sfend in $sfn\n";
  203. local @submail =
  204. &mailbox_list_mails($sfstart, $sfend, $sf, $_[3]);
  205. local $sm;
  206. foreach $sm (@submail) {
  207. if ($sm) {
  208. # ID is the original folder and ID
  209. $sm->{'id'} = $sfn."\t".$sm->{'id'};
  210. }
  211. }
  212. push(@mail, @submail);
  213. $pos += $len{$sf};
  214. }
  215. return @mail;
  216. }
  217. elsif ($_[2]->{'type'} == 6) {
  218. # A virtual folder, which just contains ids of mails in other folders
  219. local $mems = $folder->{'members'};
  220. local ($start, $end) = &compute_start_end($_[0], $_[1], scalar(@$mems));
  221. # Build a map from sub-folder names to IDs in them
  222. local (%wantmap, %namemap);
  223. for(my $i=$start; $i<=$end; $i++) {
  224. local $sf = $mems->[$i]->[0];
  225. local $sid = $mems->[$i]->[1];
  226. local $sfn = &folder_name($sf);
  227. $namemap{$sfn} = $sf;
  228. push(@{$wantmap{$sfn}}, [ $sid, $i ]);
  229. }
  230. # For each sub-folder, get the IDs we need, and put them into the
  231. # return array at the right place
  232. local @mail = map { undef } (0 .. @$mems-1);
  233. local $changed = 0;
  234. foreach my $sfn (keys %wantmap) {
  235. local $sf = $namemap{$sfn};
  236. local @wantids = map { $_->[0] } @{$wantmap{$sfn}};
  237. local @wantidxs = map { $_->[1] } @{$wantmap{$sfn}};
  238. local @sfmail = &mailbox_select_mails($sf, \@wantids, $_[3]);
  239. for(my $i=0; $i<@sfmail; $i++) {
  240. $mail[$wantidxs[$i]] = $sfmail[$i];
  241. if ($sfmail[$i]) {
  242. # Original mail exists .. add to results
  243. if ($sfmail[$i]->{'id'} ne $wantids[$i]) {
  244. # Under new ID now - fix up index
  245. print DEBUG "wanted ID ",$wantids[$i],
  246. " got ",$sfmail[$i]->{'id'},"\n";
  247. local ($m) = grep {
  248. $_->[1] eq $wantids[$i] } @$mems;
  249. if ($m) {
  250. $m->[1] = $sfmail[$i]->{'id'};
  251. $changed = 1;
  252. }
  253. }
  254. $sfmail[$i]->{'idx'} = $wantidxs[$i];
  255. $sfmail[$i]->{'id'} =
  256. $sfn."\t".$sfmail[$i]->{'id'};
  257. }
  258. else {
  259. # Take out of virtual folder index
  260. print DEBUG "underlying email $sfn $wantids[$i] is gone!\n";
  261. $mems = [ grep { $_->[0] ne $sf ||
  262. $_->[1] ne $wantids[$i] } @$mems ];
  263. $changed = 1;
  264. $mail[$wantidxs[$i]] = 'GONE';
  265. }
  266. }
  267. }
  268. if ($changed) {
  269. # Need to save virtual folder
  270. $folder->{'members'} = $mems;
  271. &save_folder($folder, $folder);
  272. }
  273. # Filter out messages that don't exist anymore
  274. @mail = grep { $_ ne 'GONE' } @mail;
  275. return @mail;
  276. }
  277. }
  278. # mailbox_select_mails(&folder, &ids, headersonly)
  279. # Returns only messages from a folder with unique IDs in the given array
  280. sub mailbox_select_mails
  281. {
  282. local ($folder, $ids, $headersonly) = @_;
  283. if ($folder->{'type'} == 0) {
  284. # mbox folder
  285. return &select_mails($folder->{'file'}, $ids, $headersonly);
  286. }
  287. elsif ($folder->{'type'} == 1) {
  288. # Maildir folder
  289. return &select_maildir($folder->{'file'}, $ids, $headersonly);
  290. }
  291. elsif ($folder->{'type'} == 3) {
  292. # MH folder
  293. return &select_mhdir($folder->{'file'}, $ids, $headersonly);
  294. }
  295. elsif ($folder->{'type'} == 2) {
  296. # POP folder
  297. # Login first
  298. local @rv = &pop3_login($folder);
  299. if ($rv[0] != 1) {
  300. # Failed to connect or login
  301. if ($_[4]) {
  302. @{$_[4]} = @rv;
  303. return ();
  304. }
  305. elsif ($rv[0] == 0) { &error($rv[1]); }
  306. else { &error(&text('save_elogin', $rv[1])); }
  307. }
  308. local $h = $rv[1];
  309. local @uidl = &pop3_uidl($h);
  310. local %uidlmap; # Map from UIDLs to POP3 indexes
  311. for(my $i=0; $i<@uidl; $i++) {
  312. $uidlmap{$uidl[$i]} = $i+1;
  313. }
  314. # Work out what we have cached
  315. local ($i, $f, %cached, %sizeneed);
  316. local @rv;
  317. local $cd = "$cache_directory/$_[2]->{'id'}.cache";
  318. if (opendir(CACHE, $cd)) {
  319. while($f = readdir(CACHE)) {
  320. if ($f =~ /^(\S+)\.body$/) {
  321. $cached{$1} = 2;
  322. }
  323. elsif ($f =~ /^(\S+)\.headers$/) {
  324. $cached{$1} = 1;
  325. }
  326. }
  327. closedir(CACHE);
  328. }
  329. else {
  330. mkdir($cd, 0700);
  331. }
  332. # For each requested uidl, get the headers or body
  333. foreach my $i (@$ids) {
  334. local $u = &safe_uidl($i);
  335. print DEBUG "need uidl $i -> $uidlmap{$i}\n";
  336. if ($cached{$u} == 2 || $cached{$u} == 1 && $headersonly) {
  337. # We already have everything that we need
  338. }
  339. elsif ($cached{$u} == 1 || !$headersonly) {
  340. # We need to get the entire mail
  341. &pop3_command($h, "retr ".$uidlmap{$i});
  342. open(CACHE, ">$cd/$u.body");
  343. while(<$h>) {
  344. s/\r//g;
  345. last if ($_ eq ".\n");
  346. print CACHE $_;
  347. }
  348. close(CACHE);
  349. unlink("$cd/$u.headers");
  350. $cached{$u} = 2;
  351. }
  352. else {
  353. # We just need the headers
  354. &pop3_command($h, "top ".$uidlmap{$i}." 0");
  355. open(CACHE, ">$cd/$u.headers");
  356. while(<$h>) {
  357. s/\r//g;
  358. last if ($_ eq ".\n");
  359. print CACHE $_;
  360. }
  361. close(CACHE);
  362. $cached{$u} = 1;
  363. }
  364. local $mail = &read_mail_file($cached{$u} == 2 ?
  365. "$cd/$u.body" : "$cd/$u.headers");
  366. if ($cached{$u} == 1) {
  367. if ($mail->{'body'} ne "") {
  368. $mail->{'size'} = length($mail->{'body'});
  369. }
  370. else {
  371. $sizeneed{$uidlmap{$i}} = $mail;
  372. }
  373. }
  374. $mail->{'idx'} = $uidlmap{$i}-1;
  375. $mail->{'id'} = $i;
  376. push(@rv, $mail);
  377. }
  378. # Get sizes for mails if needed
  379. if (%sizeneed) {
  380. &pop3_command($h, "list");
  381. while(<$h>) {
  382. s/\r//g;
  383. last if ($_ eq ".\n");
  384. if (/^(\d+)\s+(\d+)/ && $sizeneed{$1}) {
  385. # Find mail in results, and set its size
  386. local ($ns) = $sizeneed{$1};
  387. $ns->{'size'} = $2;
  388. local $u = &safe_uidl($uidl[$1-1]);
  389. open(CACHE, ">>$cd/$u.headers");
  390. print CACHE $2,"\n";
  391. close(CACHE);
  392. }
  393. }
  394. }
  395. return @rv;
  396. }
  397. elsif ($folder->{'type'} == 4) {
  398. # IMAP folder
  399. # Login and select the specified mailbox
  400. local @irv = &imap_login($folder);
  401. if ($irv[0] != 1) {
  402. # Something went wrong
  403. if ($_[4]) {
  404. @{$_[4]} = @irv;
  405. return ();
  406. }
  407. elsif ($irv[0] == 0) { &error($irv[1]); }
  408. elsif ($irv[0] == 3) { &error(&text('save_emailbox', $irv[1]));}
  409. elsif ($irv[0] == 2) { &error(&text('save_elogin2', $irv[1])); }
  410. }
  411. local $h = $irv[1];
  412. local $count = $irv[2];
  413. return () if (!$count);
  414. $folder->{'lastchange'} = $irv[3] if ($irv[3]);
  415. # Build map from IDs to original order, as UID FETCH doesn't return
  416. # mail in the order we asked for!
  417. local %wantpos;
  418. for(my $i=0; $i<@$ids; $i++) {
  419. $wantpos{$ids->[$i]} = $i;
  420. }
  421. # Fetch each mail by ID. This is done in blocks of 1000, to avoid
  422. # hitting a the IMAP server's max request limit
  423. local @rv = map { undef } @$ids;
  424. local $wanted = $headersonly ? "(RFC822.SIZE UID FLAGS RFC822.HEADER)"
  425. : "(UID FLAGS BODY.PEEK[])";
  426. if (@$ids) {
  427. for(my $chunk=0; $chunk<@$ids; $chunk+=1000) {
  428. local $chunkend = $chunk+999;
  429. if ($chunkend >= @$ids) { $chunkend = @$ids-1; }
  430. local @cids = @$ids[$chunk .. $chunkend];
  431. local @idxrv = &imap_command($h,
  432. "UID FETCH ".join(",", @cids)." $wanted");
  433. foreach my $idxrv (@{idxrv->[1]}) {
  434. local $mail = &parse_imap_mail($idxrv);
  435. if ($mail) {
  436. $mail->{'idx'} = $mail->{'imapidx'}-1;
  437. $rv[$wantpos{$mail->{'id'}}] = $mail;
  438. }
  439. }
  440. }
  441. }
  442. print DEBUG "imap rv = ",scalar(@rv),"\n";
  443. return @rv;
  444. }
  445. elsif ($folder->{'type'} == 5 || $folder->{'type'} == 6) {
  446. # Virtual or composite folder .. for each ID, work out the folder and
  447. # build a map from folders to ID lists
  448. print DEBUG "selecting ",scalar(@$ids)," ids\n";
  449. # Build a map from sub-folder names to IDs in them
  450. my $i = 0;
  451. my %wantmap;
  452. foreach my $id (@$ids) {
  453. local ($sfn, $sid) = split(/\t+/, $id, 2);
  454. push(@{$wantmap{$sfn}}, [ $sid, $i ]);
  455. $i++;
  456. }
  457. # Build map from sub-folder names to IDs
  458. my (%namemap, @allids, $mems);
  459. if ($folder->{'type'} == 6) {
  460. # For a virtual folder, we need to find all sub-folders
  461. $mems = $folder->{'members'};
  462. foreach my $m (@$mems) {
  463. local $sfn = &folder_name($m->[0]);
  464. $namemap{$sfn} = $m->[0];
  465. push(@allids, $sfn."\t".$m->[1]);
  466. }
  467. }
  468. else {
  469. # For a composite, they are simply listed
  470. foreach my $sf (@{$folder->{'subfolders'}}) {
  471. local $sfn = &folder_name($sf);
  472. $namemap{$sfn} = $sf;
  473. }
  474. @allids = &mailbox_idlist($folder);
  475. }
  476. # For each sub-folder, get the IDs we need, and put them into the
  477. # return array at the right place
  478. local @mail = map { undef } @$ids;
  479. foreach my $sfn (keys %wantmap) {
  480. local $sf = $namemap{$sfn};
  481. local @wantids = map { $_->[0] } @{$wantmap{$sfn}};
  482. local @wantidxs = map { $_->[1] } @{$wantmap{$sfn}};
  483. local @sfmail = &mailbox_select_mails($sf, \@wantids,
  484. $headersonly);
  485. for(my $i=0; $i<@sfmail; $i++) {
  486. $mail[$wantidxs[$i]] = $sfmail[$i];
  487. if ($sfmail[$i]) {
  488. # Original mail exists .. add to results
  489. $sfmail[$i]->{'id'} =
  490. $sfn."\t".$sfmail[$i]->{'id'};
  491. $sfmail[$i]->{'idx'} = &indexof(
  492. $sfmail[$i]->{'id'}, @allids);
  493. print DEBUG "looking for ",$sfmail[$i]->{'id'}," found at ",$sfmail[$i]->{'idx'},"\n";
  494. }
  495. else {
  496. # Take out of virtual folder index
  497. print DEBUG "underlying email $sfn $wantids[$i] is gone!\n";
  498. $mems = [ grep { $_->[0] ne $sf ||
  499. $_->[1] ne $wantids[$i] } @$mems ];
  500. $changed = 1;
  501. }
  502. }
  503. }
  504. if ($changed && $folder->{'type'} == 6) {
  505. # Need to save virtual folder
  506. $folder->{'members'} = $mems;
  507. &save_folder($folder, $folder);
  508. }
  509. return @mail;
  510. }
  511. }
  512. # mailbox_get_mail(&folder, id, headersonly)
  513. # Convenience function to get a single mail by ID
  514. sub mailbox_get_mail
  515. {
  516. local ($folder, $id, $headersonly) = @_;
  517. local ($mail) = &mailbox_select_mails($folder, [ $id ], $headersonly);
  518. if ($mail) {
  519. # Find the sort index for this message
  520. local ($field, $dir) = &get_sort_field($folder);
  521. if (!$field || !$folder->{'sortable'}) {
  522. # No sorting, so sort index is the opposite of real
  523. $mail->{'sortidx'} = &mailbox_folder_size($folder, 1) -
  524. $mail->{'idx'} - 1;
  525. print DEBUG "idx=$mail->{'idx'} sortidx=$mail->{'sortidx'} size=",&mailbox_folder_size($folder, 1),"\n";
  526. }
  527. else {
  528. # Need to extract from sort index
  529. local @sorter = &build_sorted_ids($folder, $field, $dir);
  530. $mail->{'sortidx'} = &indexof($id, @sorter);
  531. }
  532. }
  533. return $mail;
  534. }
  535. # mailbox_idlist(&folder)
  536. # Returns a list of IDs of messages in some folder
  537. sub mailbox_idlist
  538. {
  539. local ($folder) = @_;
  540. if ($folder->{'type'} == 0) {
  541. # mbox, for which IDs are mail positions
  542. print DEBUG "starting to get IDs from $folder->{'file'}\n";
  543. local @idlist = &idlist_mails($folder->{'file'});
  544. print DEBUG "got ",scalar(@idlist)," ids\n";
  545. return @idlist;
  546. }
  547. elsif ($folder->{'type'} == 1) {
  548. # maildir, for which IDs are filenames
  549. return &idlist_maildir($folder->{'file'});
  550. }
  551. elsif ($folder->{'type'} == 2) {
  552. # pop3, for which IDs are uidls
  553. local @rv = &pop3_login($folder);
  554. if ($rv[0] != 1) {
  555. # Failed to connect or login
  556. if ($rv[0] == 0) { &error($rv[1]); }
  557. else { &error(&text('save_elogin', $rv[1])); }
  558. }
  559. local $h = $rv[1];
  560. local @uidl = &pop3_uidl($h);
  561. return @uidl;
  562. }
  563. elsif ($folder->{'type'} == 3) {
  564. # MH directory, for which IDs are file numbers
  565. return &idlist_mhdir($folder->{'file'});
  566. }
  567. elsif ($folder->{'type'} == 4) {
  568. # IMAP, for which IDs are IMAP UIDs
  569. local @rv = &imap_login($folder);
  570. if ($rv[0] != 1) {
  571. # Something went wrong
  572. if ($rv[0] == 0) { &error($rv[1]); }
  573. elsif ($rv[0] == 3) { &error(&text('save_emailbox', $rv[1])); }
  574. elsif ($rv[0] == 2) { &error(&text('save_elogin2', $rv[1])); }
  575. }
  576. local $h = $rv[1];
  577. local $count = $rv[2];
  578. return () if (!$count);
  579. $folder->{'lastchange'} = $irv[3] if ($irv[3]);
  580. @rv = &imap_command($h, "FETCH 1:$count UID");
  581. local @uids;
  582. foreach my $uid (@{$rv[1]}) {
  583. if ($uid =~ /UID\s+(\d+)/) {
  584. push(@uids, $1);
  585. }
  586. }
  587. return @uids;
  588. }
  589. elsif ($folder->{'type'} == 5) {
  590. # Composite, IDs come from sub-folders
  591. local @rv;
  592. foreach my $sf (@{$folder->{'subfolders'}}) {
  593. local $sfn = &folder_name($sf);
  594. push(@rv, map { $sfn."\t".$_ } &mailbox_idlist($sf));
  595. }
  596. return @rv;
  597. }
  598. elsif ($folder->{'type'} == 6) {
  599. # Virtual, IDs come from sub-folders (where they exist)
  600. my (%wantmap, %namemap);
  601. foreach my $m (@{$folder->{'members'}}) {
  602. local $sf = $m->[0];
  603. local $sid = $m->[1];
  604. local $sfn = &folder_name($sf);
  605. push(@{$wantmap{$sfn}}, $sid);
  606. $namemap{$sfn} = $sf;
  607. }
  608. local @rv;
  609. foreach my $sfn (keys %wantmap) {
  610. local %wantids = map { $_, 1 } @{$wantmap{$sfn}};
  611. local $sf = $namemap{$sfn};
  612. foreach my $sfid (&mailbox_idlist($sf)) {
  613. if ($wantids{$sfid}) {
  614. push(@rv, $sfn."\t".$sfid);
  615. }
  616. }
  617. }
  618. return @rv;
  619. }
  620. }
  621. # compute_start_end(start, end, count)
  622. # Given start and end indexes (which may be negative or undef), returns the
  623. # real mail file indexes.
  624. sub compute_start_end
  625. {
  626. local ($start, $end, $count) = @_;
  627. if (!defined($start)) {
  628. return (0, $count-1);
  629. }
  630. elsif ($end < 0) {
  631. local $rstart = $count+$_[1]-1;
  632. local $rend = $count+$_[0]-1;
  633. $rstart = $rstart < 0 ? 0 : $rstart;
  634. $rend = $count - 1 if ($rend >= $count);
  635. return ($rstart, $rend);
  636. }
  637. else {
  638. local $rend = $_[1];
  639. $rend = $count - 1 if ($rend >= $count);
  640. return ($start, $rend);
  641. }
  642. }
  643. # mailbox_list_mails_sorted(start, end, &folder, [headeronly], [&error],
  644. # [sort-field, sort-dir])
  645. # Returns messages in a folder within the given range, but sorted by the
  646. # given field and condition.
  647. sub mailbox_list_mails_sorted
  648. {
  649. local ($start, $end, $folder, $headersonly, $error, $field, $dir) = @_;
  650. if (!$field) {
  651. # Default to current ordering
  652. ($field, $dir) = &get_sort_field($folder);
  653. }
  654. if (!$field || !$folder->{'sortable'}) {
  655. # No sorting .. just return newest first
  656. local @rv = reverse(&mailbox_list_mails(
  657. -$start, -$end-1, $folder, $headersonly, $error));
  658. local $i = 0;
  659. foreach my $m (@rv) {
  660. $m->{'sortidx'} = $i++;
  661. }
  662. return @rv;
  663. }
  664. # For IMAP, login first so that the lastchange can be found
  665. if ($folder->{'type'} == 4 && !$folder->{'lastchange'}) {
  666. &mailbox_select_mails($folder, [ ], 1);
  667. }
  668. # Get a sorted list of IDs, and then find the real emails within the range
  669. local @sorter = &build_sorted_ids($folder, $field, $dir);
  670. ($start, $end) = &compute_start_end($start, $end, scalar(@sorter));
  671. print DEBUG "for ",&folder_name($folder)," sorter = ",scalar(@sorter),"\n";
  672. print DEBUG "start = $start end = $end\n";
  673. local @rv = map { undef } (0 .. scalar(@sorter)-1);
  674. local @wantids = map { $sorter[$_] } ($start .. $end);
  675. print DEBUG "wantids = ",scalar(@wantids),"\n";
  676. local @mails = &mailbox_select_mails($folder, \@wantids, $headersonly);
  677. for(my $i=0; $i<@mails; $i++) {
  678. $rv[$start+$i] = $mails[$i];
  679. print DEBUG "setting $start+$i to ",$mails[$i]," id ",$wantids[$i],"\n";
  680. $mails[$i]->{'sortidx'} = $start+$i;
  681. }
  682. print DEBUG "rv = ",scalar(@rv),"\n";
  683. return @rv;
  684. }
  685. # build_sorted_ids(&folder, field, dir)
  686. # Returns a list of message IDs in some folder, sorted on some field
  687. sub build_sorted_ids
  688. {
  689. local ($folder, $field, $dir) = @_;
  690. # Delete old sort indexes
  691. &delete_old_sort_index($folder);
  692. # Build or update the sort index. This is a file mapping unique IDs and fields
  693. # to sortable values.
  694. local %index;
  695. &build_new_sort_index($folder, $field, \%index);
  696. # Get message indexes, sorted by the field
  697. my @sorter;
  698. while(my ($k, $v) = each %index) {
  699. if ($k =~ /^(.*)_\Q$field\E$/) {
  700. push(@sorter, [ $1, lc($v) ]);
  701. }
  702. }
  703. if ($field eq "size" || $field eq "date" || $field eq "x-spam-status") {
  704. # Numeric sort
  705. @sorter = sort { my $s = $a->[1] <=> $b->[1]; $dir ? $s : -$s } @sorter;
  706. }
  707. else {
  708. # Alpha sort
  709. @sorter = sort { my $s = $a->[1] cmp $b->[1]; $dir ? $s : -$s } @sorter;
  710. }
  711. return map { $_->[0] } @sorter;
  712. }
  713. # delete_old_sort_index(&folder)
  714. # Delete old index DBM files
  715. sub delete_old_sort_index
  716. {
  717. local ($folder) = @_;
  718. local $ifile = &folder_sort_index_file($folder);
  719. $ifile =~ /^(.*)\/([^\/]+)$/;
  720. local ($idir, $iname) = ($1, $2);
  721. opendir(IDIR, $idir);
  722. foreach my $f (readdir(IDIR)) {
  723. if ($f eq $iname || $f =~ /^\Q$iname\E\.[^\.]+$/) {
  724. unlink("$idir/$f");
  725. }
  726. }
  727. closedir(IDIR);
  728. }
  729. # build_new_sort_index(&folder, field, &index)
  730. # Builds and/or loads the index for sorting a folder on some field. The
  731. # index uses the mail number as the key, and the field value as the value.
  732. sub build_new_sort_index
  733. {
  734. local ($folder, $field, $index) = @_;
  735. return 0 if (!$folder->{'sortable'});
  736. local $ifile = &folder_new_sort_index_file($folder);
  737. &open_dbm_db($index, $ifile, 0600);
  738. print DEBUG "indexchange=$index->{'lastchange'} folderchange=$folder->{'lastchange'}\n";
  739. if ($index->{'lastchange'} != $folder->{'lastchange'} ||
  740. !$folder->{'lastchange'}) {
  741. # The mail file has changed .. get IDs and update the index with any
  742. # that are missing
  743. local @ids = &mailbox_idlist($folder);
  744. # Find IDs that are new
  745. local @newids;
  746. foreach my $id (@ids) {
  747. if (!defined($index->{$id."_size"})) {
  748. push(@newids, $id);
  749. }
  750. }
  751. local @mails = scalar(@newids) ?
  752. &mailbox_select_mails($folder, \@newids, 1) : ( );
  753. foreach my $mail (@mails) {
  754. foreach my $f (@index_fields) {
  755. if ($f eq "date") {
  756. # Convert date to Unix time
  757. $index->{$mail->{'id'}."_date"} =
  758. &parse_mail_date($mail->{'header'}->{'date'});
  759. }
  760. elsif ($f eq "size") {
  761. # Get mail size
  762. $index->{$mail->{'id'}."_size"} =
  763. $mail->{'size'};
  764. }
  765. elsif ($f eq "from" || $f eq "to") {
  766. # From: header .. convert to display version
  767. $index->{$mail->{'id'}."_".$f} =
  768. &simplify_from($mail->{'header'}->{$f});
  769. }
  770. elsif ($f eq "subject") {
  771. # Convert subject to display version
  772. $index->{$mail->{'id'}."_".$f} =
  773. &simplify_subject($mail->{'header'}->{$f});
  774. }
  775. elsif ($f eq "x-spam-status") {
  776. # Extract spam score
  777. $index->{$mail->{'id'}."_".$f} =
  778. $mail->{'header'}->{$f} =~ /(hits|score)=([0-9\.]+)/ ? $2 : undef;
  779. }
  780. else {
  781. # Just a header
  782. $index->{$mail->{'id'}."_".$f} =
  783. $mail->{'header'}->{$f};
  784. }
  785. }
  786. }
  787. print DEBUG "added ",scalar(@mails)," messages to index\n";
  788. # Remove IDs that no longer exist
  789. local %ids = map { $_, 1 } (@ids, @wantids);
  790. local $dc = 0;
  791. local @todelete;
  792. while(my ($k, $v) = each %$index) {
  793. if ($k =~ /^(.*)_([^_]+)$/ && !$ids{$1}) {
  794. push(@todelete, $k);
  795. $dc++ if ($2 eq "size");
  796. }
  797. }
  798. foreach my $k (@todelete) {
  799. delete($index->{$k});
  800. }
  801. print DEBUG "deleted $dc messages from index\n";
  802. # Record index update time
  803. $index->{'lastchange'} = $folder->{'lastchange'} || time();
  804. $index->{'mailcount'} = scalar(@ids);
  805. print DEBUG "new indexchange=$index->{'lastchange'}\n";
  806. }
  807. return 1;
  808. }
  809. # delete_new_sort_index_message(&folder, id)
  810. # Removes a message ID from a sort index
  811. sub delete_new_sort_index_message
  812. {
  813. local ($folder, $id) = @_;
  814. local %index;
  815. &build_new_sort_index($folder, undef, \%index);
  816. foreach my $field (@index_fields) {
  817. delete($index{$id."_".$field});
  818. }
  819. dbmclose(%index);
  820. if ($folder->{'type'} == 5 || $folder->{'type'} == 6) {
  821. # Remove from underlying folder's index too
  822. local ($sfn, $sid) = split(/\t+/, $id, 2);
  823. local $sf = &find_subfolder($folder, $sfn);
  824. if ($sf) {
  825. &delete_new_sort_index_message($sf, $sid);
  826. }
  827. }
  828. }
  829. # force_new_index_recheck(&folder)
  830. # Resets the last-updated time on a folder's index, to force a re-check
  831. sub force_new_index_recheck
  832. {
  833. local ($folder) = @_;
  834. local %index;
  835. &build_new_sort_index($folder, undef, \%index);
  836. $index{'lastchange'} = 0;
  837. dbmclose(%index);
  838. }
  839. # delete_new_sort_index(&folder)
  840. # Trashes the sort index for a folder, to force a rebuild
  841. sub delete_new_sort_index
  842. {
  843. local ($folder) = @_;
  844. local $ifile = &folder_new_sort_index_file($folder);
  845. my %index;
  846. &open_dbm_db(\%index, $ifile, 0600);
  847. %index = ( );
  848. }
  849. # folder_sort_index_file(&folder)
  850. # Returns the index file to use for some folder
  851. sub folder_sort_index_file
  852. {
  853. local ($folder) = @_;
  854. return &user_index_file(($folder->{'file'} || $folder->{'id'}).".sort");
  855. }
  856. # folder_new_sort_index_file(&folder)
  857. # Returns the new ID-style index file to use for some folder
  858. sub folder_new_sort_index_file
  859. {
  860. local ($folder) = @_;
  861. return &user_index_file(($folder->{'file'} || $folder->{'id'}).".byid");
  862. }
  863. # mailbox_search_mail(&fields, andmode, &folder, [&limit], [headersonly])
  864. # Search a mailbox for multiple matching fields
  865. sub mailbox_search_mail
  866. {
  867. local ($fields, $andmode, $folder, $limit, $headersonly) = @_;
  868. # For folders other than IMAP and composite and mbox where we already have
  869. # an index, build a sort index and use that for
  870. # the search, if it is simple enough (Subject, From and To only)
  871. local @idxfields = grep { $_->[0] eq 'from' || $_->[0] eq 'to' ||
  872. $_->[0] eq 'subject' } @{$_[0]};
  873. if ($folder->{'type'} != 4 &&
  874. $folder->{'type'} != 5 &&
  875. $folder->{'type'} != 6 &&
  876. ($folder->{'type'} != 0 || !&has_dbm_index($folder->{'file'})) &&
  877. scalar(@idxfields) == scalar(@$fields) && @idxfields &&
  878. &get_product_name() eq 'usermin') {
  879. print DEBUG "using index to search\n";
  880. local %index;
  881. &build_new_sort_index($folder, undef, \%index);
  882. local @rv;
  883. # Work out which mail IDs match the requested headers
  884. local %idxmatches = map { ("$_->[0]/$_->[1]", [ ]) } @idxfields;
  885. while(my ($k, $v) = each %index) {
  886. $k =~ /^(.+)_(\S+)$/ || next;
  887. local ($ki, $kf) = ($1, $2);
  888. next if (!$kf || $ki eq '');
  889. # Check all of the fields to see which ones match
  890. foreach my $if (@idxfields) {
  891. local $iff = $if->[0];
  892. local ($neg) = ($iff =~ s/^\!//);
  893. next if ($kf ne $iff);
  894. local $re = $if->[2] ? $if->[1] : "\Q$if->[1]\E";
  895. if (!$neg && $v =~ /$re/i ||
  896. $neg && $v !~ /$re/i) {
  897. push(@{$idxmatches{"$if->[0]/$if->[1]"}}, $ki);
  898. }
  899. }
  900. }
  901. local @matches;
  902. if ($_[1]) {
  903. # Find indexes in all arrays
  904. local %icount;
  905. foreach my $if (keys %idxmatches) {
  906. foreach my $i (@{$idxmatches{$if}}) {
  907. $icount{$i}++;
  908. }
  909. }
  910. foreach my $i (keys %icount) {
  911. }
  912. local $fif = $idxfields[0];
  913. @matches = grep { $icount{$_} == scalar(@idxfields) }
  914. @{$idxmatches{"$fif->[0]/$fif->[1]"}};
  915. }
  916. else {
  917. # Find indexes in any array
  918. foreach my $if (keys %idxmatches) {
  919. push(@matches, @{$idxmatches{$if}});
  920. }
  921. @matches = &unique(@matches);
  922. }
  923. @matches = sort { $a cmp $b } @matches;
  924. print DEBUG "matches = ",join(" ", @matches),"\n";
  925. # Select the actual mails
  926. return &mailbox_select_mails($_[2], \@matches, $headersonly);
  927. }
  928. if ($folder->{'type'} == 0) {
  929. # Just search an mbox format file (which will use its own special
  930. # field-level index)
  931. return &advanced_search_mail($folder->{'file'}, $fields,
  932. $andmode, $limit, $headersonly);
  933. }
  934. elsif ($folder->{'type'} == 1) {
  935. # Search a maildir directory
  936. return &advanced_search_maildir($folder->{'file'}, $fields,
  937. $andmode, $limit, $headersonly);
  938. }
  939. elsif ($folder->{'type'} == 2) {
  940. # Get all of the mail from the POP3 server and search it
  941. local ($min, $max);
  942. if ($limit && $limit->{'latest'}) {
  943. $min = -1;
  944. $max = -$limit->{'latest'};
  945. }
  946. local @mails = &mailbox_list_mails($min, $max, $folder,
  947. &indexof('body', &search_fields($fields)) < 0 &&
  948. $headersonly);
  949. local @rv = grep { $_ && &mail_matches($fields, $andmode, $_) } @mails;
  950. }
  951. elsif ($folder->{'type'} == 3) {
  952. # Search an MH directory
  953. return &advanced_search_mhdir($folder->{'file'}, $fields,
  954. $andmode, $limit, $headersonly);
  955. }
  956. elsif ($folder->{'type'} == 4) {
  957. # Use IMAP's remote search feature
  958. local @rv = &imap_login($_[2]);
  959. if ($rv[0] == 0) { &error($rv[1]); }
  960. elsif ($rv[0] == 3) { &error(&text('save_emailbox', $rv[1])); }
  961. elsif ($rv[0] == 2) { &error(&text('save_elogin2', $rv[1])); }
  962. local $h = $rv[1];
  963. $_[2]->{'lastchange'} = $rv[3] if ($rv[3]);
  964. # Do the search to get back a list of matching numbers
  965. local @search;
  966. foreach $f (@{$_[0]}) {
  967. local $field = $f->[0] eq "date" ? "on" : $f->[0];
  968. local $neg = ($field =~ s/^\!//);
  969. local $what = $f->[1];
  970. if ($field ne "size") {
  971. $what = "\"".$what."\""
  972. }
  973. $field = "LARGER" if ($field eq "size");
  974. local $search = uc($field)." ".$what."";
  975. $search = "NOT $search" if ($neg);
  976. push(@searches, $search);
  977. }
  978. local $searches;
  979. if (@searches == 1) {
  980. $searches = $searches[0];
  981. }
  982. elsif ($_[1]) {
  983. $searches = join(" ", @searches);
  984. }
  985. else {
  986. $searches = $searches[$#searches];
  987. for($i=$#searches-1; $i>=0; $i--) {
  988. $searches = "or $searches[$i] ($searches)";
  989. }
  990. }
  991. @rv = &imap_command($h, "UID SEARCH $searches");
  992. &error(&text('save_esearch', $rv[3])) if (!$rv[0]);
  993. # Get back the IDs we want
  994. local ($srch) = grep { $_ =~ /^\*\s+SEARCH/i } @{$rv[1]};
  995. local @ids = split(/\s+/, $srch);
  996. shift(@ids); shift(@ids); # lose * SEARCH
  997. # Call the select function to get the mails
  998. return &mailbox_select_mails($folder, \@ids, $headersonly);
  999. }
  1000. elsif ($folder->{'type'} == 5) {
  1001. # Search each sub-folder and combine the results - taking any count
  1002. # limits into effect
  1003. local $sf;
  1004. local $pos = 0;
  1005. local @mail;
  1006. local (%start, %len);
  1007. foreach $sf (@{$folder->{'subfolders'}}) {
  1008. $len{$sf} = &mailbox_folder_size($sf);
  1009. $start{$sf} = $pos;
  1010. $pos += $len{$sf};
  1011. }
  1012. local $limit = $limit ? { %$limit } : undef;
  1013. $limit = undef;
  1014. foreach $sf (reverse(@{$folder->{'subfolders'}})) {
  1015. local $sfn = &folder_name($sf);
  1016. print DEBUG "searching on sub-folder ",&folder_name($sf),"\n";
  1017. local @submail = &mailbox_search_mail($fields, $andmode, $sf,
  1018. $limit, $headersonly);
  1019. print DEBUG "found ",scalar(@submail),"\n";
  1020. foreach my $sm (@submail) {
  1021. $sm->{'id'} = $sfn."\t".$sm->{'id'};
  1022. }
  1023. push(@mail, reverse(@submail));
  1024. if ($limit && $limit->{'latest'}) {
  1025. # Adjust latest down by size of this folder
  1026. $limit->{'latest'} -= $len{$sf};
  1027. last if ($limit->{'latest'} <= 0);
  1028. }
  1029. }
  1030. return reverse(@mail);
  1031. }
  1032. elsif ($folder->{'type'} == 6) {
  1033. # Just run a search on the sub-mails
  1034. local @rv;
  1035. local ($min, $max);
  1036. if ($limit && $limit->{'latest'}) {
  1037. $min = -1;
  1038. $max = -$limit->{'latest'};
  1039. }
  1040. local $mail;
  1041. local $sfn = &folder_name($sf);
  1042. print DEBUG "searching virtual folder ",&folder_name($folder),"\n";
  1043. foreach $mail (&mailbox_list_mails($min, $max, $folder)) {
  1044. if ($mail && &mail_matches($fields, $andmode, $mail)) {
  1045. push(@rv, $mail);
  1046. }
  1047. }
  1048. return @rv;
  1049. }
  1050. }
  1051. # mailbox_delete_mail(&folder, mail, ...)
  1052. # Delete multiple messages from some folder
  1053. sub mailbox_delete_mail
  1054. {
  1055. return undef if (&is_readonly_mode());
  1056. local $f = shift(@_);
  1057. if ($userconfig{'delete_mode'} == 1 && !$f->{'trash'} && !$f->{'spam'} &&
  1058. !$f->{'notrash'}) {
  1059. # Copy to trash folder first .. if we have one
  1060. local ($trash) = grep { $_->{'trash'} } &list_folders();
  1061. if ($trash) {
  1062. my $r;
  1063. my $save_read = &get_product_name() eq "usermin";
  1064. foreach my $m (@_) {
  1065. $r = &get_mail_read($f, $m) if ($save_read);
  1066. my $mcopy = { %$m }; # Because writing changes id
  1067. &write_mail_folder($mcopy, $trash);
  1068. &set_mail_read($trash, $mcopy, $r) if ($save_read);
  1069. }
  1070. }
  1071. }
  1072. if ($f->{'type'} == 0) {
  1073. # Delete from mbox
  1074. &delete_mail($f->{'file'}, @_);
  1075. }
  1076. elsif ($f->{'type'} == 1) {
  1077. # Delete from Maildir
  1078. &delete_maildir(@_);
  1079. }
  1080. elsif ($f->{'type'} == 2) {
  1081. # Login and delete from the POP3 server
  1082. local @rv = &pop3_login($f);
  1083. if ($rv[0] == 0) { &error($rv[1]); }
  1084. elsif ($rv[0] == 2) { &error(&text('save_elogin', $rv[1])); }
  1085. local $h = $rv[1];
  1086. local @uidl = &pop3_uidl($h);
  1087. local $m;
  1088. local $cd = "$cache_directory/$f->{'id'}.cache";
  1089. foreach $m (@_) {
  1090. local $idx = &indexof($m->{'id'}, @uidl);
  1091. if ($idx >= 0) {
  1092. &pop3_command($h, "dele ".($idx+1));
  1093. local $u = &safe_uidl($m->{'id'});
  1094. unlink("$cd/$u.headers", "$cd/$u.body");
  1095. }
  1096. }
  1097. }
  1098. elsif ($f->{'type'} == 3) {
  1099. # Delete from MH dir
  1100. &delete_mhdir(@_);
  1101. }
  1102. elsif ($f->{'type'} == 4) {
  1103. # Delete from the IMAP server
  1104. local @rv = &imap_login($f);
  1105. if ($rv[0] == 0) { &error($rv[1]); }
  1106. elsif ($rv[0] == 3) { &error(&text('save_emailbox', $rv[1])); }
  1107. elsif ($rv[0] == 2) { &error(&text('save_elogin2', $rv[1])); }
  1108. local $h = $rv[1];
  1109. local $m;
  1110. foreach $m (@_) {
  1111. @rv = &imap_command($h, "UID STORE ".$m->{'id'}.
  1112. " +FLAGS (\\Deleted)");
  1113. &error(&text('save_edelete', $rv[3])) if (!$rv[0]);
  1114. }
  1115. @rv = &imap_command($h, "EXPUNGE");
  1116. &error(&text('save_edelete', $rv[3])) if (!$rv[0]);
  1117. }
  1118. elsif ($f->{'type'} == 5 || $f->{'type'} == 6) {
  1119. # Delete from underlying folder(s), and from virtual index
  1120. foreach my $sm (@_) {
  1121. local ($sfn, $sid) = split(/\t+/, $sm->{'id'}, 2);
  1122. local $sf = &find_subfolder($f, $sfn);
  1123. $sf || &error("Failed to find sub-folder named $sfn");
  1124. if ($f->{'type'} == 5 || $f->{'type'} == 6 && $f->{'delete'}) {
  1125. $sm->{'id'} = $sid;
  1126. &mailbox_delete_mail($sf, $sm);
  1127. $sm->{'id'} = $sfn."\t".$sm->{'id'};
  1128. }
  1129. if ($f->{'type'} == 6) {
  1130. $f->{'members'} = [
  1131. grep { $_->[0] ne $sf ||
  1132. $_->[1] ne $sid } @{$f->{'members'}} ];
  1133. }
  1134. }
  1135. if ($f->{'type'} == 6) {
  1136. # Save new ID list
  1137. &save_folder($f, $f);
  1138. }
  1139. }
  1140. # Always force a re-check of the index when deleting, as we may not detect
  1141. # the change (especially for IMAP, where UIDNEXT may not change). This isn't
  1142. # needed for Maildir or MH, as indexing is reliable enough
  1143. if ($f->{'type'} != 1 && $f->{'type'} != 3) {
  1144. &force_new_index_recheck($f);
  1145. }
  1146. }
  1147. # mailbox_empty_folder(&folder)
  1148. # Remove the entire contents of a mail folder
  1149. sub mailbox_empty_folder
  1150. {
  1151. return undef if (&is_readonly_mode());
  1152. local $f = $_[0];
  1153. if ($f->{'type'} == 0) {
  1154. # mbox format mail file
  1155. &empty_mail($f->{'file'});
  1156. }
  1157. elsif ($f->{'type'} == 1) {
  1158. # qmail format maildir
  1159. &empty_maildir($f->{'file'});
  1160. }
  1161. elsif ($f->{'type'} == 2) {
  1162. # POP3 server .. delete all messages
  1163. local @rv = &pop3_login($f);
  1164. if ($rv[0] == 0) { &error($rv[1]); }
  1165. elsif ($rv[0] == 2) { &error(&text('save_elogin', $rv[1])); }
  1166. local $h = $rv[1];
  1167. @rv = &pop3_command($h, "stat");
  1168. $rv[1] =~ /^(\d+)/ || return;
  1169. local $count = $1;
  1170. local $i;
  1171. for($i=1; $i<=$count; $i++) {
  1172. &pop3_command($h, "dele ".$i);
  1173. }
  1174. }
  1175. elsif ($f->{'type'} == 3) {
  1176. # mh format maildir
  1177. &empty_mhdir($f->{'file'});
  1178. }
  1179. elsif ($f->{'type'} == 4) {
  1180. # IMAP server .. delete all messages
  1181. local @rv = &imap_login($f);
  1182. if ($rv[0] == 0) { &error($rv[1]); }
  1183. elsif ($rv[0] == 3) { &error(&text('save_emailbox', $rv[1])); }
  1184. elsif ($rv[0] == 2) { &error(&text('save_elogin2', $rv[1])); }
  1185. local $h = $rv[1];
  1186. local $count = $rv[2];
  1187. local $i;
  1188. for($i=1; $i<=$count; $i++) {
  1189. @rv = &imap_command($h, "STORE ".$i.
  1190. " +FLAGS (\\Deleted)");
  1191. &error(&text('save_edelete', $rv[3])) if (!$rv[0]);
  1192. }
  1193. @rv = &imap_command($h, "EXPUNGE");
  1194. &error(&text('save_edelete', $rv[3])) if (!$rv[0]);
  1195. }
  1196. elsif ($f->{'type'} == 5) {
  1197. # Empty each sub-folder
  1198. local $sf;
  1199. foreach $sf (@{$f->{'subfolders'}}) {
  1200. &mailbox_empty_folder($sf);
  1201. }
  1202. }
  1203. elsif ($f->{'type'} == 6) {
  1204. if ($folder->{'delete'}) {
  1205. # Delete all underlying messages
  1206. local @dmails = &mailbox_list_mails(undef, undef, $f, 1);
  1207. &mailbox_delete_mail($f, @dmails);
  1208. }
  1209. else {
  1210. # Clear the virtual index
  1211. $f->{'members'} = [ ];
  1212. &save_folder($f);
  1213. }
  1214. }
  1215. # Trash the folder index
  1216. if ($folder->{'sortable'}) {
  1217. &delete_new_sort_index($folder);
  1218. }
  1219. }
  1220. # mailbox_copy_folder(&source, &dest)
  1221. # Copy all messages from one folder to another. This is done in an optimized
  1222. # way if possible.
  1223. sub mailbox_copy_folder
  1224. {
  1225. local ($src, $dest) = @_;
  1226. if ($src->{'type'} == 0 && $dest->{'type'} == 0) {
  1227. # mbox to mbox .. just read and write the files
  1228. &open_readfile(SOURCE, $src->{'file'});
  1229. &open_tempfile(DEST, ">>$dest->{'file'}");
  1230. while(read(SOURCE, $buf, 1024) > 0) {
  1231. &print_tempfile(DEST, $buf);
  1232. }
  1233. &close_tempfile(DEST);
  1234. close(SOURCE);
  1235. }
  1236. elsif ($src->{'type'} == 1 && $dest->{'type'} == 1) {
  1237. # maildir to maildir .. just copy the files
  1238. local @files = &get_maildir_files($src->{'file'});
  1239. foreach my $f (@files) {
  1240. local $fn = &unique_maildir_filename($dest);
  1241. &copy_source_dest($f, "$dest->{'file'}/$fn");
  1242. }
  1243. &mailbox_fix_permissions($dest);
  1244. }
  1245. elsif ($src->{'type'} == 1 && $dest->{'type'} == 0) {
  1246. # maildir to mbox .. append all the files
  1247. local @files = &get_maildir_files($src->{'file'});
  1248. &open_tempfile(DEST, ">>$dest->{'file'}");
  1249. local $fromline = &make_from_line("webmin\@example.com")."\n";
  1250. foreach my $f (@files) {
  1251. &open_readfile(SOURCE, $f);
  1252. &print_tempfile("DEST", $fromline);
  1253. while(read(SOURCE, $buf, 1024) > 0) {
  1254. &print_tempfile(DEST, $buf);
  1255. }
  1256. close(SOURCE);
  1257. }
  1258. &close_tempfile(DEST);
  1259. }
  1260. else {
  1261. # read in all mail and write out, in 100 message blocks
  1262. local $max = &mailbox_folder_size($src);
  1263. for(my $s=0; $s<$max; $s+=100) {
  1264. local $e = $s+99;
  1265. $e = $max-1 if ($e >= $max);
  1266. local @mail = &mailbox_list_mails($s, $e, $src);
  1267. local @want = @mail[$s..$e];
  1268. &mailbox_copy_mail($src, $dest, @want);
  1269. }
  1270. }
  1271. }
  1272. # mailbox_move_mail(&source, &dest, mail, ...)
  1273. # Move mail from one folder to another
  1274. sub mailbox_move_mail
  1275. {
  1276. return undef if (&is_readonly_mode());
  1277. local $src = shift(@_);
  1278. local $dst = shift(@_);
  1279. local $now = time();
  1280. local $hn = &get_system_hostname();
  1281. &create_folder_maildir($dst);
  1282. local $fix_index;
  1283. if (($src->{'type'} == 1 || $src->{'type'} == 3) && $dst->{'type'} == 1) {
  1284. # Can just move mail files to Maildir names
  1285. local $dd = $dst->{'file'};
  1286. &create_folder_maildir($dst);
  1287. foreach $m (@_) {
  1288. rename($m->{'file'}, "$dd/cur/$now.$$.$hn");
  1289. $now++;
  1290. }
  1291. &mailbox_fix_permissions($dst);
  1292. $fix_index = 1;
  1293. }
  1294. elsif (($src->{'type'} == 1 || $src->{'type'} == 3) && $dst->{'type'} == 3) {
  1295. # Can move and rename to MH numbering
  1296. local $dd = $dst->{'file'};
  1297. local $num = &max_mhdir($dst->{'file'}) + 1;
  1298. foreach $m (@_) {
  1299. rename($m->{'file'}, "$dd/$num");
  1300. $num++;
  1301. }
  1302. &mailbox_fix_permissions($dst);
  1303. $fix_index = 1;
  1304. }
  1305. else {
  1306. # Append to new folder file, or create in folder directory
  1307. my @mdel;
  1308. my $r;
  1309. my $save_read = &get_product_name() eq "usermin";
  1310. foreach my $m (@_) {
  1311. $r = &get_mail_read($src, $m) if ($save_read);
  1312. my $mcopy = { %$m };
  1313. &write_mail_folder($mcopy, $dst);
  1314. &set_mail_read($dst, $mcopy, $r) if ($save_read);
  1315. push(@mdel, $m);
  1316. }
  1317. local $src->{'notrash'} = 1; # Prevent saving to trash
  1318. &mailbox_delete_mail($src, @mdel);
  1319. }
  1320. }
  1321. # mailbox_fix_permissions(&folder, [&stat])
  1322. # Set the ownership on all files in a folder correctly, either based on its
  1323. # current stat or a structure passed in.
  1324. sub mailbox_fix_permissions
  1325. {
  1326. local ($f, $st) = @_;
  1327. $st ||= [ stat($f->{'file'}) ];
  1328. return 0 if ($< != 0); # Only makes sense when running as root
  1329. if ($f->{'type'} == 0) {
  1330. # Set perms on a single file
  1331. &set_ownership_permissions($st->[4], $st->[5], $st->[2], $f->{'file'});
  1332. return 1;
  1333. }
  1334. elsif ($f->{'type'} == 1 || $f->{'type'} == 3) {
  1335. # Do a whole directory
  1336. &execute_command("chown -R $st->[4]:$st->[5] ".
  1337. quotemeta($dst->{'file'}));
  1338. return 1;
  1339. }
  1340. return 0;
  1341. }
  1342. # mailbox_move_folder(&source, &dest)
  1343. # Moves all mail from one folder to another, possibly converting the type
  1344. sub mailbox_move_folder
  1345. {
  1346. return undef if (&is_readonly_mode());
  1347. local ($src, $dst) = @_;
  1348. if ($src->{'type'} == $dst->{'type'} && !$src->{'remote'}) {
  1349. # Can just move the file or dir
  1350. local @st = stat($dst->{'file'});
  1351. system("rm -rf ".quotemeta($dst->{'file'}));
  1352. system("mv ".quotemeta($src->{'file'})." ".quotemeta($dst->{'file'}));
  1353. if (@st) {
  1354. &mailbox_fix_permissions($dst, \@st);
  1355. }
  1356. }
  1357. elsif (($src->{'type'} == 1 || $src->{'type'} == 3) && $dst->{'type'} == 0) {
  1358. # For Maildir or MH to mbox moves, just append files
  1359. local @files = $src->{'type'} == 1 ? &get_maildir_files($src->{'file'})
  1360. : &get_mhdir_files($src->{'file'});
  1361. &open_tempfile(DEST, ">>$dst->{'file'}");
  1362. local $fromline = &make_from_line("webmin\@example.com");
  1363. foreach my $f (@files) {
  1364. &open_readfile(SOURCE, $f);
  1365. &print_tempfile("DEST", $fromline);
  1366. while(read(SOURCE, $buf, 1024) > 0) {
  1367. &print_tempfile(DEST, $buf);
  1368. }
  1369. &unlink_file($f);
  1370. }
  1371. &close_tempfile(DEST);
  1372. }
  1373. else {
  1374. # Need to read in and write out. But do it in 1000-message blocks
  1375. local $count = &mailbox_folder_size($src);
  1376. local $step = 1000;
  1377. for(my $start=0; $start<$count; $start+=$step) {
  1378. local $end = $start + $step - 1;
  1379. $end = $count-1 if ($end >= $count);
  1380. local @mails = &mailbox_list_mails($start, $end, $src);
  1381. @mails = @mails[$start..$end];
  1382. &mailbox_copy_mail($src, $dst, @mails);
  1383. }
  1384. &mailbox_empty_folder($src);
  1385. }
  1386. # Delete source folder index
  1387. if ($src->{'sortable'}) {
  1388. &delete_new_sort_index($src);
  1389. }
  1390. }
  1391. # mailbox_copy_mail(&source, &dest, mail, ...)
  1392. # Copy mail from one folder to another
  1393. sub mailbox_copy_mail
  1394. {
  1395. return undef if (&is_readonly_mode());
  1396. local $src = shift(@_);
  1397. local $dst = shift(@_);
  1398. local $now = time();
  1399. &create_folder_maildir($dst);
  1400. if ($src->{'type'} == 6 && $dst->{'type'} == 6) {
  1401. # Copying from one virtual folder to another, so just copy the
  1402. # reference
  1403. foreach my $m (@_) {
  1404. push(@{$dst->{'members'}}, [ $m->{'subfolder'}, $m->{'subid'},
  1405. $m->{'header'}->{'message-id'} ]);
  1406. }
  1407. }
  1408. elsif ($dst->{'type'} == 6) {
  1409. # Add this mail to the index of the virtual folder
  1410. foreach my $m (@_) {
  1411. push(@{$dst->{'members'}}, [ $src, $m->{'idx'},
  1412. $m->{'header'}->{'message-id'} ]);
  1413. }
  1414. &save_folder($dst);
  1415. }
  1416. else {
  1417. # Just write to destination folder. The read status is preserved, but
  1418. # only if in Usermin.
  1419. my $r;
  1420. my $save_read = &get_product_name() eq "usermin";
  1421. foreach my $m (@_) {
  1422. $r = &get_mail_read($src, $m) if ($save_read);
  1423. my $mcopy = { %$m };
  1424. &write_mail_folder($mcopy, $dst);
  1425. &set_mail_read($dst, $mcopy, $r) if ($save_read);
  1426. }
  1427. }
  1428. }
  1429. # folder_type(file_or_dir)
  1430. sub folder_type
  1431. {
  1432. return -d "$_[0]/cur" ? 1 : -d $_[0] ? 3 : 0;
  1433. }
  1434. # create_folder_maildir(&folder)
  1435. # Ensure that a maildir folder has the needed new, cur and tmp directories
  1436. sub create_folder_maildir
  1437. {
  1438. mkdir($folders_dir, 0700);
  1439. if ($_[0]->{'type'} == 1) {
  1440. local $id = $_[0]->{'file'};
  1441. mkdir($id, 0700);
  1442. mkdir("$id/cur", 0700);
  1443. mkdir("$id/new", 0700);
  1444. mkdir("$id/tmp", 0700);
  1445. }
  1446. }
  1447. # write_mail_folder(&mail, &folder, textonly)
  1448. # Writes some mail message to a folder
  1449. sub write_mail_folder
  1450. {
  1451. return undef if (&is_readonly_mode());
  1452. &create_folder_maildir($_[1]);
  1453. local $needid;
  1454. if ($_[1]->{'type'} == 1) {
  1455. # Add to a maildir directory. ID is set by write_maildir to the new
  1456. # relative filename
  1457. local $md = $_[1]->{'file'};
  1458. &write_maildir($_[0], $md, $_[2]);
  1459. }
  1460. elsif ($_[1]->{'type'} == 3) {
  1461. # Create a new MH file. ID is just the new message number
  1462. local $num = &max_mhdir($_[1]->{'file'}) + 1;
  1463. local $md = $_[1]->{'file'};
  1464. local @st = stat($_[1]->{'file'});
  1465. &send_mail($_[0], "$md/$num", $_[2], 1);
  1466. if ($< == 0) {
  1467. &set_ownership_permissions($st[4], $st[5], undef, "$md/$num");
  1468. }
  1469. $_[0]->{'id'} = $num;
  1470. }
  1471. elsif ($_[1]->{'type'} == 0) {
  1472. # Just append to the folder file.
  1473. &send_mail($_[0], $_[1]->{'file'}, $_[2], 1);
  1474. $needid = 1;
  1475. }
  1476. elsif ($_[1]->{'type'} == 4) {
  1477. # Upload to the IMAP server
  1478. local @rv = &imap_login($_[1]);
  1479. if ($rv[0] == 0) { &error($rv[1]); }
  1480. elsif ($rv[0] == 3) { &error(&text('save_emailbox', $rv[1])); }
  1481. elsif ($rv[0] == 2) { &error(&text('save_elogin2', $rv[1])); }
  1482. local $h = $rv[1];
  1483. # Create a temp file and use it to create the IMAP command
  1484. local $temp = &transname();
  1485. &send_mail($_[0], $temp, $_[2], 0, "dummy");
  1486. local $text = &read_file_contents($temp);
  1487. unlink($temp);
  1488. $text =~ s/^From.*\r?\n//; # Not part of IMAP format
  1489. @rv = &imap_command($h, sprintf "APPEND \"%s\" {%d}\r\n%s",
  1490. $_[1]->{'mailbox'} || "INBOX", length($text), $text);
  1491. &error(&text('save_eappend', $rv[3])) if (!$rv[0]);
  1492. $needid = 1;
  1493. }
  1494. elsif ($_[1]->{'type'} == 5) {
  1495. # Just append to the last subfolder
  1496. local @sf = @{$_[1]->{'subfolders'}};
  1497. &write_mail_folder($_[0], $sf[$#sf], $_[2]);
  1498. $needid = 1;
  1499. }
  1500. elsif ($_[1]->{'type'} == 6) {
  1501. # Add mail to first sub-folder, and to virtual index
  1502. # XXX not done
  1503. &error("Cannot add mail to virtual folders");
  1504. }
  1505. if ($needid) {
  1506. # Get the ID of the new mail
  1507. local @idlist = &mailbox_idlist($_[1]);
  1508. print DEBUG "new idlist=",join(" ", @idlist),"\n";
  1509. $_[0]->{'id'} = $idlist[$#idlist];
  1510. }
  1511. }
  1512. # mailbox_modify_mail(&oldmail, &newmail, &folder, textonly)
  1513. # Replaces some mail message with a new one
  1514. sub mailbox_modify_mail
  1515. {
  1516. local ($oldmail, $mail, $folder, $textonly) = @_;
  1517. return undef if (&is_readonly_mode());
  1518. if ($folder->{'type'} == 1) {
  1519. # Just replace the existing file
  1520. &modify_maildir($oldmail, $mail, $textonly);
  1521. }
  1522. elsif ($folder->{'type'} == 3) {
  1523. # Just replace the existing file
  1524. &modify_mhdir($oldmail, $mail, $textonly);
  1525. }
  1526. elsif ($folder->{'type'} == 0) {
  1527. # Modify the mail file
  1528. &modify_mail($folder->{'file'}, $oldmail, $mail, $textonly);
  1529. }
  1530. elsif ($folder->{'type'} == 5 || $folder->{'type'} == 6) {
  1531. # Modify in the underlying folder
  1532. local ($oldsfn, $oldsid) = split(/\t+/, $oldmail->{'id'}, 2);
  1533. local ($sfn, $sid) = split(/\t+/, $mail->{'id'}, 2);
  1534. local $sf = &find_subfolder($folder, $sfn);
  1535. $oldmail->{'id'} = $oldsid;
  1536. $mail->{'id'} = $sid;
  1537. &mailbox_modify_mail($oldmail, $mail, $sf, $textonly);
  1538. $oldmail->{'id'} = $oldsfn."\t".$oldsid;
  1539. $mail->{'id'} = $sfn."\t".$sid;
  1540. }
  1541. else {
  1542. &error("Cannot modify mail in this type of folder!");
  1543. }
  1544. # Delete the message being modified from its index, to force re-generation
  1545. # with new details
  1546. $mail->{'id'} = $oldmail->{'id'}; # Assume that it will replace the old
  1547. if ($folder->{'sortable'}) {
  1548. &delete_new_sort_index_message($folder, $mail->{'id'});
  1549. }
  1550. }
  1551. # mailbox_folder_size(&folder, [estimate])
  1552. # Returns the number of messages in some folder
  1553. sub mailbox_folder_size
  1554. {
  1555. if ($_[0]->{'type'} == 0) {
  1556. # A mbox formatted file
  1557. return &count_mail($_[0]->{'file'});
  1558. }
  1559. elsif ($_[0]->{'type'} == 1) {
  1560. # A qmail maildir
  1561. return &count_maildir($_[0]->{'file'});
  1562. }
  1563. elsif ($_[0]->{'type'} == 2) {
  1564. # A POP3 server
  1565. local @rv = &pop3_login($_[0]);
  1566. if ($rv[0] != 1) {
  1567. if ($rv[0] == 0) { &error($rv[1]); }
  1568. else { &error(&text('save_elogin', $rv[1])); }
  1569. }
  1570. local @st = &pop3_command($rv[1], "stat");
  1571. if ($st[0] == 1) {
  1572. local ($count, $size) = split(/\s+/, $st[1]);
  1573. return $count;
  1574. }
  1575. else {
  1576. &error($st[1]);
  1577. }
  1578. }
  1579. elsif ($_[0]->{'type'} == 3) {
  1580. # An MH directory
  1581. return &count_mhdir($_[0]->{'file'});
  1582. }
  1583. elsif ($_[0]->{'type'} == 4) {
  1584. # An IMAP server
  1585. local @rv = &imap_login($_[0]);
  1586. if ($rv[0] != 1) {
  1587. if ($rv[0] == 0) { &error($rv[1]); }
  1588. elsif ($rv[0] == 3) { &error(&text('save_emailbox', $rv[1])); }
  1589. elsif ($rv[0] == 2) { &error(&text('save_elogin2', $rv[1])); }
  1590. }
  1591. $_[0]->{'lastchange'} = $rv[3];
  1592. return $rv[2];
  1593. }
  1594. elsif ($_[0]->{'type'} == 5) {
  1595. # A composite folder - the size is just that of the sub-folders
  1596. my $rv = 0;
  1597. foreach my $sf (@{$_[0]->{'subfolders'}}) {
  1598. $rv += &mailbox_folder_size($sf);
  1599. }
  1600. return $rv;
  1601. }
  1602. elsif ($_[0]->{'type'} == 6 && !$_[1]) {
  1603. # A virtual folder .. we need to exclude messages that no longer
  1604. # exist in the parent folders
  1605. my $rv = 0;
  1606. foreach my $msg (@{$_[0]->{'members'}}) {
  1607. if (&mailbox_get_mail($msg->[0], $msg->[1])) {
  1608. $rv++;
  1609. }
  1610. }
  1611. return $rv;
  1612. }
  1613. elsif ($_[0]->{'type'} == 6 && $_[1]) {
  1614. # A virtual folder .. but we can just use the last member count
  1615. return scalar(@{$_[0]->{'members'}});
  1616. }
  1617. }
  1618. # mailbox_folder_unread(&folder)
  1619. # Returns the total messages in some folder, the number unread and the number
  1620. # flagged as special.
  1621. sub mailbox_folder_unread
  1622. {
  1623. local ($folder) = @_;
  1624. if ($folder->{'type'} == 4) {
  1625. # For IMAP, the server knows
  1626. local @rv = &imap_login($folder);
  1627. if ($rv[0] != 1) {
  1628. return ( );
  1629. }
  1630. local @data = ( $rv[2] );
  1631. local $h = $rv[1];
  1632. foreach my $s ("UNSEEN", "FLAGGED") {
  1633. @rv = &imap_command($h, "SEARCH ".$s);
  1634. local ($srch) = grep { $_ =~ /^\*\s+SEARCH/i } @{$rv[1]};
  1635. local @ids = split(/\s+/, $srch);
  1636. shift(@ids); shift(@ids); # lose * SEARCH
  1637. push(@data, scalar(@ids));
  1638. }
  1639. return @data;
  1640. }
  1641. elsif ($folder->{'type'} == 5) {
  1642. # Composite folder - counts are sums of sub-folders
  1643. local @data;
  1644. foreach my $sf (@{$folder->{'subfolders'}}) {
  1645. local @sfdata = &mailbox_folder_unread($sf);
  1646. if (scalar(@sfdata)) {
  1647. $data[0] += $sfdata[0];
  1648. $data[1] += $sfdata[1];
  1649. $data[2] += $sfdata[2];
  1650. }
  1651. }
  1652. return @data;
  1653. }
  1654. else {
  1655. # For all other folders, just check individual messages
  1656. # XXX faster for maildir?
  1657. local @data = ( 0, 0, 0 );
  1658. local @mails;
  1659. eval {
  1660. $main::error_must_die = 1;
  1661. @mails = &mailbox_list_mails(undef, undef, $folder, 1);
  1662. };
  1663. return ( ) if ($@);
  1664. foreach my $m (@mails) {
  1665. local $rf = &get_mail_read($folder, $m);
  1666. if ($rf == 2) {
  1667. $data[2]++;
  1668. }
  1669. elsif ($rf == 0) {
  1670. $data[1]++;
  1671. }
  1672. $data[0]++;
  1673. }
  1674. return @data;
  1675. }
  1676. }
  1677. # mailbox_set_read_flags(&folder, &mail, read, special, replied)
  1678. # Updates the status flags on some message
  1679. sub mailbox_set_read_flag
  1680. {
  1681. local ($folder, $mail, $read, $special, $replied) = @_;
  1682. if ($folder->{'type'} == 4) {
  1683. # Set flags on IMAP server
  1684. local @rv = &imap_login($folder);
  1685. if ($rv[0] == 0) { &error($rv[1]); }
  1686. elsif ($rv[0] == 3) { &error(&text('save_emailbox', $rv[1])); }
  1687. elsif ($rv[0] == 2) { &error(&text('save_elogin2', $rv[1])); }
  1688. local $h = $rv[1];
  1689. foreach my $f ([ $read, "\\Seen" ],
  1690. [ $special, "\\Flagged" ],
  1691. [ $replied, "\\Answered" ]) {
  1692. print DEBUG "setting '$f->[0]' '$f->[1]' for $mail->{'…

Large files files are truncated, but you can click here to view the full file