X-Git-Url: http://git.megabrutal.com/?a=blobdiff_plain;f=MgSMTP.pas;h=08eaa2ac4650858c3ad2cffc348cfa2c3ed7d103;hb=4fc041f69ece7911d7c6f2400817054ca2d93316;hp=cecfdc9ff054846e81f3ed3afd6b7581b40ad850;hpb=4806fe76baf12d97f1afe2f9b29ea384d37aa839;p=mgsmtp.git diff --git a/MgSMTP.pas b/MgSMTP.pas index cecfdc9..08eaa2a 100644 --- a/MgSMTP.pas +++ b/MgSMTP.pas @@ -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 = 'Release Candidate 1'; 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.