#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_icon=hex_viewer.ico
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
;##########
;25=SMTP 110=POP3 21=FTP
;##########
#NoTrayIcon
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <GUIListBox.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <_stringgetstr.au3>

ProgressOn("Starte Emailmanager - TheShadowAE","Starte TCP-Services","",Default,Default,18)
TCPStartup()
ProgressSet(25,"","Starte Programm")
global $title="Emailmanager 1.0 - TheShadowAE"
global $mail=iniread("emailm.ini","opt","mail","yourEmail@server.de")
global $pw=iniread("emailm.ini","opt","pw","")
global $sock
while $pw="" or stringlen($pw)<=5
	$pw=inputbox("Passwort","Passwort für den Email-Account (mind. 6 Zeichen):","","*")
	if $pw="exit" then
		TCPShutdown()
		Exit
	EndIf
WEnd
ProgressSet(50,"","Rufe neue Emails ab")
$msg=getmsg()
if $msg="1" then $msg="Keine Verbindung"
ProgressSet(75,"","Öffne GUI")
#Region ### START Koda GUI section ### Form=D:\Progen\AutoIt\Meine\Forms\emailmanager.kxf
$Form1 = GUICreate($title, 633, 466, 192, 124)
$List1 = GUICtrlCreateList("", 16, 16, 305, 201)
GUICtrlSetData($List1,$msg)
$Edit1 = GUICtrlCreateEdit("", 16, 224, 305, 201)
$Edit2 = GUICtrlCreateEdit("", 344, 200, 265, 225)
$Input1 = GUICtrlCreateInput("", 480, 88, 129, 21)
$Input2 = GUICtrlCreateInput($mail, 344, 16, 265, 21)
$Input3 = GUICtrlCreateInput("", 344, 152, 265, 21)
$Label1 = GUICtrlCreateLabel("Name:", 344, 88, 36, 17)
$Label2 = GUICtrlCreateLabel("Neue Email schreiben", 408, 56, 106, 17)
$Label3 = GUICtrlCreateLabel("Empfänger (mehrere getrennt durch Komma)", 344, 128, 256, 17)
$Label4 = GUICtrlCreateLabel("Text:", 344, 180, 36, 17)
$Button1 = GUICtrlCreateButton("Aktualisieren", 232, 432, 89, 25, $WS_GROUP)
$Button2 = GUICtrlCreateButton("Senden", 520, 432, 89, 25, $WS_GROUP)
$Button3 = GUICtrlCreateButton("Email ändern", 544, 40, 81, 25, $WS_GROUP)
;~ $Button4 = GUICtrlCreateButton("Email löschen", 16, 432, 105, 25, $WS_GROUP)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
ProgressSet(100,"","Fertig")
sleep(100)
ProgressOff()

While 1
	$nMsg = GUIGetMsg()
	Switch $nMsg
		Case $GUI_EVENT_CLOSE
			ende()
		case $Button1
			GUICtrlSetData($List1,"")
			$msg=getmsg()
			if $msg="1" then $msg="Keine Verbindung|"
			GUICtrlSetData($List1,$msg)
		case $Button3
			$mail=GUICtrlRead($Input2)
			$pw=InputBox("Neues Passwort","Neues Passwort eingeben:",$pw,"*")
			GUICtrlSetData($List1,"")
			$msg=getmsg()
			if $msg="1" then $msg="|Keine Verbindung|"
			GUICtrlSetData($List1,$msg)
		case $Button2
			if GUICtrlRead($Input1)="" or GUICtrlRead($Input3)="" then
				msgbox(48,"Fehler","Es gibt noch leere Felder")
				ContinueLoop
			EndIf
			$klappt=smtp(guictrlread($Input1),GUICtrlRead($Input3),GUICtrlRead($Edit2))
			if $klappt<>1 and $klappt<>2 then
				msgbox(48,"Fehler","Mail konnte nicht gesendet werden")
			elseif $klappt=1 Then
				;
			Else
				msgbox(64,"Fertig","Die Mail wurde gesendet")
			EndIf
;~ 		case $Button4
;~ 			$read=GUICtrlRead($List1)
;~ 			$z=int(stringleft($read,1))
;~ 			if $z=0 then ContinueLoop
			;send dele $z
		case $List1
			$read=GUICtrlRead($List1)
			$z=int(stringleft($read,1))
			$msg=recm($z)
			if $msg="1" then
				GUICtrlSetData($List1,"")
				$msg="Keine Verbindung|"
				GUICtrlSetData($List1,$msg)
			EndIf
			GUICtrlSetData($Edit1,$msg)
	EndSwitch
	sleep(10)
WEnd



