| 1 | # Copyrights 2001-2023 by [Mark Overmeer]. | ||
| 2 | # For other contributors see ChangeLog. | ||
| 3 | # See the manual pages for details on the licensing terms. | ||
| 4 | # Pod stripped from pm file by OODoc 2.03. | ||
| 5 | # This code is part of distribution Mail-Box-IMAP4. Meta-POD processed with | ||
| 6 | # OODoc into POD and HTML manual-pages. See README.md | ||
| 7 | # Copyright Mark Overmeer. Licensed under the same terms as Perl itself. | ||
| 8 | |||
| 9 | package Mail::Server::IMAP4::User; | ||
| 10 | use vars '$VERSION'; | ||
| 11 | $VERSION = '3.008'; | ||
| 12 | |||
| 13 | use base 'Mail::Box::Manage::User'; | ||
| 14 | |||
| 15 | use strict; | ||
| 16 | use warnings; | ||
| 17 | |||
| 18 | |||
| 19 | sub init($) | ||
| 20 | { my ($self, $args) = @_; | ||
| 21 | |||
| 22 | $self->SUPER::init($args) or return (); | ||
| 23 | |||
| 24 | my $fn = $args->{indexfile}; | ||
| 25 | $self->{MSNU_indexfile} | ||
| 26 | = defined $fn ? $fn : ($self->folderdir . '/index'); | ||
| 27 | |||
| 28 | $self; | ||
| 29 | } | ||
| 30 | |||
| 31 | #------------------------------------------- | ||
| 32 | |||
| 33 | |||
| 34 | sub indexFilename() { shift->{MSNU_indexfile} }; | ||
| 35 | |||
| 36 | #------------------------------------------- | ||
| 37 | |||
| 38 | |||
| 39 | sub folderInfo($) | ||
| 40 | { my $index = shift->index or return (); | ||
| 41 | $index->folder(shift); | ||
| 42 | } | ||
| 43 | |||
| 44 | #------------------------------------------- | ||
| 45 | |||
| 46 | |||
| 47 | sub delete($) | ||
| 48 | { my ($self, $name) = @_; | ||
| 49 | my $index = $self->index->startModify or return 0; | ||
| 50 | |||
| 51 | unless($self->_delete($index, $name)) | ||
| 52 | { $self->cancelModification($index); | ||
| 53 | return 0; | ||
| 54 | } | ||
| 55 | |||
| 56 | $index->write; | ||
| 57 | } | ||
| 58 | |||
| 59 | sub _delete($$) | ||
| 60 | { my ($self, $index, $name) = @_; | ||
| 61 | |||
| 62 | # First clean all subfolders recursively | ||
| 63 | foreach my $subf ($index->subfolders($name)) | ||
| 64 | { $self->_delete($index, $subf) or return 0; | ||
| 65 | } | ||
| 66 | |||
| 67 | # Already disappeared? Shouldn't happen, but ok | ||
| 68 | my $info = $index->folder($name) | ||
| 69 | or return 1; | ||
| 70 | |||
| 71 | # Bluntly clean-out the directory | ||
| 72 | if(my $dir = $info->{Directory}) | ||
| 73 | { # Bluntly try to remove, but error is not set | ||
| 74 | if(remove(\1, $dir) != 0 && -d $dir) | ||
| 75 | { $self->log(error => "Unable to remove folder $dir"); | ||
| 76 | return 0; | ||
| 77 | } | ||
| 78 | } | ||
| 79 | |||
| 80 | # Remove (sub)folder from index | ||
| 81 | $index->folder($name, undef); | ||
| 82 | 1; | ||
| 83 | } | ||
| 84 | |||
| 85 | #------------------------------------------- | ||
| 86 | |||
| 87 | |||
| 88 | sub create($@) | ||
| 89 | { my ($self, $name) = (shift, shift); | ||
| 90 | my $index = $self->index->startModify or return undef; | ||
| 91 | |||
| 92 | if(my $info = $index->folder($name)) | ||
| 93 | { $self->log(WARNING => "Folder $name already exists, creation skipped"); | ||
| 94 | return $info; | ||
| 95 | } | ||
| 96 | |||
| 97 | my $uniq = $index->createUnique; | ||
| 98 | |||
| 99 | # Create the directory | ||
| 100 | # Also in this case, we bluntly try to create it, and when it doesn't | ||
| 101 | # work, we check whether we did too much. This may safe an NFS stat. | ||
| 102 | |||
| 103 | my $dir = $self->home . '/F' . $uniq; | ||
| 104 | unless(mkdir $dir, 0750) | ||
| 105 | { my $rc = "$!"; | ||
| 106 | unless(-d $dir) # replaces $! | ||
| 107 | { $self->log(ERROR => "Cannot create folder directory $dir: $rc"); | ||
| 108 | return undef; | ||
| 109 | } | ||
| 110 | } | ||
| 111 | |||
| 112 | # Write folder name in directory, for recovery purposes. | ||
| 113 | my $namefile = "$dir/name"; | ||
| 114 | unless(open NAME, '>:encoding(utf-8)', $namefile) | ||
| 115 | { $self->log(ERROR => "Cannot write name for folder in $namefile: $!"); | ||
| 116 | return undef; | ||
| 117 | } | ||
| 118 | |||
| 119 | print NAME "$name\n"; | ||
| 120 | |||
| 121 | unless(close NAME) | ||
| 122 | { $self->log(ERROR => "Failed writing folder name to $namefile: $!"); | ||
| 123 | return undef; | ||
| 124 | } | ||
| 125 | |||
| 126 | # Add folder to the index | ||
| 127 | |||
| 128 | my $facts = $self->folder | ||
| 129 | ( $name | ||
| 130 | , Folder => $name | ||
| 131 | , Directory => $dir | ||
| 132 | , Messages => 0 | ||
| 133 | , Size => 0 | ||
| 134 | ); | ||
| 135 | |||
| 136 | $self->write && $facts; | ||
| 137 | } | ||
| 138 | |||
| 139 | #------------------------------------------- | ||
| 140 | |||
| 141 | |||
| 142 | 1; |