]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/POP3D.pm
imap+nntp: share COMPRESS implementation
[public-inbox.git] / lib / PublicInbox / POP3D.pm
1 # Copyright (C) all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 # represents an POP3D (currently a singleton)
5 package PublicInbox::POP3D;
6 use v5.12;
7 use parent qw(PublicInbox::Lock);
8 use DBI qw(:sql_types); # SQL_BLOB
9 use Carp ();
10 use File::Temp 0.19 (); # 0.19 for ->newdir
11 use PublicInbox::Config;
12 use PublicInbox::POP3;
13 use PublicInbox::Syscall;
14 use File::Temp 0.19 (); # 0.19 for ->newdir
15 use Fcntl qw(F_SETLK F_UNLCK F_WRLCK SEEK_SET);
16 my @FLOCK;
17 if ($^O eq 'linux' || $^O eq 'freebsd') {
18         require Config;
19         my $off_t;
20         my $sz = $Config::Config{lseeksize};
21
22         if ($sz == 8 && eval('length(pack("q", 1)) == 8')) { $off_t = 'q' }
23         elsif ($sz == 4) { $off_t = 'l' }
24         else { warn "sizeof(off_t)=$sz requires File::FcntlLock\n" }
25
26         if (defined($off_t)) {
27                 if ($^O eq 'linux') {
28                         @FLOCK = ("ss\@8$off_t$off_t\@32",
29                                 qw(l_type l_whence l_start l_len));
30                 } elsif ($^O eq 'freebsd') {
31                         @FLOCK = ("${off_t}${off_t}lss\@256",
32                                 qw(l_start l_len l_pid l_type l_whence));
33                 }
34         }
35 }
36 @FLOCK or eval { require File::FcntlLock } or
37         die "File::FcntlLock required for POP3 on $^O: $@\n";
38
39 sub new {
40         my ($cls, $pi_cfg) = @_;
41         $pi_cfg //= PublicInbox::Config->new;
42         my $d = $pi_cfg->{'publicinbox.pop3state'} //
43                 die "publicinbox.pop3state undefined\n";
44         -d $d or do {
45                 require File::Path;
46                 File::Path::make_path($d, { mode => 0700 });
47                 PublicInbox::Syscall::nodatacow_dir($d);
48         };
49         bless {
50                 err => \*STDERR,
51                 out => \*STDOUT,
52                 pi_cfg => $pi_cfg,
53                 lock_path => "$d/db.lock", # PublicInbox::Lock to protect SQLite
54                 # interprocess lock is the $pop3state/txn.locks file
55                 # txn_locks => {}, # intraworker locks
56                 # accept_tls => { SSL_server => 1, ..., SSL_reuse_ctx => ... }
57         }, $cls;
58 }
59
60 sub refresh_groups { # PublicInbox::Daemon callback
61         my ($self, $sig) = @_;
62         # TODO share pi_cfg with nntpd/imapd inside -netd
63         my $new = PublicInbox::Config->new;
64         my $old = $self->{pi_cfg};
65         my $s = 'publicinbox.pop3state';
66         $new->{$s} //= $old->{$s};
67         if ($new->{$s} ne $old->{$s}) {
68                 warn <<EOM;
69 $s changed: `$old->{$s}' => `$new->{$s}', config reload ignored
70 EOM
71         } else {
72                 $self->{pi_cfg} = $new;
73         }
74 }
75
76 # persistent tables
77 sub create_state_tables ($$) {
78         my ($self, $dbh) = @_;
79
80         $dbh->do(<<''); # map publicinbox.<name>.newsgroup to integers
81 CREATE TABLE IF NOT EXISTS newsgroups (
82         newsgroup_id INTEGER PRIMARY KEY NOT NULL,
83         newsgroup VARBINARY NOT NULL,
84         UNIQUE (newsgroup) )
85
86         # the $NEWSGROUP_NAME.$SLICE_INDEX is part of the POP3 username;
87         # POP3 has no concept of folders/mailboxes like IMAP/JMAP
88         $dbh->do(<<'');
89 CREATE TABLE IF NOT EXISTS mailboxes (
90         mailbox_id INTEGER PRIMARY KEY NOT NULL,
91         newsgroup_id INTEGER NOT NULL REFERENCES newsgroups,
92         slice INTEGER NOT NULL, /* -1 for most recent slice */
93         UNIQUE (newsgroup_id, slice) )
94
95         $dbh->do(<<''); # actual users are differentiated by their UUID
96 CREATE TABLE IF NOT EXISTS users (
97         user_id INTEGER PRIMARY KEY NOT NULL,
98         uuid VARBINARY NOT NULL,
99         last_seen INTEGER NOT NULL, /* to expire idle accounts */
100         UNIQUE (uuid) )
101
102         # we only track the highest-numbered deleted message per-UUID@mailbox
103         $dbh->do(<<'');
104 CREATE TABLE IF NOT EXISTS deletes (
105         txn_id INTEGER PRIMARY KEY NOT NULL, /* -1 == txn lock offset */
106         user_id INTEGER NOT NULL REFERENCES users,
107         mailbox_id INTEGER NOT NULL REFERENCES mailboxes,
108         uid_dele INTEGER NOT NULL DEFAULT -1, /* IMAP UID, NNTP article */
109         UNIQUE(user_id, mailbox_id) )
110
111 }
112
113 sub state_dbh_new {
114         my ($self) = @_;
115         my $f = "$self->{pi_cfg}->{'publicinbox.pop3state'}/db.sqlite3";
116         my $creat = !-s $f;
117         if ($creat) {
118                 open my $fh, '+>>', $f or Carp::croak "open($f): $!";
119                 PublicInbox::Syscall::nodatacow_fh($fh);
120         }
121
122         my $dbh = DBI->connect("dbi:SQLite:dbname=$f",'','', {
123                 AutoCommit => 1,
124                 RaiseError => 1,
125                 PrintError => 0,
126                 sqlite_use_immediate_transaction => 1,
127                 sqlite_see_if_its_a_number => 1,
128         });
129         $dbh->do('PRAGMA journal_mode = WAL') if $creat;
130         $dbh->do('PRAGMA foreign_keys = ON'); # don't forget this
131
132         # ensure the interprocess fcntl lock file exists
133         $f = "$self->{pi_cfg}->{'publicinbox.pop3state'}/txn.locks";
134         open my $fh, '+>>', $f or Carp::croak("open($f): $!");
135         $self->{txn_fh} = $fh;
136
137         create_state_tables($self, $dbh);
138         $dbh;
139 }
140
141 sub _setlk ($%) {
142         my ($self, %lk) = @_;
143         $lk{l_pid} = 0; # needed for *BSD
144         $lk{l_whence} = SEEK_SET;
145         if (@FLOCK) {
146                 fcntl($self->{txn_fh}, F_SETLK,
147                         pack($FLOCK[0], @lk{@FLOCK[1..$#FLOCK]}));
148         } else {
149                 my $fs = File::FcntlLock->new(%lk);
150                 $fs->lock($self->{txn_fh}, F_SETLK);
151         }
152 }
153
154 sub lock_mailbox {
155         my ($self, $pop3) = @_; # pop3 - PublicInbox::POP3 client object
156         my $lk = $self->lock_for_scope; # lock the SQLite DB, only
157         my $dbh = $self->{-state_dbh} //= state_dbh_new($self);
158         my ($user_id, $ngid, $mbid, $txn_id);
159         my $uuid = delete $pop3->{uuid};
160         $dbh->begin_work;
161
162         # 1. make sure the user exists, update `last_seen'
163         my $sth = $dbh->prepare_cached(<<'');
164 INSERT OR IGNORE INTO users (uuid, last_seen) VALUES (?,?)
165
166         $sth->bind_param(1, $uuid, SQL_BLOB);
167         $sth->bind_param(2, time);
168         if ($sth->execute == 0) { # existing user
169                 $sth = $dbh->prepare_cached(<<'', undef, 1);
170 SELECT user_id FROM users WHERE uuid = ?
171
172                 $sth->bind_param(1, $uuid, SQL_BLOB);
173                 $sth->execute;
174                 $user_id = $sth->fetchrow_array //
175                         die 'BUG: user '.unpack('H*', $uuid).' not found';
176                 $sth = $dbh->prepare_cached(<<'');
177 UPDATE users SET last_seen = ? WHERE user_id = ?
178
179                 $sth->execute(time, $user_id);
180         } else { # new user
181                 $user_id = $dbh->last_insert_id(undef, undef,
182                                                 'users', 'user_id')
183         }
184
185         # 2. make sure the newsgroup has an integer ID
186         $sth = $dbh->prepare_cached(<<'');
187 INSERT OR IGNORE INTO newsgroups (newsgroup) VALUES (?)
188
189         my $ng = $pop3->{ibx}->{newsgroup};
190         $sth->bind_param(1, $ng, SQL_BLOB);
191         if ($sth->execute == 0) {
192                 $sth = $dbh->prepare_cached(<<'', undef, 1);
193 SELECT newsgroup_id FROM newsgroups WHERE newsgroup = ?
194
195                 $sth->bind_param(1, $ng, SQL_BLOB);
196                 $sth->execute;
197                 $ngid = $sth->fetchrow_array // die "BUG: `$ng' not found";
198         } else {
199                 $ngid = $dbh->last_insert_id(undef, undef,
200                                                 'newsgroups', 'newsgroup_id');
201         }
202
203         # 3. ensure the mailbox exists
204         $sth = $dbh->prepare_cached(<<'');
205 INSERT OR IGNORE INTO mailboxes (newsgroup_id, slice) VALUES (?,?)
206
207         if ($sth->execute($ngid, $pop3->{slice}) == 0) {
208                 $sth = $dbh->prepare_cached(<<'', undef, 1);
209 SELECT mailbox_id FROM mailboxes WHERE newsgroup_id = ? AND slice = ?
210
211                 $sth->execute($ngid, $pop3->{slice});
212                 $mbid = $sth->fetchrow_array //
213                         die "BUG: mailbox_id for $ng.$pop3->{slice} not found";
214         } else {
215                 $mbid = $dbh->last_insert_id(undef, undef,
216                                                 'mailboxes', 'mailbox_id');
217         }
218
219         # 4. ensure the (max) deletes row exists for locking
220         $sth = $dbh->prepare_cached(<<'');
221 INSERT OR IGNORE INTO deletes (user_id,mailbox_id) VALUES (?,?)
222
223         if ($sth->execute($user_id, $mbid) == 0) {
224                 $sth = $dbh->prepare_cached(<<'', undef, 1);
225 SELECT txn_id,uid_dele FROM deletes WHERE user_id = ? AND mailbox_id = ?
226
227                 $sth->execute($user_id, $mbid);
228                 ($txn_id, $pop3->{uid_dele}) = $sth->fetchrow_array;
229         } else {
230                 $txn_id = $dbh->last_insert_id(undef, undef,
231                                                 'deletes', 'txn_id');
232         }
233         $dbh->commit;
234
235         # see if it's locked by the same worker:
236         return if $self->{txn_locks}->{$txn_id};
237
238         # see if it's locked by another worker:
239         _setlk($self, l_type => F_WRLCK, l_start => $txn_id - 1, l_len => 1)
240                 or return;
241
242         $pop3->{user_id} = $user_id;
243         $pop3->{txn_id} = $txn_id;
244         $self->{txn_locks}->{$txn_id} = 1;
245 }
246
247 sub unlock_mailbox {
248         my ($self, $pop3) = @_;
249         my $txn_id = delete($pop3->{txn_id}) // return;
250         delete $self->{txn_locks}->{$txn_id}; # same worker
251
252         # other workers
253         _setlk($self, l_type => F_UNLCK, l_start => $txn_id - 1, l_len => 1)
254                 or die "F_UNLCK: $!";
255 }
256
257 1;