/*********************************************************** * AOP mail agent capabilities functions * ************************************************************ * Description: * * * * Created by: avrami tzur * * At: Wed Sep 2 11:52:42 1992 * ***********************************************************/ /* INCLUDES */ # include # include # include # include # include # include # include # include # include # include # include # include # include # include # include # include # include /* for printing error messages */ # include /* for getting username */ # include # include /* for stat() ** needs types.h ***/ # include # include # include # include "AOPmail.h" /* DECLARATIONS */ void main(int argc, char **argv); static void GetMailFile (); static int getNewMail(); static void displayMailMsg(mail_message *msg); static void classify(char *line, mail_message *msg); static void ErrSys(char *msg); static void storeBody(char *line, mail_message *msg, int *status); static void sendToAgent(int msg_type, int data_length, void *data); static void readMailPipe(int fid); static void mailExit(); static void mailEventLoop(); static void mailCheckBox(); static int newMailMessage(mail_message *msg); static void winEventLoop(XtAppContext app_context); static void displayMail(mail_message *msg); static void createMailText(Widget parent, char *data); static void mailTextDissmisCB(Widget w, XtPointer client_data, XmPushButtonCallbackStruct *call_data); static void winWakeUp(XtPointer client_data, XtIntervalId *id); static void winPipeReady(XtPointer client_data, int *fid,XtInputId *id); static void initWindow(); /* DEFINITIONS */ #ifdef SYSV #define MAILBOX_DIRECTORY "/usr/mail" #else /* BSD unixb */ #define MAILBOX_DIRECTORY "/usr/spool/mail" #endif #define LSIZE 256 static char *mbox = "mail.default"; extern int errno; static int read_pipe, write_pipe; static int file_size; static char *id_table[10]; static int id_counter= 0; static mail_deliver = 0; static Widget top_level; static XtAppContext app_context; static window_counter = 0; /* ============================================================ | main() | |----------------------------------------------------------| | Params : 1) | | 2) | | 3) | | Desc : | | | | Returns: | |==========================================================| */ void main(int argc, char **argv) { Arg args[20]; #ifndef MAIL_DEBUG /* Called from interpreter */ /* Initilize the pipes. */ read_pipe = atoi(argv[1]); fcntl(read_pipe, F_SETFL, O_NDELAY); write_pipe = atoi(argv[2]); #else printf("Debug mode\n"); mail_deliver = 1; #endif GetMailFile() ; initWindow(); printf("Init\n"); XtAppMainLoop(app_context); /*winEventLoop(app_context);*/ } /* ============================================================ | initWindow() | |----------------------------------------------------------| | Params : 1) | | 2) | | 3) | | Desc : | | | | Returns: | | Created by: avrami tzur | | At: Tue Sep 8 16:34:31 1992 | |==========================================================| */ static void initWindow() { int argc = 0; char **argv; Arg args[20]; int counter; Widget label_w, bboard; /* Initilize the X windows. */ /* Set the geometry. */ counter = 0; XtSetArg(args[counter],XmNgeometry, "-0-0");counter++; top_level = XtAppInitialize(&app_context, "Main",NULL,0, &argc, argv, NULL,args,counter); /* Register the pipe and the timeout. */ #ifndef MAIL_DEBUG /* Add the pipe as an input event for the window loop. */ XtAppAddInput(app_context, read_pipe, XtInputReadMask, winPipeReady, NULL); #endif /* Add timer to the window manager */ XtAppAddTimeOut(app_context, 5000, winWakeUp, NULL); /* Create the shell window. */ counter = 0; XtSetArg(args[counter],XmNnoResize, True);counter++; bboard= XmCreateBulletinBoard(top_level,"interval",args,counter); XtManageChild(bboard); /* Create the label widget. */ counter = 0; XtSetArg(args[counter],XmNx, 0);counter++; XtSetArg(args[counter],XmNy, 0);counter++; label_w= XmCreateLabel(bboard, "AOPMAIL", args, counter); XtManageChild(label_w); /* Activate window manager */ XtRealizeWidget(top_level); } /* ============================================================ | winEventLoop() | |----------------------------------------------------------| | Params : 1) | | 2) | | 3) | | Desc : | | | | Returns: | | Created by: avrami tzur | | At: Tue Sep 8 14:29:32 1992 | |==========================================================| */ static void winEventLoop(XtAppContext app_context) { XEvent *event_return; XtAppNextEvent(app_context, event_return); XtDispatchEvent(event_return); } /* ============================================================ | mailEventLoop() | |----------------------------------------------------------| | Params : 1) | | 2) | | 3) | | Desc : | | | | Returns: | | Created by: avrami tzur | | At: Thu Sep 3 14:00:13 1992 | |==========================================================| */ static void mailEventLoop() { int fdmask, ret, i ; struct timeval timeout; /* Set the time out. */ timeout.tv_sec = 5; timeout.tv_usec = 0; fdmask = (1 << read_pipe); ret = select(32, &fdmask, NULL, NULL, &timeout); if (ret == -1){ if(errno != EINTR) ErrSys("select error on events"); else { printf("Alarm want off\n"); } } else { if (ret == 0){ /* No events on pipes. */ if(mail_deliver) mailCheckBox(); } else { readMailPipe(read_pipe); } } } /* ============================================================ | winWakeUp() | |----------------------------------------------------------| | Params : 1) | | 2) | | 3) | | Desc : | | | | Returns: | | Created by: avrami tzur | | At: Tue Sep 8 12:24:08 1992 | |==========================================================| */ static void winWakeUp(XtPointer client_data, XtIntervalId *id) { if(mail_deliver) mailCheckBox(); /* Set the timer. */ XtAppAddTimeOut(app_context, 5000, winWakeUp, NULL); } /* ============================================================ | mailCheckBox() | |----------------------------------------------------------| | Params : 1) | | 2) | | 3) | | Desc : | | | | Returns: | | Created by: avrami tzur | | At: Thu Sep 3 14:05:46 1992 | |==========================================================| */ static void mailCheckBox() { struct stat stbuf; int i; /* Check if the file size has changed. */ if(stat(mbox, &stbuf) == -1){ if(file_size != -1){ printf("File disapeared!\n"); file_size = -1; /* Reset the message id table */ for(i=0; ipw_name; } mbox = (char *)malloc(strlen (MAILBOX_DIRECTORY) + 1 + strlen (username) + 1); strcpy (mbox, MAILBOX_DIRECTORY); strcat (mbox, "/"); strcat (mbox, username); } /* ============================================================ | getNewMail() | |----------------------------------------------------------| | Params : 1) | | 2) | | 3) | | Desc : | | | | Returns: | |==========================================================| */ static int getNewMail() { FILE *fp; int status; mail_message *msg= NULL; static char line[LSIZE]; char *temp1, *temp2; int i; /* if we can't open the mailbox return 0. */ if (!(fp= fopen(mbox, "r"))) return(0); /* Get messages. */ status = LOOK_FOR_FROM_LINE; while ( fgets(line, LSIZE, fp) ) { /* Trim the new-line charecter. */ line[strlen(line)-1] = '\0'; if (!strncmp(line, "From ", 5)) { /* Begining of a new messge.*/ if(status != LOOK_FOR_FROM_LINE) { /* It is the end of the previus message. */ /* Check if it is a real new messsage. */ if(newMailMessage(msg)) sendToAgent(MAIL_INCOMING_MAIL, sizeof(mail_message), (char *)msg); free(msg); } /* Allocate new mail message struct. */ msg = (mail_message *)malloc(sizeof(mail_message)); msg->header.start[0] = '\0'; msg->header.from[0] = '\0'; msg->header.to[0] = '\0'; msg->header.cc[0] = '\0'; msg->header.subject[0] = '\0'; msg->header.msg_id[0] = '\0'; msg->header.return_path[0] = '\0'; msg->header.date[0] = '\0'; msg->line_counter = 0; /* Store the start line until the first blank. */ temp1 = line+5; temp2 = msg->header.start; while(*temp1 != ' ') *temp2++ = *temp1++; status = MAIL_HEADER_INFO; } else{ if(status == LOOK_FOR_FROM_LINE){ /* Error: Looking for mail message start and could not find it. */ ErrSys("Error in mailbox file1"); } else { if(status == MAIL_TO_LONG){ /* Ignore the lines. */ } else { if(status == MAIL_BODY) { storeBody(line, msg, &status); } else { if(strlen(line) == 0) { /* Beginig of mail body. */ status = MAIL_BODY; } else { /* Which special line is it? */ classify(line, msg); } } } } } } if(msg != NULL) { if(newMailMessage(msg)) sendToAgent(MAIL_INCOMING_MAIL, sizeof(mail_message), (char *)msg); free(msg); } fclose(fp); return(1); } /* ============================================================ | storeBody() | |----------------------------------------------------------| | Params : 1) | | 2) | | 3) | | Desc : | | | | Returns: | |==========================================================| */ static void storeBody(char *line, mail_message *msg, int *status) { char *temp; temp = line; while(strlen(temp)+1 > MAIL_LINE_SIZE) { strncpy(msg->body[msg->line_counter++],temp, MAIL_LINE_SIZE -1); if(msg->line_counter < MAIL_BODY_SIZE){ temp = temp +MAIL_LINE_SIZE -1; } else { /* Message too long */ printf("\tMessage too long\n"); break; } } if(msg->line_counter < MAIL_BODY_SIZE){ strcpy(msg->body[msg->line_counter++],temp); } if(msg->line_counter >= MAIL_BODY_SIZE){ printf("Mail message is to long\n"); *status = MAIL_TO_LONG; } } /* ============================================================ | displayMailMsg() | |----------------------------------------------------------| | Params : 1) | | 2) | | 3) | | Desc : | | | | Returns: | |==========================================================| */ static void displayMailMsg(mail_message *msg) { int i; printf("\n\t Mail Message (status %s)\n", (msg->line_counter >= MAIL_BODY_SIZE)?"To long":"ok"); printf("From %s\n", msg->header.start); printf("From: %s\n", msg->header.from); printf("To: %s\n", msg->header.to); printf("Cc: %s\n", msg->header.cc); printf("Message-Id:: %s\n", msg->header.msg_id); printf("Date: %s\n", msg->header.date); printf("Return-Path: <%s>\n", msg->header.return_path); printf("Subject: %s\n", msg->header.subject); printf("\n"); for(i=0;i < msg->line_counter; i++) printf("%s\n",msg->body[i]); printf("\n\t End of Mail Message\n"); } /* ============================================================ | classify() | |----------------------------------------------------------| | Params : 1) | | 2) | | 3) | | Desc : | | | | Returns: | |==========================================================| */ static void classify(char *line, mail_message *msg) { char *temp; if(!strncmp(line, "From: ", 5)) { /* The message sender. */ /* From: "Steven D. Majewski" */ temp = line+5; strcpy(msg->header.from, temp); return; } if(!strncmp(line, "To: ", 4)) { /* The message reciver. */ /* To: tzur@hplasd.hpl.hp.com */ temp = line+4; strcpy(msg->header.to, temp); return; } if(!strncmp(line, "Cc: ", 4)) { /* The message cc. */ /* Cc: tzur@cs.stanford.edu */ temp = line+4; strcpy(msg->header.cc, temp); return; } if(!strncmp(line, "Subject: ", 9)) { /* The message subject. */ /* Subject: Re: unix mailbox format. */ temp = line+9; strcpy(msg->header.subject, temp); return; } if(!strncmp(line, "Message-Id: ", 12)) { /* The message id. */ /* Message-Id: <9209021927.AA05061@hplasd.hpl.hp.com> */ temp = line+12; strcpy(msg->header.msg_id, temp); return; } if(!strncmp(line, "Return-Path: <", 14)) { char *temp2; /* The message return path. */ /* Return-Path: */ temp = line+14; /* Look for the ':' */ temp2 = strchr(temp, ':'); if(temp2 != NULL) temp = temp2; strcpy(msg->header.return_path, temp); temp = strrchr(msg->header.return_path, '>'); if(temp != NULL) *temp = '\0'; return; } if(!strncmp(line, "Date: ", 6)) { /* The message date. */ /* Date: Wed, 2 Sep 92 14:32:29 EDT */ temp = line+6; strcpy(msg->header.date, temp); return; } } /* ============================================================ | sendToAgent() | |----------------------------------------------------------| | Params : 1) | | 2) | | 3) | | Desc : | | | | Returns: | |==========================================================| */ static void sendToAgent(int msg_type, int data_length, void *data) { char *buffer = (char *)malloc(sizeof(mail_msg_header) + data_length); mail_msg_header *header = (mail_msg_header *)buffer; char *ptr1 = (char *)(buffer + sizeof(mail_msg_header)); int n; /* Check if the message was already transerd */ header->msg_type = msg_type; header->data_length = data_length; memcpy((void *)ptr1, data,data_length); #ifndef MAIL_DEBUG n = write(write_pipe, buffer, sizeof(mail_msg_header) + data_length); if(msg_type == MAIL_INCOMING_MAIL) displayMailMsg((mail_message *)data); #else if(msg_type == MAIL_INCOMING_MAIL){ displayMailMsg((mail_message *)data); displayMail((mail_message *)data); } #endif free(buffer); } /* ============================================================ | winPipeReady() | |----------------------------------------------------------| | Params : 1) | | 2) | | 3) | | Desc : | | | | Returns: | | Created by: avrami tzur | | At: Tue Sep 8 12:22:24 1992 | |==========================================================| */ static void winPipeReady(XtPointer client_data, int *fid,XtInputId *id) { readMailPipe(*fid); } /* ============================================================ | readMailPipe() | |----------------------------------------------------------| | Params : 1) | | 2) | | 3) | | Desc : | | | | Returns: | | Created by: avrami tzur | | At: Thu Sep 3 10:32:26 1992 | |==========================================================| */ static void readMailPipe(int fid) { int n; mail_msg_header temp; mail_msg_header *header; char *data; if((n = read(fid, (char *)&temp, sizeof(mail_msg_header))) > 0){ if (n!= sizeof(mail_msg_header)) { ErrSys(" Read pipe - read header"); } else { data = (char *)malloc(temp.data_length); n = read(fid, data, temp.data_length); if(n != temp.data_length ) { ErrSys(" Read pipe = read data"); } else { header = (mail_msg_header *)malloc(sizeof(mail_msg_header)); memcpy((void *)header, (void *)&temp,sizeof(mail_msg_header)); printf("Read it. type %d\n",header->msg_type ); switch (header->msg_type) { case MAIL_QUIT_REQUEST: { mailExit(); break; } case MAIL_DISPLAY_MAIL: { displayMail((mail_message *)data); break; } case MAIL_DELIVER_MAIL: { mail_deliver = 1; break; } case MAIL_HOLD_MAIL: { mail_deliver = 0; break; } } } } } } /* ============================================================ | mailExit() | |----------------------------------------------------------| | Params : 1) | | 2) | | 3) | | Desc : | | | | Returns: | | Created by: avrami tzur | | At: Thu Sep 3 10:37:38 1992 | |==========================================================| */ static void mailExit() { /* Close the pipes. */ close(read_pipe); close(write_pipe); exit(0); } /* ============================================================ | newMailMessage() | |----------------------------------------------------------| | Params : 1) | | 2) | | 3) | | Desc : | | | | Returns: | | Created by: avrami tzur | | At: Thu Sep 3 19:23:17 1992 | |==========================================================| */ static int newMailMessage(mail_message *msg) { int i; int found = 0; for(i=0; iheader.msg_id, id_table[i])){ found = 1; break; } } if(!found) { /* Add to the table */ id_table[id_counter] = (char *)malloc(sizeof(msg->header.msg_id)+1); strcpy(id_table[id_counter],msg->header.msg_id); id_counter++; } return(!found); } /* ============================================================ | ErrSys() | |----------------------------------------------------------| | Params : 1) | | 2) | | 3) | | Desc : | | | | Returns: | |==========================================================| */ static void ErrSys(char *msg) { printf("\n\n\n Error: %s\n", msg); exit(0); } /* ============================================================ | displayMail() | |----------------------------------------------------------| | Params : 1) | | 2) | | 3) | | Desc : | | | | Returns: | | Created by: avrami tzur | | At: Tue Sep 8 11:32:20 1992 | |==========================================================| */ static void displayMail(mail_message *msg) { char buffer[sizeof(mail_message)]; int i; /* Create the mail widget */ /* Set the text. */ sprintf(buffer, "From %s\nFrom: %s\nTo: %s\nCc: %s\nDate: %s\nReturn-Path: <%s>\nMessage-Id:%s\nSubject: %s\n\n", msg->header.start, msg->header.from, msg->header.to, msg->header.cc, msg->header.date, msg->header.return_path, msg->header.msg_id, msg->header.subject); for(i=0; iline_counter; i++){ strcat(buffer, msg->body[i]); strcat(buffer,"\n"); } createMailText(top_level, buffer); } /* ============================================================ | createMailText() | |----------------------------------------------------------| | Params : 1) | | 2) | | 3) | | Desc : | | | | Returns: | | Created by: avrami tzur | | At: Tue Sep 8 11:50:09 1992 | |==========================================================| */ static void createMailText(Widget parent, char *msg) { Widget board_w, text_w, control_w, och[3]; Arg args[20]; int counter; XmString xstring; Widget *data = (Widget *)malloc(sizeof(Widget)); /* Create the bulletin Board widget. */ counter = 0; xstring = XmStringCreateLtoR("Incoming Mail",XmSTRING_DEFAULT_CHARSET); XtSetArg(args[counter],XmNdialogTitle, xstring);counter++; XtSetArg(args[counter],XmNgeometry, "+100+100");counter++; board_w = XmCreateBulletinBoardDialog(parent,"mail",args,counter); free(xstring); /* Create the text widget. */ counter = 0; XtSetArg(args[counter], XmNx, 0);counter++; XtSetArg(args[counter], XmNy, 0);counter++; XtSetArg(args[counter], XmNeditable, False); counter++; XtSetArg(args[counter], XmNeditMode, XmMULTI_LINE_EDIT); counter++; XtSetArg(args[counter], XmNcolumns, 40); counter++; XtSetArg(args[counter], XmNrows, 10); counter++; XtSetArg(args[counter], XmNwordWrap, True); counter++; XtSetArg(args[counter], XmNscrollHorizontal, False); counter++; XtSetArg(args[counter], XmNvalue, msg);counter++; text_w = XmCreateScrolledText(board_w, "text", args, counter); XtManageChild(text_w); /* Create the ok, help, and cancel container. */ counter = 0; XtSetArg(args[counter],XmNx, 100);counter++; XtSetArg(args[counter],XmNy, 250);counter++; XtSetArg(args[counter],XmNnumColumns, 1);counter++; XtSetArg(args[counter],XmNpacking, XmPACK_COLUMN);counter++; XtSetArg(args[counter],XmNadjustLast, False);counter++; XtSetArg(args[counter],XmNorientation, XmHORIZONTAL);counter++; XtSetArg(args[counter],XmNspacing, 12);counter++; control_w = XmCreateRowColumn(board_w,"rowcol",args,counter); XtManageChild(control_w); counter = 0; xstring = XmStringCreateLtoR("Dissmised", XmSTRING_DEFAULT_CHARSET); XtSetArg(args[counter], XmNlabelString, xstring);counter++; och[0] = XmCreatePushButton(control_w,"controlB",args,counter); free(xstring); *data = board_w; XtAddCallback(och[0], XmNactivateCallback, (XtCallbackProc)&mailTextDissmisCB, (XtPointer)board_w); XtManageChildren(och,1); XtManageChild(board_w); } /* ============================================================ | mailTextDissmisCB() | |----------------------------------------------------------| | Params : 1) | | 2) | | 3) | | Desc : | | | | Returns: | | Created by: avrami tzur | | At: Tue Sep 8 12:04:16 1992 | |==========================================================| */ static void mailTextDissmisCB(Widget w, XtPointer client_data, XmPushButtonCallbackStruct *call_data) { Widget *bboard = (Widget *)client_data; XtDestroyWidget(*bboard); } /* From tzur@hplasd.hpl.hp.com Wed Sep 2 12:27 PDT 1992 Received: by hplasd.hpl.hp.com (16.6/15.5+IOS 3.14) id AA05061; Wed, 2 Sep 92 12:27:45 -0700 Date: Wed, 2 Sep 92 12:27:45 -0700 From: tzur@hplasd.hpl.hp.com Return-Path: Message-Id: <9209021927.AA05061@hplasd.hpl.hp.com> To: tzur@hplasd.hpl.hp.com Subject: Subject From sdm7g@aemsun.med.Virginia.EDU Wed Sep 2 11:38 PDT 1992 Received: from hplms2.hpl.hp.com by hplasd.hpl.hp.com with SMTP (16.6/15.5+IOS 3.14) id AA05042; Wed, 2 Sep 92 11:38:24 -0700 Return-Path: Received: from uvaarpa.Virginia.EDU by hplms2.hpl.hp.com with SMTP (16.5/15.5+IOS 3.20) id AA09159; Wed, 2 Sep 92 11:38:21 -0700 Received: from aemsun.med.virginia.edu by uvaarpa.Virginia.EDU id aa13695; 2 Sep 92 14:34 EDT Received: by aemsun.med.Virginia.EDU (4.1/1.34) id AA10063; Wed, 2 Sep 92 14:32:30 EDT From: "Steven D. Majewski" Message-Id: <9209021832.AA10063@aemsun.med.Virginia.EDU> Subject: Re: unix mailbox format. To: tzur@hplasd.hpl.hp.com Date: Wed, 2 Sep 92 14:32:29 EDT Cc: tzur@cs.stanford.edu In-Reply-To: <9209021826.AA05036@hplasd.hpl.hp.com>; from "tzur@hplasd.hpl.hp.com" at Sep 2, 92 11:26 am X-Mailer: ELM [version 2.3 PL11] From tzur@CS.Stanford.EDU Wed Sep 9 10:15 PDT 1992 Received: from hplms2.hpl.hp.com by hplke.hpl.hp.com with SMTP (16.7/15.5+IOS 3.14) id AA01074; Wed, 9 Sep 92 10:15:39 -0700 Return-Path: Received: from Starry.Stanford.EDU by hplms2.hpl.hp.com with SMTP (16.5/15.5+IOS 3.20) id AA21702; Wed, 9 Sep 92 10:13:13 -0700 Received: by Starry.Stanford.EDU (4.1/25-STARRY-eef) id AA20240; Wed, 9 Sep 92 10:11:53PDT Date: Wed, 9 Sep 92 10:11:53 PDT From: Avrami Tzur Message-Id: <9209091711.AA20240@Starry.Stanford.EDU> To: tzur@hplke.hpl.hp.com Subject: Testing the mail header */