'*************************************************************************
'
'      Author       : Esben Laursen (hyber@hyber.dk)
'      
'      Webpage      : http://www.hyber.dk or http://netsmsgw.sf.net
'
'      Notes        :
'
'      License      : This program is free software; you can redistribute
'                     it and/or modify it under the terms of the GNU
'                     General Public License as published by the Free
'                     Software Foundation version 2 of the License.
'
'*************************************************************************

Imports Microsoft.Win32
Imports System.IO
Imports System.IO.Ports
Imports netSMSgw.logUtils

<Serializable()> _
Public Class clsSettings

    Dim mstrIPadr As String = "0.0.0.0"
    Dim mintPort As String = 25
    Dim mstrSMTPName As String = "sms.hyber.dk"
    Dim mintWaitSec As Integer = 5
    Dim mstrCOMPort As String = "COM1"
    Dim mintPortRate As Integer = 115200
    Dim mporParity As System.IO.Ports.Parity = IO.Ports.Parity.None
    Dim mintDataBits As Integer = 8
    Dim mporStopBits As System.IO.Ports.StopBits = StopBits.One
    Dim mporHandShake As Handshake = Handshake.RequestToSend
    Dim mbolDTR As Boolean = True
    Dim mbolRTS As Boolean = True
    Dim menuLog As LogLevels = LogLevels.Info
    Dim mstrLogFile As String = System.IO.Path.GetDirectoryName(GetType(netSMSgw.netSMSgwd.clsSettings).Assembly.CodeBase.ToString.Remove(0, 8)) & "\netSMSgwd.log"
    Dim mbolSaveLog As Boolean = True
    Dim mbolSaveStats As Boolean = True
    Dim mstrStatsFile As String = System.IO.Path.GetDirectoryName(GetType(netSMSgw.netSMSgwd.clsSettings).Assembly.CodeBase.ToString.Remove(0, 8)) & "\netSMSgwd_stats.xml"
    Dim mbolAsService As Boolean = True
    Dim mbolFakeIt As Boolean = False
    Dim mstrInstallUtil As String = "C:\Windows\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe"
    Dim mbolCheckVer As Boolean = True
    Dim mintWaitRead As Integer = 4000
    Dim mintWaitWrite As Integer = 4000
    Dim mcolSMTPHosts As New netSMSgw.netSMSgwd.xmlCollection
    Dim mbolSMTPAllow As Boolean = True
    Dim mbolSMTPDeny As Boolean = False
    Dim mcolHttpUsers As New netSMSgw.httpUserDB
    Dim mintHttpPort As Integer = 80
    Dim mstrHttpDomain As String = "*"
    Dim mbolHttpEnabled As Boolean = False
    Dim mbolHttpUsersEnabled As Boolean = True
    Dim mdatSmsStartTime As Date = "1800-01-01T00:00:00"
    Dim mdatSmsEndTime As Date = "1800-01-01T23:59:59"
    Dim mbolSmsRestrictTime As Boolean = False
    Dim mbolSmsIgnoreHighPrio As Boolean = True 'ignore time restrictions for high priority messages.
    Dim mstrInstallID As String = RandomString(50)
    Dim mbolStatsAllowPH As Boolean = True
    Dim mstrStatsPHAdr As String = "stats.netsmsgw.hyber.dk"
    Dim mintStatsPHPort As Integer = 7698
    Dim mintStatsPHInterval As Integer = 86400 '24 hours
    Dim mbolStatsPrivacyState As Boolean = False
    Dim mbolSMTPEnable As Boolean = True





#Region "properties general"

    Public Property InstallationID() As String
        Get
            Return mstrInstallID
        End Get
        Set(ByVal value As String)
            mstrInstallID = value
        End Set
    End Property

    Public Property CheckNewVersion() As Boolean
        Get
            Return mbolCheckVer
        End Get
        Set(ByVal value As Boolean)
            mbolCheckVer = value
        End Set
    End Property

    Public Property InstallUtilFile() As String
        Get
            Return mstrInstallUtil
        End Get
        Set(ByVal value As String)
            mstrInstallUtil = value
        End Set
    End Property

    Public Property FakeIt() As Boolean
        Get
            Return mbolFakeIt
        End Get
        Set(ByVal value As Boolean)
            mbolFakeIt = value
        End Set
    End Property

    Public Property RunAsService() As Boolean
        Get
            Return mbolAsService
        End Get
        Set(ByVal value As Boolean)
            mbolAsService = value
        End Set
    End Property

