· 4 years ago · Mar 15, 2021, 07:20 PM
1local tArgs = { ... }
2local config = {
3 key = {}
4}
5
6function configWrite()
7 local file = fs.open("/twitter.cfg", "w")
8 file.write(textutils.serialize(config))
9 file.close()
10end
11
12function configRead()
13 local file = fs.open("/twitter.cfg", "r")
14 if file then
15 local config = textutils.unserialize(file.readAll())
16 file.close()
17 if config.key.type == "normal" then
18 authentication = {Authorization = "OAuth", oauth_consumer_key = key[1], oauth_consumer_secret = key[2], oauth_token = key[3], oauth_token_secret = key[4]}
19 elseif config.key.type == "bearer" then
20 authentication = {Authorization = "Bearer " .. key[1] .. ""}
21 end
22 else
23 error("oh noes spaghetti-oes")
24 end
25end
26
27function argsLogin() --The code that makes the login thing work.
28 if tArgs[2] == "normal" or tArgs[2] == nil then
29 key.type = "normal"
30 local keyNames = {
31 "Consumer Key",
32 "Consumer Key Secret",
33 "Access Token",
34 "Access Token Secret"
35 }
36 for i = 1, 4 do
37 write("Input " .. keyNames[i] .. ": ")
38 key[i] = read("*")
39 end
40
41 configWrite()
42 elseif tArgs[2] == "bearer" then
43 key.type = "bearer"
44 write("Input Bearer Token: ")
45 key[1] = read("*")
46
47 configWrite()
48 else
49 printError("twitter <login> [oauth type]")
50 end
51end
52
53function argsTweet()
54 configRead()
55 if config.key.type == "normal" then
56 print("Message:")
57 message = read()
58 http.post("https://api.twitter.com/1.1/statuses/update.json?status=" .. textutils.urlEncode("" .. message .. "\nDebug mode on:\nHOST: " .. _HOST .. "") .. "")
59 elseif config.key.type == "bearer" then
60 printError("This requires OAuth 1.0a, you are using OAuth 2.0a")
61 end
62end
63
64if tArgs[1] == "login" then
65 argsLogin()
66elseif fs.exists("/twitter.cfg") == false then --If config file does not exist, send message telling you that it doesn't.
67 printError("Use \"twitter login\" to add your tokens.")
68elseif tArgs[1] == "tweet" then
69 argsTweet()
70end