1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
/*
 * meli - sqlite3.rs
 *
 * Copyright 2019 Manos Pitsidianakis
 *
 * This file is part of meli.
 *
 * meli is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * meli is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with meli. If not, see <http://www.gnu.org/licenses/>.
 */

/*! Use an sqlite3 database for fast searching.
 */
use crate::melib::ResultIntoMeliError;
use melib::search::{
    escape_double_quote,
    Query::{self, *},
};
use melib::{
    backends::{MailBackend, ResultFuture},
    email::{Envelope, EnvelopeHash},
    log,
    sqlite3::{
        self as melib_sqlite3,
        rusqlite::{self, params},
        DatabaseDescription,
    },
    thread::{SortField, SortOrder},
    MeliError, Result, ERROR,
};

use smallvec::SmallVec;
use std::convert::TryInto;
use std::path::PathBuf;
use std::sync::{Arc, RwLock};

const DB: DatabaseDescription = DatabaseDescription {
name: "index.db",
init_script:Some( "CREATE TABLE IF NOT EXISTS envelopes (
                    id               INTEGER PRIMARY KEY,
                    account_id       INTEGER REFERENCES accounts ON UPDATE CASCADE,
                    hash             BLOB NOT NULL UNIQUE,
                    date             TEXT NOT NULL,
                    _from            TEXT NOT NULL,
                    _to              TEXT NOT NULL,
                    cc               TEXT NOT NULL,
                    bcc              TEXT NOT NULL,
                    subject          TEXT NOT NULL,
                    message_id       TEXT NOT NULL,
                    in_reply_to      TEXT NOT NULL,
                    _references      TEXT NOT NULL,
                    flags            INTEGER NOT NULL,
                    has_attachments  BOOLEAN NOT NULL,
                    body_text        TEXT NOT NULL,
                    timestamp        BLOB NOT NULL
                   );
        CREATE TABLE IF NOT EXISTS folders (
                    id               INTEGER PRIMARY KEY,
                    account_id       INTEGER NOT NULL REFERENCES accounts ON UPDATE CASCADE,
                    hash             BLOB NOT NULL,
                    date             TEXT NOT NULL,
                    name             TEXT NOT NULL
                  );
        CREATE TABLE IF NOT EXISTS accounts (
                    id               INTEGER PRIMARY KEY,
                    name             TEXT NOT NULL UNIQUE
                  );
        CREATE TABLE IF NOT EXISTS folder_and_envelope (
                    folder_id        INTEGER NOT NULL,
                    envelope_id      INTEGER NOT NULL,
                    PRIMARY KEY (folder_id, envelope_id),
                    FOREIGN KEY(folder_id) REFERENCES folders(id) ON UPDATE CASCADE,
                    FOREIGN KEY(envelope_id) REFERENCES envelopes(id) ON UPDATE CASCADE
                  );
      CREATE INDEX IF NOT EXISTS folder_env_idx ON folder_and_envelope(folder_id);
      CREATE INDEX IF NOT EXISTS env_folder_idx ON folder_and_envelope(envelope_id);
      CREATE UNIQUE INDEX IF NOT EXISTS acc_idx ON accounts(name);


CREATE INDEX IF NOT EXISTS envelope_timestamp_index ON envelopes (timestamp);
CREATE INDEX IF NOT EXISTS envelope__from_index ON envelopes (_from);
CREATE INDEX IF NOT EXISTS envelope__to_index ON envelopes (_to);
CREATE INDEX IF NOT EXISTS envelope_cc_index ON envelopes (cc);
CREATE INDEX IF NOT EXISTS envelope_bcc_index ON envelopes (bcc);
CREATE INDEX IF NOT EXISTS envelope_message_id_index ON envelopes (message_id);

        CREATE VIRTUAL TABLE IF NOT EXISTS fts USING fts5(subject, body_text, content=envelopes, content_rowid=id);

-- Triggers to keep the FTS index up to date.
CREATE TRIGGER IF NOT EXISTS envelopes_ai AFTER INSERT ON envelopes BEGIN
  INSERT INTO fts(rowid, subject, body_text) VALUES (new.id, new.subject, new.body_text);
END;

CREATE TRIGGER IF NOT EXISTS envelopes_ad AFTER DELETE ON envelopes BEGIN
  INSERT INTO fts(fts, rowid, subject, body_text) VALUES('delete', old.id, old.subject, old.body_text);
END;

CREATE TRIGGER IF NOT EXISTS envelopes_au AFTER UPDATE ON envelopes BEGIN
  INSERT INTO fts(fts, rowid, subject, body_text) VALUES('delete', old.id, old.subject, old.body_text);
  INSERT INTO fts(rowid, subject, body_text) VALUES (new.id, new.subject, new.body_text);
END; "),
version: 1,
};

