e64652fbd79aa4a84616be05049cfcfd0e99128b
chinhli
  Tue May 1 11:41:58 2012 -0700
Finished email forgotten username(s).
diff --git src/hg/hgLogin/hgLogin.c src/hg/hgLogin/hgLogin.c
index 9739596..ebd5833 100644
--- src/hg/hgLogin/hgLogin.c
+++ src/hg/hgLogin/hgLogin.c
@@ -29,118 +29,105 @@
 
 char *excludeVars[] = { "submit", "Submit", "debug", "fixMembers", "update", 
      "hgLogin_password", "hgLogin_password2", "hgLogin_newPassword1",
      "hgLogin_newPassword2", NULL };
 /* The excludeVars are not saved to the cart. (We also exclude
  * any variables that start "near.do.") */
 
 /* ---- Global variables. ---- */
 struct cart *cart;	/* This holds cgi and other variables between clicks. */
 char *database;		/* Name of genome database - hg15, mm3, or the like. */
 struct hash *oldCart;	/* Old cart hash. */
 char *errMsg;           /* Error message to show user when form data rejected */
 
 /* -------- utilities functions --- */
 void  displayMailSuccess()
-/* display mail success msg, and set cookie */
+/* display mail success confirmation box */
 {
 char *email = cartUsualString(cart, "hgLogin_email", "");
 char *obj=cartUsualString(cart, "hgLogin_helpWith", "");
 //safecpy(obj, sizeof(obj),object);
 hPrintf(
 "<div id=\"confirmationBox\" class=\"centeredContainer formBox\">"
 "\n"
 "<h2>UCSC Genome Browser</h2>"
 "<p id=\"confirmationMsg\" class=\"confirmationTxt\">An email has been sent to "
 " <span id=\"emailaddress\">%s</span> containing %s...</p>"
 "\n"
 "<p><a href=\"hgLogin?hgLogin.do.displayLoginPage=1\">Return to Login</a></p>"
 , email
 , obj
 );
-//  backToDoLoginPage(12);
 }
-void sendMail(char *email, char *subject, char *msg, char *object)
+void sendMail(char *email, char *subject, char *msg)
 {
 char *hgLoginHost = hgLoginLinkHost();
+char *helpWith = cartUsualString(cart, "hgLogin_helpWith", "");
 char cmd[256];
 safef(cmd,sizeof(cmd),
 // "echo 'Hello from your favoriate browser at: %s %s ' | mail -s \"Greeting form UCSC Genome Browser\" %s"
 // , msg, httpLink, email);
 //"echo '%s' | mail -s \"Greeting from uCSC\" %s"
 "echo '%s' | mail -s \"%s\" %s" , msg, subject, email); 
 int result = system(cmd);
 if (result == -1)
     {
     hPrintf( 
-    "<h2>GSID HIV Data Browser</h2>"
+    "<h2>UCSC Genome Browser</h2>"
     "<p align=\"left\">"
     "</p>"
-    "<h3>Error emailing password to: %s</h3>"
-    "Click <a href=hgLogin?hgLogin.do.signupPage=1>here</a> to return.<br>"
+    "<h3>Error emailing %s to: %s</h3>"
+    "Click <a href=hgLogin?hgLogin.do.displayAccHelpPage=1>here</a> to return.<br>"
+    , helpWith
     , email
     );
     }
 else
     {
-/*************************** old mail **************
-    hPrintf(
-    "<h2>GSID HIV Data Browser</h2>"
-    "<p align=\"left\">"
-    "</p>"
-    "<h3>Password has been emailed to: %s</h3>"
-    "Click <a href=hgLogin?hgLogin.do.signupPage=1>here</a> to return.<br>"
-    , email
-    );
-********************************** old mail ****/
-/*************************
-hPrintf(
-"<div id=\"confirmationBox\" class=\"centeredContainer formBox\">"
-"\n"
-"<h2>UCSC Genome Browser</h2>"
-"<p id=\"confirmationMsg\" class=\"confirmationTxt\">An email has been sent to "
-" <span id=\"emailaddress\">$email</span> containing %s...</p>"
-"\n"
-"<p><a href=\"hgLogin?hgLogin.do.displayLoginPage=1\">Return to Login</a></p>"
-"\n"
-"</div><!-- END - confirmationBox -->"
-"\n"
-, object);
-**********************/
-cartSetString(cart, "hgLogin_helpWith", "password");
+// cartSetString(cart, "hgLogin_helpWith", "password");
 hPrintf(
 "<script  language=\"JavaScript\">\n"
 "<!-- "
 "\n"
 
 "window.location =\"http://%s/cgi-bin//hgLogin?hgLogin.do.displayMailSuccess=1\""
 "//-->"
 "\n"
 "</script>"
 , hgLoginHost
 );
 
 cartSetString(cart, "hgLogin.do.displayMailSuccess", "1");
 //displayMailSuccess("password");
 //return;
 }
 }
 
