Synchronize notmuch mail across machines
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

notmuch_db.h 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // -*- C++ -*-
  2. #include <exception>
  3. #include <string>
  4. #include <vector>
  5. #include <unordered_set>
  6. #include <notmuch.h>
  7. #include <xapian.h>
  8. #include "cleanup.h"
  9. using std::string;
  10. struct notmuch_err : std::exception {
  11. const char *const op_;
  12. const notmuch_status_t status_;
  13. const string what_;
  14. notmuch_err(const char *op, notmuch_status_t status)
  15. : op_(op), status_(status),
  16. what_(op_ + string(": ") + notmuch_status_to_string(status_)) {}
  17. const char *what() const noexcept override { return what_.c_str(); }
  18. };
  19. class notmuch_db {
  20. notmuch_database_t *notmuch_ = nullptr;
  21. static void nmtry(const char *op, notmuch_status_t stat) {
  22. if (stat)
  23. throw notmuch_err (op, stat);
  24. }
  25. string run_notmuch(const char *const *av,
  26. const char *errprefix = nullptr,
  27. int *exit_value = nullptr);
  28. public:
  29. using tags_t = std::unordered_set<string>;
  30. using message_t = unique_obj<notmuch_message_t, notmuch_message_destroy>;
  31. const string notmuch_config;
  32. const string maildir;
  33. const tags_t new_tags;
  34. const tags_t and_tags;
  35. const bool sync_flags;
  36. static string default_notmuch_config();
  37. /* The next function is massively evil, but looking through git
  38. * history, doc_id has been the second element of the
  39. * notmuch_message_t structure for a long time. */
  40. static Xapian::docid get_docid(notmuch_message_t *msg) {
  41. struct fake_message {
  42. notmuch_database_t *notmuch;
  43. Xapian::docid doc_id;
  44. };
  45. return reinterpret_cast<const fake_message *>(msg)->doc_id;
  46. }
  47. private:
  48. tags_t make_and_tags();
  49. public:
  50. notmuch_db(string config, bool create = false);
  51. notmuch_db(const notmuch_db &) = delete;
  52. ~notmuch_db();
  53. void begin_atomic() {
  54. nmtry("begin_atomic", notmuch_database_begin_atomic(notmuch()));
  55. }
  56. void end_atomic() {
  57. nmtry("end_atomic", notmuch_database_end_atomic(notmuch()));
  58. }
  59. message_t get_message(const char *msgid);
  60. message_t add_message(const string &path,
  61. const tags_t *new_tags = nullptr,
  62. bool *was_new = nullptr);
  63. void remove_message(const string &path);
  64. void set_tags(notmuch_message_t *msg, const tags_t &tags);
  65. Xapian::docid get_dir_docid(const char *path);
  66. notmuch_database_t *notmuch();
  67. string get_config(const char *, int *err = nullptr);
  68. void set_config(const char *, ...);
  69. void close();
  70. void run_new(const char *prefix = "[notmuch] ");
  71. };