1 # Copyright (C) 2018-2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 package PublicInbox::IMAPTracker;
5 use parent qw(PublicInbox::Lock);
8 use PublicInbox::Config;
10 sub create_tables ($) {
14 CREATE TABLE IF NOT EXISTS imap_last (
15 url VARCHAR PRIMARY KEY NOT NULL,
16 uid_validity INTEGER NOT NULL,
25 my $dbh = DBI->connect("dbi:SQLite:dbname=$dbname", '', '', {
29 sqlite_use_immediate_transaction => 1,
31 $dbh->{sqlite_unicode} = 1;
32 $dbh->do('PRAGMA journal_mode = TRUNCATE');
39 my $sth = $self->{dbh}->prepare_cached(<<'', undef, 1);
40 SELECT uid_validity, uid FROM imap_last WHERE url = ?
42 $sth->execute($self->{url});
46 sub update_last ($$$) {
47 my ($self, $validity, $last) = @_;
48 my $sth = $self->{dbh}->prepare_cached(<<'');
49 INSERT OR REPLACE INTO imap_last (url, uid_validity, uid)
53 my $rv = $sth->execute($self->{url}, $validity, $last);
59 my ($class, $url) = @_;
61 # original name for compatibility with old setups:
62 my $dbname = PublicInbox::Config->config_dir() . "/imap.sqlite3";
64 # use the new XDG-compliant name for new setups:
66 $dbname = ($ENV{XDG_DATA_HOME} //
67 (($ENV{HOME} // '/nonexistent').'/.local/share')) .
68 '/public-inbox/imap.sqlite3';
72 require File::Basename;
73 File::Path::mkpath(File::Basename::dirname($dbname));
75 my $self = bless { lock_path => "$dbname.lock", url => $url }, $class;
77 $self->{dbh} = dbh_new($dbname);