-
+void sendUsername(char *email, char *users)
+/* send user name list to the email address */
+{
+char subject[256];
+char msg[256];
+char signature[256]="\nUCSC Genome Browser \nhttp://www.genome.ucsc.edu ";
+safef(subject, sizeof(subject),"Greeting form UCSC Genome Browser");
+safef(msg, sizeof(msg), "User name(s) associated with this email address at UCSC Genome Browser: \n\n  %s \n", users);
+safecat (msg, sizeof(msg), signature);
+sendMail(email, subject, msg);
+}
 
 void activateAccount(struct sqlConnection *conn)
-/* activate user account  */
+/* activate account */
 {
 struct sqlResult *sr;
 char **row;
 char query[256];
 char *token = cgiUsualString("hgLogin_activateAccount", "");
 safef(query,sizeof(query),"Token is %s ", token);
 if (!sameString(token,""))
     {
     freez(&errMsg);
     errMsg = cloneString(query);
     displayLoginPage(conn);
     return;
     }
 }
 /* -------- password functions ---- */
@@ -844,46 +831,61 @@
 /* email user username(s) or new password */
 {
 struct sqlResult *sr;
 char **row;
 char query[256];
 char *email = cartUsualString(cart, "hgLogin_email", "");
 if (sameString(email,""))
     {
     freez(&errMsg);
     errMsg = cloneString("Email address cannot be blank.");
     displayAccHelpPage(conn);
     return;
     }
 /* TODO: validate the email address is in right format */
 /* Username selcted? */
-char *helpWith = cartUsualString(cart, "helpWith", "");
+char *helpWith = cartUsualString(cart, "hgLogin_helpWith", "");
 if (sameString(helpWith,"username"))
     {
-char subject[256];
-char email[256]="chinhli@soe.ucsc.edu";
-char msg[256];
-char httpLink[256]="Visit http://www.genome.ucsc.edu ";
-safef(subject, sizeof(subject),"Greeting form UCSC about %s", email);
-safecpy(msg, sizeof(msg), "Hello from your favoriate browser at UCSC. ");
-safecat (msg, sizeof(msg), httpLink);
-    sendMail(email, subject, msg, "password");
+    /* find all the user names assocaited with this email address */
+    char userList[256]="";
+    safef(query,sizeof(query),"select * from gbMembers where email='%s'", email);
+    sr = sqlGetResult(conn, query);
+    int numUser = 0;
+    while ((row = sqlNextRow(sr)) != NULL)
+        {
+        struct gbMembers *m = gbMembersLoad(row);
+        if (numUser >= 1)  
+            safecat(userList, sizeof(userList), ", ");
+        safecat(userList, sizeof(userList), m->userName);
+        numUser += 1;
+        }
+        sqlFreeResult(&sr);
+    if (numUser == 0)
+        {
     freez(&errMsg);
-    errMsg = cloneString("Forgot user name selected!");
+        char temp[256];
+        safef(temp,sizeof(temp),"No user found with this email address.");
+        errMsg = cloneString(temp);
     displayAccHelpPage(conn);
     return;
+        } else {
+        sendUsername(email, userList);
     }
+    } /* helpWith username */
+
+
 if (sameString(helpWith,"password"))
     {
     freez(&errMsg);
     errMsg = cloneString("Forgot password selected!");
     displayAccHelpPage(conn);
     return;
     }
 displayAccHelpPage(conn);
 return;
 }
 
 /* ----- account login/display functions ---- */
 
 
 void displayLoginPage(struct sqlConnection *conn)
@@ -1213,31 +1215,31 @@
 else if (cartVarExists(cart, "hgLogin.do.lostPasswordPage"))
     lostPasswordPage(conn);
 else if (cartVarExists(cart, "hgLogin.do.lostPassword"))
     lostPassword(conn);
 else if (cartVarExists(cart, "hgLogin.do.changePasswordPage"))
     changePasswordPage(conn);
 else if (cartVarExists(cart, "hgLogin.do.changePassword"))
     changePassword(conn);
 else if (cartVarExists(cart, "hgLogin.do.displayUserInfo"))
     displayUserInfo(conn);
 else if (cartVarExists(cart, "hgLogin.do.displayAccHelpPage"))
     displayAccHelpPage(conn);
 else if (cartVarExists(cart, "hgLogin.do.accountHelp"))
     accountHelp(conn);
 else if (cartVarExists(cart, "hgLogin.do.activateAccount"))
-    accountHelp(conn);
+    activateAccount(conn);
 else if (cartVarExists(cart, "hgLogin.do.displayMailSuccess"))
     displayMailSuccess(conn);
 else if (cartVarExists(cart, "hgLogin.do.displayLoginPage"))
     displayLoginPage(conn);
 else if (cartVarExists(cart, "hgLogin.do.displayLogin"))
     displayLogin(conn);
 else if (cartVarExists(cart, "hgLogin.do.displayLogout"))
     displayLogoutSuccess();
 else if (cartVarExists(cart, "hgLogin.do.signup"))
     signup(conn);
 else
     signupPage(conn);
 
 
 hDisconnectCentral(&conn);