2 Copyright (C) 2010-2018 MegaBrutal
4 This unit is free software: you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This unit is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 It holds some common definitions for MgSMTP, and some helper functions.
29 uses Windows
, SysUtils
, DateUtils
, Classes
, INIFiles
, RFCSMTP
;
33 TStringArray
= array of string;
34 TUnixTimeStamp
= longint;
38 Option
, Value
: string;
42 TArgumentParser
= class
43 constructor Create(RawArguments
: array of string; AllowedPrefixes
: array of string);
44 destructor Destroy
; override;
46 Arguments
: array of TArgument
;
47 procedure ParseArgument(Arg
: string; const AllowedPrefixes
: array of string);
49 function GetArgument(ID
: integer): TArgument
;
50 function IsPresent(ArgumentName
: string): boolean;
51 function GetValue(ArgumentName
: string; DefValue
: string = ''): string;
52 function ValidateArguments(ValidArguments
: array of string): integer;
57 constructor Create(const Name
: string; Config
: TINIFile
; const Section
: string);
62 property Name
: string read FName
;
63 property Aliases
: TStrings read FAliases
;
64 function IsItYourName(const Name
: string): boolean; virtual;
68 TMainServerConfig
= class(TNamedObject
)
69 constructor Create(Config
: TINIFile
);
71 FPolicies
, FMailbox
, FRelay
, FLog
: boolean;
73 {FTimeCorrection: integer;}
75 FTimeOffsetStr
: string;
76 FListenAddresses
, FListenAddresses6
: TStrings
;
78 function GetVersionStr
: string;
79 property ListenAddresses
: TStrings read FListenAddresses
;
80 property ListenAddresses6
: TStrings read FListenAddresses6
;
81 property Databytes
: longint read FDatabytes
;
82 {property TimeCorrection: integer read FTimeCorrection;}
83 property TimeOffset
: integer read FTimeOffset
;
84 property TimeOffsetStr
: string read FTimeOffsetStr
;
85 property Policies
: boolean read FPolicies
;
86 property Mailbox
: boolean read FMailbox
;
87 property Relay
: boolean read FRelay
;
88 property Log
: boolean read FLog
;
89 property VersionStr
: string read GetVersionStr
;
95 TEMailProperties
= class
101 procedure SetSize(Value
: longint);
102 procedure WriteFlags(Value
: TEMailFlags
);
103 procedure SetFlag(Flag
: TEMailFlags
);
104 function HasFlag(Flag
: TEMailFlags
): boolean;
105 property Size
: longint read FSize write SetSize
;
106 property Flags
: TEMailFlags read FFlags write WriteFlags
;
111 Address
, RMsg
: string;
117 constructor Create(const Name
, IP
: string);
121 property Name
: string read FName
;
122 property IP
: string read FIP
;
123 function Copy
: TIPNamePair
;
129 destructor Destroy
; override;
131 FReturnPath
, FRelayHost
: string;
132 FReturnPathSpecified
: boolean;
133 FRecipients
: array of TRecipient
;
135 function GetNumberOfRecipients
: integer;
136 function GetRecipient(Index
: integer): TRecipient
;
137 function IsComplete
: boolean;
138 procedure AddRecipient(Address
: string; Data
: integer = 0; RMsg
: string = ''); overload
;
139 procedure AddRecipient(Recipient
: TRecipient
); overload
;
140 procedure SetReturnPath(Address
: string);
141 procedure SetRecipientData(Index
, Data
: integer; RMsg
: string = '');
142 procedure SetAllRecipientData(Data
: integer; RMsg
: string = '');
143 procedure SetRelayHost(HostName
: string);
144 property ReturnPath
: string read FReturnPath write SetReturnPath
;
145 property RelayHost
: string read FRelayHost write SetRelayHost
;
149 TEnvelopeArray
= array of TEnvelope
;
152 function EMailUserName(EMail
: string): string;
153 function EMailHost(EMail
: string): string;
154 function CleanEMailAddress(EMail
: string): string;
155 function IsValidEMailAddress(EMail
: string): boolean;
156 function EMailTimeStamp(DateTime
: TDateTime
): string;
157 function EMailTimeStampCorrected(DateTime
: TDateTime
): string;
158 function StatusToStr(Status
: integer): string;
159 procedure AssignDeliveryStatusToSMTPCodes(Envelope
: TEnvelope
; TransactionComplete
: boolean);
161 function CleanEOLN(S
: string): string;
162 function GenerateRandomString(Length
: integer): string;
163 function GetAlphabetStr
: string;
164 function GetServiceCodeStr(Ctrl
: dword
): string;
165 function GetWinMajorVersion
: longword
;
166 function IsPrintableString(S
: string): boolean;
167 function UnixTimeStamp(DateTime
: TDateTime
): TUnixTimeStamp
;
168 function CmdlineToStringArray
: TStringArray
;
169 procedure ParseIPv6Address(S
: string; var Address
: string; var Port
: word);
170 procedure SplitParameters(S
: string; var FirstPrm
, Remainder
: string; Separator
: char = #32);
172 function ReadLineFromStream(Stream
: TStream
): string;
173 function WriteLineToStream(Stream
: TStream
; Line
: string): boolean;
179 VERSION_STR
= '0.9t';
190 { Delivery statuses: }
191 DS_DELIVERED
= 1 shl 10;
192 DS_DELAYED
= 1 shl 11;
193 DS_PERMANENT
= 1 shl 12;
194 DS_INTERNALFAIL
= 1 shl 13;
195 DS_CONNECTIONFAIL
= 1 shl 14;
196 DS_UNEXPECTEDFAIL
= 1 shl 15;
197 DS_SMTPFAIL
= 1 shl 16;
198 DS_SMTPREPLYMASK
= $000003FF;
199 DS_ALLFLAGS
= $FFFFFFFF;
201 { E-mail property flags: }
204 DayNames
: array[1..7] of shortstring
= ('Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat');
205 MonthNames
: array[1..12] of shortstring
= ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
206 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
208 { Support for PRESHUTDOWN is not yet present in the Free Pascal library,
209 therefore I define the necessary constants here. It's a temporary
210 solution, I hope I won't need it for the next release of FPC. }
211 SERVICE_ACCEPT_PRESHUTDOWN
= $00000100;
212 SERVICE_CONTROL_PRESHUTDOWN
= $0000000F;
217 MainServerConfig
: TMainServerConfig
;
223 { Unit-private functions/prodecures: }
225 function InStringArray(const S
: string; const SA
: array of string): boolean;
229 while (i
< Length(SA
)) and (SA
[i
] <> S
) do Inc(i
);
230 Result
:= i
< Length(SA
);
233 function MakeTimeOffsetStr(TimeOffset
: integer): string;
234 var CorrS
: string; CorrI
: integer;
237 CorrS
:= IntToStr(Abs(CorrI
));
238 while Length(CorrS
) < 4 do CorrS
:= '0' + CorrS
;
239 if CorrI
>= 0 then CorrS
:= '+' + CorrS
else CorrS
:= '-' + CorrS
;
244 { Unit-public functions/procedures: }
246 function EMailUserName(EMail
: string): string;
250 while (p
> 0) and (EMail
[p
] <> '@') do Dec(p
);
252 Result
:= Copy(EMail
, 1, p
- 1);
257 function EMailHost(EMail
: string): string;
261 while (p
> 0) and (EMail
[p
] <> '@') do Dec(p
);
262 if (p
<> 0) and (p
< Length(EMail
)) then begin
263 Result
:= Copy(EMail
, p
+ 1, Length(EMail
) - p
);
268 function CleanEMailAddress(EMail
: string): string;
269 var po
, pc
, p
: integer;
271 po
:= Pos('<', EMail
);
272 pc
:= Pos('>', EMail
);
273 if (po
<> 0) and (pc
<> 0) and (po
< pc
) then begin
274 Result
:= Copy(EMail
, po
+ 1, pc
- po
- 1);
275 p
:= Pos(':', Result
);
277 Result
:= Copy(Result
, p
+ 1, Length(Result
) - p
);
283 function IsValidEMailAddress(EMail
: string): boolean;
285 { !!! TODO: Implement more strict checking later !!! }
286 Result
:= Pos('@', EMail
) <> 0;
289 function EMailTimeStamp(DateTime
: TDateTime
): string;
290 var Year
, Month
, Day
: word;
292 DecodeDate(DateTime
, Year
, Month
, Day
);
293 Result
:= DayNames
[DayOfWeek(DateTime
)] + ' ' + MonthNames
[Month
] + ' '
294 + FormatDateTime('dd hh:nn:ss', DateTime
) + ' ' + IntToStr(Year
);
297 function EMailTimeStampCorrected(DateTime
: TDateTime
): string;
299 Result
:= EMailTimeStamp(DateTime
) + ' ' + MainServerConfig
.TimeOffsetStr
;
302 function StatusToStr(Status
: integer): string;
303 { Returns the delivery status code in human-readable format. }
305 Result
:= IntToStr(Status
and (DS_ALLFLAGS
- DS_SMTPREPLYMASK
- DS_SMTPFAIL
))
306 + '+' + IntToStr(Status
and DS_SMTPREPLYMASK
);
309 procedure AssignDeliveryStatusToSMTPCodes(Envelope
: TEnvelope
; TransactionComplete
: boolean);
310 var i
, code
, cond
, status
: integer; Recipient
: TRecipient
;
312 for i
:= 0 to Envelope
.GetNumberOfRecipients
- 1 do begin
313 Recipient
:= Envelope
.GetRecipient(i
);
314 code
:= Recipient
.Data
and DS_SMTPREPLYMASK
;
317 0: status
:= DS_DELAYED
or DS_UNEXPECTEDFAIL
;
318 2: if TransactionComplete
then status
:= DS_DELIVERED
319 else status
:= DS_DELAYED
or DS_UNEXPECTEDFAIL
;
320 4: status
:= DS_DELAYED
;
321 5: status
:= DS_PERMANENT
;
322 else status
:= DS_PERMANENT
or DS_UNEXPECTEDFAIL
;
324 if code
<> 0 then status
:= status
or DS_SMTPFAIL
;
325 Envelope
.SetRecipientData(i
, code
or status
, Recipient
.RMsg
);
330 function CleanEOLN(S
: string): string;
332 while (Length(S
) <> 0) and (S
[Length(S
)] in [#13, #10]) do Delete(S
, Length(S
), 1);
336 function GenerateRandomString(Length
: integer): string;
337 var What
, Chrn
, i
: integer; Value
: string;
340 for i
:= 1 to Length
do begin
343 0: begin Chrn
:= Random(10)+48; Value
:= Value
+ Chr(Chrn
); end;
344 1: begin Chrn
:= Random(26)+65; Value
:= Value
+ Chr(Chrn
); end;
345 2: begin Chrn
:= Random(26)+97; Value
:= Value
+ Chr(Chrn
); end;
351 function GetAlphabetStr
: string;
355 for i
:= Ord('0') to Ord('9') do Result
:= Result
+ Chr(i
);
356 for i
:= Ord('A') to Ord('Z') do Result
:= Result
+ Chr(i
);
359 function GetServiceCodeStr(Ctrl
: dword
): string;
362 SERVICE_CONTROL_STOP
: Result
:= 'STOP';
363 SERVICE_CONTROL_SHUTDOWN
: Result
:= 'SHUTDOWN';
364 SERVICE_CONTROL_PRESHUTDOWN
: Result
:= 'PRESHUTDOWN';
365 else Result
:= IntToStr(Ctrl
);
369 function GetWinMajorVersion
: longword
;
370 var OSVersionInfo
: TOSVersionInfo
;
372 { Get OS version info. }
373 OSVersionInfo
.dwOSVersionInfoSize
:= SizeOf(TOSVersionInfo
);
374 GetVersionEx(OSVersionInfo
);
375 Result
:= OSVersionInfo
.dwMajorVersion
;
378 function IsPrintableString(S
: string): boolean;
379 { Check if string contains only printable ASCII characters. }
384 while Result
and (i
<= Length(S
)) do begin
385 Result
:= (Ord(S
[i
]) > 31) and (Ord(S
[i
]) < 127);
390 procedure ParseIPv6Address(S
: string; var Address
: string; var Port
: word);
391 { IPv6 addresses can be supplied in the following formats:
392 [<IPv6 address>]:<port> e.g. [::1]:25
394 <hostname>:<port> e.g. mail.example.com:25 }
395 var SPort
: string; c
: integer;
397 if S
[1] = '[' then begin
398 { Guess format is "[<IPv6 address>]:<port>". }
401 Address
:= Copy(S
, 2, c
- 2);
402 if c
= Length(S
) then begin
403 { There is no port to extract. }
404 Port
:= STANDARD_SMTP_PORT
;
407 { The closing bracket should be followed by a colon. }
409 { Extract port number. }
410 Port
:= StrToIntDef(Copy(S
, c
+2, Length(S
) - (c
+1)), 0)
417 { Format is incorrect, return invalid data. }
423 { Guess format is "<hostname>:<port>". }
424 SplitParameters(S
, Address
, SPort
, ':');
425 if (pos(':', Address
) = 0) and (pos(':', SPort
) = 0) then begin
426 { Format seems correct. }
427 if SPort
= '' then Port
:= STANDARD_SMTP_PORT
428 else Port
:= StrToIntDef(SPort
, 0);
431 { Format is incorrect, return invalid data. }
438 procedure SplitParameters(S
: string; var FirstPrm
, Remainder
: string; Separator
: char = #32);
441 i
:= pos(Separator
, S
);
443 FirstPrm
:= Copy(S
, 1, i
- 1);
444 Remainder
:= Copy(S
, i
+ 1, Length(S
) - i
);
452 function CmdlineToStringArray
: TStringArray
;
455 SetLength(Result
, ParamCount
);
456 for i
:= 1 to ParamCount
do
457 Result
[i
-1]:= ParamStr(i
);
460 function UnixTimeStamp(DateTime
: TDateTime
): TUnixTimeStamp
;
462 {Result:= Trunc((DateTime - EncodeDate(1970, 1 ,1)) * 24 * 60 * 60);}
463 Result
:= DateTimeToUnix(DateTime
);
467 function ReadLineFromStream(Stream
: TStream
): string;
468 var S
: string; B
: char;
473 B
:= Char(Stream
.ReadByte
);
474 if not (B
in [#10, #13]) then S
:= S
+ B
;
481 function WriteLineToStream(Stream
: TStream
; Line
: string): boolean;
487 Stream
.WriteBuffer(PChar(Line
)^, Length(Line
));
494 { Object constructors/destructors: }
496 constructor TArgumentParser
.Create(RawArguments
: array of string; AllowedPrefixes
: array of string);
499 for i
:= 0 to Length(RawArguments
) - 1 do
500 ParseArgument(RawArguments
[i
], AllowedPrefixes
);
503 destructor TArgumentParser
.Destroy
;
505 SetLength(Arguments
, 0);
508 constructor TNamedObject
.Create(const Name
: string; Config
: TINIFile
; const Section
: string);
512 FAliases
:= TStringList
.Create
;
513 FAliases
.Delimiter
:= ',';
514 FAliases
.DelimitedText
:= FName
+ ',' + Config
.ReadString(Section
, 'Alias', '');
517 constructor TMainServerConfig
.Create(Config
: TINIFile
);
518 var i
: integer; rawaddresslist
: string; portlist
: TStringList
;
520 inherited Create(Config
.ReadString('Server', 'Name', ''), Config
, 'Server');
521 FListenAddresses
:= TStringList
.Create
;
522 FListenAddresses
.Delimiter
:= ',';
524 rawaddresslist
:= Config
.ReadString('Server', 'ListenAddress', '');
525 if rawaddresslist
<> '' then
526 FListenAddresses
.DelimitedText
:= rawaddresslist
528 portlist
:= TStringList
.Create
;
529 portlist
.Delimiter
:= ',';
530 portlist
.DelimitedText
:= Config
.ReadString('Server', 'ListenPort', '25');
531 for i
:= 0 to portlist
.Count
- 1 do
532 FListenAddresses
.Add('0.0.0.0:' + portlist
.Strings
[i
]);
536 FListenAddresses6
:= TStringList
.Create
;
537 FListenAddresses6
.Delimiter
:= ',';
538 FListenAddresses6
.DelimitedText
:= Config
.ReadString('Server', 'ListenAddress6', '');
540 FDatabytes
:= Config
.ReadInteger('Server', 'Databytes', 1024 * 1024 * 1024);
541 FTimeOffset
:= Config
.ReadInteger('Server', 'TimeOffset', Config
.ReadInteger('Server', 'TimeCorrection', 0) * 100);
542 FTimeOffsetStr
:= MakeTimeOffsetStr(FTimeOffset
);
544 FPolicies
:= Config
.ReadBool('Server', 'Policies', false);
545 FMailbox
:= Config
.ReadBool('Server', 'Mailbox', false);
546 FRelay
:= Config
.ReadBool('Server', 'Relay', false);
547 FLog
:= Config
.ReadBool('Server', 'Log', false);
550 constructor TEMailProperties
.Create
;
557 constructor TIPNamePair
.Create(const Name
, IP
: string);
563 constructor TEnvelope
.Create
;
567 FReturnPathSpecified
:= false;
569 SetLength(FRecipients
, 0);
572 destructor TEnvelope
.Destroy
;
574 SetLength(FRecipients
, 0);
581 procedure TArgumentParser
.ParseArgument(Arg
: string; const AllowedPrefixes
: array of string);
582 var i
, n
: integer; found
: boolean;
584 { Strip prefix if present. }
585 i
:= 0; found
:= false;
586 while ((i
< Length(AllowedPrefixes
)) and (not found
)) do begin
587 if pos(AllowedPrefixes
[i
], Arg
) = 1 then
589 Delete(Arg
, 1, Length(AllowedPrefixes
[i
]));
595 n
:= Length(Arguments
);
596 SetLength(Arguments
, n
+ 1);
597 SplitParameters(Arg
, Arguments
[n
].Option
, Arguments
[n
].Value
, '=');
598 { To be case-insensitive: }
599 Arguments
[n
].Option
:= UpCase(Arguments
[n
].Option
);
602 function TArgumentParser
.GetArgument(ID
: integer): TArgument
;
604 { No index checking... you'd better use it return value of ValidateArguments. }
605 Result
:= Arguments
[ID
];
608 function TArgumentParser
.IsPresent(ArgumentName
: string): boolean;
612 while (i
< Length(Arguments
)) and (Arguments
[i
].Option
<> UpCase(ArgumentName
)) do
614 Result
:= i
< Length(Arguments
);
617 function TArgumentParser
.GetValue(ArgumentName
: string; DefValue
: string = ''): string;
621 while (i
< Length(Arguments
)) and (Arguments
[i
].Option
<> UpCase(ArgumentName
)) do
624 if i
< Length(Arguments
) then begin
625 if Arguments
[i
].Value
<> '' then
626 Result
:= Arguments
[i
].Value
634 function TArgumentParser
.ValidateArguments(ValidArguments
: array of string): integer;
635 { Returns -1 if all arguments are valid. Otherwise, returns the ID of the first
640 while (i
< Length(Arguments
)) and InStringArray(Arguments
[i
].Option
, ValidArguments
) do
643 if i
>= Length(Arguments
) then
650 function TNamedObject
.IsItYourName(const Name
: string): boolean;
652 Result
:= FAliases
.IndexOf(Name
) <> -1;
656 function TMainServerConfig
.GetVersionStr
: string;
658 Result
:= VERSION_STR
;
662 function TIPNamePair
.Copy
: TIPNamePair
;
664 Result
:= TIPNamePair
.Create(Name
, IP
);
668 procedure TEMailProperties
.SetSize(Value
: longint);
673 procedure TEMailProperties
.WriteFlags(Value
: TEMailFlags
);
678 procedure TEMailProperties
.SetFlag(Flag
: TEMailFlags
);
680 FFlags
:= FFlags
or Flag
;
683 function TEMailProperties
.HasFlag(Flag
: TEMailFlags
): boolean;
685 Result
:= (FFlags
and Flag
) = Flag
;
689 function TEnvelope
.GetNumberOfRecipients
: integer;
691 Result
:= Length(FRecipients
);
694 function TEnvelope
.GetRecipient(Index
: integer): TRecipient
;
696 Result
:= FRecipients
[Index
];
699 function TEnvelope
.IsComplete
: boolean;
701 Result
:= FReturnPathSpecified
and (Length(FRecipients
) > 0);
704 procedure TEnvelope
.AddRecipient(Address
: string; Data
: integer = 0; RMsg
: string = '');
707 i
:= Length(FRecipients
);
708 SetLength(FRecipients
, i
+ 1);
709 FRecipients
[i
].Address
:= Address
;
710 FRecipients
[i
].RMsg
:= RMsg
;
711 FRecipients
[i
].Data
:= Data
;
714 procedure TEnvelope
.AddRecipient(Recipient
: TRecipient
);
717 i
:= Length(FRecipients
);
718 SetLength(FRecipients
, i
+ 1);
719 FRecipients
[i
]:= Recipient
;
722 procedure TEnvelope
.SetRecipientData(Index
, Data
: integer; RMsg
: string = '');
724 FRecipients
[Index
].RMsg
:= RMsg
;
725 FRecipients
[Index
].Data
:= Data
;
728 procedure TEnvelope
.SetAllRecipientData(Data
: integer; RMsg
: string = '');
731 for i
:= 0 to Length(FRecipients
) - 1 do
732 SetRecipientData(i
, Data
, RMsg
);
735 procedure TEnvelope
.SetReturnPath(Address
: string);
737 FReturnPath
:= Address
;
738 FReturnPathSpecified
:= true;
741 procedure TEnvelope
.SetRelayHost(HostName
: string);
743 FRelayHost
:= HostName
;