expire-plugin.c revision 4d9c5b872a985e9ff028dae97e6996c67116298b
2454dfa32c93c20a8522c6ed42fe057baaac9f9aStephan Bosch/* Copyright (C) 2006 PT.COM / SAPO. Code by Tianyan Liu */
1bdda5c0c30463160c47151537e6bb2c6c994841Timo Sirainen *((void **)array_idx_modifiable(&(obj)->module_contexts, \
1bdda5c0c30463160c47151537e6bb2c6c994841Timo Sirainen void (*next_hook_mail_storage_created)(struct mail_storage *storage);
cf52b37d807553e91a2d6fb7cb2c8b4c34589e1dTimo Sirainenconst char *expire_plugin_version = PACKAGE_VERSION;
cf52b37d807553e91a2d6fb7cb2c8b4c34589e1dTimo Sirainenexpire_mailbox_transaction_begin(struct mailbox *box,
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen struct expire_mailbox *xpr_box = EXPIRE_CONTEXT(box);
c04f9a724a7b3cc649485a61b0a540868d25d71bTimo Sirainen t = xpr_box->super.transaction_begin(box, flags);
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen xt = i_new(struct expire_transaction_context, 1);
5cda0bfea032000c4a51134c748d9efe6614870bTimo Sirainen array_idx_set(&t->module_contexts, expire.storage_module_id, &xt);
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainenstatic int first_nonexpunged_timestamp(struct mailbox_transaction_context *_t,
87460b08cb97b31cde640d4975a6aa2c1d0e7226Timo Sirainen struct expire_transaction_context *xt = EXPIRE_CONTEXT(_t);
8b12e7b44abca3bd51a1c46e19ca504f3b55e723Timo Sirainen /* find the first non-expunged mail. we're here because the first
8b12e7b44abca3bd51a1c46e19ca504f3b55e723Timo Sirainen mail was expunged, so don't bother checking it. */
8b12e7b44abca3bd51a1c46e19ca504f3b55e723Timo Sirainen for (seq = 2; seq <= hdr->messages_count; seq++) {
1bdda5c0c30463160c47151537e6bb2c6c994841Timo Sirainen /* everything expunged */
1bdda5c0c30463160c47151537e6bb2c6c994841Timo Sirainenexpire_mailbox_transaction_commit(struct mailbox_transaction_context *t,
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen struct expire_mailbox *xpr_box = EXPIRE_CONTEXT(t->box);
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen struct expire_transaction_context *xt = EXPIRE_CONTEXT(t);
1bdda5c0c30463160c47151537e6bb2c6c994841Timo Sirainen key = t_strconcat(DICT_PATH_SHARED, expire.username, "/",
1bdda5c0c30463160c47151537e6bb2c6c994841Timo Sirainen /* first mail expunged. dict needs updating. */
1bdda5c0c30463160c47151537e6bb2c6c994841Timo Sirainen update_dict = first_nonexpunged_timestamp(t, &new_stamp) == 0;
86ad841251a38aa9ffcf4db4ee2c9fd449121bcbTimo Sirainen /* saved new mails. dict needs to be updated only if this is
c04f9a724a7b3cc649485a61b0a540868d25d71bTimo Sirainen the first mail in the database */
c04f9a724a7b3cc649485a61b0a540868d25d71bTimo Sirainen ret = dict_lookup(expire.db, pool_datastack_create(),
86ad841251a38aa9ffcf4db4ee2c9fd449121bcbTimo Sirainen update_dict = ret == 0 || strtoul(value, NULL, 10) == 0;
70612e07102b75a8511aa7f9de60771176b18de0Timo Sirainen if (xpr_box->super.transaction_commit(t, flags) < 0) {
77af0bd168cf3e3ddc3ae68abc82bfad7e9b5ff4Timo Sirainenexpire_mailbox_transaction_rollback(struct mailbox_transaction_context *t)
ecd69c4e8371853667e01b0c16d436ef7f7393e2Timo Sirainen struct expire_mailbox *xpr_box = EXPIRE_CONTEXT(t->box);
ae1b268ffff743ad9927c304a1344c5cbd7f909dTimo Sirainen struct expire_transaction_context *xt = EXPIRE_CONTEXT(t);
77af0bd168cf3e3ddc3ae68abc82bfad7e9b5ff4Timo Sirainenstatic int expire_mail_expunge(struct mail *_mail)
eddd9bf1a1369aea4a2715f6be1137da6d17d293Timo Sirainen struct mail_private *mail = (struct mail_private *)_mail;
c04f9a724a7b3cc649485a61b0a540868d25d71bTimo Sirainen struct expire_mail *xpr_mail = EXPIRE_CONTEXT(mail);
eddd9bf1a1369aea4a2715f6be1137da6d17d293Timo Sirainen /* first mail expunged, database needs to be updated */
eddd9bf1a1369aea4a2715f6be1137da6d17d293Timo Sirainenstatic struct mail *
eddd9bf1a1369aea4a2715f6be1137da6d17d293Timo Sirainenexpire_mail_alloc(struct mailbox_transaction_context *t,
eddd9bf1a1369aea4a2715f6be1137da6d17d293Timo Sirainen struct mailbox_header_lookup_ctx *wanted_headers)
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen struct expire_mailbox *xpr_box = EXPIRE_CONTEXT(t->box);
e217d6fce33746e198ecc21bff0bc658664c9ef4Timo Sirainen _mail = xpr_box->super.mail_alloc(t, wanted_fields, wanted_headers);
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen xpr_mail = p_new(mail->pool, struct expire_mail, 1);
91dca97b367c54a139c268b56a0c67f564bd9197Timo Sirainen array_idx_set(&mail->module_contexts, expire.storage_module_id,
1bdda5c0c30463160c47151537e6bb2c6c994841Timo Sirainenmail_set_save_time(struct mailbox_transaction_context *t, uint32_t seq)
86ad841251a38aa9ffcf4db4ee2c9fd449121bcbTimo Sirainen struct expire_transaction_context *xt = EXPIRE_CONTEXT(t);
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen mail_cache_add(it->cache_trans, seq, MAIL_CACHE_SAVE_DATE,
cf52b37d807553e91a2d6fb7cb2c8b4c34589e1dTimo Sirainenexpire_save_init(struct mailbox_transaction_context *t,
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen enum mail_flags flags, struct mail_keywords *keywords,
1bdda5c0c30463160c47151537e6bb2c6c994841Timo Sirainen const char *from_envelope, struct istream *input,
ecd69c4e8371853667e01b0c16d436ef7f7393e2Timo Sirainen struct mail *dest_mail, struct mail_save_context **ctx_r)
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen struct expire_transaction_context *xt = EXPIRE_CONTEXT(t);
cf52b37d807553e91a2d6fb7cb2c8b4c34589e1dTimo Sirainen struct expire_mailbox *xpr_box = EXPIRE_CONTEXT(t->box);
19e8adccba16ff419f5675b1575358c2956dce83Timo Sirainen ret = xpr_box->super.save_init(t, flags, keywords, received_date,
e217d6fce33746e198ecc21bff0bc658664c9ef4Timo Sirainenexpire_copy(struct mailbox_transaction_context *t, struct mail *mail,
e217d6fce33746e198ecc21bff0bc658664c9ef4Timo Sirainen enum mail_flags flags, struct mail_keywords *keywords,
e217d6fce33746e198ecc21bff0bc658664c9ef4Timo Sirainen struct expire_transaction_context *xt = EXPIRE_CONTEXT(t);
ecd69c4e8371853667e01b0c16d436ef7f7393e2Timo Sirainen struct expire_mailbox *xpr_box = EXPIRE_CONTEXT(t->box);
e217d6fce33746e198ecc21bff0bc658664c9ef4Timo Sirainen ret = xpr_box->super.copy(t, mail, flags, keywords, dest_mail);
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainenstatic void mailbox_expire_hook(struct mailbox *box, time_t expire_secs)
469a6cf705835ade57dcb59979b6e054207ae5d7Timo Sirainen xpr_box = p_new(box->pool, struct expire_mailbox, 1);
5f44975ec6c5755dd74bcd4c47a123a7242ecab3Timo Sirainen box->v.transaction_begin = expire_mailbox_transaction_begin;
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen box->v.transaction_commit = expire_mailbox_transaction_commit;
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen box->v.transaction_rollback = expire_mailbox_transaction_rollback;
d2b94d25f842cd1b7acaf4dd7de858f7c6a821c9Timo Sirainenstatic struct mailbox *
1bdda5c0c30463160c47151537e6bb2c6c994841Timo Sirainenexpire_mailbox_open(struct mail_storage *storage, const char *name,
d22301419109ed4a38351715e6760011421dadecTimo Sirainen struct istream *input, enum mailbox_open_flags flags)
cf52b37d807553e91a2d6fb7cb2c8b4c34589e1dTimo Sirainen struct expire_mail_storage *xpr_storage = EXPIRE_CONTEXT(storage);
70612e07102b75a8511aa7f9de60771176b18de0Timo Sirainen box = xpr_storage->super.mailbox_open(storage, name, input, flags);
645f258ea29afaf09b673fc65d1bd788dfec8db8Timo Sirainen expire_box = expire_box_find(expire.env, name);
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen mailbox_expire_hook(box, expire_box->expire_secs);
946f22af116d5af80d5bbe1710ac121aa5acef71Stephan Boschstatic void expire_mail_storage_created(struct mail_storage *storage)
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen if (expire.next_hook_mail_storage_created != NULL)
1bdda5c0c30463160c47151537e6bb2c6c994841Timo Sirainen expire.next_hook_mail_storage_created(storage);
1bdda5c0c30463160c47151537e6bb2c6c994841Timo Sirainen xpr_storage = p_new(storage->pool, struct expire_mail_storage, 1);
ae1b268ffff743ad9927c304a1344c5cbd7f909dTimo Sirainen storage->v.mailbox_open = expire_mailbox_open;
eddd9bf1a1369aea4a2715f6be1137da6d17d293Timo Sirainen expire.storage_module_id = mail_storage_module_id++;
eddd9bf1a1369aea4a2715f6be1137da6d17d293Timo Sirainen i_fatal("expire plugin: expire_dict setting missing");
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen expire.db = dict_init(dict_uri, DICT_DATA_TYPE_UINT32, NULL);