#include-once
#include <inet.au3>
#include <array.au3>
#include <string.au3>
#include <Constants.au3>

;===============================================================================
;
; Function Name:    _random09az()
; Description:      Returns a given number of signs, one sign can be a-z or 0-9 or A-Z
; Parameter(s):     $iCount - Number of random signs ( standard = 1 )
; Requirement(s):   None
; Return Value(s):  On Success - Returns the random sign(s)
;                   On Failure - Returns -1 and sets @error
; Author(s):        qon
;
;===============================================================================

func _random09az($iCount = 1)
	If Stringisdigit($iCount) = 0 Then
		Seterror(1)
		Return -1
	EndIf
	
	Local $sString = ""
	
	For $i = $iCount to 1 step -1
		Local $iRandom = Random(1,4,1)
		IF $iRandom = 1 Then
			$iRandom = Chr(Random(48,57,1))
			$sString = $sString & $iRandom
			ContinueLoop
		EndIf
		If $iRandom = 2 Or $iRandom = 3 then
			$iRandom = Chr(Random(97,122,1))
			$sString = $sString & $iRandom
			ContinueLoop
		EndIf
		IF $iRandom = 4 Then
			$iRandom = Chr(Random(65,90,1))
			$sString = $sString & $iRandom
			ContinueLoop
		EndIf
	Next
	
	Return $sString
EndFunc

;===============================================================================
;
; Function Name:    _inetgetau3time()
; Langue:			German
; Description:      Returns an array, [0] = Time; [1] = Day in Month. Month, Year; [2] = Day in week ( for example Samstag )
; Requirement(s):   <inet.au3>, <array.au3>
; Return Value(s):  On Success - Returns the random sign(s)
;                   On Failure - Returns 0 and sets @error
;						1 = Homepage offline
;						2 = Other Date/Time format on homepage
; Author(s):        qon
;
;===============================================================================

func _inetgetau3time()
	$sSource = _inetgetsource("http://www.autoit.de/index.php?page=News")
	IF @error Then
		Seterror(1)
		Return 0
	EndIf
	$sSplit = Stringsplit($sSource,'<img src="wcf/icon/dateS.png" alt="" /> <span>',1)
	IF @error Then
		Seterror(2)
		Return 0
	EndIf
	$sCut = Stringleft($sSplit[2],70)
	$sSplit2 = Stringsplit($sCut,",",1)
	$sTime = Stringstripws($sSplit2[3],1)
	$sSplit3 = Stringsplit($sTime,"UTC",1)
	
	$nTime = Stringstripws($sSplit3[1],3)
	$sDay = Stringstripws($sSplit2[1],3)
	$nDate = Stringstripws($sSplit2[2],3)
	
	$aArray = _Arraycreate($nTime,$nDate,$sDay)
	Return $aArray
EndFunc