func smtp($name,$emps,$texte)
	if $mail="yourEmail@server.de" or $pw="" then Return
	$serv="smtp."&stringmid($mail,stringinstr($mail,"@",0,1)+1)
	$server=TCPNameToIP($serv)
	if $server="" then return msgbox(48,"Fehler","Server nicht gefunden")
	$sock=TCPConnect($server,25)
	$rec=wait()
	TCPSend($sock,"helo "&$name&@CRLF)
	$rec=wait()
	TCPSend($sock,"mail from:<"&$mail&">"&@CRLF)
	$rec=wait()
	$to=_stringgetnum($emps,",")
	for $x=1 to $to
		TCPSend($sock,"rcpt to:<"&_Stringgetstr($emps,",",$x)&">"&@CRLF)
		$rec=wait()
	Next
	TCPSend($sock,"data"&@CRLF)
	$rec=wait()
	TCPSend($sock,$texte&@CRLF)
	TCPSend($sock,"."&@CRLF)
	$rec=wait()
	TCPSend($sock,"quit"&@CRLF)
	$rec=wait()
	TCPCloseSocket($sock)
	return 2
EndFunc

func getmsg()
	if $mail="yourEmail@server.de" or $pw="" then Return
	$server=TCPNameToIP("pop3."&stringmid($mail,stringinstr($mail,"@",0,1)+1))
	if $server="" then return msgbox(48,"Fehler","Server nicht gefunden")
	$user=stringmid($mail,1,stringinstr($mail,"@",0,1)-1)
	$sock=TCPConnect($server,110)
	$rec=wait()
	if not ok($rec) Then
		TCPSend($sock,"quit"&@CRLF)
		TCPCloseSocket($sock)
		return msgbox(48,"Fehler","Es ist ein Fehler beim Verbinden aufgetreten")
	EndIf
	TCPSend($sock,"user "&$user&@CRLF)
	$rec=wait()
	if not ok($rec) Then
		TCPSend($sock,"quit"&@CRLF)
		TCPCloseSocket($sock)
		return msgbox(48,"Fehler","Fehler mit Username")
	EndIf
	TCPSend($sock,"pass "&$pw&@CRLF)
	$rec=wait()
	if not ok($rec) Then
		TCPSend($sock,"quit"&@CRLF)
		TCPCloseSocket($sock)
		return msgbox(48,"Fehler","Passwort oder Username ist Falsch oder der Server"&@CRLF&"blockiert POP3 aufgrund zu ofter Aktualisierung")
	EndIf
	TCPSend($sock,"list"&@CRLF)
	$rec=wait()
	if not ok($rec) Then
		TCPSend($sock,"quit"&@CRLF)
		TCPCloseSocket($sock)
		return msgbox(48,"Fehler","Konnte Nachrichtenliste nicht aufrufen")
	EndIf
	$anz=_stringgetnum($rec,@CRLF)-4
	$msg="Nachrichten: "&$anz&"|"
	for $x=1 to $anz
		TCPSend($sock,"retr "&$x&@CRLF)
		$rec=wait()
		if not ok($rec) then ExitLoop
		$rec=wait()
		$n=TCPRecv($sock,1024)
		if $n<>"" then $rec&=$n
		$n=TCPRecv($sock,1024)
		if $n<>"" then $rec&=$n
		$msg&=$x&" "&_Stringgetstr($rec,@crlf,1)&"|"
		$file=FileOpen(@TempDir&"\mail"&$x&".dat",2)
		FileWrite($file,$rec)
		FileClose($file)
	Next
	TCPSend($sock,"quit"&@CRLF)
	$rec=wait()
	TCPCloseSocket($sock)
	return $msg
EndFunc

func recm($zahl=0)
	if $zahl=0 then return GUICtrlRead($List1)
	if FileExists(@TempDir&"\mail"&$zahl&".dat")=0 then Return GUICtrlRead($List1)
	$rmsg=""
	$file=FileOpen(@TempDir&"\mail"&$zahl&".dat",0)
	While 1
		$rmsg&=FileRead($file,1024)
		if @error=-1 then ExitLoop
	WEnd
	FileClose($file)
	return $rmsg
EndFunc

func wait()
	do
		$tmp=TCPRecv($sock,1024)
	until $tmp<>""
	$rec=$tmp&@CRLF
;~ 	If $rec <> "" Then
        While 1
            $tmp= TCPRecv($sock, 1024)
            If $tmp="" Then ExitLoop
			$rec &=$tmp&@CRLF
        WEnd
;~ 		msgbox(64,"Angekommen",$rec)
;~     EndIf
	return $rec
EndFunc

func ok($txt="")
	if stringleft($txt,3)="+OK" then return 1
	if stringleft($txt,4)="-ERR" then return 0
	return -1
EndFunc

func ende()
	IniWrite("emailm.ini","opt","mail",$mail)
	IniWrite("emailm.ini","opt","pw",$pw)
	TCPShutdown()
	Exit
EndFunc

func err($txt,$titel="Error")
	msgbox(16,$titel,$txt)
EndFunc