pub fn db_path() -> Result<PathBuf> {
    melib_sqlite3::db_path(DB.name)
}

//#[inline(always)]
//fn fts5_bareword(w: &str) -> Cow<str> {
//    if w == "AND" || w == "OR" || w == "NOT" {
//        Cow::from(w)
//    } else {
//        if !w.is_ascii() {
//            Cow::from(format!("\"{}\"", escape_double_quote(w)))
//        } else {
//            for &b in w.as_bytes() {
//                if !(b > 0x2f && b < 0x3a)
//                    || !(b > 0x40 && b < 0x5b)
//                    || !(b > 0x60 && b < 0x7b)
//                    || b != 0x60
//                    || b != 26
//                {
//                    return Cow::from(format!("\"{}\"", escape_double_quote(w)));
//                }
//            }
//            Cow::from(w)
//        }
//    }
//}
//
//
pub async fn insert(
    envelope: Envelope,
    backend: Arc<RwLock<Box<dyn MailBackend>>>,
    acc_name: String,
) -> Result<()> {
    let db_path = db_path()?;
    if !db_path.exists() {
        return Err(MeliError::new(
            "Database hasn't been initialised. Run `reindex` command",
        ));
    }

    let conn = melib_sqlite3::open_db(db_path)?;

    let op = backend
        .read()
        .unwrap()
        .operation(envelope.hash())?
        .as_bytes()?;

    let body = match op.await.map(|bytes| envelope.body_bytes(&bytes)) {
        Ok(body) => body.text(),
        Err(err) => {
            debug!(
                "{}",
                format!(
                    "Failed to open envelope {}: {}",
                    envelope.message_id_display(),
                    err.to_string()
                )
            );
            log(
                format!(
                    "Failed to open envelope {}: {}",
                    envelope.message_id_display(),
                    err.to_string()
                ),
                ERROR,
            );
            return Err(err);
        }
    };

    if let Err(err) = conn.execute(
        "INSERT OR IGNORE INTO accounts (name) VALUES (?1)",
        params![acc_name,],
    ) {
        debug!(
            "Failed to insert envelope {}: {}",
            envelope.message_id_display(),
            err.to_string()
        );
        log(
            format!(
                "Failed to insert envelope {}: {}",
                envelope.message_id_display(),
                err.to_string()
            ),
            ERROR,
        );
        return Err(MeliError::new(err.to_string()));
    }
    let account_id: i32 = {
        let mut stmt = conn
            .prepare("SELECT id FROM accounts WHERE name = ?")
            .unwrap();
        let x = stmt
            .query_map(params![acc_name], |row| row.get(0))
            .unwrap()
            .next()
            .unwrap()
            .unwrap();
        x
    };
    if let Err(err) = conn.execute(
            "INSERT OR REPLACE INTO envelopes (account_id, hash, date, _from, _to, cc, bcc, subject, message_id, in_reply_to, _references, flags, has_attachments, body_text, timestamp)
              VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12, ?13, ?14, ?15)",
              params![account_id, envelope.hash().to_be_bytes().to_vec(), envelope.date_as_str(), envelope.field_from_to_string(), envelope.field_to_to_string(), envelope.field_cc_to_string(), envelope.field_bcc_to_string(), envelope.subject().into_owned().trim_end_matches('\u{0}'), envelope.message_id_display().to_string(), envelope.in_reply_to_display().map(|f| f.to_string()).unwrap_or(String::new()), envelope.field_references_to_string(), i64::from(envelope.flags().bits()), if envelope.has_attachments() { 1 } else { 0 }, body, envelope.date().to_be_bytes().to_vec()],
        )
            .map_err(|e| MeliError::new(e.to_string())) {
                debug!(
                        "Failed to insert envelope {}: {}",
                        envelope.message_id_display(),
                        err.to_string()
                    );
                log(
                    format!(
                        "Failed to insert envelope {}: {}",
                        envelope.message_id_display(),
                        err.to_string()
                    ),
                    ERROR,
                );
              }
    Ok(())
}

