This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
NOTUSEDPLEASEDELETE's profile picture

"pastebin" erroring

Started by NOTUSEDPLEASEDELETE, 21 January 2014 - 11:49 PM
NOTUSEDPLEASEDELETE #1
Posted 22 January 2014 - 12:49 AM
The program "pastebin" errors with:


pastebin:101: attempt to index ? (a nil value)
Engineer #2
Posted 22 January 2014 - 03:07 AM
You probably overwritten the program yourself, because pastebin works for me and everyone else who don't have a modified pastebin
CometWolf #3
Posted 22 January 2014 - 03:53 AM
Can't really overwrite a program in rom/programs, as it is read-only. Unless he did it externally i guess. He could however have put a program named pastebin in the root folder.

Try running edit pastebin and see if there's anything in the file.
Bomb Bloke #4
Posted 22 January 2014 - 04:51 AM
It could be a replacement in root, or an overridden function. But I think it's rather more likely that he just threw some parameters at it that it can't make sense of.

What exactly led up to that error? You've only told a fraction of the story, Flick.
oeed #5
Posted 22 January 2014 - 05:20 AM
Yea, taking a look at the pastebin file reveals this:

    -- GET the contents from pastebin
    local res = get(sCode)
    if res then	   
	    local file = fs.open( sPath, "w" )
	    file.write( res ) --this is line 101
	    file.close()
	   
	    print( "Downloaded as "..sFile )
    end

Which seems an unlikely error really, unless the path you're providing is full of weird characters maybe. You've probably got a custom rom or something. The programs are pretty well built to prevent errors.
CometWolf #6
Posted 22 January 2014 - 05:31 AM
Judging by the code Oeed posted, this might be the result of trying to save a paste to a non-existant folder.
oeed #7
Posted 22 January 2014 - 05:47 AM
Judging by the code Oeed posted, this might be the result of trying to save a paste to a non-existant folder.

I just tried doing that, it throws a different error (line 92). But that's something that should be fxied.

For everyone's reference, here's the code of the vanilla pastebin (1.56).

Spoiler

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

It's even numbered :P/>

I keep a copy of the rom in a folder, it's very useful to reference. Especially if you don't have access to the wiki.
Edited on 22 January 2014 - 04:48 AM
Lyqyd #8
Posted 22 January 2014 - 10:12 AM
Moved to Ask a Pro. Try reproducing this on a computer with no startup file after a fresh boot.
NOTUSEDPLEASEDELETE #9
Posted 31 January 2014 - 05:40 PM
Works now! Thanks for trying to help. Could this please be locked?
oeed #10
Posted 31 January 2014 - 06:06 PM
Works now! Thanks for trying to help. Could this please be locked?

I've already done it, but in the future click the 'Report' button in the bottom right of the post and type 'Please lock this'.
Edited on 31 January 2014 - 05:06 PM
Lyqyd #11
Posted 31 January 2014 - 06:48 PM
We generally don't lock Ask a Pro threads anyhow.