#include <sys_defs.h>
#include <sys/time.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <msg.h>
#include <vstream.h>
#include <vstring.h>
#include <mymalloc.h>
#include <events.h>
#include <mail_params.h>
#include <record.h>
#include <rec_type.h>
#include <mail_proto.h>
#include <cleanup_user.h>
#include <post_mail.h>
#include <mail_date.h>
typedef struct {
char *sender;
char *recipient;
int source_class;
int trace_flags;
int utf8_flags;
POST_MAIL_NOTIFY notify;
void *context;
VSTREAM *stream;
VSTRING *queue_id;
} POST_MAIL_STATE;
typedef struct {
int status;
VSTREAM *stream;
POST_MAIL_FCLOSE_NOTIFY notify;
void *context;
} POST_MAIL_FCLOSE_STATE;
static void post_mail_init(VSTREAM *stream, const char *sender,
const char *recipient,
int source_class, int trace_flags,
int utf8_flags, VSTRING *queue_id)
{
VSTRING *id = queue_id ? queue_id : vstring_alloc(100);
struct timeval now;
const char *date;
int cleanup_flags =
int_filt_flags(source_class) | CLEANUP_FLAG_MASK_INTERNAL
| smtputf8_autodetect(source_class)
| ((utf8_flags & SMTPUTF8_FLAG_REQUESTED) ? CLEANUP_FLAG_SMTPUTF8 : 0);
GETTIMEOFDAY(&now);
date = mail_date(now.tv_sec);
vstream_control(stream, VSTREAM_CTL_BUFSIZE, 2 * VSTREAM_BUFSIZE,
VSTREAM_CTL_END);
if (attr_scan(stream, ATTR_FLAG_STRICT,
RECV_ATTR_STR(MAIL_ATTR_QUEUEID, id),
ATTR_TYPE_END) != 1
|| attr_print(stream, ATTR_FLAG_NONE,
SEND_ATTR_INT(MAIL_ATTR_FLAGS, cleanup_flags),
ATTR_TYPE_END) != 0)
msg_fatal("unable to contact the %s service", var_cleanup_service);
rec_fprintf(stream, REC_TYPE_TIME, REC_TYPE_TIME_FORMAT,
REC_TYPE_TIME_ARG(now));
rec_fprintf(stream, REC_TYPE_ATTR, "%s=%s",
MAIL_ATTR_LOG_ORIGIN, MAIL_ATTR_ORG_LOCAL);
rec_fprintf(stream, REC_TYPE_ATTR, "%s=%d",
MAIL_ATTR_TRACE_FLAGS, trace_flags);
rec_fputs(stream, REC_TYPE_FROM, sender);
rec_fputs(stream, REC_TYPE_RCPT, recipient);
rec_fputs(stream, REC_TYPE_MESG, "");
post_mail_fprintf(stream, "Received: by %s (%s)",
var_myhostname, var_mail_name);
post_mail_fprintf(stream, "\tid %s; %s", vstring_str(id), date);
post_mail_fprintf(stream, "Date: %s", date);
if (queue_id == 0)
vstring_free(id);
}
VSTREAM *post_mail_fopen(const char *sender, const char *recipient,
int source_class, int trace_flags,
int utf8_flags, VSTRING *queue_id)
{
VSTREAM *stream;
stream = mail_connect_wait(MAIL_CLASS_PUBLIC, var_cleanup_service);
post_mail_init(stream, sender, recipient, source_class, trace_flags,
utf8_flags, queue_id);
return (stream);
}
VSTREAM *post_mail_fopen_nowait(const char *sender, const char *recipient,
int source_class, int trace_flags,
int utf8_flags, VSTRING *queue_id)
{
VSTREAM *stream;
if ((stream = mail_connect(MAIL_CLASS_PUBLIC, var_cleanup_service,
BLOCKING)) != 0)
post_mail_init(stream, sender, recipient, source_class, trace_flags,
utf8_flags, queue_id);
else
msg_warn("connect to %s/%s: %m",
MAIL_CLASS_PUBLIC, var_cleanup_service);
return (stream);
}
static void post_mail_open_event(int event, void *context)
{
POST_MAIL_STATE *state = (POST_MAIL_STATE *) context;
const char *myname = "post_mail_open_event";
switch (event) {
case EVENT_READ:
if (msg_verbose)
msg_info("%s: read event", myname);
event_cancel_timer(post_mail_open_event, context);
event_disable_readwrite(vstream_fileno(state->stream));
non_blocking(vstream_fileno(state->stream), BLOCKING);
post_mail_init(state->stream, state->sender,
state->recipient, state->source_class,
state->trace_flags, state->utf8_flags,
state->queue_id);
myfree(state->sender);
myfree(state->recipient);
state->notify(state->stream, state->context);
myfree((void *) state);
return;
case EVENT_TIME:
if (state->stream) {
msg_warn("timeout connecting to service: %s", var_cleanup_service);
event_disable_readwrite(vstream_fileno(state->stream));
vstream_fclose(state->stream);
} else {
msg_warn("connect to service: %s: %m", var_cleanup_service);
}
myfree(state->sender);
myfree(state->recipient);
state->notify((VSTREAM *) 0, state->context);
myfree((void *) state);
return;
case EVENT_XCPT:
msg_warn("error connecting to service: %s", var_cleanup_service);
event_cancel_timer(post_mail_open_event, context);
event_disable_readwrite(vstream_fileno(state->stream));
vstream_fclose(state->stream);
myfree(state->sender);
myfree(state->recipient);
state->notify((VSTREAM *) 0, state->context);
myfree((void *) state);
return;
default:
msg_panic("%s: unknown event type %d", myname, event);
}
}
void post_mail_fopen_async(const char *sender, const char *recipient,
int source_class, int trace_flags,
int utf8_flags, VSTRING *queue_id,
void (*notify) (VSTREAM *, void *),
void *context)
{
VSTREAM *stream;
POST_MAIL_STATE *state;
stream = mail_connect(MAIL_CLASS_PUBLIC, var_cleanup_service, NON_BLOCKING);
state = (POST_MAIL_STATE *) mymalloc(sizeof(*state));
state->sender = mystrdup(sender);
state->recipient = mystrdup(recipient);
state->source_class = source_class;
state->trace_flags = trace_flags;
state->utf8_flags = utf8_flags;
state->notify = notify;
state->context = context;
state->stream = stream;
state->queue_id = queue_id;
if (stream != 0) {
event_enable_read(vstream_fileno(stream), post_mail_open_event,
(void *) state);
event_request_timer(post_mail_open_event, (void *) state,
var_daemon_timeout);
} else {
event_request_timer(post_mail_open_event, (void *) state, 0);
}
}
int post_mail_fprintf(VSTREAM *cleanup, const char *format,...)
{
int status;
va_list ap;
va_start(ap, format);
status = rec_vfprintf(cleanup, REC_TYPE_NORM, format, ap);
va_end(ap);
return (status != REC_TYPE_NORM ? CLEANUP_STAT_WRITE : 0);
}
int post_mail_buffer(VSTREAM *cleanup, const char *buf, int len)
{
return (rec_put(cleanup, REC_TYPE_NORM, buf, len) != REC_TYPE_NORM ?
CLEANUP_STAT_WRITE : 0);
}
int post_mail_fputs(VSTREAM *cleanup, const char *str)
{
ssize_t len = str ? strlen(str) : 0;
return (rec_put(cleanup, REC_TYPE_NORM, str, len) != REC_TYPE_NORM ?
CLEANUP_STAT_WRITE : 0);
}
int post_mail_fclose(VSTREAM *cleanup)
{
int status = 0;
if (vstream_ferror(cleanup) != 0) {
status = CLEANUP_STAT_WRITE;
} else {
rec_fputs(cleanup, REC_TYPE_XTRA, "");
rec_fputs(cleanup, REC_TYPE_END, "");
if (vstream_fflush(cleanup)
|| attr_scan(cleanup, ATTR_FLAG_MISSING,
RECV_ATTR_INT(MAIL_ATTR_STATUS, &status),
ATTR_TYPE_END) != 1)
status = CLEANUP_STAT_WRITE;
}
(void) vstream_fclose(cleanup);
return (status);
}
static void post_mail_fclose_event(int event, void *context)
{
POST_MAIL_FCLOSE_STATE *state = (POST_MAIL_FCLOSE_STATE *) context;
int status = state->status;
switch (event) {
case EVENT_READ:
if (status == 0) {
if (vstream_ferror(state->stream) != 0
|| attr_scan(state->stream, ATTR_FLAG_MISSING,
ATTR_TYPE_INT, MAIL_ATTR_STATUS, &status,
ATTR_TYPE_END) != 1)
status = CLEANUP_STAT_WRITE;
}
break;
default:
msg_warn("error talking to service: %s", var_cleanup_service);
status = CLEANUP_STAT_WRITE;
break;
}
event_cancel_timer(post_mail_fclose_event, context);
event_disable_readwrite(vstream_fileno(state->stream));
state->notify(status, state->context);
(void) vstream_fclose(state->stream);
myfree((void *) state);
}
void post_mail_fclose_async(VSTREAM *stream,
void (*notify) (int status, void *context),
void *context)
{
POST_MAIL_FCLOSE_STATE *state;
int status = 0;
if (vstream_ferror(stream) != 0) {
status = CLEANUP_STAT_WRITE;
} else {
rec_fputs(stream, REC_TYPE_XTRA, "");
rec_fputs(stream, REC_TYPE_END, "");
if (vstream_fflush(stream))
status = CLEANUP_STAT_WRITE;
}
state = (POST_MAIL_FCLOSE_STATE *) mymalloc(sizeof(*state));
state->status = status;
state->stream = stream;
state->notify = notify;
state->context = context;
if (status == 0) {
event_enable_read(vstream_fileno(stream), post_mail_fclose_event,
(void *) state);
event_request_timer(post_mail_fclose_event, (void *) state,
var_daemon_timeout);
} else {
event_request_timer(post_mail_fclose_event, (void *) state, 0);
}
}