]> SAFE projects GIT repository - jmp/mailleur/commitdiff
gesql.c include sql_mngusr function
authorJean-Marc Pigeon (Delson) <jmp@safe.ca>
Fri, 11 Jul 2025 10:28:10 +0000 (06:28 -0400)
committerJean-Marc Pigeon (Delson) <jmp@safe.ca>
Fri, 11 Jul 2025 10:28:10 +0000 (06:28 -0400)
lib/devsql.c
lib/devsql.h
lib/gessql.c
lib/gessql.h
lib/unisql.h

index ba8b497e9a0d75d551d1356a76f4025b9d343553..1b54805d13e4c45e3f51d3e496c9912f01af3d89 100644 (file)
@@ -242,114 +242,6 @@ return sql;
 */
 /********************************************************/
 /*                                                      */
-/*      Procedure to select a user from emails tables   */
-/*      and return contents;                            */
-/*                                                      */
-/********************************************************/
-static USRTYP *select_user(SQLTYP *sql,char *email)
-
-{
-#define OPEP    "devsql.c:select_user,"
-#define SELUSR  "SELECT * FROM "EMAILS" WHERE email=%s"
-
-//field available in table "emails"
-static const FLDTYP usrfield[]={
-          {1,"email"},
-          {2,"password"},
-          {3,"hash"},
-          {4,"space"},
-          {5,"mxspace"},
-          {6,"locked"},
-          {0,(char *)0}
-          };
-
-USRTYP *usr;
-SQLRES *rs;
-char cmd[300];
-register int phase;
-register _Bool proceed;
-
-usr=(USRTYP *)0;
-rs=(SQLRES *)0;
-(void) snprintf(cmd,sizeof(cmd),SELUSR,email);
-phase=0;
-proceed=true;
-while (proceed==true) {
-  switch (phase) {
-    case 0      :       //Selecting user
-      if ((rs=gettupple(sql,cmd))==(SQLRES *)0) {
-        (void) rou_alert(0,"%s Unable to get data for user <%s> (Database?)",
-                            OPEP,email);
-        phase=999;      //user not found within database 
-        }
-      break;
-    case 1      :       //do we have ONE user
-      int nbr;
-
-      nbr=nbrtupple(sql,rs);
-      switch (nbr) {
-        case 0  :       //No user email found
-          phase=999;    //this could append
-          break;
-        case 1  :       //we have only one record, found user
-          break;
-        default :      //such case should never ever happen
-          (void) rou_alert(0,"<%s> return '%d' record %s (Database corrupted?)",
-                              cmd,nbr,"while only 1 is expected");
-          phase=999;
-          break;
-        }
-      break;
-    case 2      :       //user data extraction
-      usr=(USRTYP *)calloc(1,sizeof(USRTYP));
-      for (int i=0;usrfield[i].num!=0;i++) {
-        char *locval;
-      
-        if ((locval=getvalue(sql,rs,0,usrfield[i].name))==(char *)0)
-          continue;
-        switch (usrfield[i].num) {
-          case 1        :       //user email
-            usr->email=strdup(locval);
-            break;
-          case 2        :       //User crypted password
-            usr->password=strdup(locval);
-            break;
-          case 3        :       //'email:realm:password' MD5
-            usr->hash=strdup(locval);
-            break;
-          case 4        :       //user used space
-            usr->space=atoi(locval);
-            break;
-          case 5        :       //user max space available
-            usr->mxspace=atoi(locval);
-            break;
-          case 6        :       //lock status
-            usr->lock=atoi(locval);
-            break;
-          default       :
-            (void) rou_alert(0,"%s field <%d:%s> not implemented (Bug?)",
-                                   OPEP,usrfield[i].num,usrfield[i].name);
-            break;
-          }
-        }
-      break;
-    default     :       //SAFE Guard
-      rs=dropresult(sql,rs);
-      proceed=false;
-      break;
-    }
-  phase++;
-  }
-return usr;
-
-#undef  SELUSR
-#undef  OPEP
-}
-/*
-\f
-*/
-/********************************************************/
-/*                                                      */
 /*      Procedure to establish a link with the          */
 /*      designated SQL server.                          */
 /*                                                      */
