From 7cf318551c4dff12484712fb80aeae56ea64a26c Mon Sep 17 00:00:00 2001 From: "Jean-Marc Pigeon (Delson)" Date: Thu, 8 May 2025 07:19:15 -0400 Subject: [PATCH] Starting to implement session_id trans file. --- lib/lvleml.c | 61 ++++++++++++++++++++++++++++++++++++--- lib/unieml.c | 81 +++++++++++++++++++++++++++++++++++++++++----------- lib/unieml.h | 11 ++++--- 3 files changed, 129 insertions(+), 24 deletions(-) diff --git a/lib/lvleml.c b/lib/lvleml.c index 34c31e8..becf33e 100644 --- a/lib/lvleml.c +++ b/lib/lvleml.c @@ -168,6 +168,54 @@ return done; */ /************************************************/ /* */ +/* Procedure to set RCPT directive to */ +/* forward email to SMTP peers. */ +/* */ +/************************************************/ +static _Bool setdirectives(CONTYP *contact,char *ext) + +{ +#define OPEP "lvleml.c:setdirectives," + +_Bool status; +FILE *trans; +int phase; +_Bool proceed; + +status=false; +trans=(FILE *)0; +phase=0; +proceed=true; +while (proceed==true) { + //(void) rou_alert(0,"JMPDBG %s phase='%d'",OPEP,phase); + switch (phase) { + case 0 : //Creating the file + if ((trans=eml_createqfile(contact->cursesid,ext))==(FILE *)0) + phase=999; //trouble trouble + break; + case 1 : //write data to trans file; + break; + case 2 : //closing transfile + if (eml_closeqfile(trans)<0) + phase=999; //Trouble trouble + break; + case 3 : //everythin fine + status=true; + break; + default : //SAFE guard + proceed=false; + break; + } + phase++; + } +#undef OPEP +return status; +} +/* + +*/ +/************************************************/ +/* */ /* Procedure to accept EMAIL contents from */ /* SMTP peers. */ /* Return true, if everything is fine */ @@ -192,12 +240,12 @@ empty=false; queue=(FILE *)0; got=0; phase=0; -proceed=true; +proceed=setdirectives(contact,"tmp"); while (proceed==true) { //(void) rou_alert(0,"JMPDBG %s phase='%d' empty='%d'",OPEP,phase,empty); switch (phase) { case 0 : //opening the queue email - if ((queue=eml_opennewqueue(contact->cursesid))==(FILE *)0) + if ((queue=eml_createqfile(contact->cursesid,""))==(FILE *)0) phase=999; //trouble trouble break; case 1 : //Do we have a parameter @@ -219,9 +267,14 @@ while (proceed==true) { line=rou_freestr(line); break; case 4 : //got all data - if (eml_closenewqueue(queue)<0) + if (eml_closeqfile(queue)<0) + phase=999; //Trouble trouble + break; + case 5 : //renameing directive + if (eml_renameqfile(contact->cursesid,"tmp","trans")==false) phase=999; //Trouble trouble - case 5 : //everything fine + break; + case 6 : //everything fine (void) transmit(contact,"%d 3.5.3 %s", CMDOK,"Message accepted for delivery"); done=true; diff --git a/lib/unieml.c b/lib/unieml.c index 4f3a7ee..f7f2d30 100644 --- a/lib/unieml.c +++ b/lib/unieml.c @@ -6,6 +6,7 @@ /* */ /********************************************************/ #include +#include #include #include #include @@ -153,21 +154,55 @@ return done; */ /********************************************************/ /* */ -/* Procedure to open a file to store the email in */ -/* within the spool queue. */ +/* Procedure to change qfile extension */ +/* directory. */ /* */ /********************************************************/ -PUBLIC FILE *eml_opennewqueue(char *sessionid) +PUBLIC _Bool eml_renameqfile(char *qfilename,char *oldext,char *newext) { -#define OPEP "unieml.c:eml_opennewqueue," +#define OPEP "unieml.c:eml_renameqfile," -FILE *newqueue; +_Bool status; +char *filename; +char fold[300]; +char fnew[300]; + +status=true; +filename=rou_apppath(QDIR); +(void) snprintf(fold,sizeof(fold),"%s/%s.%s",filename,qfilename,oldext); +(void) snprintf(fnew,sizeof(fnew),"%s/%s.%s",filename,qfilename,newext); +if (rename(fold,fnew)<0) { + (void) rou_alert(0,"%s Unable to rename file <%s> to <%s> (error=<%s>)", + OPEP,fold,fnew,strerror(errno)); + status=false; + } +filename=rou_freestr(filename); +return status; +#undef OPEP +} +/* +^L +*/ +/********************************************************/ +/* */ +/* Procedure to open a file within the queue */ +/* directory. */ +/* */ +/********************************************************/ +PUBLIC FILE *eml_createqfile(char *qfilename,char *ext) + +{ +#define OPEP "unieml.c:eml_createqfile," + +FILE *qfile; +int handle; char *filename; int phase; int proceed; -newqueue=(FILE *)0; +qfile=(FILE *)0; +handle=-1; filename=(char *)0; phase=0; proceed=true; @@ -175,29 +210,43 @@ while (proceed==true) { //(void) rou_alert(0,"JMPDBG %s phase='%d'",OPEP,phase); switch (phase) { case 0 : //Do we have a session ID - if ((sessionid==(char *)0)||(strlen(sessionid)==0)) { + if ((qfilename==(char *)0)||(strlen(qfilename)==0)) { (void) rou_core_dump("%s sessionID is not set (bug?)",OPEP); phase=999; //never reached } break; case 1 : //"Computing" the file new queue filename filename=rou_apppath(QDIR); - filename=(char *)realloc(filename,strlen(filename)+strlen(sessionid)+10); + filename=(char *)realloc(filename,strlen(filename)+strlen(qfilename)+10); (void) strcat(filename,"/"); - (void) strcat(filename,sessionid); - if ((newqueue=fopen(filename,"w"))==(FILE *)0) { + (void) strcat(filename,qfilename); + if ((ext!=(char *)0)&&(strlen(ext)>0)) { + filename=(char *)realloc(filename,strlen(filename)+strlen(ext)+10); + (void) strcat(filename,"."); + (void) strcat(filename,ext); + } + break; + case 2 : //creating and opening the file + if ((handle=open(filename,O_EXCL|O_CREAT|O_WRONLY|O_TRUNC,0640))<0) { (void) rou_alert(0,"%s Unable to open file <%s> (error=<%s>)", OPEP,filename,strerror(errno)); + phase=999; //Trouble trouble + } + break; + case 3 : //convert handle to FILE * + if ((qfile=fdopen(handle,"w"))==(FILE *)0) { + (void) rou_alert(0,"%s Unable to fdopen file <%s> (error=<%s>)", + OPEP,filename,strerror(errno)); } - filename=rou_freestr(filename); break; default : //SAFE guard + filename=rou_freestr(filename); proceed=false; break; } phase++; } -return newqueue; +return qfile; #undef OPEP } /* @@ -209,14 +258,14 @@ return newqueue; /* stored within queue. */ /* */ /********************************************************/ -PUBLIC int eml_closenewqueue(FILE *newqueue) +PUBLIC int eml_closeqfile(FILE *qfile) { -#define OPEP "unieml.c:eml_closenewqueue," +#define OPEP "unieml.c:eml_closeqfile," int status; -if ((status=fclose(newqueue))<0) { - (void) rou_alert(0,"%s Unable to close queuefile (error=<%s>)", +if ((status=fclose(qfile))<0) { + (void) rou_alert(0,"%s Unable to close qfile (error=<%s>)", OPEP,strerror(errno)); } return status; diff --git a/lib/unieml.h b/lib/unieml.h index fb8e93a..6b40151 100644 --- a/lib/unieml.h +++ b/lib/unieml.h @@ -47,10 +47,13 @@ extern CODTYP eml_getcode(char *keyword); //remove CRLF from string extern int eml_removecrlf(char *string); -//procedure to open a queue file -extern FILE *eml_opennewqueue(char *sessid); +//procedure to change qfile extension +extern _Bool eml_renameqfile(char *qfilename,char *oldext,char *newext); -//procedure to open a queue file -extern int eml_closenewqueue(FILE *queue); +//procedure to open a file within queue directory +extern FILE *eml_createqfile(char *qfilename,char *ext); + +//procedure to close a file within the queue directory +extern int eml_closeqfile(FILE *qfile); #endif -- 2.47.3