Option to add Reply-To header to forwarded e-mails
[mgsmtp.git] / Common.pas
index b7a6ec84f0a163429ee7b05bca17f1d05e399aae..8e0b7fb443ffa93db0871a7522e42a007bbc40f6 100644 (file)
@@ -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
@@ -40,16 +40,16 @@ type
 
 
    TArgumentParser = class
-      constructor Create(RawArguments: TStringArray; AllowedPrefixes: TStringArray = []);
+      constructor Create(RawArguments: array of string; AllowedPrefixes: array of string);
       destructor Destroy; override;
    private
       Arguments: array of TArgument;
-      procedure ParseArgument(Arg: string; const AllowedPrefixes: TStringArray);
+      procedure ParseArgument(Arg: string; const AllowedPrefixes: array of string);
    public
       function GetArgument(ID: integer): TArgument;
       function IsPresent(ArgumentName: string): boolean;
       function GetValue(ArgumentName: string; DefValue: string = ''): string;
-      function ValidateArguments(ValidArguments: TStringArray): integer;
+      function ValidateArguments(ValidArguments: array of string): integer;
    end;
 
 
@@ -173,7 +173,7 @@ type
 const
 
    { MgSMTP version: }
-   VERSION_STR       = '0.9s';
+   VERSION_STR       = '0.9t';
 
    { Architecture: }
 {$IFDEF CPU64}
@@ -219,6 +219,14 @@ implementation
 
 { Unit-private functions/prodecures: }
 
+function InStringArray(const S: string; const SA: array of string): boolean;
+var i: integer;
+begin
+   i:= 0;
+   while (i < Length(SA)) and (SA[i] <> S) do Inc(i);
+   Result:= i < Length(SA);
+end;
+
 function MakeTimeOffsetStr(TimeOffset: integer): string;
 var CorrS: string; CorrI: integer;
 begin
@@ -389,6 +397,14 @@ begin
    end;
 end;
 
+function CmdlineToStringArray: TStringArray;
+var i: integer;
+begin
+   SetLength(Result, ParamCount);
+   for i:= 1 to ParamCount do
+      Result[i-1]:= ParamStr(i);
+end;
+
 function UnixTimeStamp(DateTime: TDateTime): TUnixTimeStamp;
 begin
    {Result:= Trunc((DateTime - EncodeDate(1970, 1 ,1)) * 24 * 60 * 60);}
@@ -425,7 +441,7 @@ end;
 
 { Object constructors/destructors: }
 
-constructor TArgumentParser.Create(RawArguments: TStringArray; AllowedPrefixes: TStringArray = []);
+constructor TArgumentParser.Create(RawArguments: array of string; AllowedPrefixes: array of string);
 var i: integer;
 begin
    for i:= 0 to Length(RawArguments) - 1 do
@@ -495,7 +511,7 @@ end;
 
 { Object methods: }
 
-procedure TArgumentParser.ParseArgument(Arg: string; const AllowedPrefixes: TStringArray);
+procedure TArgumentParser.ParseArgument(Arg: string; const AllowedPrefixes: array of string);
 var i, n: integer; found: boolean;
 begin
    { Strip prefix if present. }
@@ -548,16 +564,16 @@ begin
       Result:= DefValue;
 end;
 
-function TArgumentParser.ValidateArguments(ValidArguments: TStringArray): integer;
+function TArgumentParser.ValidateArguments(ValidArguments: array of string): integer;
 { Returns -1 if all arguments are valid. Otherwise, returns the ID of the first
   invalid parameter. }
 var i: integer;
 begin
    i:= 0;
-   while (i < Length(Arguments)) and (Arguments[i] in ValidArguments) do
+   while (i < Length(Arguments)) and InStringArray(Arguments[i].Option, ValidArguments) do
       Inc(i);
 
-   if i < Length(Arguments) then
+   if i >= Length(Arguments) then
       Result:= -1
    else
       Result:= i;