#End Region

#Region "properties SMS"

    Public Property smsIgnoreRestrictionOnHighPriority() As Boolean
        Get
            Return mbolSmsIgnoreHighPrio
        End Get
        Set(ByVal value As Boolean)
            mbolSmsIgnoreHighPrio = value
        End Set
    End Property

    Public Property smsRestrictTime() As Boolean
        Get
            Return mbolSmsRestrictTime
        End Get
        Set(ByVal value As Boolean)
            mbolSmsRestrictTime = value
        End Set
    End Property

    Public Property smsPeekHourStart() As Date
        Get
            Return mdatSmsStartTime
        End Get
        Set(ByVal value As DateTime)
            mdatSmsStartTime = value
        End Set
    End Property

    Public Property smsPeekHourEnd() As Date
        Get
            Return mdatSmsEndTime
        End Get
        Set(ByVal value As DateTime)
            mdatSmsEndTime = value
        End Set
    End Property

    Public Property smsTimeOutReadMs() As Integer
        Get
            Return mintWaitRead
        End Get
        Set(ByVal value As Integer)
            mintWaitRead = value
        End Set
    End Property

    Public Property smsTimeOutWriteMs() As Integer
        Get
            Return mintWaitWrite
        End Get
        Set(ByVal value As Integer)
            mintWaitWrite = value
        End Set
    End Property

    Public Property smsDataBits() As Integer
        Get
            Return mintDataBits
        End Get
        Set(ByVal value As Integer)
            mintDataBits = value
        End Set
    End Property

    Public Property smsStopBits() As System.IO.Ports.StopBits
        Get
            Return mporStopBits
        End Get
        Set(ByVal value As System.IO.Ports.StopBits)
            mporStopBits = value
        End Set
    End Property

    Public WriteOnly Property smsStopBitsStr() As String
        Set(ByVal value As String)
            Select Case LCase(value)
                Case "none" : mporStopBits = StopBits.None
                Case "one" : mporStopBits = StopBits.One
                Case "onepointfive" : mporStopBits = StopBits.OnePointFive
                Case "two" : mporStopBits = StopBits.Two
            End Select
        End Set
    End Property

    Public Property smsHandShake() As System.IO.Ports.Handshake
        Get
            Return mporHandShake
        End Get
        Set(ByVal value As System.IO.Ports.Handshake)
            mporHandShake = value
        End Set
    End Property

    'for some reason we cant set HandShake enum with a string. We can do that with every other..
    'we therefore the "extra writeonly" property.
    Public WriteOnly Property smsHandShakeStr() As String
        Set(ByVal value As String)
            Select Case LCase(value)
                Case "none" : mporHandShake = Handshake.None
                Case "requesttosend" : mporHandShake = Handshake.RequestToSend
                Case "requesttoSendxonxoff" : mporHandShake = Handshake.RequestToSendXOnXOff
                Case "xonxoff" : mporHandShake = Handshake.XOnXOff
            End Select
        End Set
    End Property


    Public Property smsBaudRate() As Integer
        Get
            Return mintPortRate
        End Get
        Set(ByVal value As Integer)
            mintPortRate = value
        End Set
    End Property

    Public Property smsParity() As System.IO.Ports.Parity
        Get
            Return mporParity
        End Get
        Set(ByVal value As System.IO.Ports.Parity)
            mporParity = value
        End Set
    End Property

    Public WriteOnly Property smsParityStr() As String
        Set(ByVal value As String)
            Select Case LCase(value)
                Case "none" : mporParity = Parity.None
                Case "even" : mporParity = Parity.Even
                Case "mark" : mporParity = Parity.Mark
                Case "odd" : mporParity = Parity.Odd
                Case "space" : mporParity = Parity.Space
            End Select
        End Set
    End Property


    Public Property smsDTR() As Boolean
        Get
            Return mbolDTR
        End Get
        Set(ByVal value As Boolean)
            mbolDTR = value
        End Set
    End Property

    Public Property smsRTS() As Boolean
        Get
            Return mbolRTS
        End Get
        Set(ByVal value As Boolean)
            mbolRTS = value
        End Set
    End Property

    Public Property smsComPort() As String
        Get
            Return mstrCOMPort
        End Get
        Set(ByVal value As String)
            mstrCOMPort = value
        End Set
    End Property

    Public Property smsWaitSec() As Integer
        Get
            Return mintWaitSec
        End Get
        Set(ByVal value As Integer)
            mintWaitSec = value
        End Set
    End Property


