examples/x400_mslist.c

This is an example program which shows you how to list messages in the Message Store. A summary is printed for each message in the mailbox.

/* Copyright (c) 2005-2010, Isode Limited, London, England.
* All rights reserved.
*
* Acquisition and use of this software and related materials for any
* purpose requires a written licence agreement from Isode Limited,
* or a written licence from an organisation licenced by Isode Limited
* to grant such a licence.
*
*/
/*
*
* @VERSION@
* Simple example program for listing the contents of an X.400 Message Store mailbox.
*/
#include <stdio.h>
#include <stdlib.h>
#include <x400_msapi.h>
#include "example.h"
#include "ms_example.h"
static void usage(void);
static int fetch_and_print_msg (struct X400msSession *sp,
int seqnum);
static char *optstr = "371um:p:d:w:t:znT:S:h:V:I";
int main (
int argc,
char **argv
)
{
char buffer[BUFSIZ];
char pa[BUFSIZ];
char orn[BUFSIZ];
int status;
struct X400msSession *sp;
int contype;
char *def_oraddr;
char *def_dn;
char *def_pa;
int i;
struct X400msListResult *lr;
/* Magic value as this parameter can be used for entry status
* this value results in either 'any' or 'new' being used.
*/
bin_bp_size = 10000;
if (get_args(argc, argv, optstr)) {
usage();
exit(-1);
}
printf("Connection type (0 = P7, 1 = P3 submit only, 2 = P3 both directions) [%d]: ", x400_contype);
contype = ic_fgetc(x400_contype, stdin);
if (contype != 10)
ic_fgetc(x400_contype, stdin);
if ( contype < '0' || '2' < contype )
contype = x400_contype;
else
contype -= '0';
if (contype == 0) {
def_oraddr = x400_ms_user_addr;
def_dn = x400_ms_user_dn;
def_pa = x400_ms_presentation_address;
} else {
def_oraddr = x400_mta_user_addr;
def_dn = x400_mta_user_dn;
def_pa = x400_mta_presentation_address;
}
printf("Your ORAddress [%s] > ", def_oraddr);
ic_fgets (orn, sizeof orn, stdin);
if ( orn[strlen(orn)-1] == '\n' )
orn[strlen(orn)-1] = '\0';
if (orn[0] == '\0')
strcpy(orn, def_oraddr);
/* Prompt for password; note: reflected. */
printf ("Password [%s]: ",
contype == 0 ? x400_p7_password : x400_p3_password);
if ( ic_fgets (buffer, sizeof buffer, stdin) == NULL )
exit (1);
if (buffer[strlen(buffer)-1] == '\n' )
buffer[strlen(buffer)-1] = '\0';
if (buffer[0] == '\0')
strcpy(buffer, contype == 0 ? x400_p7_password : x400_p3_password);
printf("Presentation Address [%s] > ", def_pa);
ic_fgets (pa, sizeof pa, stdin);
if ( pa[strlen(pa)-1] == '\n' )
pa[strlen(pa)-1] = '\0';
if (pa[0] == '\0')
strcpy(pa, def_pa);
if (talking_to_marben_ms)
status = X400msOpen (contype, orn, def_dn, buffer, pa, NULL, &sp);
if ( status != X400_E_NOERROR ) {
fprintf (stderr, "Error in Open: %s\n", X400msError (status));
exit (status);
}
if (talking_to_marben_ms)
/* setup logging from $(ETCDIR)x400api.xml or $(SHAREDIR)x400api.xml */
for (i = 0; attrs[i] != NULL; i++) {
X400msSetStrDefault(sp, X400_S_LIST_ATTR + i, attrs[i], -1);
}
/* Now list messages, since we have a P7 connection */
/* List messages since a specific time and date - 20/01/2005, 12:05:06
in this case. Specify NULL string instead to list all of them */
status = X400msListExAuxPriBefore(sp, since_time, until_time,
(submitted_messages == 1) ?
(bin_bp_size == 10000) ?
(new_messages == 1) ? 0 : -1 :
bin_bp_size,
priority,
&lr);
if (status != X400_E_NOERROR) {
fprintf (stderr, "X400msList returned status: %s\n",
X400msError (status));
exit(1);
}
i = 1;
printf("\nGot a list result\n");
/* Traverse list result, extracting information about each message */
while (status == X400_E_NOERROR) {
int n;
int seqnum;
int substat;
char buf[1024];
size_t len;
/* Get sequence number */
&seqnum);
if (status == X400_E_NOERROR)
printf("\nSequence number = %d\n", seqnum);
else if (status == X400_E_NO_MORE_RESULTS)
break;
/* Get type of entry */
substat = X400msListGetStrParam(lr,
i,
buf,
1024,
&len);
if (substat == X400_E_NOERROR) {
printf("Object type = %s\n", buf);
} else {
printf("No object type present \n");
}
/* Get Subject field, if any */
substat = X400msListGetStrParam(lr,
i,
buf,
1024,
&len);
if (substat == X400_E_NOERROR) {
printf("Subject = %s\n", buf);
} else {
printf("No subject present \n");
}
/* Get sender address */
substat = X400msListGetStrParam(lr,
i,
buf,
1024,
&len);
if (substat == X400_E_NOERROR) {
printf("Sender = %s\n", buf);
} else {
printf("No sender present \n");
}
substat = X400msListGetStrParam(lr,
i,
buf,
1024,
&len);
if (substat == X400_E_NOERROR) {
printf("Msg Id = %s\n", buf);
} else {
printf("No Msg Id present \n");
}
substat = X400msListGetStrParam(lr,
i,
buf,
1024,
&len);
if (substat == X400_E_NOERROR) {
printf("Subject Id = %s\n", buf);
} else {
printf("No Subject Id present \n");
}
/* Priority */
substat = X400msListGetIntParam(lr, X400_N_PRIORITY, i, &n);
if (substat == X400_E_NOERROR) {
printf("Priority = %d\n", n);
} else {
printf("No priority present \n");
}
/* Length */
&n);
if (substat == X400_E_NOERROR) {
printf("Content length = %d\n", n);
} else {
printf("No content length present \n");
}
buf, 1024, &len);
if (substat == X400_E_NOERROR) {
printf("Content type = %s\n", buf);
} else {
printf("No content type present \n");
}
/* Entry status */
&n);
if (substat == X400_E_NOERROR) {
printf("MS entry status = %d, (New message)\n", n);
} else if ( n == X400_MS_ENTRY_STATUS_LISTED ) {
printf("MS entry status = %d, (Message listed)\n", n);
} else if ( n == X400_MS_ENTRY_STATUS_FETCHED ) {
printf("MS entry status = %d, (Message fetched)\n", n);
} else {
printf("MS entry status = %d, (Unknown status)\n", n);
}
} else {
printf("No entry status present \n");
}
if (gen_ipn == 1) {
// Fetch and print by sequence number
fetch_and_print_msg(sp, seqnum);
}
i++;
}
printf("The list result contained %d entries\n", i-1);
status = X400msClose (sp);
return (status);
}
static int fetch_and_print_msg (struct X400msSession *sp,
int seqnum)
{
int type;
int seqn;
int status;
struct X400msMessage *mp;
char c;
size_t retlen;
char *buf = NULL;
char *databuf = &c;
printf("Getting message\n");
status = X400msMsgGet(sp, seqnum, &mp, &type, &seqn);
switch (status) {
fprintf(stderr, "MsgGet successfully got message; type = %d\n", type);
break;
default :
fprintf(stderr, "Error from MsgGet: %s\n", X400msError(status));
/* tidily close the session */
status = X400msClose(sp);
exit(status);
break;
}
status = print_msg(mp, type);
if (status != X400_E_NOERROR) {
(void) X400msMsgDelete(mp, 0);
exit(status);
}
/* Get binary representation and store to file */
/* First pass to find out how big it needs to be */
status = X400msMsgGetRaw(sp, mp, databuf, 0, &retlen);
if (status != X400_E_NOSPACE) {
fprintf(stderr, "Failed to get raw message length: error %d\n", status);
} else {
struct X400msMessage *newmsg;
int newtype;
databuf = buf = (char *)malloc(retlen);
printf("Data buffer length required = %d\n", (int)retlen);
status = X400msMsgGetRaw(sp, mp, databuf, retlen, &retlen);
if (status != X400_E_NOERROR) {
fprintf(stderr, "Failed to get raw message: error %d\n", status);
} else {
/* Dump to file */
FILE *fd = fopen("message.dump", "w");
if (fd == NULL) {
fprintf(stderr, "Failed to open file message.dump\n");
} else {
if (fwrite(databuf, 1, retlen, fd) < retlen) {
fprintf(stderr, "Failed to write message to file message.dump\n");
} else {
fprintf(stdout, "Dumped message to file message.dump\n");
}
fclose(fd);
}
}
/* And try to reconstruct message from bytes */
status = X400msMsgFromRaw(sp, databuf, retlen, &newmsg, &newtype);
if (status != X400_E_NOERROR) {
fprintf(stderr, "Failed to create message from bytes: error %d\n", status);
}
printf("************************** Printing cached message ************************\n");
print_msg(newmsg, newtype);
if (buf != NULL)
free(buf);
X400msMsgDelete(newmsg, 0);
}
/* Deletes message in message store as well as internal copy */
printf("\n+++ got message - deleting it and returning+++\n");
status = X400msMsgDelete(mp, 0);
return status;
}
static void usage(void) {
printf("usage: %s\n", optstr);
printf("\t where:\n");
printf("\t -u : Don't prompt to override defaults \n");
printf("\t -m : OR Address in P7 bind arg \n");
printf("\t -p : Presentation Address of P7 Store \n");
printf("\t -d : DN in P7 bind arg \n");
printf("\t -w : P7 password of P7 user \n");
printf("\t -z : List submitted messages\n");
printf("\t -n : List messages with status 'new' only\n");
printf("\t -T : List messages with delivery time after <UTCTime>\n");
printf("\t -V : List messages with delivery time before <UTCTime>\n");
printf("\t -S : Specify ms-entry-status value for list operation\n");
printf("\t -h : Specify priority value for list operation\n");
printf("\t -I : Fetch and delete listed messages\n");
return;
}