]> SAFE projects GIT repository - jmp/mailleur/commitdiff
Implementing unix time conversion from/to database format
authorJean-Marc Pigeon (Delson) <jmp@safe.ca>
Sat, 12 Jul 2025 14:52:50 +0000 (10:52 -0400)
committerJean-Marc Pigeon (Delson) <jmp@safe.ca>
Sat, 12 Jul 2025 14:52:50 +0000 (10:52 -0400)
13 files changed:
Makefile
lib/Makefile
lib/devsql.c
lib/devsql.h
lib/gessql.c
lib/unimar.c
lib/unimar.h
lib/unipos.c
lib/unipos.h
lib/unisql.c
lib/unisql.h
sql/datatest.sql
sql/mailleur.sql

index 87b31a6f198adec50910fe2e80c66e28a88ceb6c..8e63b3b66c22d97fd0117f02fb3ee8f5bfc47ee5 100644 (file)
--- 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
index 0f5cfe216b3938aaf39c95e91df944660bc1dfe8..9617ca36fd9f2bc88e6934ac365ba4ae0a0b27a2 100644 (file)
@@ -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                    \
index f24686c951b414e3d83f918a8be2481be720ad91..c4be9ffddae02a7704f637ecec92b0642a96d298 100644 (file)
@@ -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
 }
 /*
index 4c5d5defcd42fb55346031f203f6e49efcf7b05e..6fc45705b0f29161b81022cb74d280e84a327d95 100644 (file)
@@ -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);
index 12ea3341461855db39a57d91f6ffe9b279e80a5d..1fb6834bff405cc21e07a7246c124faf2f3c6707 100644 (file)
@@ -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?)",
index af502f317629afb3713c6731366f543e9752ff51..e8cb7d2f8f9053b1790bcf635b2f594f8629a3d2 100644 (file)
@@ -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(&timestamp);
+ (void) strftime(unixdate,sizeof(unixdate),TSTAMP,tminfo);
+ }
+#endif
+
+return unixdate;
+}
+/*
+\f
+*/
+/********************************************************/
+/*                                                     */
+/*      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
+}
+/*
+\f
+*/
+/********************************************************/
+/*                                                     */
 /*      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;
index ca6b61f924927a29783c890d919c5a2b2614917c..babbf389d70565337a2d8aa1bb60c2861c7c7ade 100644 (file)
@@ -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);
 
index ec7fccf142fc79ace26d221ac5b053e56ccd5018..40105c7c45d98f833114e09af4b2c6c24e05f52f 100644 (file)
@@ -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(&timestamp);
+ (void) strftime(unixdate,sizeof(unixdate),TSTAMP,tminfo);
+ }
+#endif
+
+return unixdate;
+}
+/*
+\f
+*/
+/********************************************************/
+/*                                                     */
+/*      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
+}
+/*
+\f
+*/
+/********************************************************/
+/*                                                     */
 /*      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;
index f695869e62b37bd184d9db12c9519e082644b149..f2d4e4d6d4f32e7cf7ae57ad45ae0eb365879ad9 100644 (file)
@@ -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);
 
index b3aba123990023c805e9a3a9ff2863360b4164f2..a413ea7ceb6c7b0fe99d44ed5b2c78660b4d886f 100644 (file)
@@ -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;
index 8c30dcd95b083135ee5d5c8ad81db53b2a6c1365..33c2a611622b5f6c5bd39f5022e4170922a9273e 100644 (file)
@@ -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
index 9e746728242f234019da447958613e0b3f8f567c..1bcaba30589727775c81f329ad96bddb90b7d317 100644 (file)
@@ -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)                                 \
index 5de459c1ec4e374eb3dc4c55aca7e0dfb573d961..42cdce15fdad7564e5cfa0eb4a1a98c99f3f1657 100644 (file)
@@ -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