#End Region

#Region "properties SMTP"

    Public Property smtpEnabled() As Boolean
        Get
            Return mbolSMTPEnable
        End Get
        Set(ByVal value As Boolean)
            mbolSMTPEnable = value
        End Set
    End Property

    Public Property smtpListenIPAdr() As String
        Get
            Return mstrIPadr
        End Get
        Set(ByVal value As String)
            mstrIPadr = value
        End Set
    End Property

    Public Property smtpListenPort() As Integer
        Get
            Return mintPort
        End Get
        Set(ByVal value As Integer)
            mintPort = value
        End Set
    End Property

    Public Property smtpServerName() As String
        Get
            Return mstrSMTPName
        End Get
        Set(ByVal value As String)
            mstrSMTPName = value
        End Set
    End Property

    Public Property smtpHosts() As netSMSgw.netSMSgwd.xmlCollection
        Get
            Return mcolSMTPHosts
        End Get
        Set(ByVal value As netSMSgw.netSMSgwd.xmlCollection)
            mcolSMTPHosts = value
        End Set
    End Property

    Public Property smtpDenyAll() As Boolean
        Get
            Return mbolSMTPDeny
        End Get
        Set(ByVal value As Boolean)
            mbolSMTPDeny = value
        End Set
    End Property

    Public Property smtpAllowAll() As Boolean
        Get
            Return mbolSMTPAllow
        End Get
        Set(ByVal value As Boolean)
            mbolSMTPAllow = value
        End Set
    End Property

#End Region

#Region "properties HTTP"

    Public Property httpEnabled() As Boolean
        Get
            Return mbolHttpEnabled
        End Get
        Set(ByVal value As Boolean)
            mbolHttpEnabled = value
        End Set
    End Property

    Public Property httpPort() As Integer
        Get
            Return mintHttpPort
        End Get
        Set(ByVal value As Integer)
            mintHttpPort = value
        End Set
    End Property

    Public Property httpListenDomain() As String
        Get
            Return mstrHttpDomain
        End Get
        Set(ByVal value As String)
            mstrHttpDomain = value
        End Set
    End Property

    Public Property httpEnableUsers() As Boolean
        Get
            Return mbolHttpUsersEnabled
        End Get
        Set(ByVal value As Boolean)
            mbolHttpUsersEnabled = value
        End Set
    End Property

    Public Property httpUsersDB() As netSMSgw.httpUserDB
        Get
            Return mcolHttpUsers
        End Get
        Set(ByVal value As netSMSgw.httpUserDB)
            mcolHttpUsers = value
        End Set
    End Property

#End Region

#Region "properties - Stats"

    Public Property statsSave() As Boolean
        Get
            Return mbolSaveStats
        End Get
        Set(ByVal value As Boolean)
            mbolSaveStats = value
        End Set
    End Property

    Public Property statsFile() As String
        Get
            Return mstrStatsFile
        End Get
        Set(ByVal value As String)
            mstrStatsFile = value
        End Set
    End Property

    Public Property statsAllowPhoneHome() As Boolean
        Get
            Return mbolStatsAllowPH
        End Get
        Set(ByVal value As Boolean)
            mbolStatsAllowPH = value
        End Set
    End Property

    Public Property statsShownPrivacyStatement() As Boolean
        Get
            Return mbolStatsPrivacyState
        End Get
        Set(ByVal value As Boolean)
            mbolStatsPrivacyState = value
        End Set
    End Property

    Public Property statsPhoneHomeInterval() As Integer
        Get
            Return mintStatsPHInterval
        End Get
        Set(ByVal value As Integer)
            mintStatsPHInterval = value
        End Set
    End Property


    Public Property statsPhoneHomeAdr() As String
        Get
            Return mstrStatsPHAdr
        End Get
        Set(ByVal value As String)
            mstrStatsPHAdr = value
        End Set
    End Property

    Public Property statsPhoneHomePort() As Integer
        Get
            Return mintStatsPHPort
        End Get
        Set(ByVal value As Integer)
            mintStatsPHPort = value
        End Set
    End Property

