Автор: sproxy
Дата сообщения: 15.04.2009 06:27
Пример XML консоли для тестирования XMPP функций:
[more]
Код: #region: - Options
Opt('GUIOnEventMode', 1)
Opt('MustDeclareVars', 1)
Opt('TrayIconDebug', 1)
Opt('TrayIconHide', 0)
#endregion
#region: - Include
;Defined Functions:
#include <Array.au3>
;~ #include <ComboConstants.au3>
;~ #include <Constants.au3>
;~ #include <Inet.au3>
#Include <Misc.au3>
#include <GuiEdit.au3>
;~ #include <GuiButton.au3>
#include <GUIComboBox.au3>
#include <GUIConstantsEx.au3>
;~ #Include <GuiTab.au3>
;~ #Include <GuiStatusBar.au3>
;~ #include <StaticConstants.au3>
;~ #include <string.au3>
#Include <Timers.au3>
;~ #include <WinAPI.au3>
#include <WindowsConstants.au3>
#endregion
#region: - Program Global, AutoIt Win Title, One instance
Global $sProName = 'XML console for testing Jabber XMPP functions'
Global $sProVerNo = '0.1'
Global $sProVer = 'v' & $sProVerNo
Global $sProNameVer = $sProName & ' ' & $sProVer
Global $sProIDText = $sProNameVer & ' [ID] ' & $sProNameVer
Global $sProGUID = _Get_GUID()
Global $iProOneInstance = 0
Global $iProSleepTime = 10
AutoItWinSetTitle(@AutoItPID & ' ' & $sProIDText)
Global $hAutoItWin = WinGetHandle(@AutoItPID & ' ' & $sProIDText)
If $iProOneInstance Then
If NOT _Singleton($sProIDText, 1) Then
Opt('WinTitleMatchMode', 2)
ControlSetText('[TITLE:' & $sProIDText & '; CLASS:AutoIt v3;]', '', '[CLASS:Edit; INSTANCE:1]', 'Already run')
_Pro_Exit()
EndIf
EndIf
Func _Get_GUID()
Local $oTypeLib = ObjCreate('Scriptlet.TypeLib')
If IsObj($oTypeLib) Then Return $oTypeLib.GUID
Return SetError(1, 0, 0)
EndFunc
Global $i, $hToolTipTimerID
Global $hConnect, $sConnectedJid, $iId
#endregion
#region: - Ini Global, Functions
Global $sIniFilePath = StringRegExpReplace(@ScriptFullPath, '(.+\.).*', '\1') & 'ini'
Global $aJidList[1][2]
$aJidList[0][0] = 0
Local $aJidListTemp, $iJidNo
$aJidListTemp = IniReadSection($sIniFilePath, 'Jid List')
If IsArray($aJidListTemp) Then
For $i=1 To $aJidListTemp[0][0]
If StringInStr($aJidListTemp[$i][0], 'login') Then
$iJidNo = StringReplace($aJidListTemp[$i][0], 'login', '')
$aJidListTemp[$i][1] = StringReplace(StringLower($aJidListTemp[$i][1]), '"', '')
If StringInStr($aJidList[0][1], '|' & $aJidListTemp[$i][1] & '|') Then ContinueLoop
If StringIsDigit($iJidNo) Then
$aJidList[0][1] &= '|' & $aJidListTemp[$i][1] & '|'
$aJidList[0][0] += 1
ReDim $aJidList[$aJidList[0][0]+1][2]
$aJidList[$aJidList[0][0]][0] = $aJidListTemp[$i][1]
$aJidList[$aJidList[0][0]][1] = IniRead($sIniFilePath, 'Jid List', 'pass' & $iJidNo, '')
EndIf
EndIf
Next
EndIf
$aJidList[0][1] = StringReplace($aJidList[0][1], '||', '|')
_JidList_SaveToIni()
#endregion
#region: - Main Win Global, Functions
;~ Handle
Global $hMainWin
;~ Title, Width, Height, X, Y, Style, ExStyle
Global $sMainWin_Title = $sProNameVer
Global $iMainWin_Width = 750
Global $iMainWin_Height = 530
Global $iMainWin_X = -1
Global $iMainWin_Y = -1
Global $iMainWin_Style = $WS_OVERLAPPEDWINDOW
Global $iMainWin_ExStyle = -1
;~ GUI Ctrl
Global $hMainWin_XML_Console, $hMainWin_Messages
Global $hMainWin_Message_Input, $hMainWin_JidList
Global $hMainWin_JidSettings, $hMainWin_JidSettings_Menu
Func _MainWin_Create()
$hMainWin = GUICreate($sMainWin_Title, $iMainWin_Width, $iMainWin_Height, $iMainWin_X, $iMainWin_Y, $iMainWin_Style, $iMainWin_ExStyle)
GUISetOnEvent($GUI_EVENT_CLOSE, '_Pro_Exit')
GUICtrlCreateLabel('XML-Console:', 11, 10, 100, 18)
GUICtrlSetResizing(-1, $GUI_DOCKALL)
$hMainWin_XML_Console = GUICtrlCreateEdit('', 10, 25, $iMainWin_Width-20, 200, BitOR($ES_READONLY, $WS_VSCROLL, $WS_HSCROLL))
GUICtrlSetBkColor(-1, 0xffffff)
GUICtrlSetResizing(-1, 2+4+32+512)
GUICtrlCreateLabel('Messages:', 11, 10+200+19, 100, 18)
GUICtrlSetResizing(-1, $GUI_DOCKALL)
$hMainWin_Messages = GUICtrlCreateEdit('', 10, 25+200+20, $iMainWin_Width-20, 200, BitOR($ES_READONLY, $WS_VSCROLL, $WS_HSCROLL))
GUICtrlSetBkColor(-1, 0xffffff)
GUICtrlSetResizing(-1, 2+4+32+512)
$hMainWin_Message_Input = GUICtrlCreateEdit('', 10, 450, $iMainWin_Width-95, 40, BitOR($WS_VSCROLL, $ES_AUTOVSCROLL))
GUICtrlSetResizing(-1, 2+4+32+512)
GUICtrlCreateButton('Send', $iMainWin_Width-80, 450, 70, 40)
GUICtrlSetResizing(-1, 4+32+768)
$hMainWin_JidList = GUICtrlCreateCombo('No Jid', 10, 495, 200, 20, $CBS_DROPDOWNLIST)
GUICtrlSetResizing(-1, $GUI_DOCKALL)
_JidList_Update()
$hMainWin_JidSettings = GUICtrlCreateButton('Settings', 10+204, 494, 70, 23)
GUICtrlSetResizing(-1, $GUI_DOCKALL)
GUICtrlSetOnEvent(-1, '_MainWin_JidSettings')
$hMainWin_JidSettings_Menu = GUICtrlCreateContextMenu($hMainWin_JidSettings)
GUICtrlCreateMenuItem('Add Jid', $hMainWin_JidSettings_Menu)
GUICtrlSetOnEvent(-1, '_AddJidWin_Create')
GUICtrlCreateMenuItem('Del Jid', $hMainWin_JidSettings_Menu)
GUICtrlSetOnEvent(-1, '_JidList_Del')
GUICtrlCreateMenuItem('', $hMainWin_JidSettings_Menu)
GUICtrlCreateMenuItem('Show all Jid', $hMainWin_JidSettings_Menu)
GUICtrlSetOnEvent(-1, '_MainWin_ShowAllJid')
GUICtrlCreateMenuItem('Open ini file', $hMainWin_JidSettings_Menu)
GUICtrlSetOnEvent(-1, '_MainWin_OpenIniFile')
GUICtrlCreateButton('Connect', 10+205+72, 494, 70, 23)
GUICtrlSetResizing(-1, $GUI_DOCKALL)
GUICtrlSetOnEvent(-1, '_MainWin_Connect')
GUICtrlCreateButton('Disconnect', 10+205+72+73, 494, 70, 23)
GUICtrlSetResizing(-1, $GUI_DOCKALL)
GUICtrlSetOnEvent(-1, '_MainWin_Disconnect')
GUICtrlCreateButton('Exit', $iMainWin_Width-80, 494, 70, 23)
GUICtrlSetResizing(-1, 4+32+768)
GUICtrlSetOnEvent(-1, '_Pro_Exit')
GUISetState(@SW_SHOW, $hMainWin)
EndFunc
Func _MainWin_JidSettings()
_ShowMenu($hMainWin, $hMainWin_JidSettings, $hMainWin_JidSettings_Menu)
EndFunc
Func _MainWin_ShowAllJid()
_ArrayDisplay($aJidList, 'Jid List')
EndFunc
Func _MainWin_OpenIniFile()
Run('notepad.exe ' & $sIniFilePath)
;~ ShellExecute($sIniFilePath)
EndFunc
Func _MainWin_Connect($sJid_P='')
Local $sJid
If $sConnectedJid Then _MainWin_Disconnect()
If NOT IsDeclared('sJid_P') Then
$sJid = GUICtrlRead($hMainWin_JidList)
Else
$sJid = $sJid_P
If $sJid = '' Then GUICtrlRead($hMainWin_JidList)
EndIf
Local $sLogin = StringRegExpReplace($sJid, '(.+)@.+', '\1')
;~ Local $sServer = StringRegExpReplace($sJid, '.+@(.+)', '\1')
Local $sServer = 'jabber.org'; WARRNING !!!!!!!!!!
$hConnect = _J_Connect_Server($sServer, 5222)
If $hConnect == -1 Then
Return
Else
$sConnectedJid = $sJid
EndIf
AdlibEnable('_MainWin_Listen', 100)
_J_Auth($hConnect, $sServer, $sLogin, 'ru', 'test')
EndFunc
Func _MainWin_Listen()
_J_Listen($hConnect)
EndFunc
Func _MainWin_Disconnect()
_J_Disconnect_Server($hConnect)
AdlibDisable()
EndFunc
#endregion
#region: - Add Jid Win Global, Functions
;~ Handle
Global $hAddJidWin
;~ Title, Width, Height, X, Y, Style, ExStyle
Global $sAddJidWin_Title = 'Add Jid'
Global $iAddJidWin_Width = 240
Global $iAddJidWin_Height = 160
Global $iAddJidWin_X = -1
Global $iAddJidWin_Y = -1
Global $iAddJidWin_Style = BitOR($WS_BORDER, $WS_POPUP)
Global $iAddJidWin_ExStyle = -1
;~ GUI Ctrl
Global $hAddJidWin_Jid, $hAddJidWin_Pass
Func _AddJidWin_Create()
$hAddJidWin = GUICreate($sAddJidWin_Title, $iAddJidWin_Width, $iAddJidWin_Height, $iAddJidWin_X, $iAddJidWin_Y, $iAddJidWin_Style, $iAddJidWin_ExStyle, $hMainWin)
GUISetOnEvent($GUI_EVENT_CLOSE, '_AddJidWin_Close')
GUICtrlCreateGroup('Add new Jid:', 5, 5, $iAddJidWin_Width-10, $iAddJidWin_Height-10)
GUICtrlSetFont(-1, 9.5, 700)
GUICtrlCreateLabel('Login:', 15, 10+15, 70, 18)
$hAddJidWin_Jid = GUICtrlCreateInput('', 15, 25+15, $iAddJidWin_Width-30, 22)
GUICtrlCreateLabel('Password:', 15, 10+45+15, 70, 18)
$hAddJidWin_Pass = GUICtrlCreateInput('', 15, 10+45+15+15, $iAddJidWin_Width-30, 22)
GUICtrlCreateButton('OK', $iAddJidWin_Width-15-50*2-2, $iAddJidWin_Height-40, 50, 25)
GUICtrlSetOnEvent(-1, '_AddJidWin_AddJid')
GUICtrlCreateButton('Cancel', $iAddJidWin_Width-15-50, $iAddJidWin_Height-40, 50, 25)
GUICtrlSetOnEvent(-1, '_AddJidWin_Close')
GUISetState(@SW_DISABLE, $hMainWin)
GUISetState(@SW_SHOW, $hAddJidWin)
EndFunc
Func _AddJidWin_Close()
_ToolTip_Close('', '', '', '')
GUISetState(@SW_ENABLE, $hMainWin)
GUIDelete($hAddJidWin)
EndFunc
Func _AddJidWin_AddJid()
Local $aAddJidWin_Pos = WinGetPos($hAddJidWin)
Local $sJid = StringStripWS(GUICtrlRead($hAddJidWin_Jid), 8)
If $sJid == '' Then
GUICtrlSetData($hAddJidWin_Jid, '')
GUICtrlSetState($hAddJidWin_Jid, $GUI_FOCUS)
_ToolTip(3, 'Enter Login', $aAddJidWin_Pos[0]+20, $aAddJidWin_Pos[1]+60, '', Default, 1)
Return
EndIf
Local $sPass = StringStripWS(GUICtrlRead($hAddJidWin_Pass), 8)
If $sPass == '' Then
GUICtrlSetData($hAddJidWin_Pass, '')
GUICtrlSetState($hAddJidWin_Pass, $GUI_FOCUS)
_ToolTip(3, 'Enter Password', $aAddJidWin_Pos[0]+20, $aAddJidWin_Pos[1]+60+45, '', Default, 1)
Return
EndIf
_JidList_Add($sJid, $sPass)
_AddJidWin_Close()
EndFunc
#endregion
#region: - Jid List Functions
Func _JidList_Update($sJidList_Selected_P='')
Local $sJidList_Selected
If NOT IsDeclared('sJidList_Selected_P') Then
$sJidList_Selected = GUICtrlRead($hMainWin_JidList)
Else
$sJidList_Selected = $sJidList_Selected_P
If $sJidList_Selected = '' Then GUICtrlRead($hMainWin_JidList)
EndIf
GUICtrlSetData($hMainWin_JidList, '')
If NOT StringInStr($aJidList[0][1], '|' & $sJidList_Selected & '|') AND $aJidList[0][0] > 0 Then $sJidList_Selected = $aJidList[1][0]
If $aJidList[0][0] == 0 Then
GUICtrlSetData($hMainWin_JidList, 'No Jid', 'No Jid')
Else
GUICtrlSetData($hMainWin_JidList, $aJidList[0][1], $sJidList_Selected)
EndIf
EndFunc
Func _JidList_Add($sJid, $sPass)
$aJidList[0][1] &= '|' & $sJid & '|'
$aJidList[0][0] += 1
$aJidList[0][1] = StringReplace($aJidList[0][1], '||', '|')
ReDim $aJidList[$aJidList[0][0]+1][2]
$aJidList[$aJidList[0][0]][0] = $sJid
$aJidList[$aJidList[0][0]][1] = $sPass
IniWrite($sIniFilePath, 'Jid List', 'login' & $aJidList[0][0], '"' & $sJid & '"')
IniWrite($sIniFilePath, 'Jid List', 'pass' & $aJidList[0][0], $sPass)
_JidList_Update($sJid)
EndFunc
Func _JidList_Del()
Local $sJid = GUICtrlRead($hMainWin_JidList)
$aJidList[0][1] = StringReplace($aJidList[0][1], '|' & $sJid & '|', '|')
For $i=1 To $aJidList[0][0]
If $aJidList[$i][0] == $sJid Then
$aJidList[0][0] -= 1
_ArrayDelete($aJidList, $i)
ExitLoop
EndIf
Next
_JidList_SaveToIni()
GUICtrlSetData($hMainWin_JidList, '')
_JidList_Update()
EndFunc
Func _JidList_SaveToIni()
IniWriteSection($sIniFilePath, 'Jid List', '')
For $i=1 To $aJidList[0][0]
IniWrite($sIniFilePath, 'Jid List', 'login' & $i, '"' & $aJidList[$i][0] & '"')
IniWrite($sIniFilePath, 'Jid List', 'pass' & $i, $aJidList[$i][1])
Next
EndFunc
#endregion
#region: - After creating all GUI
_MainWin_Create()
#endregion
#region: - Sleep, Exit, OnAutoItExit
While 1
Sleep($iProSleepTime)
WEnd
Func _Pro_Exit()
Exit
EndFunc
#endregion
#region: - ToolTip
Func _ToolTip($iTimeShow, $sText, $iX=Default, $iY=Default, $sTitle=Default, $iIcon=Default, $iOptions=Default)
If $hToolTipTimerID <> '' Then _Timer_KillTimer($hAutoItWin, $hToolTipTimerID)
ToolTip($sText, $iX, $iY, $sTitle, $iIcon, $iOptions)
$hToolTipTimerID = _Timer_SetTimer($hAutoItWin, $iTimeShow*1000, '_ToolTip_Close')
EndFunc
Func _ToolTip_Close($hWnd, $Msg, $iIDTimer, $dwTime)
_Timer_KillTimer($hAutoItWin, $hToolTipTimerID)
$hToolTipTimerID = ''
ToolTip('')
EndFunc
#endregion
#region: - Context Menu
; Show a menu in a given GUI window which belongs to a given GUI ctrl
Func _ShowMenu($hWnd, $CtrlID, $nContextID)
Local $arPos, $x, $y
Local $hMenu = GUICtrlGetHandle($nContextID)
$arPos = ControlGetPos($hWnd, "", $CtrlID)
$x = $arPos[0]
$y = $arPos[1] + $arPos[3]
_ClientToScreen($hWnd, $x, $y)
_TrackPopupMenu($hWnd, $hMenu, $x, $y)
EndFunc ;==>ShowMenu
; Convert the client (GUI) coordinates to screen (desktop) coordinates
Func _ClientToScreen($hWnd, ByRef $x, ByRef $y)
Local $stPoint = DllStructCreate("int;int")
DllStructSetData($stPoint, 1, $x)
DllStructSetData($stPoint, 2, $y)
DllCall("user32.dll", "int", "ClientToScreen", "hwnd", $hWnd, "ptr", DllStructGetPtr($stPoint))
$x = DllStructGetData($stPoint, 1)
$y = DllStructGetData($stPoint, 2)
; release Struct not really needed as it is a local
$stPoint = 0
EndFunc ;==>ClientToScreen
; Show at the given coordinates (x, y) the popup menu (hMenu) which belongs to a given GUI window (hWnd)
Func _TrackPopupMenu($hWnd, $hMenu, $x, $y)
DllCall("user32.dll", "int", "TrackPopupMenuEx", "hwnd", $hMenu, "int", 0, "int", $x, "int", $y, "hwnd", $hWnd, "ptr", 0)
EndFunc ;==>TrackPopupMenu
#endregion
;
; Next only XMPP functions, UDF type
;
Func _J_Listen($hSocket)
Local $sResult = TCPRecv($hSocket, 2048)
if $sResult <> '' then
$sResult = StringReplace($sResult, '><', '>' & @CRLF & '<'); debug
$sResult = 'Server response: ' & @CRLF & $sResult; debug
_GUICtrlEdit_InsertText($hMainWin_XML_Console, @CRLF & @CRLF & $sResult, _GUICtrlEdit_GetTextLen($hMainWin_XML_Console)); debug
EndIf
EndFunc
Func _J_Connect_Server($sServer, $iPort)
Local $sMsg = 'Connecting to server: ' & $sServer & ', port: ' & $iPort & ', please wait...'; debug
_GUICtrlEdit_InsertText($hMainWin_XML_Console, @CRLF & $sMsg, _GUICtrlEdit_GetTextLen($hMainWin_XML_Console)); debug
TCPStartUp()
Local $iResult = TCPConnect(TCPNameToIP($sServer), $iPort); -1 if error
$sMsg = 'TCP Connect: ' & $iResult & ', server: ' & $sServer & ', port: ' & $iPort; debug
_GUICtrlEdit_InsertText($hMainWin_XML_Console, @CRLF & $sMsg, _GUICtrlEdit_GetTextLen($hMainWin_XML_Console)); debug
Return $iResult
EndFunc
Func _J_Disconnect_Server($hSocket)
Local $iResult = TCPCloseSocket($hSocket)
Local $sMsg = 'TCP Disconnect: ' & $iResult; debug
_GUICtrlEdit_InsertText($hMainWin_XML_Console, @CRLF & @CRLF & $sMsg, _GUICtrlEdit_GetTextLen($hMainWin_XML_Console)); debug
TCPShutDown()
Return $iResult
EndFunc
Func _J_Auth($hSocket, $sServer, $sLogin, $slang, $sId='test')
$iId += 1
Local $sMsg = _
'<stream:stream to="' & $sServer & '" xmlns="jabber:client" xmlns:stream="http://etherx.jabber.org/streams" xml:lang="' & $slang & '" />' & @CRLF & _
'<iq type="get" id="' & $sId & '_' & $iId & '">' & @CRLF & _
'<query xmlns="jabber:iq:auth">' & @CRLF & _
'<username>"' & $sLogin & '"</username>' & @CRLF & _
'</query>' & @CRLF & _
'</iq>'; @CRLF - debug
_GUICtrlEdit_InsertText($hMainWin_XML_Console, @CRLF & @CRLF & $sMsg, _GUICtrlEdit_GetTextLen($hMainWin_XML_Console)); debug
$sMsg = StringReplace($sMsg, @CRLF, ''); debug
TCPSend($hSocket, $sMsg)
EndFunc