Inet.au3 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. #include-once
  2. #include "Date.au3"
  3. #include "InetConstants.au3"
  4. #include "StringConstants.au3"
  5. #include "WinAPIInternals.au3"
  6. ; #INDEX# =======================================================================================================================
  7. ; Title .........: Edit Constants
  8. ; AutoIt Version : 3.3.14.5
  9. ; Language ......: English
  10. ; Description ...: Functions that assist with Internet.
  11. ; Author(s) .....: Larry, Ezzetabi, Jarvis Stubblefield, Wes Wolfe-Wolvereness, Wouter, Walkabout, Florian Fida, guinness
  12. ; Dll ...........: wininet.dll, ws2_32.dll
  13. ; ===============================================================================================================================
  14. ; #CURRENT# =====================================================================================================================
  15. ; _GetIP
  16. ; _INetExplorerCapable
  17. ; _INetGetSource
  18. ; _INetMail
  19. ; _INetSmtpMail
  20. ; _TCPIpToName
  21. ; ===============================================================================================================================
  22. ; #INTERNAL_USE_ONLY# ===========================================================================================================
  23. ; __SmtpTrace
  24. ; __SmtpSend
  25. ; __TCPIpToName_szStringRead
  26. ; ===============================================================================================================================
  27. ; #FUNCTION# ====================================================================================================================
  28. ; Author ........: guinness, Mat
  29. ; ===============================================================================================================================
  30. Func _GetIP()
  31. Local Const $GETIP_TIMER = 300000 ; Constant for how many milliseconds between each check. This is 5 minutes.
  32. Local Static $hTimer = 0 ; Create a static variable to store the timer handle.
  33. Local Static $sLastIP = 0 ; Create a static variable to store the last IP.
  34. If TimerDiff($hTimer) < $GETIP_TIMER And Not $sLastIP Then ; If still in the timer and $sLastIP contains a value.
  35. Return SetExtended(1, $sLastIP) ; Return the last IP instead and set @extended to 1.
  36. EndIf
  37. #cs
  38. Additional list of possible IP disovery sites by z3r0c00l12.
  39. http://corz.org/ip
  40. http://icanhazip.com
  41. http://ip.appspot.com
  42. http://ip.eprci.net/text
  43. http://ip.jsontest.com/
  44. http://services.packetizer.com/ipaddress/?f=text
  45. http://whatthehellismyip.com/?ipraw
  46. http://wtfismyip.com/text
  47. http://www.networksecuritytoolkit.org/nst/tools/ip.php
  48. http://www.telize.com/ip
  49. http://www.trackip.net/ip
  50. #ce
  51. Local $aGetIPURL = ["https://api.ipify.org", "http://checkip.dyndns.org", "http://www.myexternalip.com/raw", "http://bot.whatismyipaddress.com"], _
  52. $aReturn = 0, _
  53. $sReturn = ""
  54. For $i = 0 To UBound($aGetIPURL) - 1
  55. $sReturn = InetRead($aGetIPURL[$i])
  56. If @error Or $sReturn == "" Then ContinueLoop
  57. $aReturn = StringRegExp(BinaryToString($sReturn), "((?:\d{1,3}\.){3}\d{1,3})", $STR_REGEXPARRAYGLOBALMATCH) ; [\d\.]{7,15}
  58. If Not @error Then
  59. $sReturn = $aReturn[0]
  60. ExitLoop
  61. EndIf
  62. $sReturn = ""
  63. Next
  64. $hTimer = TimerInit() ; Create a new timer handle.
  65. $sLastIP = $sReturn ; Store this IP.
  66. If $sReturn == "" Then Return SetError(1, 0, -1)
  67. Return $sReturn
  68. EndFunc ;==>_GetIP
  69. ; #FUNCTION# ====================================================================================================================
  70. ; Author ........: Wes Wolfe-Wolvereness <Weswolf at aol dot com>
  71. ; ===============================================================================================================================
  72. Func _INetExplorerCapable($sIEString)
  73. If StringLen($sIEString) <= 0 Then Return SetError(1, 0, '')
  74. Local $s_IEReturn
  75. Local $n_IEChar
  76. For $i_IECount = 1 To StringLen($sIEString)
  77. $n_IEChar = '0x' & Hex(Asc(StringMid($sIEString, $i_IECount, 1)), 2)
  78. If $n_IEChar < 0x21 Or $n_IEChar = 0x25 Or $n_IEChar = 0x2f Or $n_IEChar > 0x7f Then
  79. $s_IEReturn = $s_IEReturn & '%' & StringRight($n_IEChar, 2)
  80. Else
  81. $s_IEReturn = $s_IEReturn & Chr($n_IEChar)
  82. EndIf
  83. Next
  84. Return $s_IEReturn
  85. EndFunc ;==>_INetExplorerCapable
  86. ; #FUNCTION# ====================================================================================================================
  87. ; Author ........: Wouter van Kesteren.
  88. ; ===============================================================================================================================
  89. Func _INetGetSource($sURL, $bString = True)
  90. Local $sString = InetRead($sURL, $INET_FORCERELOAD)
  91. Local $iError = @error, $iExtended = @extended
  92. If $bString = Default Or $bString Then $sString = BinaryToString($sString)
  93. Return SetError($iError, $iExtended, $sString)
  94. EndFunc ;==>_INetGetSource
  95. ; #FUNCTION# ====================================================================================================================
  96. ; Author ........: Wes Wolfe-Wolvereness <Weswolf at aol dot com>, modified by Emiel Wieldraaijer
  97. ; ===============================================================================================================================
  98. Func _INetMail($sMailTo, $sMailSubject, $sMailBody)
  99. Local $iPrev = Opt("ExpandEnvStrings", 1)
  100. Local $sVar, $sDflt = RegRead('HKCU\Software\Clients\Mail', "")
  101. If $sDflt = "Windows Live Mail" Then
  102. $sVar = RegRead('HKCR\WLMail.Url.Mailto\Shell\open\command', "")
  103. Else
  104. $sVar = RegRead('HKCR\mailto\shell\open\command', "")
  105. EndIf
  106. Local $iRet = Run(StringReplace($sVar, '%1', _INetExplorerCapable('mailto:' & $sMailTo & '?subject=' & $sMailSubject & '&body=' & $sMailBody)))
  107. Local $iError = @error, $iExtended = @extended
  108. Opt("ExpandEnvStrings", $iPrev)
  109. Return SetError($iError, $iExtended, $iRet)
  110. EndFunc ;==>_INetMail
  111. ; #FUNCTION# ====================================================================================================================
  112. ; Author ........: Asimzameer, Walkabout
  113. ; Modified.......: Jpm
  114. ; ===============================================================================================================================
  115. Func _INetSmtpMail($sSMTPServer, $sFromName, $sFromAddress, $sToAddress, $sSubject = "", $aBody = "", $sEHLO = "", $sFirst = "", $bTrace = 0)
  116. If $sSMTPServer = "" Or $sFromAddress = "" Or $sToAddress = "" Or $sFromName = "" Or StringLen($sFromName) > 256 Then Return SetError(1, 0, 0)
  117. If $sEHLO = "" Then $sEHLO = @ComputerName
  118. If TCPStartup() = 0 Then Return SetError(2, 0, 0)
  119. Local $s_IPAddress, $i_Count
  120. If StringRegExp($sSMTPServer, "^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$") Then
  121. $s_IPAddress = $sSMTPServer
  122. Else
  123. $s_IPAddress = TCPNameToIP($sSMTPServer)
  124. EndIf
  125. If $s_IPAddress = "" Then
  126. TCPShutdown()
  127. Return SetError(3, 0, 0)
  128. EndIf
  129. Local $vSocket = TCPConnect($s_IPAddress, 25)
  130. If $vSocket = -1 Then
  131. TCPShutdown()
  132. Return SetError(4, 0, 0)
  133. EndIf
  134. Local $aSend[6], $aReplyCode[6] ; Return code from SMTP server indicating success
  135. $aSend[0] = "HELO " & $sEHLO & @CRLF
  136. If StringLeft($sEHLO, 5) = "EHLO " Then $aSend[0] = $sEHLO & @CRLF
  137. $aReplyCode[0] = "250"
  138. $aSend[1] = "MAIL FROM: <" & $sFromAddress & ">" & @CRLF
  139. $aReplyCode[1] = "250"
  140. $aSend[2] = "RCPT TO: <" & $sToAddress & ">" & @CRLF
  141. $aReplyCode[2] = "250"
  142. $aSend[3] = "DATA" & @CRLF
  143. $aReplyCode[3] = "354"
  144. Local $aResult = _Date_Time_GetTimeZoneInformation()
  145. Local $iBias = -$aResult[1] / 60
  146. Local $iBiasH = Int($iBias)
  147. Local $iBiasM = 0
  148. If $iBiasH <> $iBias Then $iBiasM = Abs($iBias - $iBiasH) * 60
  149. $iBias = StringFormat(" (%+.2d%.2d)", $iBiasH, $iBiasM)
  150. $aSend[4] = "From:" & $sFromName & "<" & $sFromAddress & ">" & @CRLF & _
  151. "To:" & "<" & $sToAddress & ">" & @CRLF & _
  152. "Subject:" & $sSubject & @CRLF & _
  153. "Mime-Version: 1.0" & @CRLF & _
  154. "Date: " & _DateDayOfWeek(@WDAY, 1) & ", " & @MDAY & " " & _DateToMonth(@MON, 1) & " " & @YEAR & " " & @HOUR & ":" & @MIN & ":" & @SEC & $iBias & @CRLF & _
  155. "Content-Type: text/plain; charset=US-ASCII" & @CRLF & _
  156. @CRLF
  157. $aReplyCode[4] = ""
  158. $aSend[5] = @CRLF & "." & @CRLF
  159. $aReplyCode[5] = "250"
  160. ; open stmp session
  161. If __SmtpSend($vSocket, $aSend[0], $aReplyCode[0], $bTrace, "220", $sFirst) Then Return SetError(50, 0, 0)
  162. ; send header
  163. For $i_Count = 1 To UBound($aSend) - 2
  164. If __SmtpSend($vSocket, $aSend[$i_Count], $aReplyCode[$i_Count], $bTrace) Then Return SetError(50 + $i_Count, 0, 0)
  165. Next
  166. ; send body records (a record can be multiline : take care of a subline beginning with a dot should be ..)
  167. For $i_Count = 0 To UBound($aBody) - 1
  168. ; correct line beginning with a dot
  169. If StringLeft($aBody[$i_Count], 1) = "." Then $aBody[$i_Count] = "." & $aBody[$i_Count]
  170. If __SmtpSend($vSocket, $aBody[$i_Count] & @CRLF, "", $bTrace) Then Return SetError(500 + $i_Count, 0, 0)
  171. Next
  172. ; close the smtp session
  173. $i_Count = UBound($aSend) - 1
  174. If __SmtpSend($vSocket, $aSend[$i_Count], $aReplyCode[$i_Count], $bTrace) Then Return SetError(5000, 0, 0)
  175. TCPCloseSocket($vSocket)
  176. TCPShutdown()
  177. Return 1
  178. EndFunc ;==>_INetSmtpMail
  179. ; #INTERNAL_USE_ONLY# ===========================================================================================================
  180. ; Name...........: __SmtpTrace
  181. ; Description ...: Used internally within this file, not for general use
  182. ; Syntax.........: __SmtpTrace ( $sStr [, $iTimeout = 0] )
  183. ; Author ........: Asimzameer, Walkabout
  184. ; Modified.......: Jpm
  185. ; ===============================================================================================================================
  186. Func __SmtpTrace($sStr, $iTimeout = 0)
  187. Local $sW_TITLE = "SMTP trace"
  188. Local $sSmtpTrace = ControlGetText($sW_TITLE, "", "Static1")
  189. $sStr = StringLeft(StringReplace($sStr, @CRLF, ""), 70)
  190. $sSmtpTrace &= @HOUR & ":" & @MIN & ":" & @SEC & " " & $sStr & @LF
  191. If WinExists($sW_TITLE) Then
  192. ControlSetText($sW_TITLE, "", "Static1", $sSmtpTrace)
  193. Else
  194. SplashTextOn($sW_TITLE, $sSmtpTrace, 400, 500, 500, 100, 4 + 16, "", 8)
  195. EndIf
  196. If $iTimeout Then Sleep($iTimeout * 1000)
  197. EndFunc ;==>__SmtpTrace
  198. ; #INTERNAL_USE_ONLY# ===========================================================================================================
  199. ; Name...........: __SmtpSend
  200. ; Description ...: Used internally within this file, not for general use
  201. ; Syntax.........: __SmtpSend ( $vSocket, $sSend, $sReplyCode, $bTrace [, $sIntReply="" [, $sFirst=""]] )
  202. ; Author ........: Asimzameer, Walkabout
  203. ; Modified.......: Jpm
  204. ; ===============================================================================================================================
  205. Func __SmtpSend($vSocket, $sSend, $sReplyCode, $bTrace, $sIntReply = "", $sFirst = "")
  206. Local $sReceive, $i, $hTimer
  207. If $bTrace Then __SmtpTrace($sSend)
  208. If $sIntReply <> "" Then
  209. ; Send special first char to awake smtp server
  210. If $sFirst <> -1 Then
  211. If TCPSend($vSocket, $sFirst) = 0 Then
  212. TCPCloseSocket($vSocket)
  213. TCPShutdown()
  214. Return 1; cannot send
  215. EndIf
  216. EndIf
  217. ; Check intermediate reply before HELO acceptation
  218. $sReceive = ""
  219. $hTimer = TimerInit()
  220. While StringLeft($sReceive, StringLen($sIntReply)) <> $sIntReply And TimerDiff($hTimer) < 45000
  221. $sReceive = TCPRecv($vSocket, 1000)
  222. If $bTrace And $sReceive <> "" Then __SmtpTrace("intermediate->" & $sReceive)
  223. WEnd
  224. EndIf
  225. ; Send string.
  226. If TCPSend($vSocket, $sSend) = 0 Then
  227. TCPCloseSocket($vSocket)
  228. TCPShutdown()
  229. Return 1; cannot send
  230. EndIf
  231. $hTimer = TimerInit()
  232. $sReceive = ""
  233. While $sReceive = "" And TimerDiff($hTimer) < 45000
  234. $i += 1
  235. $sReceive = TCPRecv($vSocket, 1000)
  236. If $sReplyCode = "" Then ExitLoop
  237. WEnd
  238. If $sReplyCode <> "" Then
  239. ; Check replycode
  240. If $bTrace Then __SmtpTrace($i & " <- " & $sReceive)
  241. If StringLeft($sReceive, StringLen($sReplyCode)) <> $sReplyCode Then
  242. TCPCloseSocket($vSocket)
  243. TCPShutdown()
  244. If $bTrace Then __SmtpTrace("<-> " & $sReplyCode, 5)
  245. Return 2; bad receive code
  246. EndIf
  247. EndIf
  248. Return 0
  249. EndFunc ;==>__SmtpSend
  250. ; #FUNCTION# ====================================================================================================================
  251. ; Author ........: Florian Fida
  252. ; ===============================================================================================================================
  253. Func _TCPIpToName($sIp, $iOption = Default, $hDll = Default)
  254. Local $iINADDR_NONE = 0xffffffff, $iAF_INET = 2, $sSeparator = @CR
  255. If $iOption = Default Then $iOption = 0
  256. If $hDll = Default Then $hDll = "ws2_32.dll"
  257. Local $avDllCall = DllCall($hDll, "ulong", "inet_addr", "STR", $sIp)
  258. If @error Then Return SetError(1, 0, "") ; inet_addr DllCall Failed
  259. Local $vBinIP = $avDllCall[0]
  260. If $vBinIP = $iINADDR_NONE Then Return SetError(2, 0, "") ; inet_addr Failed
  261. $avDllCall = DllCall($hDll, "ptr", "gethostbyaddr", "ptr*", $vBinIP, "int", 4, "int", $iAF_INET)
  262. If @error Then Return SetError(3, 0, "") ; gethostbyaddr DllCall Failed
  263. Local $pvHostent = $avDllCall[0]
  264. If $pvHostent = 0 Then
  265. $avDllCall = DllCall($hDll, "int", "WSAGetLastError")
  266. If @error Then Return SetError(5, 0, "") ; gethostbyaddr Failed, WSAGetLastError Failed
  267. Return SetError(4, $avDllCall[0], "") ; gethostbyaddr Failed, WSAGetLastError = @extended
  268. EndIf
  269. Local $tHostent = DllStructCreate("ptr;ptr;short;short;ptr", $pvHostent)
  270. Local $sHostnames = __TCPIpToName_szStringRead(DllStructGetData($tHostent, 1))
  271. If @error Then Return SetError(6, 0, $sHostnames) ; strlen/sZStringRead Failed
  272. If $iOption = 1 Then
  273. Local $tAliases
  274. $sHostnames &= $sSeparator
  275. For $i = 0 To 63 ; up to 64 Aliases
  276. $tAliases = DllStructCreate("ptr", DllStructGetData($tHostent, 2) + ($i * 4))
  277. If DllStructGetData($tAliases, 1) = 0 Then ExitLoop ; Null Pointer
  278. $sHostnames &= __TCPIpToName_szStringRead(DllStructGetData($tAliases, 1))
  279. If @error Then
  280. SetError(7) ; Error reading array
  281. ExitLoop
  282. EndIf
  283. Next
  284. Return StringSplit(StringStripWS($sHostnames, $STR_STRIPTRAILING), @CR)
  285. Else
  286. Return $sHostnames
  287. EndIf
  288. EndFunc ;==>_TCPIpToName
  289. ; #INTERNAL_USE_ONLY# ===========================================================================================================
  290. ; Name...........: __TCPIpToName_szStringRead
  291. ; Description ...: Used internally within this file, not for general use
  292. ; Syntax.........: __TCPIpToName_szStringRead ( $pStr [, $iLen = -1] )
  293. ; Author ........: Florian Fida
  294. ; ===============================================================================================================================
  295. Func __TCPIpToName_szStringRead($pStr, $iLen = -1)
  296. Local $tString
  297. If $pStr < 1 Then Return "" ; Null Pointer
  298. If $iLen < 0 Then $iLen = _WinAPI_StrLen($pStr, False)
  299. $tString = DllStructCreate("char[" & $iLen & "]", $pStr)
  300. If @error Then Return SetError(2, 0, "")
  301. Return SetExtended($iLen, DllStructGetData($tString, 1))
  302. EndFunc ;==>__TCPIpToName_szStringRead