#End Region

#Region "properties - Logging"

    Public Property SaveLogToFile() As Boolean
        Get
            Return mbolSaveLog
        End Get
        Set(ByVal value As Boolean)
            mbolSaveLog = value
        End Set
    End Property

    Public Property LogFile() As String
        Get
            Return mstrLogFile
        End Get
        Set(ByVal value As String)
            mstrLogFile = value
        End Set
    End Property

    Public Property LogLevel() As LogLevels
        Get
            Return menuLog
        End Get
        Set(ByVal value As LogLevels)
            menuLog = value
        End Set
    End Property

#End Region

    Public Shared Sub Serialize(ByVal filename As String, ByVal item As clsSettings)
        Try

            If Not Directory.Exists(Path.GetDirectoryName(filename)) Then
                Directory.CreateDirectory(Path.GetDirectoryName(filename))
            End If

            ' Create a new XmlSerializer instance.
            Dim s As New Xml.Serialization.XmlSerializer(GetType(clsSettings))


            ' Writing the XML file to disk requires a TextWriter.
            Dim writer As New StreamWriter(filename)

            ' Serialize the object, and close the StreamWriter.
            s.Serialize(writer, item)

            writer.Close()


        Catch x As System.InvalidOperationException
            Throw New Exception("Check class cItem, all derrived classes must be listed with the [ XmlInclude(GetType(derrivedClass)) ] attribute!")
        End Try
    End Sub

    Public Shared Function Deserialize(ByVal filename As String) As clsSettings
        Try
            If IO.File.Exists(filename) Then
                Dim fs As New IO.FileStream(filename, FileMode.Open)
                Dim w As New Xml.Serialization.XmlSerializer(GetType(clsSettings))
                Dim xmlReader As Xml.XmlReader = New Xml.XmlTextReader(fs)

                If w.CanDeserialize(xmlReader) Then
                    Dim objSettings2 As clsSettings = CType(w.Deserialize(xmlReader), clsSettings)

                    fs.Close()
                    Return objSettings2
                End If
            End If

            Dim objSettings As New clsSettings
            Return objSettings
        Catch ex As Exception
            Call AddToLog(gstrCriticalErr & ex.Message.ToString, netSMSgw.logUtils.LogLevels.Critical)
            Call AddToLog(gstrReturnNew, LogLevels.Warnings)
            Dim oSet As New clsSettings
            Return oSet
        End Try

    End Function

    Private Function RandomString(ByVal pintLength As Integer) As String
        Dim builder As New System.Text.StringBuilder
        Dim r As New Random()
        Dim ch As Char
        Dim i As Integer
        Dim i2 As Integer


        For i = 0 To pintLength - 1
            i2 = r.Next(45, 125)
            Select Case Char.IsLetterOrDigit(Convert.ToChar(i2))
                Case True
                    ch = Convert.ToChar(i2)
                    builder.Append(ch)
                Case False
                    i -= 1
            End Select
        Next
        Return builder.ToString()
    End Function


    Protected Overrides Sub Finalize()
        MyBase.Finalize()
    End Sub
End Class

'<Serializable()> _
'Public Class SMTPHosts
'    Public Host As String
'    Public CIDR As Integer

'End Class


<Serializable()> _
Public Class xmlCollection
    Inherits System.Collections.CollectionBase


    Public Overridable Function Add(ByVal Value As String) As Integer
        MyBase.List.Add(Value)
    End Function

    Public Overridable Sub Remove(ByVal pintIndex As Integer)
        MyBase.List.RemoveAt(pintIndex)
    End Sub

    Default Public Overridable Property Item(ByVal Index As Integer) As String
        Get
            Return DirectCast(MyBase.List.Item(Index), String)
        End Get
        Set(ByVal Value As String)
            MyBase.List.Item(Index) = Value
        End Set
    End Property

End Class