From: Jean-Marc Pigeon (Delson) Date: Fri, 27 Jun 2025 13:34:03 +0000 (-0400) Subject: Defining sql_mngusr procedure X-Git-Tag: tag-0.11~38 X-Git-Url: https://jmp-git.ovh.safe.ca/?a=commitdiff_plain;h=56b1778f5bbda1772d1905eaae89be9dbaa10d25;p=jmp%2Fmailleur Defining sql_mngusr procedure --- diff --git a/lib/devsql.c b/lib/devsql.c index 13be582..7ecc6f8 100644 --- a/lib/devsql.c +++ b/lib/devsql.c @@ -32,8 +32,17 @@ typedef struct { }SQLTYP; //SQL database request -#define SELFROM "SELECT * FROM" #define EMAILS "emails" //emails tables + +//field available in table "EMAILS" +const char *usrfield[]={ + "email", + "passwd", + "space", + "mxspace", + (char *)0 + }; + /* */ @@ -102,7 +111,7 @@ if (value==(char *)0) { return value; #undef OPEP } -#ifdef JMPDBG +#ifdef GETFIELD /* */ @@ -240,6 +249,96 @@ 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'" + +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]!=(char *)0;i++) { + char *locval; + + if ((locval=getvalue(sql,rs,0,usrfield[i]))==(char *)0) + continue; + switch (i) { + case 0 : //user email + usr->email=strdup(locval); + break; + case 1 : //user passwd + usr->passwd=strdup(locval); + break; + case 2 : //user used space + usr->space=atoi(locval); + break; + case 3 : //user max space available + usr->mxspace=atoi(locval); + break; + default : + (void) rou_alert(0,"%s field <%s> not used (Bug?)",OPEP,usrfield[i]); + break; + } + } + break; + default : //SAFE Guard + rs=dropresult(sql,rs); + proceed=false; + break; + } + phase++; + } +return usr; + +#undef SELUSR +#undef OPEP +} +/* + +*/ +/********************************************************/ +/* */ /* Procedure to establish a link with the */ /* designated SQL server. */ /* */ @@ -428,28 +527,18 @@ return cleanstr; /* known within database. */ /* */ /********************************************************/ -PUBLIC USRTYP *sql_getusr(SQLPTR *sqlptr,char *email,USRTYP **usr) +PUBLIC _Bool sql_mngusr(SQLPTR *sqlptr,SQLENUM action,char *email,USRTYP **usr) { -#define OPEP "devsql.c:sql_getuser," -#define SELUSR "%s %s WHERE email='%s'" - -//field avalable in table "EMAILS" -const char *usrfield[]={ - "email", - "passwd", - "space", - "mxspace", - (char *)0 - }; +#define OPEP "devsql.c:sql_mngusr," SQLTYP *sql; -SQLRES *rs; +_Bool isok; int phase; _Bool proceed; sql=(SQLTYP *)sqlptr; -rs=(SQLRES *)0; +isok=false; phase=0; proceed=true; while (proceed==true) { @@ -466,72 +555,22 @@ while (proceed==true) { } break; case 1 : //getting user information - if ((rs=gettupple(sql,SELUSR,SELFROM,EMAILS,email))==(SQLRES *)0) { - (void) rou_alert(0,"%s Unable to get data for user <%s> (Database?)", - OPEP,email); - phase=999; //Trouble trouble - } - break; - case 2 : //do we have a user information? - int nbr; - - nbr=nbrtupple(sql,rs); - switch (nbr) { - case 0 : //No user email found - phase=999; - break; - case 1 : //we have only one record - break; - default : { - char cmt[200]; - - (void) memset(cmt,'\000',sizeof(cmt)); - (void) snprintf(cmt,sizeof(cmt)-1,SELUSR,SELFROM,EMAILS,email); - (void) rou_alert(0,"<%s> return '%d' record %s (Database corrupted?)", - cmt,nbr,"while only 1 is expected"); - } - phase=999; + switch (action) { + case sql_select : + if ((*usr=select_user(sql,email))!=(USRTYP *)0) + isok=true; break; - } - break; - case 3 : //preparing user data - *usr=sql_freeusr(*usr); - *usr=(USRTYP *)calloc(1,sizeof(USRTYP)); - break; - case 4 : //extracting user data - for (int i=0;usrfield[i]!=(char *)0;i++) { - char *locval; - - if ((locval=getvalue(sql,rs,0,usrfield[i]))==(char *)0) - continue; - switch (i) { - case 0 : //user email - (*usr)->email=strdup(locval); - break; - case 1 : //user passwd - (*usr)->passwd=strdup(locval); - break; - case 2 : //user used space - (*usr)->space=atoi(locval); - break; - case 3 : //user max space available - (*usr)->mxspace=atoi(locval); - break; - default : - (void) rou_alert(0,"%s field <%s> not used (Bug?)",OPEP,usrfield[i]); - break; - } + default : + (void) rou_alert(0,"%s action='%d' not yet implemented!",OPEP,action); } break; default : - rs=dropresult(sql,rs); proceed=false; break; } phase++; } -return *usr; +return isok; -#undef SELUSR #undef OPEP } diff --git a/lib/devsql.h b/lib/devsql.h index ecf1dfb..308df00 100644 --- a/lib/devsql.h +++ b/lib/devsql.h @@ -24,6 +24,6 @@ extern SQLPTR *sql_closesql(SQLPTR *sqlptr); extern char *sql_cleanstr(SQLPTR *sqlptr,char *str); //procedure to get information on exiting user -extern USRTYP *sql_getusr(SQLPTR *sqlptr,char *email,USRTYP **usr); +extern _Bool sql_mngusr(SQLPTR *sqlptr,SQLENUM action,char *email,USRTYP **usr); #endif diff --git a/lib/gesspf.h b/lib/gesspf.h index 940e27b..dfd63ff 100644 --- a/lib/gesspf.h +++ b/lib/gesspf.h @@ -10,7 +10,7 @@ #include "subafn.h" -typedef enum { +typedef enum { spf_pass, //OK if condition apply spf_fail, //NOK if condition apply spf_softfail, //Msg origin is dubious diff --git a/lib/lvleml.c b/lib/lvleml.c index 86e2525..0f1cc22 100644 --- a/lib/lvleml.c +++ b/lib/lvleml.c @@ -279,7 +279,7 @@ proceed=true; while (proceed==true) { switch (phase) { case 0 : //is user a local user - if ((usr=sql_getusr(contact->sqlptr,rcptto,&usr))==(USRTYP *)0) { + if ((sql_mngusr(contact->sqlptr,sql_select,rcptto,&usr))==false) { (void) transmit(contact,true,"%d 5.6.5 <%s> unknown user",UKNUSER,rcptto); phase=999; //No user found in database } diff --git a/lib/unisql.h b/lib/unisql.h index 1f4b198..d830c65 100644 --- a/lib/unisql.h +++ b/lib/unisql.h @@ -9,6 +9,13 @@ #include +typedef enum { + sql_select, //select a record + sql_update, //update a record + sql_delete, //delete a record + sql_uknown //No action + }SQLENUM; + //structure about user within the database typedef struct { char *email; //user emails