@@ -594,65 +486,6 @@ if ((rou_vasprintf(&cmd,fmt,args))>0) {
 va_end(args);
 return number;
 
-#undef  OPEP
-}
-/*
-\f
-*/
-/********************************************************/
-/*                                                      */
-/*      Procedure to retreive information about user    */
-/*      known within database.                          */
-/*                                                      */
-/********************************************************/
-PUBLIC _Bool sql_mngusr(SQLPTR *sqlptr,SQLENUM action,char *email,USRTYP **usr)
-
-{
-#define OPEP    "devsql.c:sql_mngusr,"
-
-_Bool isok;
-char *gooddata;
-SQLTYP *sql;
-int phase;
-_Bool proceed;
-
-isok=false;
-gooddata=(char *)0;
-sql=(SQLTYP *)sqlptr;
-phase=0;
-proceed=true;
-while (proceed==true) {
-  switch (phase) {
-    case 0      :       //checking SQL
-      if (sql==(SQLTYP *)0) {
-        (void) rou_alert(0,"%s SQL pointer is NUll (Bug?)",OPEP);
-        phase=999;
-        }
-      if ((gooddata=sql_gooddata(sql,email))==(char *)0) {
-        (void) rou_alert(0,"%s %s table, key <%s> is empty (Bug?!)",
-                            OPEP,EMAILS,email);
-        phase=999;
-        }
-      break;
-    case 1      :       //getting user information
-      switch (action) {
-        case sql_select :
-          if ((*usr=select_user(sql,gooddata))!=(USRTYP *)0)
-            isok=true;
-          break;
-        default :
-          (void) rou_alert(0,"%s action='%d' not yet implemented!",OPEP,action);
-        }
-      gooddata=rou_freestr(gooddata);
-      break;
-    default     :
-      proceed=false;
-      break;
-    }
-  phase++;
-  }
-return isok;
-
 #undef  OPEP
 }
 /*
index e62bde02c9622403f80c84a0b57dd06d708b5eb0..afa5e329d3f418275470e0362b25255ca3ebe636 100644 (file)
 
 #include        "unisql.h"
 
-//reference to a SQL pointer reference
+//An SQL database pointer reference
 typedef void SQLPTR;
 
+//An SQL database result reference
+typedef void SQLRES;
+
 //to connect a remote SQL server
 extern SQLPTR *sql_opensql();
 
@@ -23,15 +26,19 @@ extern SQLPTR *sql_closesql(SQLPTR *sqlptr);
 //as key search by database
 extern char *sql_gooddata(SQLPTR *sqlptr,char *key);
 
-//procedure to manage information on exiting user
-extern _Bool sql_mngusr(SQLPTR *sqlptr,SQLENUM action,char *email,USRTYP **usr);
+//procedure to transmit a simple data-base action
+extern int sql_request(SQLPTR *sqlptr,const char *fmt,...);
+
+//to extract data from database
+extern char *sql_getvalue(SQLPTR *sqlptr,SQLRES *rs,int row,char *name);
 
-//procedure to manage information on email exchange session
-extern _Bool sql_mngses(SQLPTR *sqlptr,SQLENUM action,SESTYP **ses);
+//to extract data from database
+extern SQLRES *sql_gettupple(SQLPTR *sqlptr,const char *fmt,...);
 
-//procedure to manage action stats on email exhange status
-extern _Bool sql_mngact(SQLPTR *sqlptr,SQLENUM action,ACTTYP *act);
+//to retreive the number of tupple available within result
+extern int sql_getnbrtupple(SQLPTR *sqlptr,SQLRES *);
 
-extern int sql_request(SQLPTR *sqlptr,const char *fmt,...);
+//to free memory used by a database tupple
+extern SQLRES *sql_droptupple(SQLPTR *sqlptr,SQLRES *res);
 
 #endif
index 03f22c08b075ac0aaf908d77cc870f556e3603a3..0df04f59a4f3b81f612add5e609df6bf95a24b09 100644 (file)
@@ -4,6 +4,8 @@
 /*     Module to manage complexe SQL request           */
 /*                                                     */
 /********************************************************/
+#include        <malloc.h>
+#include        <stdlib.h>
 #include        <string.h>
 
 #include        "subrou.h"
 #include        "gessql.h"
 
 //DATABASE Table names
