#include #include #include #include #include #include #include "mailsql.m" #include "mailsql.h" #include //#include static HELP_FILE help_users ("mailsql","users"); /* Edit one user account for a given domain. If name is NULL we can create a new account */ static int sqlusers_editone(int domainid, const char *name) { glocal SSTRING id; glocal SSTRING fullname; glocal char receive,retrieve; glocal int quota = 0; glocal SSTRING createdate,expiredate,noticedate; if (name != NULL){ glocal.id.setfrom (name); ("select fullname,mayreceive,mayretrieve,quota,createdate,expiredate,noticedate from accounts" " where domainid=%d and name='%s'",domainid,name); glocal.fullname.setfrom (row[0]); glocal.receive = row[1][0] == 'Y'; glocal.retrieve = row[2][0] == 'Y'; glocal.quota = atoi(row[3]); glocal.createdate = row[4]; glocal.expiredate = row[5]; glocal.noticedate = row[6]; } DIALOG dia; dia.newf_str (MSG_U(F_USERID,"User ID"),glocal.id); dia.newf_str (MSG_U(F_USERNAME,"User name"),glocal.fullname); dia.newf_chk ("",glocal.receive,MSG_U(F_MAYRECEIVE,"Accept new messages")); dia.newf_chk ("",glocal.retrieve,MSG_U(F_MAYRETRIEVE,"May retrieve messages")); static int tbnum[]={0}; static const char *tbvals[]={MSG_U(I_DEFAULT,"Domain default"),NULL}; dia.newf_chkm_num (MSG_U(F_UQUOTA,"Max folder size (k)"),glocal.quota,0,tbnum,tbvals); dia.newf_str (MSG_U(F_CREATE,"Creation date"),glocal.createdate); dia.newf_str (MSG_R(F_EXPIRE),glocal.expiredate); dia.newf_str (MSG_U(F_NOTICE,"Notice date"),glocal.noticedate); SSTRING pass1,pass2; dia.newf_pass (MSG_U(F_PASSWORD,"Password"),pass1); if (name == NULL) dia.last_noempty(); dia.newf_pass (MSG_U(F_PASSWORD2,"Password (repeat)"),pass2); dia.delwhat (MSG_U(I_DELACCT,"Hit Del to delete the account")); int nof = 0; int butmask = MENUBUT_ACCEPT|MENUBUT_CANCEL; if (name != NULL) butmask |= MENUBUT_DEL; while (1){ MENU_STATUS code = dia.edit (MSG_U(T_USERACCOUNT,"User account") ,"",help_users,nof,butmask); if (code == MENU_CANCEL || code == MENU_ESCAPE){ break; }else if (code == MENU_DEL){ if (xconf_delok()){ sql_action ("delete from accounts where domainid=%d" " and name='%s'",domainid,name); break; } }else if (!master_registry.in_session() && pass1.cmp(pass2)!=0){ xconf_error (MSG_U(E_PASSDIFFER,"The passwords differs, try again")); pass1.setfrom (""); pass2.setfrom (""); dia.reload(); nof = 3; }else{ char mayreceive = glocal.receive ? 'Y' : 'N'; char mayretrieve = glocal.retrieve ? 'Y' : 'N'; SSTRING passcrypt; if (pass1.is_filled()){ passcrypt.setfrom (crypt(pass1.get(),"$1$")); } if (name == NULL){ NSQL_ENCODE enc; SSTRING tmpdate; if (glocal.createdate.is_empty()){ tmpdate = "now()"; }else{ tmpdate.setfromf ("'%s'",glocal.createdate.get()); } if (sql_action ("insert into accounts (domainid,name,fullname,password,mayreceive,mayretrieve,quota,createdate,expiredate) values" " (%d,'%s','%s','%s','%c','%c',%d,%s,'%s','%s')" ,domainid,glocal.id.get() ,enc.enc(glocal.fullname) ,passcrypt.get(),mayreceive,mayretrieve,glocal.quota ,tmpdate.get(),glocal.expiredate.get() ,glocal.noticedate.get())!=-1){ // We create the inbox ("select userid from accounts where name='%s'" " and domainid=%d" ,glocal.id.get(),domainid); sql_action("insert into folders (foldername,userid)" " values ('inbox','%s')",row[0]); break; } }else if (glocal.id.cmp(name)!=0){ xconf_error(MSG_U(E_CANTRENAME,"Can't rename account")); }else{ NSQL_ENCODE enc; SSTRING passcmd; if (pass1.is_filled()){ passcmd.setfromf (",password='%s'",enc.enc(passcrypt)); } if (sql_action ("update accounts" " set fullname='%s',mayreceive='%c',mayretrieve='%c',quota=%d%s,createdate='%s',expiredate='%s',noticedate='%s'" " where domainid=%d and name='%s'" ,enc.enc(glocal.fullname) ,mayreceive,mayretrieve ,glocal.quota ,passcmd.get() ,glocal.createdate.get() ,glocal.expiredate.get() ,glocal.noticedate.get() ,domainid,name)!=-1){ break; } } } } return 0; } int sqlusers_getdomainid(const char *domain) { int domainid; NSQL_ENCODE enc; ("select domainid from domains where domain='%s'" ,enc.enc(domain)); glocal.domainid = -1; glocal.domainid = atoi(row[0]); return glocal.domainid; } void sqlusers_edit(const char *domain) { const char *domain; int domainid; _F_editrecords *c; glocal.domain = domain; glocal.domainid = sqlusers_getdomainid(domain); if (glocal.domainid == -1){ tlmp_error (MSG_U(E_NODOMAIN,"Domain %s not defined in database\n") ,domain); }else{ (MSG_U(T_VUSERS,"Virtual SQL email users") ,"",help_users); newf_head (MSG_U(H_USERS,"User ID\tName\tQuota\tFolders size\tMay receive\tMay retrieve\tCreated\tExpire\tNotice")); addwhat (MSG_U(I_ADDUSER,"Select Add to add a new user account")); sortpolicy ("aanaaaaa"); sortable(); sethdispo ("llrrccllc"); glocal.c = this; ("select" " name,fullname,quota,mayreceive,mayretrieve,used" ",createdate,expiredate,noticedate from accounts" " where domainid=%d order by name",glocal.domainid); int used = atoi(row[5])/1024; glocal.c->new_menuitemf (row[0],"%s\t%s\t%d\t%c\t%c\t%s\t%s\t%c" ,row[1] ,strcmp(row[2],"0")==0 ? MSG_U(I_NONE,"none") : row[2] ,used ,row[3][0] == 'Y' ? 'X' : ' ' ,row[4][0] == 'Y' ? 'X' : ' ' ,row[6],row[7] ,strcmp(row[7],row[8])==0 ? 'X' : ' '); sqlusers_editone (glocal.domainid,NULL); ("select name from accounts" " where domainid=%d order by name limit %d,1" ,glocal.domainid,no); sqlusers_editone (glocal.domainid,row[0]); } } static PUBLISH_VARIABLES_MSG tbvar[]={ {"userid",P_MSG_R(F_USERID)}, {"fullname",P_MSG_R(F_USERNAME)}, {"mayreceive",P_MSG_R(F_MAYRECEIVE)}, {"mayretrieve",P_MSG_R(F_MAYRETRIEVE)}, {"quota",P_MSG_R(F_UQUOTA)}, {"createdate",P_MSG_R(F_CREATE)}, {"expiredate",P_MSG_R(F_EXPIRE)}, {"noticedate",P_MSG_R(F_NOTICE)}, {"password",P_MSG_R(F_PASSWORD)}, {NULL,NULL}, }; static void sqlusers_edituser (const char *key, bool setting) { const char *pt = strchr (key,'/'); if (pt != NULL){ mailsql_initdb(); SSTRING domain; domain.setfrom (key,pt-key); glocal const char *userid = pt+1; glocal int domainid = sqlusers_getdomainid(domain.get()); glocal bool setting = setting; if (glocal.domainid != -1){ NSQL_ENCODE enc; ("select userid from accounts" " where domainid=%d and name='%s'" ,glocal.domainid,enc.enc(glocal.userid)); sqlusers_editone (glocal.domainid,glocal.userid); if (glocal.setting){ sqlusers_editone (glocal.domainid,glocal.userid); } } } } static void sqlusers_listusers (SSTRINGS &tb) { glocal SSTRINGS *tb = &tb; mailsql_initdb(); ("select domain,name from domains,accounts" " where domains.domainid=accounts.domainid order by domain,name"); SSTRING tmp; tmp.setfromf ("%s/%s",row[0],row[1]); glocal.tb->add (new SSTRING(tmp)); } static REGISTER_VARIABLES reg1 ("mailsql","users",tbvar,NULL ,sqlusers_edituser,sqlusers_listusers); #if 0 static int sqlusers_editaccount (USERACCT_API *, const char *domain, const char *account, bool setting) { mailsql_initdb (); return 0; } static int sqlusers_listaccounts (USERACCT_API *, const char *domain, SSTRINGS &tb) { SSTRINGS *tb; glocal.tb = &tb; int ret = 0; mailsql_initdb (); int domainid = sqlusers_getdomainid(domain); if (domainid != -1){ ("select name from accounts" " where domainid=%d order by name",domainid); glocal.tb->add (new SSTRING(row[0])); return ret; } return ret; } void *sqlusers_useracct_api_get() { USERACCT_API *api = new USERACCT_API; api->editaccount = sqlusers_editaccount; api->listaccounts = sqlusers_listaccounts; return api; } void sqlusers_useracct_api_release(void *api) { delete (USERACCT_API*)api; } #endif