Fall back to HELO when remote server doesn't understand EHLO
[mgsmtp.git] / MgSMTP.pas
index cecfdc9ff054846e81f3ed3afd6b7581b40ad850..181d969171d98cc44c1173ade478ec05d23ff81e 100644 (file)
@@ -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
@@ -42,19 +42,24 @@ const
          )
       );
 
+   ArgumentPrefixes: array[0..2] of string = ('/', '--', '-');
+   ValidArguments: array[0..4] of string = ('?', 'HELP', 'INSTALL', 'UNINSTALL', 'USERMODE');
+
    { For development test builds, you can add a developer comment here to
      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  =  'Testing new parameters';
+   DEVCOMMENT  =  'EHLO->HELO fallback';
 
 var
 
+   Cmdline: TArgumentParser;
    Config: TINIFile;
    hSCManager, hService: THandle;
    hSvcStatusHandle: THandle;
    SvcStatus: TServiceStatus;
    ServiceMode, Stopping: boolean;
+   WrongArgument: integer;
 
 
 procedure AddDevComment(Log: TStreamLogger);
@@ -236,17 +241,14 @@ begin
    Out.writeln;
 
    { TODO: Process arguments here. }
+   Cmdline:= TArgumentParser.Create(CmdlineToStringArray, ArgumentPrefixes);
+   WrongArgument:= Cmdline.ValidateArguments(ValidArguments);
 
-   ServiceMode:= false;
-
-   if ParamCount > 0 then begin
+   if WrongArgument = -1 then begin
 
-      if UpperCase(ParamStr(1)) = '/USERMODE' then begin
-         Out.writeln('Starting MgSMTP in user mode...');
-         Service(0, nil);
-      end
+      ServiceMode:= false;
 
-      else if ParamStr(1) = '/?' then begin
+      if Cmdline.IsPresent('?') or Cmdline.IsPresent('HELP') then begin
          Out.writeln('Supported arguments:');
          Out.writeln('/INSTALL   - registers the actual MgSMTP binary');
          Out.writeln('             as a Windows service.');
@@ -265,11 +267,11 @@ begin
          Out.writeln('https://sourceforge.net/projects/mgsmtp/');
       end
 
-      else if (UpperCase(ParamStr(1)) = '/INSTALL') or (UpperCase(ParamStr(1)) = '/UNINSTALL') then begin
+      else if (Cmdline.IsPresent('INSTALL') or Cmdline.IsPresent('UNINSTALL')) then begin
          { Register / unregister service. }
          hSCManager:= OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
          if hSCManager <> 0 then begin
-            if UpperCase(ParamStr(1)) = '/INSTALL' then begin
+            if Cmdline.IsPresent('INSTALL') then begin
                if CreateService(hSCManager, 'MgSMTP', 'MegaBrutal''s SMTP Server (MgSMTP)', SC_MANAGER_ALL_ACCESS,
                   SERVICE_WIN32_OWN_PROCESS,
                   SERVICE_AUTO_START, SERVICE_ERROR_NORMAL,
@@ -283,7 +285,7 @@ begin
                   else
                      Out.writeln('CreateService failed!');
             end
-            else if UpperCase(ParamStr(1)) = '/UNINSTALL' then begin
+            else if Cmdline.IsPresent('UNINSTALL') then begin
                hService:= OpenService(hSCManager, 'MgSMTP', SC_MANAGER_ALL_ACCESS);
                if hService <> 0 then begin
                   if DeleteService(hService) then
@@ -292,25 +294,39 @@ begin
                      Out.writeln('DeleteService failed!');
                end
                else Out.writeln('OpenService failed!');
-            end
-            else Out.writeln('Unknown parameter.');
+            end;
          end
          else Out.writeln('OpenSCManager failed!');
       end
-      else Out.writeln('Unknown parameter specified!');
+
+      else begin
+         if Cmdline.IsPresent('USERMODE') then begin
+            Out.writeln('Starting MgSMTP in user mode...');
+            Service(0, nil);
+         end
+         else 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;
+            ServiceMode:= true;
+            if not StartServiceCtrlDispatcher(ServiceTable) then begin
+               ServiceMode:= false;
+               Out.writeln('Failed!');
+               Out.writeln;
+               Out.writeln('You need to start MgSMTP as a service,');
+               Out.writeln('or supply proper arguments!');
+               Out.writeln('Issue with /? for more information.');
+            end;
+         end;
+      end
+
    end
+
    else begin
 
-      Out.writeln('Trying to contact Service Control Manager...');
-      ServiceMode:= true;
-      if not StartServiceCtrlDispatcher(ServiceTable) then begin
-         ServiceMode:= false;
-         Out.writeln('Failed!');
-         Out.writeln;
-         Out.writeln('You need to start MgSMTP as a service,');
-         Out.writeln('or supply proper arguments!');
-         Out.writeln('Issue with /? for more information.');
-      end;
+      Out.writeln('Invalid argument: ' + Cmdline.GetArgument(WrongArgument).Option + '!');
 
    end;
 end.