pub fn remove(env_hash: EnvelopeHash) -> Result<()> {
    let db_path = db_path()?;
    if !db_path.exists() {
        return Err(MeliError::new(
            "Database hasn't been initialised. Run `reindex` command",
        ));
    }

    let conn = melib_sqlite3::open_db(db_path)?;
    if let Err(err) = conn
        .execute(
            "DELETE FROM envelopes WHERE hash = ?",
            params![env_hash.to_be_bytes().to_vec(),],
        )
        .map_err(|e| MeliError::new(e.to_string()))
    {
        debug!(
            "Failed to remove envelope {}: {}",
            env_hash,
            err.to_string()
        );
        log(
            format!(
                "Failed to remove envelope {}: {}",
                env_hash,
                err.to_string()
            ),
            ERROR,
        );
        return Err(err);
    }
    Ok(())
}

pub fn index(context: &mut crate::state::Context, account_index: usize) -> ResultFuture<()> {
    let account = &context.accounts[account_index];
    let (acc_name, acc_mutex, backend_mutex): (String, Arc<RwLock<_>>, Arc<_>) = (
        account.name().to_string(),
        account.collection.envelopes.clone(),
        account.backend.clone(),
    );
    let conn = melib_sqlite3::open_or_create_db(&DB, None)?;
    let env_hashes = acc_mutex
        .read()
        .unwrap()
        .keys()
        .cloned()
        .collect::<Vec<_>>();

    /* Sleep, index and repeat in order not to block the main process */
    Ok(Box::pin(async move {
        conn.execute(
            "INSERT OR REPLACE INTO accounts (name) VALUES (?1)",
            params![acc_name.as_str(),],
        )
        .chain_err_summary(|| "Failed to update index:")?;
        let account_id: i32 = {
            let mut stmt = conn
                .prepare("SELECT id FROM accounts WHERE name = ?")
                .unwrap();
            let x = stmt
                .query_map(params![acc_name.as_str()], |row| row.get(0))
                .unwrap()
                .next()
                .unwrap()
                .unwrap();
            x
        };
        let mut ctr = 0;
        debug!(
            "{}",
            format!(
                "Rebuilding {} index. {}/{}",
                acc_name,
                ctr,
                env_hashes.len()
            )
        );
        for chunk in env_hashes.chunks(200) {
            ctr += chunk.len();
            for env_hash in chunk {
                let mut op = backend_mutex.read().unwrap().operation(*env_hash)?;
                let bytes = op
                    .as_bytes()?
                    .await
                    .chain_err_summary(|| format!("Failed to open envelope {}", env_hash))?;
                let envelopes_lck = acc_mutex.read().unwrap();
                if let Some(e) = envelopes_lck.get(&env_hash) {
                    let body = e.body_bytes(&bytes).text().replace('\0', "");
                    conn.execute("INSERT OR REPLACE INTO envelopes (account_id, hash, date, _from, _to, cc, bcc, subject, message_id, in_reply_to, _references, flags, has_attachments, body_text, timestamp)
              VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12, ?13, ?14, ?15)",
              params![account_id, e.hash().to_be_bytes().to_vec(), e.date_as_str(), e.field_from_to_string(), e.field_to_to_string(), e.field_cc_to_string(), e.field_bcc_to_string(), e.subject().into_owned().trim_end_matches('\u{0}'), e.message_id_display().to_string(), e.in_reply_to_display().map(|f| f.to_string()).unwrap_or(String::new()), e.field_references_to_string(), i64::from(e.flags().bits()), if e.has_attachments() { 1 } else { 0 }, body, e.date().to_be_bytes().to_vec()],
                        ).chain_err_summary(|| format!( "Failed to insert envelope {}", e.message_id_display()))?;
                }
            }
            let sleep_dur = std::time::Duration::from_millis(20);
            std::thread::sleep(sleep_dur);
        }
        Ok(())
    }))
}

pub fn search(
    query: &Query,
    (sort_field, sort_order): (SortField, SortOrder),
) -> ResultFuture<SmallVec<[EnvelopeHash; 512]>> {
    let db_path = db_path()?;
    if !db_path.exists() {
        return Err(MeliError::new(
            "Database hasn't been initialised. Run `reindex` command",
        ));
    }

    let conn = melib_sqlite3::open_db(db_path)?;

    let sort_field = match debug!(sort_field) {
        SortField::Subject => "subject",
        SortField::Date => "timestamp",
    };

    let sort_order = match debug!(sort_order) {
        SortOrder::Asc => "ASC",
        SortOrder::Desc => "DESC",
    };

    let mut stmt = conn
        .prepare(
            debug!(format!(
                "SELECT hash FROM envelopes WHERE {} ORDER BY {} {};",
                query_to_sql(&query),
                sort_field,
                sort_order
            ))
            .as_str(),
        )
        .map_err(|e| MeliError::new(e.to_string()))?;

    let results = stmt
        .query_map(rusqlite::NO_PARAMS, |row| Ok(row.get(0)?))
        .map_err(|e| MeliError::new(e.to_string()))?
        .map(|r: std::result::Result<Vec<u8>, rusqlite::Error>| {
            Ok(u64::from_be_bytes(
                r.map_err(|e| MeliError::new(e.to_string()))?
                    .as_slice()
                    .try_into()
                    .map_err(|e: std::array::TryFromSliceError| MeliError::new(e.to_string()))?,
            ))
        })
        .collect::<Result<SmallVec<[EnvelopeHash; 512]>>>();
    Ok(Box::pin(async { results }))
}