+#define EMLTBL  "emails"        //Table about user list
 #define RMTTBL  "remotes"       //Table about remotes site
 #define ACTTBL  "actions"       //action tables
 #define SESTBL  "sessions"      //session tables
 
+/*
+\f
+*/
+/********************************************************/
+/*                                                      */
+/*      Procedure to select a user from emails tables   */
+/*      and return contents;                            */
+/*                                                      */
+/********************************************************/
+static _Bool select_user(SQLPTR *sqlptr,char *email,USRTYP **usr)
+
+{
+#define OPEP    "gessql.c:select_user,"
+
+static const char *sel="SELECT * FROM "EMLTBL" WHERE email=%s";
+
+//field available in table "emails"
+static const FLDTYP usrfield[]={
+          {1,"email"},
+          {2,"password"},
+          {3,"hash"},
+          {4,"space"},
+          {5,"mxspace"},
+          {6,"locked"},
+          {0,(char *)0}
+          };
+
+_Bool isok;
+USRTYP *locusr;
+SQLRES *rs;
+register int phase;
+register _Bool proceed;
+
+isok=false;
+locusr=(USRTYP *)0;
+rs=(SQLRES *)0;
+phase=0;
+proceed=true;
+while (proceed==true) {
+  switch (phase) {
+    case 0      :       //checking parameters
+      if ((usr==(USRTYP **)0)||(email=(char *)0)||(strlen(email)==0)) {
+        (void) rou_alert(0,"%s usr=<%p> or email=<%s> sing (Bug?)",OPEP,usr,email);
+        phase=999;
+        }
+      break;
+    case 1      :       //Selecting user
+      if ((rs=sql_gettupple(sqlptr,sel,email))==(SQLRES *)0) {
+        (void) rou_alert(0,"%s Unable to get data for user <%s> (Database?)",
+                            OPEP,email);
+        phase=999;      //user not found within database 
+        }
+      break;
+    case 2      :       //do we have ONE user
+      int nbr;
+
+      nbr=sql_getnbrtupple(sqlptr,rs);
+      switch (nbr) {
+        case 0  :       //No user email found
+          phase=999;    //this could append
+          break;
+        case 1  :       //we have only one record, found user
+          break;
+        default :      //such case should never ever happen
+          (void) rou_alert(0,"%s got '%d' users record %s (Database corrupted?)",
+                             OPEP,nbr,"while only 1 is expected");
+          phase=999;
+          break;
+        }
+      break;
+    case 3      :       //user data extraction
+      isok=true;
+      locusr=(USRTYP *)calloc(1,sizeof(USRTYP));
+      for (int i=0;i<(sizeof(usrfield)/sizeof(FLDTYP))&&(isok==true);i++) {
+        char *locval;
+      
+        if ((locval=sql_getvalue(sqlptr,rs,0,usrfield[i].name))==(char *)0)
+          continue;
+        switch (usrfield[i].num) {
+          case 1        :       //user email
+            locusr->email=strdup(locval);
+            break;
+          case 2        :       //User crypted password
+            locusr->password=strdup(locval);
+            break;
+          case 3        :       //'email:realm:password' MD5
+            locusr->hash=strdup(locval);
+            break;
+          case 4        :       //user used space
+            locusr->space=atoi(locval);
+            break;
+          case 5        :       //user max space available
+            locusr->mxspace=atoi(locval);
+            break;
+          case 6        :       //lock status
+            locusr->lock=atoi(locval);
+            break;
+          default       :
+            (void) rou_alert(0,"%s field <%d:%s> not implemented (Bug?)",
+                                   OPEP,usrfield[i].num,usrfield[i].name);
+            locusr=sql_freeusr(locusr);
+            isok=false;
+            break;
+          }
+        }
+      break;
+    default     :       //SAFE Guard
+      rs=sql_droptupple(sqlptr,rs);
+      proceed=false;
+      break;
+    }
+  phase++;
+  }
+*usr=locusr;
+return isok;
+
+#undef  OPEP
+}
 /*
 \f
 */
@@ -94,6 +215,68 @@ if (isok==true)
 cmtset=rou_freestr(cmtset);
 return isok;
 
