#include <sys_defs.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <string.h>
#include <sysexits.h>
#include <vstring.h>
#include <vstream.h>
#include <stringops.h>
#include <mymalloc.h>
#include <msg.h>
#include <mail_proto.h>
#include <mail_queue.h>
#include <mail_date.h>
#include <mail_params.h>
#include <postqueue.h>
#define S_STRING_FORMAT "%-11s %7s %-20s %s\n"
#define S_SENDER_FORMAT "%-11s %7ld %20.20s %s\n"
#define S_HEADINGS "-Queue ID-", "--Size--", \
"----Arrival Time----", "-Sender/Recipient-------"
#define L_STRING_FORMAT "%-17s %8s %-19s %s\n"
#define L_SENDER_FORMAT "%-17s %8ld %19.19s %s\n"
#define L_HEADINGS "----Queue ID-----", "--Size--", \
"---Arrival Time----", "--Sender/Recipient------"
#define STR(x) vstring_str(x)
static unsigned long showq_message(VSTREAM *showq_stream)
{
static VSTRING *queue_name = 0;
static VSTRING *queue_id = 0;
static VSTRING *id_status = 0;
static VSTRING *addr = 0;
static VSTRING *why = 0;
long arrival_time;
long message_size;
int message_status;
char *saved_reason = mystrdup("");
const char *show_reason;
int padding;
int showq_status;
time_t time_t_arrival_time;
if (queue_name == 0) {
queue_name = vstring_alloc(100);
queue_id = vstring_alloc(100);
id_status = vstring_alloc(100);
addr = vstring_alloc(100);
why = vstring_alloc(100);
}
if (attr_scan(showq_stream, ATTR_FLAG_MORE | ATTR_FLAG_STRICT,
RECV_ATTR_STR(MAIL_ATTR_QUEUE, queue_name),
RECV_ATTR_STR(MAIL_ATTR_QUEUEID, queue_id),
RECV_ATTR_LONG(MAIL_ATTR_TIME, &arrival_time),
RECV_ATTR_LONG(MAIL_ATTR_SIZE, &message_size),
RECV_ATTR_STR(MAIL_ATTR_SENDER, addr),
ATTR_TYPE_END) != 5)
msg_fatal_status(EX_SOFTWARE, "malformed showq server response");
message_status = (strcmp(STR(queue_name), MAIL_QUEUE_ACTIVE) == 0 ? '*' :
strcmp(STR(queue_name), MAIL_QUEUE_HOLD) == 0 ? '!' : ' ');
vstring_sprintf(id_status, "%s%c", STR(queue_id), message_status);
time_t_arrival_time = arrival_time;
vstream_printf(var_long_queue_ids ?
L_SENDER_FORMAT : S_SENDER_FORMAT, STR(id_status),
message_size, asctime(localtime(&time_t_arrival_time)),
STR(addr));
while ((showq_status = attr_scan_more(showq_stream)) > 0) {
if (attr_scan(showq_stream, ATTR_FLAG_MORE | ATTR_FLAG_STRICT,
RECV_ATTR_STR(MAIL_ATTR_RECIP, addr),
RECV_ATTR_STR(MAIL_ATTR_WHY, why),
ATTR_TYPE_END) != 2)
msg_fatal_status(EX_SOFTWARE, "malformed showq server response");
if (strcmp(saved_reason, STR(why)) != 0) {
myfree(saved_reason);
saved_reason = mystrdup(STR(why));
show_reason = *saved_reason ? saved_reason : "reason unavailable";
if ((padding = 76 - strlen(show_reason)) < 0)
padding = 0;
vstream_printf("%*s(%s)\n", padding, "", show_reason);
}
vstream_printf(var_long_queue_ids ?
L_STRING_FORMAT : S_STRING_FORMAT,
"", "", "", STR(addr));
}
if (showq_status < 0)
msg_fatal_status(EX_SOFTWARE, "malformed showq server response");
myfree(saved_reason);
return (message_size);
}
void showq_compat(VSTREAM *showq_stream)
{
unsigned long file_count = 0;
unsigned long queue_size = 0;
int showq_status;
while ((showq_status = attr_scan_more(showq_stream)) > 0) {
if (file_count > 0) {
vstream_printf("\n");
} else if (var_long_queue_ids) {
vstream_printf(L_STRING_FORMAT, L_HEADINGS);
} else {
vstream_printf(S_STRING_FORMAT, S_HEADINGS);
}
queue_size += showq_message(showq_stream);
file_count++;
vstream_fflush(VSTREAM_OUT);
}
if (showq_status < 0)
msg_fatal_status(EX_SOFTWARE, "malformed showq server response");
if (file_count == 0)
vstream_printf("Mail queue is empty\n");
else {
vstream_printf("\n-- %lu Kbytes in %lu Request%s.\n",
queue_size / 1024, file_count,
file_count == 1 ? "" : "s");
}
vstream_fflush(VSTREAM_OUT);
}