From d7be810271cecd3383edf9fb832be3749ab5be80 Mon Sep 17 00:00:00 2001
From: MegaBrutal <code+git@megabrutal.com>
Date: Mon, 2 Feb 2015 08:28:24 +0100
Subject: [PATCH] Option to add Reply-To header to forwarded e-mails

	modified:   Common.pas
	modified:   Mailbox.pas
	modified:   MgSMTP.pas
	modified:   mgsmtp_server_example.ini
	modified:   todo.txt
---
 Common.pas                |  4 ++--
 Mailbox.pas               | 12 +++++++++++-
 MgSMTP.pas                |  6 +++---
 mgsmtp_server_example.ini | 18 +++++++++++++++++-
 todo.txt                  |  1 +
 5 files changed, 34 insertions(+), 7 deletions(-)

diff --git a/Common.pas b/Common.pas
index 7260c08..8e0b7fb 100644
--- a/Common.pas
+++ b/Common.pas
@@ -1,5 +1,5 @@
 {
-   Copyright (C) 2010-2014 MegaBrutal
+   Copyright (C) 2010-2015 MegaBrutal
 
    This unit is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
@@ -173,7 +173,7 @@ type
 const
 
    { MgSMTP version: }
-   VERSION_STR       = '0.9s';
+   VERSION_STR       = '0.9t';
 
    { Architecture: }
 {$IFDEF CPU64}
diff --git a/Mailbox.pas b/Mailbox.pas
index b600a2a..3fc2020 100644
--- a/Mailbox.pas
+++ b/Mailbox.pas
@@ -1,6 +1,6 @@
 {
    MegaBrutal's SMTP Server (MgSMTP)
-   Copyright (C) 2010-2014 MegaBrutal
+   Copyright (C) 2010-2015 MegaBrutal
 
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as published by
@@ -90,6 +90,7 @@ type
       ForwardToList: TStringList;
       SpoolObject: TSpoolObjectCreator;
       OrigSpoolID: string;
+      FReplyTo: string;
       FForwardHeaders, FRemail: boolean;
    public
       function CheckQuota(MailSize: longint): boolean; override;
@@ -98,6 +99,7 @@ type
       function FinishDeliverMessage(LockID: longint): boolean; override;
       function Lock: longint; override;
       function Release(LockID: longint): boolean; override;
+      property ReplyTo: string read FReplyTo;
       property ForwardHeaders: boolean read FForwardHeaders;
       property Remail: boolean read FRemail;
    end;
@@ -198,6 +200,7 @@ begin
    inherited Create(Name, Domain, Config, Slave, DefaultQuota);
    Self.PhysicalMailbox:= PhysicalMailbox;
 
+   FReplyTo:= GetMailboxConfig(Config, Name, Domain, 'ReplyTo', '');
    FForwardHeaders:= GetMailboxConfig(Config, Name, Domain, 'ForwardHeaders', true);
    FRemail:= GetMailboxConfig(Config, Name, Domain, 'Remail', false);
 
@@ -516,6 +519,13 @@ begin
             Headers.Insert(0, 'X-Forwarded-To: ' + ForwardToList.DelimitedText);
          end;
 
+         if ReplyTo <> '' then begin
+            if ReplyTo = '!' then
+               Headers.Insert(0, 'Reply-To: <' + Recipient + '>')
+            else
+               Headers.Insert(0, 'Reply-To: ' + ReplyTo);
+         end;
+
          SpoolObject.Open;
          for i:= 0 to Headers.Count - 1 do
             SpoolObject.DeliverMessagePart(Headers.Strings[i]);
diff --git a/MgSMTP.pas b/MgSMTP.pas
index e0ee636..808a90d 100644
--- a/MgSMTP.pas
+++ b/MgSMTP.pas
@@ -1,6 +1,6 @@
 {
    MegaBrutal's SMTP Server (MgSMTP)
-   Copyright (C) 2010-2014 MegaBrutal
+   Copyright (C) 2010-2015 MegaBrutal
 
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as published by
@@ -49,7 +49,7 @@ const
      document what bugfix/feature are you testing with the actual build.
      This will be logged to help you differentiate outputs of subsequent
      builds in your logs. If left empty, it won't be added to the logs. }
-   DEVCOMMENT  =  '';
+   DEVCOMMENT  =  'New feature to add "Reply-To" to forwarded messages';
 
 var
 
@@ -308,7 +308,7 @@ begin
             Out.writeln('Trying to contact Service Control Manager...');
             Out.writeln('(If you see this message on console, you tried to');
             Out.writeln('start up the program incorrectly. Your current');
-            Out.writeln('attempt will fail, or it may hang under Wine.');
+            Out.writeln('attempt will fail, or it may hang under Wine.)');
             Out.writeln;
             ServiceMode:= true;
             if not StartServiceCtrlDispatcher(ServiceTable) then begin
diff --git a/mgsmtp_server_example.ini b/mgsmtp_server_example.ini
index 258e2c0..f414a68 100644
--- a/mgsmtp_server_example.ini
+++ b/mgsmtp_server_example.ini
@@ -1,5 +1,5 @@
 ; MegaBrutal's SMTP Server (MgSMTP)
-; Copyright (C) 2010-2014 MegaBrutal
+; Copyright (C) 2010-2015 MegaBrutal
 
 ; This is an example configuration file for MgSMTP.
 ; You may inspect it to learn about the configuration options of MgSMTP, and (if
@@ -657,6 +657,22 @@ StoreLocalCopy = On
 ; (You can still have "StoreLocalCopy" off, if you don't want your message to be
 ; actually delivered to the mailbox.)
 
+; Reply addresses and simple distribution lists:
+; As the following example shows, with the combination of "ForwardTo" and "ReplyTo",
+; it is possible to create simple, static distribution lists, where replies are
+; addressed back to the distribution list by default.
+; The value of "ReplyTo" will be inserted as a "Reply-To" header to the e-mails being
+; forwarded. Note, it will only affect forwarded e-mails: the local copy won't have
+; the "Reply-To" header.
+; There is a special value for "ReplyTo": "!". When used, it will be substituted with
+; the appropriate mailbox address (in this example, it is "party@example.com").
+
+;[Mailbox\party]
+;ForwardTo = benga@example.com,hugo@example.com,kitty@example.com
+;ReplyTo = Party Unit <party@example.com>
+; Or:
+;ReplyTo = !
+
 ; Domain-specific mailboxes:
 
 ;[Mailbox\@mydomain.tld]
diff --git a/todo.txt b/todo.txt
index 209b244..7a59bc3 100644
--- a/todo.txt
+++ b/todo.txt
@@ -25,6 +25,7 @@ v0.9t:
 - Process CTRL-C properly to quit from user mode gracefully
 - IPv6
 - Bind to user-specified IPs
++ Option to add Reply-To header to forwarded e-mails
 
 v0.9s:
 + Change "Client disconnected, and thread exited successfully." to "Client disconnected."
-- 
2.43.0