From 0978c23e9588fcf2e40fb24182e803565e79bf3b Mon Sep 17 00:00:00 2001 From: "Jean-Marc Pigeon (Delson)" Date: Mon, 9 Jun 2025 07:00:24 -0400 Subject: [PATCH] Starting to send data email to remote --- lib/gestcp.c | 64 +++++++++++++++++++++++++++++++++++++++++++ lib/gestcp.h | 3 +++ lib/lvleml.c | 76 +++++++++++++++++++++++++++++++++++++++++++++++----- lib/unieml.c | 3 ++- lib/unieml.h | 3 ++- 5 files changed, 141 insertions(+), 8 deletions(-) diff --git a/lib/gestcp.c b/lib/gestcp.c index 7427ec5..f666582 100644 --- a/lib/gestcp.c +++ b/lib/gestcp.c @@ -183,3 +183,67 @@ va_end(args); (void) log_fprintlog(rmt->logptr,false,strloc); return tcp_get_smtp_reply(rmt,WAITRMT); } +/* +^L +*/ +/********************************************************/ +/* */ +/* Procedure to send a queued email data contents */ +/* to a remote server. */ +/* Return the number of character sent. */ +/* */ +/********************************************************/ +PUBLIC int tcp_send_smtp_data(RMTTYP *rmt,const char *qemail) + +{ +#define OPEP "gestcpc.c:tcp_send_smtp_data," + +int sent; +FILE *qfile; +int phase; +_Bool proceed; + +sent=-1; +qfile=(FILE *)0; +phase=0; +proceed=true; +while (proceed==true) { + switch (phase) { + case 0 : //opending email data file + if ((qfile=eml_openqfile(qemail,(const char *)0))==(FILE *)0) { + (void) rou_alert(0,"%s, Unable to open file <%s> (bug?)", + OPEP,qemail); + phase=999; //no need to go further + } + break; + case 1 : { //transmitting data to remote + _Bool empty; + char data[300]; + + sent=0; + empty=false; + while (fgets(data,sizeof(data),qfile)!=(char *)0) { + if ((empty==true)&&(strcmp(data,".")==0)) + (void) strcat(data,"."); + empty=(strlen(data)==0); + sent+=tcp_write(rmt->socptr,data); + (void) rou_alert(0,"%s JMPDBG data=<%s>",OPEP,data); + } + break; + } + case 2 : //transmitting and empty line + (void) tcp_write(rmt->socptr,""); + break; + case 3 : //closing datafile + qfile=eml_closeqfile(qfile); + break; + default : //SAFE Guard + proceed=false; + break; + } + phase++; + } +return sent; + +#undef OPEP +} diff --git a/lib/gestcp.h b/lib/gestcp.h index 191aaca..8115f96 100644 --- a/lib/gestcp.h +++ b/lib/gestcp.h @@ -36,4 +36,7 @@ extern int tcp_get_smtp_reply(RMTTYP *rmt,int wait); //Transmit a command to remote SMTP server extern int tcp_smtp_command(RMTTYP *rmt,char *fmt,...); +//send the email data +extern int tcp_send_smtp_data(RMTTYP *rmt,const char *qemail); + #endif diff --git a/lib/lvleml.c b/lib/lvleml.c index adac141..790f2b4 100644 --- a/lib/lvleml.c +++ b/lib/lvleml.c @@ -837,6 +837,60 @@ return done; */ /********************************************************/ /* */ +/* Procedure to send the data to the remote server */ +/* */ +/********************************************************/ +static int senddata(RMTTYP *rmt,TRATYP **tosend,const char *sessid) + +{ +#define OPEP "lvleml.c:senddata," + +int rspcode; +int phase; +_Bool proceed; + +rspcode=0; +phase=0; +proceed=true; +while (proceed==true) { + switch (phase) { + case 0 : //Sendig data request + rspcode=tcp_smtp_command(rmt,"DATA"); + switch (rspcode) { + case DATAOK : //data accepted + break; + default : //Unexpected return code + phase=999; //Not accepting data + break; + } + break; + case 1 : //sending session data + (void) tcp_send_smtp_data(rmt,sessid); + break; + case 2 : //sending session data + rspcode=tcp_smtp_command(rmt,"."); + switch (rspcode) { + case MALABRT : //Data rejected + break; + default : //Unexpected return code + break; + } + break; + default : //SAFE Guard + proceed=false; + break; + } + phase++; + } +return rspcode; + +#undef OPEP +} +/* +^L +*/ +/********************************************************/ +/* */ /* Procedure to send the whole email transaction */ /* */ /********************************************************/ @@ -847,6 +901,7 @@ static void sending_email(char *orgdomain,char *dstdomain,char *sessid,TRATYP ** time_t isnow; RMTTYP rmt; +int tobesend; TRATYP **tosend; int phase; _Bool proceed; @@ -857,6 +912,7 @@ rmt.mxs=dns_getmx(dstdomain); rmt.dstdomain=dstdomain; rmt.orgdomain=orgdomain; tosend=(TRATYP **)0; +tobesend=0; phase=0; proceed=true; while (proceed==true) { @@ -888,18 +944,19 @@ while (proceed==true) { } break; case 2 : //Checking if we have email to be sent + if (tosend==(TRATYP **)0) + phase=-1; //lets continue to see other email + break; + case 3 : { //checking if connection is open char cmt[100]; (void) snprintf(cmt,sizeof(cmt),"session-id=%s",sessid); rmt.logptr=log_openlog(sessid,false,cmt); - if (tosend==(TRATYP **)0) - phase=-1; //lets continue to see other email - break; - case 3 : //checking if connection is open if (rmt.socptr!=(SOCPTR *)0) { //reset the connection phase++; //NO need to open connection } break; + } case 4 : //Opening connection if not open if (connect_to_mx(&rmt)==false) { char cmt[100]; @@ -910,7 +967,7 @@ while (proceed==true) { phase=999; //No need to go further } break; - case 5 : //sending originator + case 5 : { //sending originator int rspcode; rspcode=tcp_smtp_command(&rmt,"MAIL FROM: <%s>",(*tosend)->mailfrom); @@ -924,14 +981,18 @@ while (proceed==true) { break; } break; - case 6 : //sending recipient list + } + case 6 : { //sending recipient list + int rspcode; TRATYP **dest; + tobesend=0; dest=tosend; while (*dest!=(TRATYP *)0) { rspcode=tcp_smtp_command(&rmt,"RCPT TO: <%s>",(*dest)->rcptto); switch (rspcode) { case CMDOK : //originator accepted + tobesend++; break; default : //Not accepted recipient break; @@ -939,7 +1000,10 @@ while (proceed==true) { dest++; } break; + } case 7 : //send email data + if (tobesend>0) + (void) senddata(&rmt,tosend,sessid); break; case 8 : //cleaning recipient list (void) free(tosend); diff --git a/lib/unieml.c b/lib/unieml.c index b259a4f..f320e89 100644 --- a/lib/unieml.c +++ b/lib/unieml.c @@ -18,6 +18,7 @@ #include "subrou.h" #include "unieml.h" +//defining queued email repository #define QDIR "/var/spool/"APPNAME"/queue" typedef struct { @@ -507,7 +508,7 @@ return dnames; /* directory. */ /* */ /********************************************************/ -PUBLIC FILE *eml_openqfile(char *qfilename,const char *ext) +PUBLIC FILE *eml_openqfile(const char *qfilename,const char *ext) { #define OPEP "unieml.c:eml_openqfile," diff --git a/lib/unieml.h b/lib/unieml.h index a1ed727..9016176 100644 --- a/lib/unieml.h +++ b/lib/unieml.h @@ -20,6 +20,7 @@ #define BADPAR 501 //error in parameters #define CMDBAD 502 //command not implemented #define DATRJC 521 //Data Rejected +#define MALABRT 552 //email data rejected #define NOTEML 553 //Not an email address #define MISSMX 563 //NO MX found for recipient @@ -102,7 +103,7 @@ extern FILE *eml_closeqfile(FILE *qfile); extern char **eml_getqfilelist(char **dnames,const char *ext); //procedure to open a specific qfile -extern FILE *eml_openqfile(char *qfilename,const char *ext); +extern FILE *eml_openqfile(const char *qfilename,const char *ext); //procedure to duplicate a specific qfile to another file extern _Bool eml_dupqfile(char *qfilename,const char *dest); -- 2.47.3