From: MegaBrutal <code+git@megabrutal.com>
Date: Mon, 12 Sep 2016 20:33:12 +0000 (+0200)
Subject: Catch CTRL-C event in user mode
X-Git-Url: http://git.megabrutal.com/?a=commitdiff_plain;h=a24c25ac25193f6cc296af1765fa25b68aa8525f;p=mgsmtp.git

Catch CTRL-C event in user mode

	modified:   MgSMTP.pas
	modified:   todo.txt
---

diff --git a/MgSMTP.pas b/MgSMTP.pas
index b172a10..1cee119 100644
--- a/MgSMTP.pas
+++ b/MgSMTP.pas
@@ -1,6 +1,6 @@
 {
    MegaBrutal's SMTP Server (MgSMTP)
-   Copyright (C) 2010-2015 MegaBrutal
+   Copyright (C) 2010-2016 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  =  'Detect unexpected disconnection';
+   DEVCOMMENT  =  'Catch CTRL-C in user mode';
 
 var
 
@@ -130,6 +130,16 @@ begin
    end;
 end;
 
+function ConsoleCtrlHandler(Signal: dword): longbool; stdcall;
+{ Handle CTRL-C event in user mode. }
+begin
+   if Signal = CTRL_C_EVENT then begin
+      Out.writeln('Caught CTRL-C signal.');
+      Stopping:= true;
+   end;
+   Result:= true;
+end;
+
 procedure Service(Argc: dword; Argv: pointer); stdcall;
 var ProposedExitCode: integer;
 begin
@@ -302,6 +312,7 @@ begin
       else begin
          if Cmdline.IsPresent('USERMODE') then begin
             Out.writeln('Starting MgSMTP in user mode...');
+            SetConsoleCtrlHandler(ConsoleCtrlHandler, true);
             Service(0, nil);
          end
          else begin
diff --git a/todo.txt b/todo.txt
index 58ac308..a76e304 100644
--- a/todo.txt
+++ b/todo.txt
@@ -22,7 +22,7 @@ v0.9u:
 v0.9t:
 - Option to disable MX lookups
 - Separate config files or work directories supplied in parameters
-- Process CTRL-C properly to quit from user mode gracefully
++ 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