Include the name/email in email body if not in envelope

If sendemail.from is configured to use a generic server account for
all envelopes, include the actual user's name and email in the body
of the message using a fake "From: " header that is similar to the
RFC header used in the envelope, and similar to how git format-patch
records an author in the body of the email.

Bug: issue 475
Change-Id: Id2bbf12ec174f0a660c5f8bf380291b81941ce3d
Signed-off-by: Shawn O. Pearce <sop@google.com>
This commit is contained in:
Shawn O. Pearce
2010-03-04 19:45:42 -08:00
parent 8bca34b156
commit 6f2ccde49b
3 changed files with 51 additions and 6 deletions

View File

@@ -18,5 +18,7 @@ import com.google.gerrit.reviewdb.Account;
/** Constructs an address to send email from. */
public interface FromAddressGenerator {
public boolean isGenericAddress(Account.Id fromId);
public Address from(Account.Id fromId);
}

View File

@@ -41,9 +41,8 @@ public class FromAddressGeneratorProvider implements
final Address srvAddr = toAddress(myIdent);
if (from == null || "MIXED".equalsIgnoreCase(from)) {
final String name = "${user} (Code Review)";
final String email = srvAddr.email;
generator = new PatternGen(srvAddr, accountCache, name, email);
ParamertizedString name = new ParamertizedString("${user} (Code Review)");
generator = new PatternGen(srvAddr, accountCache, name, srvAddr.email);
} else if ("USER".equalsIgnoreCase(from)) {
generator = new UserGen(accountCache, srvAddr);
@@ -53,7 +52,12 @@ public class FromAddressGeneratorProvider implements
} else {
final Address a = Address.parse(from);
generator = new PatternGen(srvAddr, accountCache, a.name, a.email);
final ParamertizedString name = new ParamertizedString(a.name);
if (name.getParameterNames().isEmpty()) {
generator = new ServerGen(a);
} else {
generator = new PatternGen(srvAddr, accountCache, name, a.email);
}
}
}
@@ -75,6 +79,11 @@ public class FromAddressGeneratorProvider implements
this.srvAddr = srvAddr;
}
@Override
public boolean isGenericAddress(Account.Id fromId) {
return from(fromId) != srvAddr;
}
@Override
public Address from(final Account.Id fromId) {
if (fromId != null) {
@@ -94,6 +103,11 @@ public class FromAddressGeneratorProvider implements
this.srvAddr = srvAddr;
}
@Override
public boolean isGenericAddress(Account.Id fromId) {
return true;
}
@Override
public Address from(final Account.Id fromId) {
return srvAddr;
@@ -107,11 +121,22 @@ public class FromAddressGeneratorProvider implements
private final ParamertizedString namePattern;
PatternGen(final Address serverAddress, final AccountCache accountCache,
final String namePattern, final String senderEmail) {
final ParamertizedString namePattern, final String senderEmail) {
this.senderEmail = senderEmail;
this.serverAddress = serverAddress;
this.accountCache = accountCache;
this.namePattern = new ParamertizedString(namePattern);
this.namePattern = namePattern;
}
@Override
public boolean isGenericAddress(Account.Id fromId) {
if (fromId != null) {
final Account account = accountCache.get(fromId).getAccount();
final String name = account.getFullName();
return name == null || "".equals(name);
} else {
return true;
}
}
@Override

View File

@@ -253,6 +253,24 @@ public abstract class OutgoingEmail {
body = new StringBuilder();
inFooter = false;
if (fromId != null && fromAddressGenerator.isGenericAddress(fromId)) {
final Account account = accountCache.get(fromId).getAccount();
final String name = account.getFullName();
final String email = account.getPreferredEmail();
if ((name != null && !name.isEmpty())
|| (email != null && !email.isEmpty())) {
body.append("From");
if (name != null && !name.isEmpty()) {
body.append(" ").append(name);
}
if (email != null && !email.isEmpty()) {
body.append(" <").append(email).append(">");
}
body.append(":\n\n");
}
}
if (change != null && db != null) {
if (patchSet == null) {
try {