/// Translates a `Query` to an Sqlite3 expression in a `String`.
pub fn query_to_sql(q: &Query) -> String {
    fn rec(q: &Query, s: &mut String) {
        match q {
            Subject(t) => {
                s.push_str("subject LIKE \"%");
                s.extend(escape_double_quote(t).chars());
                s.push_str("%\" ");
            }
            From(t) => {
                s.push_str("_from LIKE \"%");
                s.extend(escape_double_quote(t).chars());
                s.push_str("%\" ");
            }
            To(t) => {
                s.push_str("_to LIKE \"%");
                s.extend(escape_double_quote(t).chars());
                s.push_str("%\" ");
            }
            Cc(t) => {
                s.push_str("cc LIKE \"%");
                s.extend(escape_double_quote(t).chars());
                s.push_str("%\" ");
            }
            Bcc(t) => {
                s.push_str("bcc LIKE \"%");
                s.extend(escape_double_quote(t).chars());
                s.push_str("%\" ");
            }
            AllText(t) => {
                s.push_str("body_text LIKE \"%");
                s.extend(escape_double_quote(t).chars());
                s.push_str("%\" ");
            }
            And(q1, q2) => {
                s.push_str("(");
                rec(q1, s);
                s.push_str(") AND (");
                rec(q2, s);
                s.push_str(") ");
            }
            Or(q1, q2) => {
                s.push_str("(");
                rec(q1, s);
                s.push_str(") OR (");
                rec(q2, s);
                s.push_str(") ");
            }
            Not(q) => {
                s.push_str("NOT (");
                rec(q, s);
                s.push_str(") ");
            }
            Flags(v) => {
                let total = v.len();
                if total > 1 {
                    s.push_str("(");
                }
                for (i, f) in v.iter().enumerate() {
                    match f.as_str() {
                        "draft" => {
                            s.push_str(" (flags & 8 > 0) ");
                        }
                        "deleted" | "trashed" => {
                            s.push_str(" (flags & 6 > 0) ");
                        }
                        "flagged" => {
                            s.push_str(" (flags & 16 > 0) ");
                        }
                        "recent" => {
                            s.push_str(" (flags & 4 == 0) ");
                        }
                        "seen" | "read" => {
                            s.push_str(" (flags & 4 > 0) ");
                        }
                        "unseen" | "unread" => {
                            s.push_str(" (flags & 4 == 0) ");
                        }
                        "answered" | "replied" => {
                            s.push_str(" (flags & 2 > 0) ");
                        }
                        "unanswered" => {
                            s.push_str(" (flags & 2 == 0) ");
                        }
                        _ => {
                            continue;
                        }
                    }
                    if total > 1 && i != total - 1 {
                        s.push_str(" AND ");
                    }
                }
                if total > 1 {
                    s.push_str(") ");
                }
            }
            HasAttachment => {
                s.push_str("has_attachments == 1 ");
            }
            _ => {}
        }
    }
    let mut ret = String::new();
    rec(q, &mut ret);
    ret
}

#[test]
fn test_query_to_sql() {
    use melib::parsec::Parser;
    use melib::search::query;
    assert_eq!(
        "(subject LIKE \"%test%\" ) AND (body_text LIKE \"%i%\" ) ",
        &query_to_sql(&query().parse_complete("subject: test and i").unwrap().1)
    );
    assert_eq!(
        "(subject LIKE \"%github%\" ) OR ((_from LIKE \"%epilys%\" ) AND ((subject LIKE \"%lib%\" ) OR (subject LIKE \"%meli%\" ) ) ) ",
        &query_to_sql(
            &query()
                .parse_complete(
                    "subject: github or (from: epilys and (subject:lib or subject: meli))"
                )
                .unwrap()
                .1
        )
    );
}