Skip to content

Commit

Permalink
Added option to send an email when errors occur while handling tuner …
Browse files Browse the repository at this point in the history
…requests

Made tuner count logic more robust by parsing UPnP response XML
Fixed bug where video streams could get stuck streaming even after the client/dvr software disconnects
  • Loading branch information
Craig Moksnes committed Apr 4, 2020
1 parent e05841c commit e8d88a2
Show file tree
Hide file tree
Showing 12 changed files with 1,097 additions and 51 deletions.
177 changes: 177 additions & 0 deletions EmailUtils.pas
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
unit EmailUtils;

interface

uses
System.Classes,
System.SysUtils,

IdSMTP,
IdEmailAddress,
IdMessage,
IdMessageBuilder,
IdSSLOpenSSL,
IdSSLOpenSSLHeaders,
IdExplicitTLSClientServerBase;

{$SCOPEDENUMS ON}

const
cEmailDefaultPort = 25;

type
TEmailTLSOption = (
None,
NoTLSSupport,
UseImplicitTLS,
UseRequireTLS,
UseExplicitTLS
);
TEmailSSLVersion = (
None,
SSLv2,
SSLv23,
SSLv3,
TLSv1,
TLSv1_1,
TLSv1_2
);

TEmailServerSettings = class(TPersistent)
private
fServerPort: Integer;
fServerAddress: String;
fSSLVersion: TEmailSSLVersion;
fPassword: String;
fUsername: String;
fTLSOption: TEmailTLSOption;
public
constructor Create;

procedure Assign(Source: TPersistent); override;
procedure AssignTo(Dest: TPersistent); override;
published
property ServerAddress: String read fServerAddress write fServerAddress;
property ServerPort: Integer read fServerPort write fServerPort;
property TLSOption: TEmailTLSOption read fTLSOption write fTLSOption;
property SSLVersion: TEmailSSLVersion read fSSLVersion write fSSLVersion;
property Username: String read fUsername write fUsername;
property Password: String read fPassword write fPassword;
end;

TEmail = class(TPersistent)
private
fBody: String;
fSubject: String;
fRecipients: String;
fSender: String;
public
property Sender: String read fSender write fSender;
property Recipients: String read fRecipients write fRecipients;
property Subject: String read fSubject write fSubject;
property Body: String read fBody write fBody;
end;

TEmailUtils = class abstract
public
class procedure Send(const aSettings: TEmailServerSettings; const aEmail: TEmail); static;
end;

implementation

{ TEmailServerSettings }

procedure TEmailServerSettings.AssignTo(Dest: TPersistent);
begin
if Dest is TEmailServerSettings then
begin
TEmailServerSettings(Dest).fServerAddress := fServerAddress;
TEmailServerSettings(Dest).fServerPort := fServerPort;
TEmailServerSettings(Dest).fTLSOption := fTLSOption;
TEmailServerSettings(Dest).fSSLVersion := fSSLVersion;
TEmailServerSettings(Dest).fUsername := fUsername;
TEmailServerSettings(Dest).fPassword := fPassword;
end
else
inherited;
end;

procedure TEmailServerSettings.Assign(Source: TPersistent);
begin
if not Assigned(Source) then
begin
fServerPort := cEmailDefaultPort;
end
else
inherited;
end;


constructor TEmailServerSettings.Create;
begin
Assign(nil);
end;

{ TEmailUtils }

class procedure TEmailUtils.Send(const aSettings: TEmailServerSettings;
const aEmail: TEmail);
var
lSMTP: TIdSMTP;
lMessage: TIdMessage;
begin
lSMTP := TIdSMTP.Create(nil);
try
lSMTP.IOHandler := TIdSSLIOHandlerSocketOpenSSL.Create;
case aSettings.TLSOption of
TEmailTLSOption.NoTLSSupport: lSMTP.UseTLS := utNoTLSSupport;
TEmailTLSOption.UseImplicitTLS: lSMTP.UseTLS := utUseImplicitTLS;
TEmailTLSOption.UseRequireTLS: lSMTP.UseTLS := utUseRequireTLS;
TEmailTLSOption.UseExplicitTLS: lSMTP.UseTLS := utUseExplicitTLS;
end;
case aSettings.SSLVersion of
TEmailSSLVersion.SSLv2: TIdSSLIOHandlerSocketOpenSSL(lSMTP.IOHandler).SSLOptions.Method := sslvSSLv2;
TEmailSSLVersion.SSLv23: TIdSSLIOHandlerSocketOpenSSL(lSMTP.IOHandler).SSLOptions.Method := sslvSSLv23;
TEmailSSLVersion.SSLv3: TIdSSLIOHandlerSocketOpenSSL(lSMTP.IOHandler).SSLOptions.Method := sslvSSLv3;
TEmailSSLVersion.TLSv1: TIdSSLIOHandlerSocketOpenSSL(lSMTP.IOHandler).SSLOptions.Method := sslvTLSv1;
TEmailSSLVersion.TLSv1_1: TIdSSLIOHandlerSocketOpenSSL(lSMTP.IOHandler).SSLOptions.Method := sslvTLSv1_1;
TEmailSSLVersion.TLSv1_2: TIdSSLIOHandlerSocketOpenSSL(lSMTP.IOHandler).SSLOptions.Method := sslvTLSv1_2;
end;

lSMTP.Host := aSettings.ServerAddress;
lSMTP.Port := aSettings.ServerPort;

if aSettings.Username <> '' then
begin
lSMTP.AuthType := satDefault;
lSMTP.Username := aSettings.Username;
lSMTP.Password := aSettings.Password;
end
else
lSMTP.AuthType := satNone;

lMessage := TIdMessage.Create(nil);
try
lMessage.From.Text := aEmail.Sender;
lMessage.ReplyTo.EMailAddresses := aEmail.Sender;

lMessage.Recipients.EMailAddresses := aEmail.Recipients;

lMessage.Subject := aEmail.Subject;

lMessage.Body.Text := aEmail.Body;

lMessage.Date := Now;

lSMTP.Connect;
lSMTP.Send(lMessage);
finally
lMessage.Free;
end;
finally
lSMTP.Free;
end;

end;

end.
16 changes: 16 additions & 0 deletions LogUtils.pas
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ interface
ILogger = interface
['{772B05D3-D06A-4E0D-A259-929772F8704D}']
procedure Log(const aLogName: String; const aMessage: String);
procedure LogError(const aLogName: String; const aMessage: String);
end;

TLogger = class abstract
Expand All @@ -24,6 +25,9 @@ TLogger = class abstract
class procedure Log(const aLogName: String; const aMessage: String); static;
class procedure LogFmt(const aLogName: String; const aMessage: String; const aArgs: array of const); static;

class procedure LogError(const aLogName: String; const aMessage: String); static;
class procedure LogErrorFmt(const aLogName: String; const aMessage: String; const aArgs: array of const); static;

class procedure SetLogger(const aLogger: ILogger); static;
end;

Expand All @@ -47,4 +51,16 @@ class procedure TLogger.LogFmt(const aLogName: String; const aMessage: String; c
Log(aLogName, Format(aMessage, aArgs));
end;

class procedure TLogger.LogError(const aLogName, aMessage: String);
begin
if Assigned(fLogger) then
fLogger.LogError(aLogName, aMessage);
end;

class procedure TLogger.LogErrorFmt(const aLogName, aMessage: String;
const aArgs: array of const);
begin
LogError(aLogName, Format(aMessage, aArgs));
end;

end.
Loading

0 comments on commit e8d88a2

Please sign in to comment.