#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <TreeViewConstants.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>
#include <array.au3>
#include <_pop3.au3>
#include <Constants.au3>
#include <EditConstants.au3>
#include <ListViewConstants.au3>
#include <Base64.au3>
#include <String.au3>

;#NoTrayIcon

;Opt("TrayMenuMode",1)   ; Default tray menu items (Script Paused/Exit) will not be shown.

;$settingsitem   = TrayCreateMenu("Settings")
;$displayitem    = TrayCreateItem("Display", $settingsitem)
;$printeritem    = TrayCreateItem("Printer", $settingsitem)
;TrayCreateItem("")
;$aboutitem      = TrayCreateItem("About")
;TrayCreateItem("")
;$exititem       = TrayCreateItem("Exit")

;TraySetState()
;TraySetClick(16)

Global $MyPopServer = ""
Global $MyLogin = ""
Global $MyPasswd = ""

$Form1_1 = GUICreate("Inem Mail", 500, 480)
$sLVhdr = "ID|Date|From|Subject"
$aLVhdr = StringSplit($sLVhdr, "|")
$ListView1 = GUICtrlCreateListView($sLVhdr, 100, 34, 390, 169)
GUICtrlSendMsg(-1, 0x101E, 0, 48)
GUICtrlSendMsg(-1, 0x101E, 1, 100)
GUICtrlSendMsg(-1, 0x101E, 2, 100)
GUICtrlSendMsg(-1, 0x101E, 3, 140)
$Edit1 = GUICtrlCreateEdit("", 10, 220, 480, 249)
$TreeView1 = GUICtrlCreateTreeView(5, 34, 86, 169)
$TreeView1_0 = GUICtrlCreateTreeViewItem("INBOX", $TreeView1)
$TreeView1_1 = GUICtrlCreateTreeViewItem("Unread", $TreeView1_0)
$TreeView1_2 = GUICtrlCreateTreeViewItem("Read", $TreeView1_0)
$TreeView1_3 = GUICtrlCreateTreeViewItem("All Mail", $TreeView1_0)
$TreeView1_4 = GUICtrlCreateTreeViewItem("Send Mail", $TreeView1)
$TreeView1_5 = GUICtrlCreateTreeViewItem("Drafts", $TreeView1)
$TreeView1_6 = GUICtrlCreateTreeViewItem("Spam", $TreeView1)

$Button1 = GUICtrlCreateButton("Compose", 4, 4, 65, 21, 0)
$Button2 = GUICtrlCreateButton("Send / Recieve", 72, 4, 85, 21, 0)

GUISetState(@SW_SHOW)

GUIRegisterMsg($WM_NOTIFY, "WM_Notify_Events")
While 1
    $nMsg = GUIGetMsg()
    Select
        Case $nMsg = $GUI_EVENT_CLOSE
            Exit
        Case $nMsg = $Button2
            checkmail()
    EndSelect
WEnd

Func Click()
    _pop3Connect($MyPopServer, $MyLogin, $MyPasswd)
    $iRow = _GUICtrlListView_GetNextItem($ListView1) ; current selected
    If $iRow = -1 Then Return
    $aMsgNum = StringSplit(_GUICtrlListView_GetItemText($ListView1, $iRow), " ")
    If $aMsgNum[0] = 1 Then Return
    GUICtrlSetData($Edit1, _Pop3Retr($aMsgNum[1]))
	;ConsoleWrite(_Pop3Retr($aMsgNum[1]))
	$mail = _Pop3Retr($aMsgNum[1])
	;ConsoleWrite($mail)
	_pop3Disconnect()
	;$s = FileRead("D:\Data\scripte\POP3\_pop3\Pop3_OK\mail.txt")
	;MsgBox(0,"",$s)
	$aRegEx = StringRegExp($mail, "(?s)filename=""(.+?)""; size=\d+;.+?Content-Transfer-Encoding:(.+?)\R\R--", 3)
	$aRegEx = StringRegExp($mail, "(?s)filename=""(.+?)""; size=\d+;.+?Content-Transfer-Encoding: base64(.+?)\R\R--", 3)
	;_ArrayDisplay($aRegEx)
	;consolewrite($aRegEx[0] & @crlf)
	;consolewrite($aRegEx[1])

	_export($aRegEx[0],$aRegEx[1])

EndFunc   ;==>Click

Func _export($filename,$string)

	If FileExists(@ScriptDir & "\temp.txt") Then FileDelete(@ScriptDir & "\temp.txt")
	FileWrite(@ScriptDir & "\temp.txt", $string)
	;$RetVal = Base64DecFile("D:\Data\scripte\POP3\_pop3\Pop3_OK\mail.txt", "D:\Data\scripte\POP3\_pop3\Pop3_OK\" & $filename)
	$RetVal = Base64DecFile(@ScriptDir & "\temp.txt", @ScriptDir & "\" & $filename)

EndFunc


Func WM_Notify_Events($hWndGUI, $MsgID, $wParam, $lParam)
    Local $tagNMHDR, $iEvent
    If $wParam = $ListView1 Then
        $tagNMHDR = DllStructCreate("int;int;int", $lParam)
        $iEvent = DllStructGetData($tagNMHDR, 3)
        ;If $iEvent = $NM_DBLCLK Then Click()
        If $iEvent = $NM_CLICK Then Click()
    EndIf
EndFunc   ;==>WM_Notify_Events

Func checkmail()
    _pop3Connect($MyPopServer, $MyLogin, $MyPasswd)
    If @error Then
        MsgBox(262144 + 16, "", "Unable to connect to mail server @error=" & @error)
        Exit
    EndIf
    $aLoopNum = _Pop3Stat()
    $iCurrent = 0
    While $iCurrent < $aLoopNum[1]
        $iCurrent += 1
        $sMsgHdr = _Pop3Top($iCurrent, 1)
        $stat = _Pop3List($iCurrent)

		$sItem = $stat[1] & "|"

		;ConsoleWrite($stat[1] & @CRLF)

        For $i = 2 To $aLVhdr[0]
            $aItem = StringRegExp($sMsgHdr, "(" & $aLVhdr[$i] & " :) (.*[\r\n])", 3)
            If @error = 0 Then
                If UBound($aItem) = 2 Then
                    $sItem &= StringStripWS($aItem[1], 3) & "|"
                EndIf
            EndIf
            ;StringTrimRight($sItem, 1)
        Next
        GUICtrlCreateListViewItem($sItem, $ListView1)

    WEnd
    _pop3Disconnect()
EndFunc   ;==>checkmail