;===============================================================================
;
; Function Name:    _regread()
; Description:      Reads a registry entry ( with a given standard )
; Parameter(s):     $iKeyname - The registry key to read
;					$iValuename - The value to read
;					$iStandard - The standard that will be returned if registry entry does not exists [optional]
; Requirement(s):   None
; Return Value(s):  On Success - Returns the Registry Value, if @error occurres ( can't find key / unknown format ) the standard will be returned.
;                   On Failure - If no standard is set and can't read Key, the @error number from regread will be returned ( Helpfile ).
; Author(s):        qon
;
;===============================================================================

func _regread($sKeyname,$sValuename,$vStandard = "")
	Local $vValue
	Local $iError
	$vValue = Regread($sKeyname,$sValuename)
	$iError = @error
	If $iError <> 0 Then
		If $vStandard = "" Then
			Seterror($iError)
			Return ""
		EndIf
		Return $vStandard
	EndIf
	Return $vValue
EndFunc

;===============================================================================
;
; Function Name:    _wordinstr()
; Description:      Checks if a string contains a given word. ( <> Stringinstr! )
; Parameter(s):     $sString - The string to evaluate.
;					$iWord - The word to search.
; Requirement(s):   None
; Return Value(s):  On Success - Returns 1
;                   On Failure - Returns 0 and sets @error 1
; Author(s):        qon
;
;===============================================================================

Func _wordinstr($sString, $sWord)
	$sString = Stringstripws($sString,3)
	$sString = " " & $sString & " "
    If Stringinstr($sString," " & $sWord & " ") Then Return 1
	Seterror(1)
    Return 0
EndFunc

;===============================================================================
;
; Function Name:    _qoncrypt()
; Description:      En/de-crypts a given text.
; Parameter(s):     $iString - The string ( text ) to en/de-crypt.
;					$iMode - 1 = encrypt
;							 0 = decrypt
;					$iLevel [optional] - Level to encrypt/decrypt. Default = 3, higher = safer, maximum = 25
;										When you encrypt a string with lvl 5, you must decrypt it with the same level (5).
; Requirement(s):   Function: _random09az()
;					<string.au3>
; Return Value(s):  On Success - Returns the en/de-crypted string
;                   On Failure - Returns 0 and sets @error
; Author(s):        qon
;
;===============================================================================

func _qoncrypt($sString,$iMode,$iLevel = 3)
	Local $sEndString = ""
	Local $iFirst = 1
	
	If $iLevel > 25 Then
		Seterror(1)
		Return 0
	EndIf
	
	IF $iMode = 1 Then
		$vSplitted = Stringsplit($sString,"")
		For $s = $vSplitted[0] to 1 step -1
			If StringIsSpace($vSplitted[$s]) = 1 Then
				If $iFirst = 1 Then
					$sEndString = $sEndstring & _random09az($iLevel) & _random09az($iLevel) & Chr(127) & _random09az($iLevel)
					$iFirst = 0
				Else
					$sEndString = $sEndstring & _random09az($iLevel) & Chr(127) & _random09az($iLevel)
				EndIf
			Else
				If $iFirst = 1 Then
					$sEndString = $sEndstring & _random09az($iLevel) & _random09az($iLevel) & $vSplitted[$s] & _random09az($iLevel)
					$iFirst = 0
				Else
					$sEndString = $sEndstring & _random09az($iLevel) & $vSplitted[$s] & _random09az($iLevel)
				EndIf
			EndIf
		Next
		Return $sEndString
	EndIf
	
	If $iMode = 0 Then
		while 1
			$sString = Stringtrimleft($sString,$iLevel*2)
			IF $sString = "" then Exitloop
			$vCache = Stringleft($sString,1)
			If $vCache = Chr(127) Then
				$sEndString = $sEndString & " "
			Else
				$sEndString = $sEndString & $vCache
			EndIf
			$sString = StringTrimleft($sString,1)
		WEnd
		$sEndString = _stringreverse($sEndString)
		Return $sEndString
	EndIf
EndFunc

;===============================================================================
;
; Function Name:    _xGetIp()
; Description:      Get public IP address of a network/computer.
; Parameter(s):     None
; Requirement(s):   Internet access.
; Return Value(s):  On Success - Returns the public IP Address
;                   On Failure - 0 and sets @Error from Inetgetsource()
; Author(s):        qon
;
;===============================================================================

Func _xGetIp()
	$sSource = _inetgetsource("http://checkip.dyndns.org/")
	IF @error then Seterror(@error,0,0)
	$sSource = Stringsplit(StringStripws($sSource,3),"Current IP Address: ",1)
	Return Stringstripws(StringTrimRight($sSource[2],14),3)
EndFunc

;===============================================================================
;
; Description:      Checks if a given number is a prime number
; Syntax:           _IsPrime($iNumber)
; Parameter(s):     $iNumber - The Number to check
; Requirement(s):   None
; Return Value(s):  On Success - Returns 1
;                   On Failure - Returns 0 and sets @error
;					1 = number is no prime number
;					2 = number is lower than 2 or no integer
; Author(s):        qon
; Note(s):          this function can take a long time if the number is a prime number
;					and higher than 1.000.000
;
;===============================================================================

Func _IsPrime($iNumber)
	IF StringIsInt($iNumber) = 0 or $iNumber < 2 Then
		Seterror(2)
		Return 0
	EndIf
	
	For $i = 2 to $iNumber-1
		If Mod($iNumber,$i) = 0 Then
			Seterror(1)
			Return 0
		EndIf
	Next
	
	Return 1
EndFunc

;===============================================================================
;
; Description:     	Returns short handle from Windows Language
; Syntax:           _GetCountry()
; Parameter(s):     None
; Requirement(s):   None
; Return Value(s):  On Success - Returns Country
;                   On Failure - Returns 'NA' and sets @error fom RegRead()
; Author(s):        qon
; Note(s):          None
;
;===============================================================================

Func _GetCountry()
	$country = RegRead("HKEY_CURRENT_USER\Control Panel\International\", "sLanguage")
	If ($country = "") Then
		Seterror(@error)
		Return "NA"
	EndIf
	Return $country
EndFunc

;===============================================================================
;
; Description:      Checks if a connection to a specific ip adress exists.
; Syntax:           _connectioncheck($ip)
; Parameter(s):     $ip - The IP Adress to look for
; Requirement(s):   None
; Return Value(s):  On Success - Returns 1
;                   On Failure - Returns 0 and sets @error
;					1 = No connection exists
; Author(s):        qon
; Note(s):          None
;
;===============================================================================

func _CheckIpConnect($ip)
	Local $istring
	$foo = Run(@ComSpec & " /c netstat -n", @SystemDir, @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
	While 1
		$line = StdoutRead($foo)
		If @error Then ExitLoop
		$istring = $istring & $line
	Wend
	
	IF (Stringinstr($istring,$ip) <> 0) Then
		Return 1
	Else
		Seterror(1)
		Return 0
	EndIf
EndFunc

;===============================================================================
;
; Description:      Checks if a process is connected - to the internet / to a specific IP
; Syntax:           _CheckProcessConnect($sProcess,$iIp = False)
; Parameter(s):     $sProcess = Processname / Process ID to look for
;					$iIp = The IP that the Process should be connected to [ optional! ]
; Requirement(s):   None
; Return Value(s):  On Success - Returns 1
;                   On Failure - Returns 0 and sets @error
;					1 = No connection exists
;					2 = Process does not exist
;					3 = No german Windows
; Author(s):        qon
; Note(s):          None
;
;===============================================================================

Func _CheckProcessConnect($sProcess,$iIp = False)
	If RegRead("HKEY_CURRENT_USER\Control Panel\International\", "sLanguage") <> "DEU" Then
		SetError(3)
		Return 0
	EndIf
	$iPID = ProcessExists($sProcess)
	If $iPID = 0 then
		SetError(2)
		Return 0
	EndIf
	$foo = Run(@ComSpec & " /c netstat -on", @SystemDir, @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
	While 1
		$line = StdoutRead($foo)
		If @error Then ExitLoop
		Switch $iIp
			Case False
				If StringInStr(" " & $line & " ",$iPID) and StringInStr(" " & $line & " ","HERGESTELLT") Then
					Return 1
				EndIf
			Case Else
				If StringInStr(" " & $line & " ",$iPID) and StringInStr(" " & $line & " ","HERGESTELLT") and StringInStr(" " & $line & " ",$iIp) Then
					Return 1
				EndIf
		EndSwitch
	Wend
	SetError(1)
	Return 0
EndFunc