mbox-mail.c revision 01cbf4ac5d44137ab434791be7f838d98d0fcf3b
89a126810703c666309310d0f3189e9834d70b5bTimo Sirainen/* Copyright (C) 2003 Timo Sirainen */
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen#include "lib.h"
16f816d3f3c32ae3351834253f52ddd0212bcbf3Timo Sirainen#include "istream.h"
6789ed17e7ca4021713507baf0dcf6979bb42e0cTimo Sirainen#include "index-mail.h"
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen#include "mbox-storage.h"
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen#include "mbox-file.h"
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen#include "istream-raw-mbox.h"
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen#include <fcntl.h>
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen#include <unistd.h>
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen#include <sys/stat.h>
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenstatic int mbox_mail_seek(struct index_mail *mail)
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen{
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen struct index_mailbox *ibox = mail->ibox;
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen const void *data;
3ddbbe03fe74b3ee7b1dff4e08ec706d7880d052Timo Sirainen
3ddbbe03fe74b3ee7b1dff4e08ec706d7880d052Timo Sirainen if (mail_index_lookup_extra(ibox->view, mail->mail.seq,
3ddbbe03fe74b3ee7b1dff4e08ec706d7880d052Timo Sirainen ibox->mbox_extra_idx, &data) < 0) {
4bbb027a827d27d1816b8d60c5392cfd9c79e4bdTimo Sirainen mail_storage_set_index_error(ibox);
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen return -1;
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen }
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen
6789ed17e7ca4021713507baf0dcf6979bb42e0cTimo Sirainen // FIXME: lock the file. sync if needed.
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen
6789ed17e7ca4021713507baf0dcf6979bb42e0cTimo Sirainen if (mbox_file_open_stream(ibox) < 0)
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen return -1;
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen
6789ed17e7ca4021713507baf0dcf6979bb42e0cTimo Sirainen istream_raw_mbox_seek(ibox->mbox_stream, *((const uint64_t *)data));
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen return 0;
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen}
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainenstatic const struct mail_full_flags *mbox_mail_get_flags(struct mail *_mail)
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen{
6789ed17e7ca4021713507baf0dcf6979bb42e0cTimo Sirainen return index_mail_get_flags(_mail);
4b058f90f9e8a2c6b2eed275de4eb8cc5195a71dTimo Sirainen /*FIXME:struct index_mail *mail = (struct index_mail *)_mail;
4b058f90f9e8a2c6b2eed275de4eb8cc5195a71dTimo Sirainen struct index_mail_data *data = &mail->data;
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen
6789ed17e7ca4021713507baf0dcf6979bb42e0cTimo Sirainen i_assert(_mail->seq <= mail->ibox->mbox_data_count);
8d80659e504ffb34bb0c6a633184fece35751b18Timo Sirainen
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen (void)index_mail_get_flags(_mail);
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen if ((mail->ibox->mbox_data[_mail->seq-1] & 1) != 0)
6789ed17e7ca4021713507baf0dcf6979bb42e0cTimo Sirainen data->flags.flags |= MAIL_RECENT;
6789ed17e7ca4021713507baf0dcf6979bb42e0cTimo Sirainen
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen return &data->flags;*/
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen}
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainenstatic time_t mbox_mail_get_received_date(struct mail *_mail)
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen{
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen struct index_mail *mail = (struct index_mail *)_mail;
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen struct index_mail_data *data = &mail->data;
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen (void)index_mail_get_received_date(_mail);
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen if (data->received_date != (time_t)-1)
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen return data->received_date;
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen if (mbox_mail_seek(mail) < 0)
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen return (time_t)-1;
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen data->received_date =
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen istream_raw_mbox_get_received_time(mail->ibox->mbox_stream);
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainen if (data->received_date != (time_t)-1) {
f1e9611e93dcb3b745c1904029084fa81644e1b3Timo Sirainen index_mail_cache_add(mail, MAIL_CACHE_RECEIVED_DATE,
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen &data->received_date,
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen sizeof(data->received_date));
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
4c07b08af30e1065f7022980b60474f229d8cadfTimo Sirainen return data->received_date;
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen}
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainenstatic const char *
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainenmbox_mail_get_special(struct mail *_mail, enum mail_fetch_field field)
baf1148108b7d9739626b47cc57298c36929586aTimo Sirainen{
e60a349c641bb2f4723e4a395a25f55531682d2bTimo Sirainen struct index_mail *mail = (struct index_mail *)_mail;
4c07b08af30e1065f7022980b60474f229d8cadfTimo Sirainen
baf1148108b7d9739626b47cc57298c36929586aTimo Sirainen if (field == MAIL_FETCH_FROM_ENVELOPE) {
baf1148108b7d9739626b47cc57298c36929586aTimo Sirainen if (mbox_mail_seek(mail) < 0)
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen return NULL;
4c07b08af30e1065f7022980b60474f229d8cadfTimo Sirainen
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen return istream_raw_mbox_get_sender(mail->ibox->mbox_stream);
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen
e60a349c641bb2f4723e4a395a25f55531682d2bTimo Sirainen }
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen
e60a349c641bb2f4723e4a395a25f55531682d2bTimo Sirainen return index_mail_get_special(_mail, field);
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen}
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainenstatic struct istream *mbox_mail_get_stream(struct mail *_mail,
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen struct message_size *hdr_size,
fd1f0e9ef52b3e157cfd1a01c464c2ac7458ab17Timo Sirainen struct message_size *body_size)
6389aeec8c26b585e583c364b48ad12adf741898Timo Sirainen{
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen struct index_mail *mail = (struct index_mail *)_mail;
e60a349c641bb2f4723e4a395a25f55531682d2bTimo Sirainen struct index_mail_data *data = &mail->data;
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen struct istream *raw_stream;
e60a349c641bb2f4723e4a395a25f55531682d2bTimo Sirainen
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen if (data->stream == NULL) {
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen if (mbox_mail_seek(mail) < 0)
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen return NULL;
e60a349c641bb2f4723e4a395a25f55531682d2bTimo Sirainen
91dca97b367c54a139c268b56a0c67f564bd9197Timo Sirainen // FIXME: need to hide the headers
9c4e6f29ad07fbd27cb9ac510fa69f8c60709f1fTimo Sirainen raw_stream = mail->ibox->mbox_stream;
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen (void)i_stream_read(raw_stream); /* fix v_offset */
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen data->stream = i_stream_create_limit(default_pool,
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen raw_stream,
2cfe9983ce7a6280636ee12beccc2e865111967bTimo Sirainen raw_stream->v_offset,
79fcd3f95a6266cc62ceaa753e56dd4456ab7c4bTimo Sirainen (uoff_t)-1);
79fcd3f95a6266cc62ceaa753e56dd4456ab7c4bTimo Sirainen }
3ddbbe03fe74b3ee7b1dff4e08ec706d7880d052Timo Sirainen
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen return index_mail_init_stream(mail, hdr_size, body_size);
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen}
88ea893b45d3ed8d68000921db9156c03cbe1b00Timo Sirainen
88ea893b45d3ed8d68000921db9156c03cbe1b00Timo Sirainenstruct mail mbox_mail = {
88ea893b45d3ed8d68000921db9156c03cbe1b00Timo Sirainen 0, 0, 0, 0, 0, 0,
88ea893b45d3ed8d68000921db9156c03cbe1b00Timo Sirainen
88ea893b45d3ed8d68000921db9156c03cbe1b00Timo Sirainen mbox_mail_get_flags,
88ea893b45d3ed8d68000921db9156c03cbe1b00Timo Sirainen index_mail_get_parts,
88ea893b45d3ed8d68000921db9156c03cbe1b00Timo Sirainen mbox_mail_get_received_date,
88ea893b45d3ed8d68000921db9156c03cbe1b00Timo Sirainen index_mail_get_date,
88ea893b45d3ed8d68000921db9156c03cbe1b00Timo Sirainen index_mail_get_size,
88ea893b45d3ed8d68000921db9156c03cbe1b00Timo Sirainen index_mail_get_header,
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen index_mail_get_headers,
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen mbox_mail_get_stream,
88ea893b45d3ed8d68000921db9156c03cbe1b00Timo Sirainen mbox_mail_get_special,
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen index_mail_update_flags,
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen index_mail_expunge
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen};
88ea893b45d3ed8d68000921db9156c03cbe1b00Timo Sirainen