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;
7 use PublicInbox::Config;
9 sub create_tables ($) {
13 CREATE TABLE IF NOT EXISTS imap_last (
14 url VARCHAR PRIMARY KEY NOT NULL,
15 uid_validity INTEGER NOT NULL,
24 my $dbh = DBI->connect("dbi:SQLite:dbname=$dbname", '', '', {
28 sqlite_use_immediate_transaction => 1,
30 $dbh->{sqlite_unicode} = 1;
31 $dbh->do('PRAGMA journal_mode = TRUNCATE');
37 my ($self, $url) = @_;
38 my $sth = $self->{dbh}->prepare_cached(<<'', undef, 1);
39 SELECT uid_validity, uid FROM imap_last WHERE url = ?
45 sub update_last ($$$$) {
46 my ($self, $url, $validity, $last) = @_;
47 my $sth = $self->{dbh}->prepare_cached(<<'');
48 INSERT OR REPLACE INTO imap_last (url, uid_validity, uid)
51 $sth->execute($url, $validity, $last);
55 my ($class, $dbname) = @_;
57 # original name for compatibility with old setups:
58 $dbname //= PublicInbox::Config->config_dir() . "/imap.sqlite3";
60 # use the new XDG-compliant name for new setups:
62 $dbname = ($ENV{XDG_DATA_HOME} //
63 (($ENV{HOME} // '/nonexistent').'/.local/share')) .
64 '/public-inbox/imap.sqlite3';
68 require File::Basename;;
69 File::Path::mkpath(File::Basename::dirname($dbname));
72 my $dbh = dbh_new($dbname);
73 bless { dbname => $dbname, dbh => $dbh }, $class;