Sign in to follow this  
Followers 0
JRSmile

Best practice UDF

2 posts in this topic

#1 ·  Posted (edited)

HI there,

im currently developing a wrapper UDF for µTorrent, but i don't know the best way to create one :)

The logic itself is clear i just want to know how YOU would do the structure the function naming and how detailed you would go :(

here my source i have so far, will be a lot more to do...:

#include-once
#include <array.au3>
#include <String.au3>
#include "vardump.au3"
#include "hash.au3"

Global $server = "127.0.0.1"
Global $port = "31337"
Global $user = "admin"
Global $pass = "test"

;$pid = __UTorrent_Start()
ConsoleWrite(_VarDumpNotByRef(__UTorrent_Torrent_GetList(1)))
;__UTorrent_Stop($pid)

#region UTorrent functions
Func __UTorrent_Start()
    Local $pid = Run(@ScriptDir & "\utorrent.exe", @ScriptDir, @SW_HIDE)
    If @error Then Return 0
    If ProcessWait($pid, 5) = 0 Then Return 0 ; 5 seconds timeout to give the program time to start.
    Return $pid
EndFunc   ;==>__UTorrent_Start

Func __UTorrent_Stop($pid)
    ProcessClose($pid)
    If @error Then Return 0
    Return 1
EndFunc   ;==>__UTorrent_Stop

Func __UTorrent_GetSettings()
    Local $tmp, $option_name, $value
    Local $res = _StringBetween(send_request($server, $port, $user, $pass, "action=getsettings"), '[', ']', 1)
    If @error Then Return 0
    Local $oHash = _HashNew()
    If @error Then Return 0
    For $i = 0 To UBound($res) - 1
        $tmp = StringSplit($res[$i], ",", 1)
        If $tmp[0] = 3 Then
            $tmp[1] = StringReplace($tmp[1], '"', "")
            $tmp[1] = StringReplace($tmp[1], '[', "")
            $option_name = StringReplace($tmp[1], @LF, "")
            $value = String(StringReplace($tmp[3], '"', ""))
            _HashAdd($oHash, $option_name, $value)
            If @error Then _HashSet($oHash, $option_name, $value)
        EndIf
    Next
    Return $oHash
EndFunc   ;==>__UTorrent_GetSettings

Func __UTorrent_SetSetting($setting, $value)
    Local $result = send_request($server, $port, $user, $pass, "action=setsetting&s=" & $setting & "&v=" & $value)
    Return $result
EndFunc   ;==>__UTorrent_SetSetting

Func __UTorrent_Torrent_GetList($list)
    Local $result = send_request($server, $port, $user, $pass, "list=" & $list)
    $array = StringRegExp($result, "[1234567890ABCDEF]{40}", 3)
    Return $array
EndFunc   ;==>__UTorrent_Torrent_GetList

Func __UTorrent_GetSetting_WebUI_Username()
    Local $oSettings = __UTorrent_GetSettings()
    Return _HashGet($oSettings, "webui.username")
EndFunc   ;==>__UTorrent_GetSetting_WebUI_Username

Func __UTorrent_GetSetting_WebUI_Password()
    Local $oSettings = __UTorrent_GetSettings()
    Return _HashGet($oSettings, "webui.password")
EndFunc   ;==>__UTorrent_GetSetting_WebUI_Password

Func __UTorrent_GetSetting_WebUI_Guest()
    Local $oSettings = __UTorrent_GetSettings()
    Return _HashGet($oSettings, "webui.guest")
EndFunc   ;==>__UTorrent_GetSetting_WebUI_Guest

Func __UTorrent_GetSetting_WebUI_Restrict()
    Local $oSettings = __UTorrent_GetSettings()
    Return _HashGet($oSettings, "webui.restrict")
EndFunc   ;==>__UTorrent_GetSetting_WebUI_Restrict

Func __UTorrent_GetSetting_WebUI_Port()
    Local $oSettings = __UTorrent_GetSettings()
    Return _HashGet($oSettings, "webui.port")
EndFunc   ;==>__UTorrent_GetSetting_WebUI_Port

Func __UTorrent_GetSetting_WebUI_Cookie()
    Local $oSettings = __UTorrent_GetSettings()
    Return _HashGet($oSettings, "webui.cookie")
EndFunc   ;==>__UTorrent_GetSetting_WebUI_Cookie

Func __UTorrent_GetSetting_Language()
    Local $oSettings = __UTorrent_GetSettings()
    Return _HashGet($oSettings, "language")
EndFunc   ;==>__UTorrent_GetSetting_Language

Func __UTorrent_Torrent_Start($hash)
    Local $result = send_request($server, $port, $user, $pass, "action=start&hash=" & $hash)
    Return $result
EndFunc   ;==>__UTorrent_Torrent_Start

Func __UTorrent_Torrent_Stop($hash)
    Local $result = send_request($server, $port, $user, $pass, "action=stop&hash=" & $hash)
    Return $result
EndFunc   ;==>__UTorrent_Torrent_Stop

Func __UTorrent_Torrent_Pause($hash)
    Local $result = send_request($server, $port, $user, $pass, "action=pause&hash=" & $hash)
    Return $result
EndFunc   ;==>__UTorrent_Torrent_Pause

Func __UTorrent_Torrent_UnPause($hash)
    Local $result = send_request($server, $port, $user, $pass, "action=unpause&hash=" & $hash)
    Return $result
EndFunc   ;==>__UTorrent_Torrent_UnPause

Func __UTorrent_Torrent_ForceStart($hash)
    Local $result = send_request($server, $port, $user, $pass, "action=forcestart&hash=" & $hash)
    Return $result
EndFunc   ;==>__UTorrent_Torrent_ForceStart

Func __UTorrent_Torrent_Recheck($hash)
    Local $result = send_request($server, $port, $user, $pass, "action=recheck&hash=" & $hash)
    Return $result
EndFunc   ;==>__UTorrent_Torrent_Recheck

Func __UTorrent_Torrent_Remove($hash)
    Local $result = send_request($server, $port, $user, $pass, "action=remove&hash=" & $hash)
    Return $result
EndFunc   ;==>__UTorrent_Torrent_Remove

Func __UTorrent_Torrent_RemoveData($hash)
    Local $result = send_request($server, $port, $user, $pass, "action=removedata&hash=" & $hash)
    Return $result
EndFunc   ;==>__UTorrent_Torrent_RemoveData

#endregion UTorrent functions

#region helper functions
Func send_request($server, $port, $user, $pass, $action)
    Local $http = ObjCreate("Msxml2.XMLHTTP.3.0")
    If Not IsObj($http) Then Return 0
    $http.open("GET", "http://" & $server & ":" & $port & "/gui/?" & $action, False, $user, $pass)
    $http.send
    Local $result = $http.responseText
    $http = 0
    Return $result
EndFunc   ;==>send_request
#endregion helper functions

comments are needed :-)

