View difference between Paste ID: Uu2XZ8wn and ENGZKgUc
SHOW: | | - or go back to the newest paste.
1
local function printUsage()
2
    print( "Usages:" )
3
    print( "pastebin put <filename>" )
4
    print( "pastebin get <code> <filename>" )
5
    print( "pastebin run <code> <arguments>" )
6
end
7
 
8
local tArgs = { ... }
9
if #tArgs < 2 then
10
    printUsage()
11
    return
12
end
13
 
14
if not http then
15
    printError( "Pastebin requires http API" )
16
    printError( "Set http_enable to true in ComputerCraft.cfg" )
17
    return
18
end
19
 
20
local function get(paste)
21
    write( "Connecting to pastebin.com... " )
22
    local response = http.get(
23
        "http://pastebin.com/raw.php?i="..textutils.urlEncode( paste )
24
    )
25
        
26
    if response then
27
        print( "Success." )
28
        
29
        local sResponse = response.readAll()
30
        response.close()
31
        return sResponse
32
    else
33
        printError( "Failed." )
34
    end
35
end
36
 
37
local sCommand = tArgs[1]
38
if sCommand == "put" then
39
    -- Upload a file to pastebin.com
40
    -- Determine file to upload
41
    local sFile = tArgs[2]
42
43-
    local sPath = shell.resolve( sFile )
43+
	if kernel.gwd() ~= "/" then
44
	    sPath = kernel.gwd().."/"..sFile
45
	else
46
		sPath = kernel.gwd()..sFile
47
	end
48
    if not fs.exists( sPath ) or fs.isDir( sPath ) then
49
        print( "No such file" )
50
        return
51
    end
52
    
53
    -- Read in the file
54
    local sName = fs.getName( sPath )
55
    local file = fs.open( sPath, "r" )
56
    local sText = file.readAll()
57
    file.close()
58
    
59
    -- POST the contents to pastebin
60
    write( "Connecting to pastebin.com... " )
61
    local key = "0ec2eb25b6166c0c27a394ae118ad829"
62
    local response = http.post(
63
        "http://pastebin.com/api/api_post.php", 
64
        "api_option=paste&"..
65
        "api_dev_key="..key.."&"..
66
        "api_paste_format=lua&"..
67
        "api_paste_name="..textutils.urlEncode(sName).."&"..
68
        "api_paste_code="..textutils.urlEncode(sText)
69
    )
70
        
71
    if response then
72
        print( "Success." )
73
        
74
        local sResponse = response.readAll()
75
        response.close()
76
                
77
        local sCode = string.match( sResponse, "[^/]+$" )
78
        print( "Uploaded as "..sResponse )
79
        print( "Run \"pastebin get "..sCode.."\" to download anywhere" )
80
 
81
    else
82
        print( "Failed." )
83
    end
84
    
85
elseif sCommand == "get" then
86
    -- Download a file from pastebin.com
87
    if #tArgs < 3 then
88
        printUsage()
89
        return
90
    end
91-
    local sPath = shell.resolve( sFile )
91+
92
    -- Determine file to download
93
    local sCode = tArgs[2]
94
    local sFile = tArgs[3]
95
    local sPath
96
	if kernel.gwd() ~= "/" then
97
	    sPath = kernel.gwd().."/"..sFile
98
	else
99
		sPath = kernel.gwd()..sFile
100
	end
101
102
    if fs.exists( sPath ) then
103
        print( "File already exists" )
104
        return
105
    end
106
    
107
    -- GET the contents from pastebin
108
    local res = get(sCode)
109
    if res then        
110
        local file = fs.open( sPath, "w" )
111
        file.write( res )
112
        file.close()
113
        
114
        print( "Downloaded as "..sFile )
115
    end 
116
elseif sCommand == "run" then
117
    local sCode = tArgs[2]
118
 
119
    local res = get(sCode)
120
    if res then
121
        local func, err = load(res, sCode, "t", _ENV)
122
        if not func then
123
            printError( err )
124
            return
125
        end
126
        local success, msg = pcall(func, table.unpack(tArgs, 3))
127
        if not success then
128
            printError( msg )
129
        end
130
    end
131
else
132
    printUsage()
133
    return
134
end