-
Notifications
You must be signed in to change notification settings - Fork 0
/
SpamAssassinService.cs
99 lines (89 loc) · 2.93 KB
/
SpamAssassinService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#region using
using System;
using System.ServiceProcess;
using System.IO;
using System.Reflection;
using System.Configuration.Install;
#if DEBUG
using System.Runtime.InteropServices;
#endif
#endregion
namespace SpamAssassinService
{
partial class SpamAssassinService : ServiceBase
{
public SpamAssassinService()
{
InitializeComponent();
}
private readonly PollingService _pollingService = new PollingService();
public static void Main(string[] args)
{
Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);
if (Environment.UserInteractive)
{
string str = string.Concat(args);
if (string.IsNullOrEmpty(str))
return;
switch (str)
{
case "/install":
case "-install":
case "--install":
ManagedInstallerClass.InstallHelper(new string[] { Assembly.GetExecutingAssembly().Location });
break;
case "/uninstall":
case "-uninstall":
case "--uninstall":
ManagedInstallerClass.InstallHelper(new string[] { "/u", Assembly.GetExecutingAssembly().Location });
break;
#if DEBUG
case "/run":
case "-run":
case "--run":
RunConsole(args);
break;
#endif
default:
throw new NotImplementedException();
}
}
else
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[] { new SpamAssassinService() };
Run(ServicesToRun);
}
}
protected override void OnStart(string[] args)
{
_pollingService.StartPolling();
}
protected override void OnStop()
{
_pollingService.StopPolling();
}
#if DEBUG
public static void RunConsole(string[] args)
{
SpamAssassinService svc = new SpamAssassinService();
svc.OnStart(args);
_handler = new ConsoleEventDelegate(ConsoleEventCallback);
SetConsoleCtrlHandler(_handler, true);
}
private static bool ConsoleEventCallback(int eventType)
{
if (eventType == 2)
{
SpamAssassinService svc = new SpamAssassinService();
svc.OnStop();
}
return false;
}
private static ConsoleEventDelegate _handler;
private delegate bool ConsoleEventDelegate(int eventType);
[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool SetConsoleCtrlHandler(ConsoleEventDelegate callback, bool add);
#endif
}
}