Edited by JRSmile

$a=StringSplit("547275737420796F757220546563686E6F6C75737421","")For $b=1 To UBound($a)+(-1*-1*-1)step(2^4/8);&$b+=1*2/40*µ&Asc(4)Assign("c",Eval("c")&Chr(Dec($a[$b]&$a[$b+1])))''Chr("a")&"HI"Next;time_U&r34d,ths,U-may=get$the&c.l.u.e;b3st-regards,JRSmile;MsgBox(0x000000,"",Eval("c"));PiEs:d0nt+*b3.s4d.4ft3r.1st-try:-)

Share this post


Link to post
Share on other sites



two days and 75 views later...

I'm sad to see that noone of the good UDF programmers is willed to give me some inspiration...


$a=StringSplit("547275737420796F757220546563686E6F6C75737421","")For $b=1 To UBound($a)+(-1*-1*-1)step(2^4/8);&$b+=1*2/40*µ&Asc(4)Assign("c",Eval("c")&Chr(Dec($a[$b]&$a[$b+1])))''Chr("a")&"HI"Next;time_U&r34d,ths,U-may=get$the&c.l.u.e;b3st-regards,JRSmile;MsgBox(0x000000,"",Eval("c"));PiEs:d0nt+*b3.s4d.4ft3r.1st-try:-)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0