From: Jean-Marc Pigeon (Delson) Date: Sat, 12 Jul 2025 14:52:50 +0000 (-0400) Subject: Implementing unix time conversion from/to database format X-Git-Tag: tag-0.14~121 X-Git-Url: https://jmp-git.ovh.safe.ca/?a=commitdiff_plain;h=607cd0a713a272f6699baecab5deab909581ca6c;p=jmp%2Fmailleur Implementing unix time conversion from/to database format --- diff --git a/Makefile b/Makefile index 87b31a6..8e63b3b 100644 --- a/Makefile +++ b/Makefile @@ -15,7 +15,7 @@ debug \ : @ for i in $(SUBDIR) ; \ do \ - $(MAKE) DB=$(DB) -s -C $$i $@ ; \ + $(MAKE) DB=$(DB) -C $$i $@ ; \ done @ #ln -nsf rcvrpsql bin/receiver @ #ln -nsf sndrpsql bin/sender diff --git a/lib/Makefile b/lib/Makefile index 0f5cfe2..9617ca3 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -166,6 +166,7 @@ libpos.a: unimar.o \ subrou.h \ unipos.h unipos.c @ $(CC) \ + $(CFLAGS) \ $(CPPFLAGS) \ -DDATABASE=1 \ -Dwith_postgres \ @@ -178,6 +179,7 @@ libmar.a: unipos.o \ subrou.h \ unimar.h unimar.c @ $(CC) \ + $(CFLAGS) \ $(CPPFLAGS) \ -DDATABASE=2 \ -Dwith_mysql \ diff --git a/lib/devsql.c b/lib/devsql.c index f24686c..c4be9ff 100644 --- a/lib/devsql.c +++ b/lib/devsql.c @@ -303,6 +303,82 @@ while (proceed==true) { phase++; } return (SQLPTR *)sql; +#undef OPEP +} +/* +^L +*/ +/********************************************************/ +/* */ +/* Procedure to return a unix time in acsii */ +/* format (data-base compatible). */ +/* */ +/********************************************************/ +PUBLIC const char *sql_fromunixtime(SQLPTR *sqlptr,time_t timestamp) + +{ +#define OPEP "devsql.c:sql_fromunixtime," + +const char *unixdate; + +unixdate=""; +if (sqlptr!=(SQLPTR *)0) { + SQLTYP *sql; + + sql=(SQLTYP *)sqlptr; + switch(sql->sqldb) { + case db_postgres : + unixdate=pos_fromunixtime(timestamp); + break; + case db_maria : + unixdate=mar_fromunixtime(timestamp); + break; + default : + (void) rou_alert(0,"%s Unexpected db type='%d' (BUG!?)", + OPEP,(int)sql->sqldb); + break; + } + } +return unixdate; + +#undef OPEP +} +/* +^L +*/ +/********************************************************/ +/* */ +/* Procedure to return a unix time_t from an ASCII*/ +/* string (data-base compatible). */ +/* */ +/********************************************************/ +PUBLIC time_t sql_tounixtime(SQLPTR *sqlptr,char *date) + +{ +#define OPEP "devsql.c:sql_tounixtime," + +time_t datetime; + +datetime=(time_t)0; +if (sqlptr!=(SQLPTR *)0) { + SQLTYP *sql; + + sql=(SQLTYP *)sqlptr; + switch(sql->sqldb) { + case db_postgres : + datetime=pos_tounixtime(date); + break; + case db_maria : + datetime=mar_tounixtime(date); + break; + default : + (void) rou_alert(0,"%s Unexpected db type='%d' (BUG!?)", + OPEP,(int)sql->sqldb); + break; + } + } +return datetime; + #undef OPEP } /* diff --git a/lib/devsql.h b/lib/devsql.h index 4c5d5de..6fc4570 100644 --- a/lib/devsql.h +++ b/lib/devsql.h @@ -26,8 +26,14 @@ extern SQLPTR *sql_opensql(); //procedure to close an previously opened SQL channel extern SQLPTR *sql_closesql(SQLPTR *sqlptr); +//converting a time to a database representation +extern const char *sql_fromunixtime(SQLPTR *sqlptr,time_t timestamp); + +//converting a database time representation to unix time +extern time_t sql_tounixtime(SQLPTR *sqlptr,char *date); + //procedure to LOCK access to a database table -extern _Bool sql_lock(SQLPTR *sqlptr,char *tablename); +extern _Bool sql_lock(SQLPTR *sqlPTR,char *tablename); //procedure to UNLOCK access to a database table extern _Bool sql_unlock(SQLPTR *sqlptr,_Bool commit); diff --git a/lib/gessql.c b/lib/gessql.c index 12ea334..1fb6834 100644 --- a/lib/gessql.c +++ b/lib/gessql.c @@ -272,7 +272,7 @@ static const FLDTYP usrfield[]={ {3,"lastupdate"}, {4,"links"}, {5,"credit"}, - {6,"status"}, + {6,"listing"}, {0,(char *)0} }; @@ -340,7 +340,7 @@ while (proceed==true) { locsrv->credit=atoi(locval); break; case 6 : //scanning status - locsrv->status=strdup(locval); + locsrv->listing=strdup(locval); break; default : (void) rou_alert(0,"%s field <%d:%s> not implemented (Bug?)", diff --git a/lib/unimar.c b/lib/unimar.c index af502f3..e8cb7d2 100644 --- a/lib/unimar.c +++ b/lib/unimar.c @@ -153,6 +153,84 @@ return cleanstr; */ /********************************************************/ /* */ +/* Procedure to return a unix time in acsii */ +/* format (data-base compatible). */ +/* */ +/********************************************************/ +PUBLIC const char *mar_fromunixtime(time_t timestamp) + +{ +#define TSTAMP "%Y-%m-%d %H:%M:%S" + +static char unixdate[50]; + +(void) memset(unixdate,'\000',sizeof(unixdate)); + +#ifdef DB_MYSQL +/*Note: + *syntax MYSQL ("FROM_UNIXTIME(%ld)",timestamp) + */ + { + struct tm *tminfo; + + tminfo=localtime(×tamp); + (void) strftime(unixdate,sizeof(unixdate),TSTAMP,tminfo); + } +#endif + +return unixdate; +} +/* + +*/ +/********************************************************/ +/* */ +/* Procedure to return a unix time_t from an ASCII*/ +/* string (data-base compatible). */ +/* */ +/********************************************************/ +PUBLIC time_t mar_tounixtime(char *date) + +{ +#define OPEP "unimar.c:mar_tounixtime," + +time_t datetime; + +datetime=(time_t)0; +#ifdef DB_MYSQL + { + static const char *stampeon="0000-00-00 00:00:00"; + + if ((date!=(char *)0)&&(strcmp(date,stampeon)!=0)) { + static const char *dbtounix="%Y-%m-%d %H:%M:%S"; + + char *ptr; + struct tm tm; + + if ((ptr=strchr(date,'.'))!=(char *)0) { + *ptr='\000'; + } + (void) memset(&tm,'\000',sizeof(struct tm)); + tm.tm_isdst=-1; + if (strptime(date,dbtounix,&tm)!=(char *)0) { + datetime=mktime(&tm); + } + else { + (void) rou_alert(0," Unable to convert <%s> to time_t (Bug?)",OPEP,date); + } + } + } +#endif + +return datetime; + +#undef OPEP +} +/* + +*/ +/********************************************************/ +/* */ /* procedure to lock access to a specific table */ /* within database. */ /* */ @@ -231,7 +309,7 @@ unlocked=false; unlocked=true; cmd="ROLLBACK"; - if (commit==true); + if (commit==true) cmd="COMMIT"; if (mar_request(marptr,cmd)<0) { unlocked=false; diff --git a/lib/unimar.h b/lib/unimar.h index ca6b61f..babbf38 100644 --- a/lib/unimar.h +++ b/lib/unimar.h @@ -23,6 +23,12 @@ extern MARPTR *mar_closesql(MARPTR *marptr); //Procedure to detect and 'clean' any single quote within a string extern char *mar_cleanquote(char *sequence); +//converting a time to a database representation +extern const char *mar_fromunixtime(time_t timestamp); + +//converting a database time representation to unix time +extern time_t mar_tounixtime(char *date); + //locking database one table access extern _Bool mar_lock(MARPTR *marptr,char *tablename); diff --git a/lib/unipos.c b/lib/unipos.c index ec7fccf..40105c7 100644 --- a/lib/unipos.c +++ b/lib/unipos.c @@ -201,6 +201,79 @@ return cleanstr; */ /********************************************************/ /* */ +/* Procedure to return a unix time in acsii */ +/* format (data-base compatible). */ +/* */ +/********************************************************/ +PUBLIC const char *pos_fromunixtime(time_t timestamp) + +{ +#define TSTAMP "%Y-%m-%d %H:%M:%S" + +static char unixdate[50]; + +(void) memset(unixdate,'\000',sizeof(unixdate)); + +#ifdef DB_POSTGRESQL + { + struct tm *tminfo; + + tminfo=localtime(×tamp); + (void) strftime(unixdate,sizeof(unixdate),TSTAMP,tminfo); + } +#endif + +return unixdate; +} +/* + +*/ +/********************************************************/ +/* */ +/* Procedure to return a unix time_t from an ASCII*/ +/* string (data-base compatible). */ +/* */ +/********************************************************/ +PUBLIC time_t pos_tounixtime(char *date) + +{ +#define OPEP "unipos.c:pos_tounixtime," + +time_t datetime; + +datetime=(time_t)0; +#ifdef DB_POSTGRESQL + { + if (date!=(char *)0) { + static const char *dbtounix="%Y-%m-%d %H:%M:%S"; + + char *ptr; + struct tm tm; + + if ((ptr=strchr(date,'.'))!=(char *)0) { + *ptr='\000'; + } + (void) memset(&tm,'\000',sizeof(struct tm)); + tm.tm_isdst=-1; + if (strptime(date,dbtounix,&tm)!=(char *)0) { + datetime=mktime(&tm); + } + else { + (void) rou_alert(0," Unable to convert <%s> to time_t (Bug?)",OPEP,date); + } + } + } +#endif + +return datetime; + +#undef OPEP +} +/* + +*/ +/********************************************************/ +/* */ /* procedure to lock access to a specific table */ /* within database. */ /* */ @@ -279,7 +352,7 @@ unlocked=false; unlocked=true; cmd="ROLLBACK"; - if (commit==true); + if (commit==true) cmd="COMMIT"; if (pos_request(posptr,cmd)<0) { unlocked=false; diff --git a/lib/unipos.h b/lib/unipos.h index f695869..f2d4e4d 100644 --- a/lib/unipos.h +++ b/lib/unipos.h @@ -23,6 +23,12 @@ extern POSPTR *pos_closesql(POSPTR *posptr); //Procedure to detect and 'clean' any single quote within a string extern char *pos_cleanquote(char *sequence); +//converting a time to a database representation +extern const char *pos_fromunixtime(time_t timestamp); + +//converting a database time representation to unix time +extern time_t pos_tounixtime(char *date); + //locking database one table access extern _Bool pos_lock(POSPTR *posptr,char *tablename); diff --git a/lib/unisql.c b/lib/unisql.c index b3aba12..a413ea7 100644 --- a/lib/unisql.c +++ b/lib/unisql.c @@ -150,7 +150,7 @@ PUBLIC SRVTYP *sql_freesrv(SRVTYP *smtpdata) { if (smtpdata!=(SRVTYP *)0) { - smtpdata->status=rou_freestr(smtpdata->status); + smtpdata->listing=rou_freestr(smtpdata->listing); smtpdata->rmtip=rou_freestr(smtpdata->rmtip); (void) free(smtpdata); smtpdata=(SRVTYP *)0; diff --git a/lib/unisql.h b/lib/unisql.h index 8c30dcd..33c2a61 100644 --- a/lib/unisql.h +++ b/lib/unisql.h @@ -60,7 +60,7 @@ typedef struct { time_t lupdate; //last update time int links; //Number of connection from this remote int credit; //Remote credit number - char *status; //Remote record current status + char *listing; //Remote listing explaination }SRVTYP; //procedure to free space used by an USRTYP diff --git a/sql/datatest.sql b/sql/datatest.sql index 9e74672..1bcaba3 100644 --- a/sql/datatest.sql +++ b/sql/datatest.sql @@ -29,8 +29,8 @@ INSERT INTO emails (email,password) \ values ('user2@mardb.example.com','user2'); //List of remote IP status -INSERT INTO remotes (remoteip,credit,status) \ - values ('127.127.0.25',10,'valide'); +INSERT INTO remotes (remoteip,credit,lastscan) \ + values ('127.127.0.25',10,now()); //--------------------------------------------------------------- //selected IP from 'known' spammer INSERT INTO remotes (remoteip) \ diff --git a/sql/mailleur.sql b/sql/mailleur.sql index 5de459c..42cdce1 100644 --- a/sql/mailleur.sql +++ b/sql/mailleur.sql @@ -102,22 +102,20 @@ GRANT SELECT ON sessions TO mailapache; //defining table about remote server CREATE TABLE remotes ( remoteip TEXTUNIQUE, //remote IP number - lastscan DBTIMESTAMP //record laste update - DFLT NOW(), + lastscan DBTIMESTAMP //record last update + DFLT NULL, lastupdate DBTIMESTAMP //record creation DFLT NOW(), credit INTEGER //Remote IP current credit (-100..+100) DFLT -100, - status TEXT //'toscan','inscan','valide' - DFLT 'toscan', - listed TEXT //explaination obout black listing + listing TEXT //explaination obout black listing DFLT NULL, links INTEGER DFLT 1 //how many time the remote connected ); -CREATE INDEX remotes_ndx ON remotes (INS(status)); -INSERT INTO remotes (remoteip,credit,status) \ - values ('127.0.0.1',99,'valide'); +CREATE INDEX remotes_ndx ON remotes(lastscan); +INSERT INTO remotes (remoteip,credit,lastscan) \ + values ('127.0.0.1',99,now()); //-------------------------------------------------------------- //Defining TRIGGER fonctions according database type