20int main(
int argc,
char **argv) {
21 fprintf(stderr,
"This example is not available on Windows\n");
27static void usage(
void);
35#include <sys/select.h>
50#if defined(__GNUC__) || defined(__GNUG__)
52#define IC_ATTRIBUTE(x) __attribute__(x)
55#define IC_ATTRIBUTE(x)
61#if (defined(__GNUG__) && __GNUG__ >= 4) || (!defined(__GNUG__) && defined(__GNUC__) && __GNUC__ >= 3)
62#define ARGNOTUSED IC_ATTRIBUTE((unused))
67static void connection_established(
struct X400msSession *session,
int status,
int num_messages_waiting);
69static void connection_lost(
struct X400msSession *session,
int reason_code,
char *diagnostic);
71static void submission_complete(
struct X400msSession *session,
struct X400msMessage *message,
int errorcode);
73static void fetch_complete(
struct X400msSession *session,
struct X400msMessage *message,
int type,
int seq,
int errorcode);
75static void delete_complete(
struct X400msSession *session,
int seqnum,
int errorcode);
77static void wait_complete(
struct X400msSession *session,
int num_messages_waiting,
int errorcode);
79static void list_complete(
struct X400msSession *session,
struct X400msListResult *listres,
int errorcode);
81static void register_complete(
struct X400msSession *session,
int errorcode);
83static void alert_event(
struct X400msSession *session);
85static void event_loop(
void);
87static int manage_callback(
struct X400msSession *session,
int fd,
int eventmask);
89static int unmanage_callback(
struct X400msSession *session,
int fd,
int eventmask);
97 struct X400msSession *sp;
100#define MAX_SESSIONS 10
103static int dosubmit = 1;
104int millisecs = 10000;
106int nummsgs = 9999999;
107static char *optstr =
"u371m:p:w:l:sc:o:";
110static void usage(
void) {
111 printf(
"usage: %s\n", optstr);
112 printf(
"\t where:\n");
113 printf(
"\t -u : Don't prompt to override defaults \n");
114 printf(
"\t -3 : P3 connection \n");
115 printf(
"\t -7 : P7 connection \n");
116 printf(
"\t -m : OR Address in P7 bind arg \n");
117 printf(
"\t -p : Presentation Address of P7 Store \n");
118 printf(
"\t -w : P7 password of P7 user \n");
119 printf(
"\t -s : Don't do submission\n");
120 printf(
"\t -l : Sleep time (milliseconds)\n");
121 printf(
"\t -c : Num associations\n");
122 printf(
"\t -o : Exit after retrieving n messages\n");
126char *credentials = NULL;
128int main(
int argc,
char **argv) {
137 x400_default_alternate_recipient_allowed = 1;
139 x400_default_originator =
"1000000";
141 for (i = 0; i <= MAX_SESSIONS; i++) {
145 if (get_args(argc, argv, optstr)) {
150 def_oraddr = x400_ms_user_addr;
151 def_pa = x400_ms_presentation_address;
152 if (x400_logline != NULL) {
153 millisecs = atoi(x400_logline);
156 dosubmit = x400_default_disclosure_of_recipients;
157 numcons = atoi(x400_channel);
158 nummsgs = atoi(x400_default_originator);
160 printf(
"Connection type (0 = P7, 1 = P3 submit only, 2 = P3 both directions) [%d]: ", x400_contype);
161 contype = ic_fgetc(x400_contype, stdin);
163 ic_fgetc(x400_contype, stdin);
166 if (contype <
'0' ||
'2' < contype) {
167 contype = x400_contype;
173 for (i = 0; i < numcons; i++) {
174 printf(
"ORAddress [%s] > ", def_oraddr);
175 ic_fgets(ora,
sizeof ora, stdin);
177 if (ora[strlen(ora) - 1] ==
'\n') {
178 ora[strlen(ora) - 1] =
'\0';
181 if (ora[0] ==
'\0') {
182 strcpy(ora, def_oraddr);
186 printf(
"Password [%s]: ", contype == 0 ? x400_p7_password : x400_p3_password);
187 if (ic_fgets(buffer,
sizeof buffer, stdin) == NULL) {
191 if (buffer[strlen(buffer) - 1] ==
'\n') {
192 buffer[strlen(buffer) - 1] =
'\0';
194 if (buffer[0] ==
'\0') {
195 strcpy(buffer, contype == 0 ? x400_p7_password : x400_p3_password);
198 credentials = strdup(buffer);
200 printf(
"Presentation Address [%s] > ", def_pa);
201 ic_fgets(pa,
sizeof pa, stdin);
203 if (pa[strlen(pa) - 1] ==
'\n') {
204 pa[strlen(pa) - 1] =
'\0';
211 st[i].oraddr = strdup(ora);
212 st[i].psw = strdup(buffer);
213 st[i].pa = strdup(pa);
216 if (talking_to_marben_ms) {
221 for (i = 0; (st[i].oraddr != NULL) && (i < numcons); i++) {
228 connection_established,
242 fprintf(stderr,
"Error in Open for %s: %s\n", st[i].oraddr,
X400msError(status));
245 printf(
"Sent off connection request for session %p\n", st[i].sp);
248 if (talking_to_marben_ms) {
261static int unmanage_callback(
struct X400msSession *session,
int fd ARGNOTUSED,
int eventmask) {
264 printf(
"unmanage_callback, session = %p, mask = %d\n", session, eventmask);
266 for (i = 0; st[i].oraddr != NULL; i++) {
267 if (st[i].sp == session) {
268 st[i].eventmask &= ~eventmask;
275static int manage_callback(
struct X400msSession *session,
int fd,
int eventmask) {
278 printf(
"manage_callback, session = %p, fd = %d, eventmask = %d\n", session, fd, eventmask);
280 for (i = 0; st[i].oraddr != NULL; i++) {
281 if (st[i].sp == session) {
282 st[i].eventmask |= eventmask;
289static void send_messages(
void) {
292 struct X400msMessage *mp;
293 struct X400Recipient *rp;
299 for (i = 0; st[i].oraddr != NULL; i++) {
300 if (st[i].fd != -1) {
304 fprintf(stderr,
"x400msMsgNew returned error: %s\n",
X400msError(status));
310 fprintf(stderr,
"x400msRecipNew returned error: %s\n",
X400msError(status));
316 fprintf(stderr,
"x400msRecipAddStrParam returned error: %s\n",
X400msError(status));
320 sprintf(buf,
"Subject for session %p", st[i].sp);
323 fprintf(stderr,
"x400msMsgAddStrParam returned error: %s\n",
X400msError(status));
329 fprintf(stderr,
"x400ms returned error: %s\n",
X400msError(status));
332#define BINSIZE 6000000
333 s = (
char *)malloc(BINSIZE);
334 memset(s, 0, BINSIZE);
338 sprintf(contid,
"CONTID%d", num);
348 printf(
"Sending message from & to %s\n", st[i].oraddr);
352 fprintf(stderr,
"Error sending message: %d\n", status);
360static void connection_established(
struct X400msSession *session,
int status,
int alert) {
365 printf(
"Connection established for session %p, alert = %d\n", session, alert);
369 printf(
"Sending Register request\n");
372 fprintf(stderr,
"Failed to issue Register for session %p, err = %d\n", session, retval);
374 printf(
"Registered AutoAlert autoaction (id = 9) OK\n");
376 printf(
"Sending Deregister request\n");
379 fprintf(stderr,
"Failed to issue Deregister for session %p, err = %d\n", session, retval);
381 printf(
"Deegistered AutoAlert autoaction (id = 5) OK\n");
383 printf(
"Sending List request\n");
386 fprintf(stderr,
"Failed to issue List for session %p, err = %d\n", session, retval);
394 fprintf(stderr,
"Failed to issue Wait for session %p, err = %d\n", session, retval);
401 fprintf(stderr,
"Connection failed for session %p\n", session);
402 for (i = 0; st[i].oraddr != NULL; i++) {
403 if (st[i].sp == session) {
411static void connection_lost(
struct X400msSession *session ARGNOTUSED,
int reason_code,
char *diagnostic) {
412 fprintf(stderr,
"Connection lost callback, reason = %d, diag = %s\n", reason_code, (diagnostic == NULL) ?
"NULL" : diagnostic);
415static void submission_complete(
struct X400msSession *session ARGNOTUSED,
struct X400msMessage *message,
int errorcode) {
421 memset(buf, 0, 1024);
423 printf(
"Submission successful, identifier = %s\n", buf);
426 fprintf(stderr,
"Submission successful but failed to get ID\n");
431 fprintf(stderr,
"No MessageSubmissionTime present from submission result: error %d\n", status);
435 printf(
"MessageSubmissionTime from Submission Result = %s\n", buf);
440 fprintf(stderr,
"No ContentIdentifier present from submission result: error %d\n", status);
444 printf(
"ContentIdentifier from Submission Result = %s\n", buf);
450 fprintf(stderr,
"Submission failed, errorcode = %d\n", errorcode);
454static void list_complete(
struct X400msSession *session,
struct X400msListResult *lr,
int errorcode) {
458 printf(
"list_complete, errorcode = %d\n", errorcode);
498 sender = strdup(buf);
506 subjid = strdup(buf);
516 printf(
"S=%d, P=%d, len=%d, stat=%d, type=%s, subj=%s, orig=%s, msgid=%s, subjid=%s\n",
521 (type == NULL) ?
"NULL" : type,
522 (subj == NULL) ?
"NULL" : subj,
523 (sender == NULL) ?
"NULL" : sender,
524 (msgid == NULL) ?
"NULL" : msgid,
525 (subjid == NULL) ?
"NULL" : subjid);
527 if (sender != NULL) {
539 if (subjid != NULL) {
547 printf(
"Session %p, issing MsgGet\n", session);
548 retval =
X400msMsgGet(session, seqnum, NULL, NULL, NULL);
550 fprintf(stderr,
"MsgGet failed !\n");
558 fprintf(stderr,
"List failed, errorcode = %d\n", errorcode);
562static void fetch_complete(
struct X400msSession *session,
struct X400msMessage *message,
int type,
int seqno,
int errorcode) {
563 printf(
"Fetch complete callback, session=%p, seqno=%d, type=%d, errorcode = %d\n", session, seqno, type, errorcode);
575 printf(
"Read raw content into file %s\n", buffer);
578 fprintf(stderr,
"Failed to read raw content into file, error %d\n", status);
583 printf(
"Read raw content into buffer, length = %ld\n", (
long)length);
586 fprintf(stderr,
"Failed to read raw content into buffer, error %d\n", status);
589 memset(buf, 0, 1024);
591 printf(
"Object type = %s\n", buf);
594 memset(buf, 0, 1024);
596 printf(
"Subject = %s\n", buf);
602 fprintf(stderr,
"MsgGetFinish failed !\n");
609 fprintf(stderr,
"MsgDelete failed !\n");
612 printf(
"Session %p, issued MsgDelete for %d\n", session, seqno);
617 fprintf(stderr,
"MsgGet failed !\n");
619 else printf(
"Session %p, issued MsgGet\n", session);
624 fprintf(stderr,
"Fetch failed, errcode = %d\n", errorcode);
628static void delete_complete(
struct X400msSession *session,
int seqnum,
int errorcode) {
630 printf(
"Session %p, - delete complete callback for seqnum %d\n", session, seqnum);
633 fprintf(stderr,
"******* SESSION %p, - DELETE FAILED %d **********\n", session, errorcode);
637static void register_complete(
struct X400msSession *session,
int errorcode) {
639 printf(
"Session %p, - register complete callback\n", session);
642 fprintf(stderr,
"******* SESSION %p, - REGISTER FAILED %d **********\n", session, errorcode);
646static void wait_complete(
struct X400msSession *session,
int num_messages_waiting,
int errorcode) {
647 printf(
"Wait complete callback, errorcode = %d, num = %d\n", errorcode, num_messages_waiting);
649 if (num_messages_waiting > 0) {
653 printf(
"Session %p, issuing MsgGet OK\n", session);
656 fprintf(stderr,
"MsgGet failed !\n");
661static void alert_event(
struct X400msSession *session) {
664 printf(
"Got an alert !\n");
666 printf(
"Issuing List op for session %p\n", session);
669 fprintf(stderr,
"Failed to issue List op for session %p, error %d\n", session, retval);
673static void event_loop() {
674 fd_set rfds, wfds, efds;
675 struct timeval timeout, *tp;
686 for (i = 0; st[i].oraddr != NULL; i++) {
687 if (st[i].fd != -1) {
689 printf(
"Expecting ");
692 FD_SET(st[i].fd, &rfds);
697 FD_SET(st[i].fd, &wfds);
702 FD_SET(st[i].fd, &efds);
704 printf(
"on fd %d\n", st[i].fd);
707 if (st[i].fd > num_fds) {
712 if (millisecs >= 0) {
713 timeout.tv_sec = millisecs / 1000;
714 timeout.tv_usec = (millisecs % 1000) * 1000;
721 n = select(num_fds + 1, &rfds, &wfds, &efds, tp);
724 fprintf(stderr,
"select returned %d: errno = %d\n", n, errno);
729 printf(
"Exiting now\n");
730 for (i = 0; st[i].oraddr != NULL; i++) {
731 if (st[i].fd != -1) {
741 printf(
">>>>> Timeout\n");
744 for (i = 0; st[i].oraddr != NULL; i++) {
747 if (st[i].fd != -1) {
748 printf(
"Issuing List for session %p\n", st[i].sp);
751 fprintf(stderr,
"Failed to issue List for session %p, err = %d\n", st[i].sp, retval);
int X400msProcessEvent(int num_fds, fd_set *read_fds, fd_set *write_fds, fd_set *error_fds)
Process outstanding read, write and error events on the specified set of file descriptors....
int X400msMsgAddStrParam(struct X400msMessage *mp, int paramtype, const char *value, size_t length)
Add string-valued parameter to the message.
int X400msList(struct X400msSession *sp, char *since_time, struct X400msListResult **lrp)
List messages in the P7 Message Store.
int X400msGetHandle(struct X400msSession *session)
Get a handle suitable for use in a call to select()
int X400msListGetStrParam(struct X400msListResult *lr, int paramtype, int number, char *buffer, size_t buflen, size_t *paramlenp)
Get a string attribute value from an element of a ListResult.
int X400msListGetIntParam(struct X400msListResult *lr, int paramtype, int number, int *valp)
Get an integer attribute value from an element of a ListResult.
int X400msMsgGetStrParam(struct X400msMessage *mp, int paramtype, char *buffer, size_t buflen, size_t *paramlenp)
Return a string-valued parameter from the message object.
int X400msRecipNew(struct X400msMessage *mp, int type, struct X400Recipient **rpp)
Add new recipient to a message.
int X400msWait(struct X400msSession *sp, int seconds, int *count)
Wait for messages to be ready to be read.
int X400msOpenAsync(int type, const char *oraddr, const char *dirname, const char *credentials, const char *pa, const char *ret_psw, X400msConnEstablishedCb *conupcb, X400msConnDroppedCb *condowncb, X400msMsgSubmittedCb *msgsubcb, X400msMsgFetchedCb *msgfetchcb, X400msMsgDeletedCb *msgdelcb, X400msMsgWaitingCb *msgwaitcb, X400msListCb *listcb, X400msRegisterCb *registercb, X400msAlertCb *alertcb, X400msManageCb *managecb, X400msManageCb *unmanagecb, struct X400msSession **spp)
Initiate an asynchronous opening of a session to a Message Store (P7)
int X400msMsgGet(struct X400msSession *sp, int number, struct X400msMessage **mpp, int *typep, int *seqp)
Get message object for transfer out from MS or MTA via P3.
const char * X400msError(int error)
Obtain a string describing the meaning of the given error code.
int X400msRecipAddStrParam(struct X400Recipient *rp, int paramtype, const char *value, size_t length)
Add string-valued parameter to the message.
int X400msRegisterAutoAction(struct X400msSession *sp, int type, int id, struct X400msAutoActionParameter *aa_param)
Register an autoaction with the Message Store.
void X400msListFree(struct X400msListResult *lr)
Free the memory occupied by a ListResult.
int X400msSetIntDefault(struct X400msSession *sp, int paramtype, int value)
Set a default integer parameter value in a session.
int X400msMsgSend(struct X400msMessage *mp)
Send message object.
int X400msMsgDelete(struct X400msMessage *mp, int retain)
Delete message object.
int X400msMsgAddAttachment(struct X400msMessage *mp, int type, const char *string, size_t length)
Add attachment to the message.
int X400msMsgGetFinish(struct X400msMessage *mp, int errnum, int problem)
Generate delivery result or error for a message.
void X400msSetConfigRequest(int val)
Disable and enable configuration requests in MS Bind operations.
int X400msMsgNew(struct X400msSession *sp, int type, struct X400msMessage **mpp)
Creates new message.
int X400msDeregisterAutoAction(struct X400msSession *sp, int type, int id)
Deregister an autoaction from the Message Store.
int X400msClose(struct X400msSession *sp)
Close a X400 Session.
int X400msSetStrDefault(struct X400msSession *sp, int paramtype, const char *value, size_t length)
Set a default string parameter value in a session.
#define X400_S_OR_ADDRESS
#define X400_S_CONTENT_FILENAME
#define X400_S_CONTENT_STRING
#define X400_S_LOG_CONFIGURATION_FILE
#define X400_S_CONTENT_IDENTIFIER
#define X400_S_OBJECTTYPE
#define X400_S_MESSAGE_SUBMISSION_TIME
#define X400_N_CONTENT_LENGTH
#define X400_S_MESSAGE_IDENTIFIER
#define X400_E_WAIT_READ_WRITE
#define X400_E_NO_MORE_RESULTS
#define X400_E_WAIT_WRITE
#define X400_N_MS_SEQUENCE_NUMBER
#define X400_N_STRICT_P7_1988
#define X400_N_MS_ENTRY_STATUS
#define X400_RECIP_STANDARD
#define X400_S_SUBJECT_IDENTIFIER
X400 MS (P7) Interface asynchronous event handler.
X400 MA/MS (P3/P7) Interface.