+#undef  OPEP
+}
+/*
+\f
+*/
+/********************************************************/
+/*                                                      */
+/*      Procedure to retreive information about user    */
+/*      known within database.                          */
+/*                                                      */
+/********************************************************/
+PUBLIC _Bool sql_mngusr(SQLPTR *sqlptr,SQLENUM action,char *key,USRTYP **usr)
+
+{
+#define OPEP    "gessql.c:sql_mngusr,"
+
+_Bool isok;
+char *gooddata;
+int phase;
+_Bool proceed;
+
+isok=false;
+gooddata=(char *)0;
+phase=0;
+proceed=true;
+while (proceed==true) {
+  switch (phase) {
+    case 0      :       //checking SQL and user record
+      if (sqlptr==(SQLPTR *)0) {
+        (void) rou_alert(0,"%s SQL pointer is NULL (Bug?)",OPEP);
+        phase=999;
+        }
+      if (usr==(USRTYP **)0) {
+        (void) rou_alert(0,"%s USER pointer is NULL (Bug?)",OPEP);
+        phase=999;
+        }
+      break;
+    case 1      :       //do we have a good key
+      if ((gooddata=sql_gooddata(sqlptr,key))==(char *)0) {
+        (void) rou_alert(0,"%s %s table, key <%s> is empty (Bug?!)",
+                            OPEP,EMLTBL,key);
+        phase=999;
+        }
+      break;
+    case 2      :       //getting user information
+      switch (action) {
+        case sql_select :
+          isok=select_user(sqlptr,gooddata,usr);
+          break;
+        default :
+          (void) rou_alert(0,"%s action='%d' not yet implemented!",OPEP,action);
+        }
+      gooddata=rou_freestr(gooddata);
+      break;
+    default     :
+      proceed=false;
+      break;
+    }
+  phase++;
+  }
+return isok;
+
 #undef  OPEP
 }
 /*
@@ -108,7 +291,7 @@ return isok;
 PUBLIC _Bool sql_mngses(SQLPTR *sqlptr,SQLENUM action,SESTYP **ses)
 
 {
-#define OPEP    "devsql.c:sql_mngses,"
+#define OPEP    "gessql.c:sql_mngses,"
 
 static const char *ins="INSERT INTO "SESTBL" (sessid) VALUES(%s)";
 
@@ -170,7 +353,7 @@ return isok;
 PUBLIC _Bool sql_mngact(SQLPTR *sqlptr,SQLENUM action,ACTTYP *act)
 
 {
-#define OPEP    "devsql.c:sql_mngact,"
+#define OPEP    "gessql.c:sql_mngact,"
 
 static const char *del="DELETE FROM "ACTTBL" WHERE sessid=%s AND rcptto=%s";
 static const char *ins="INSERT INTO "ACTTBL" (%s) VALUES(%s,%s,%s,%d,%s)";
index 4da35059eb38ac6b73fcbbfde191e7ce0a2f52ba..64b44eef27b5e2865e2f0c21c9201f334e5de7fd 100644 (file)
 
 #include        "devsql.h"
 
+//procedure to manage information on exiting user
+extern _Bool sql_mngusr(SQLPTR *sqlptr,SQLENUM action,char *key,USRTYP **usr);
+
 //procedure to manage action stats on email exhange status
 extern _Bool sql_mngact(SQLPTR *sqlptr,SQLENUM action,ACTTYP *act);
 
+//procedure to manage information on email exchange session
+extern _Bool sql_mngses(SQLPTR *sqlptr,SQLENUM action,SESTYP **ses);
+
 //Procedure to increment (or decrement) the remoteip
 //connection number
 extern _Bool sql_newconnect(SQLPTR *sqlptr,char *rmtip,int delta);
index e37fd7d79a1434507363d03b87c9176637f9e1b1..ae9932348e2ea0c47b7b7758b8a2566646737af8 100644 (file)
@@ -17,21 +17,10 @@ typedef enum    {
         sql_uknown      //No action
         }SQLENUM;
 
-typedef enum    {
-        row_integer,    //row data is an integer
-        row_char,       //row data is a char
-        row_unknown     //row data unsoecifier
-        }FLDENUM;
-
 //defining a database column definition retrieval
 typedef struct  {
         int num;        //field number
         char *name;     //field name;
-        FLDENUM specie; //field type 
-        union   {
-          u_long entier;//data is a number
-          char *string; //data is a string
-          }data;
         }FLDTYP;
 
 //structure about user within the database