http://www.computercraft.info/wiki/api.php?action=feedcontributions&user=188.223.251.127&feedformat=atomComputerCraft Wiki - User contributions [en]2024-03-29T13:40:29ZUser contributionsMediaWiki 1.24.1http://www.computercraft.info/wiki/index.php?title=Textutils_(API)&diff=3221Textutils (API)2012-09-28T07:14:37Z<p>188.223.251.127: </p>
<hr />
<div>Text utilities is used to mess around with text easier.<br />
<br />
Its functions include:<br />
{| border="1" cellpadding="2" cellspacing="0"<br />
!style="background:#EEE" width="200px"|Method name<br />
!style="background:#EEE" width="*"|Description<br />
|-<br />
|[[textutils.slowPrint|textutils.slowPrint( text , nRate)]]<br />
|Slowly prints the text. Only difference is, it leaves a line after it.<br />
|-<br />
|[[textutils.slowWrite|textutils.slowWrite( text , nRate)]]<br />
|Slowly writes the text.<br />
|-<br />
|[[textutils.formatTime|textutils.formatTime( time, bTwentyFourHour)]]<br />
|Put a time into it, and it spews it out in a different format. For example:<br />
local nTime = os.time()<br />
<br />
print(textutils.formatTime( nTime, false ))<br />
|-<br />
|[[textutils.tabulate|textutils.tabulate( table, table2, so on)]]<br />
|Prints tables in an ordered form. Each table is a row, columns' width is auto-adjusted.<br />
|-<br />
|[[textutils.pagedTabulate|textutils.pagedTabulate( table, table2, so on)]]<br />
|<no description given><br />
|-<br />
|[[textutils.serialize|textutils.serialize( t )]]<br />
|Returns a string representation of the table t for storage or transmission.<br />
|-<br />
|[[textutils.unserialize|textutils.unserialize( s )]]<br />
|Returns a table reassembled from the string s.<br />
|-<br />
|[[textutils.urlEncode|textutils.urlEncode( str )]]<br />
|<no description given><br />
|}<br />
<br />
[[Category:APIs]]</div>188.223.251.127http://www.computercraft.info/wiki/index.php?title=Login_with_Roaming_Profiles&diff=3179Login with Roaming Profiles2012-09-24T21:18:35Z<p>188.223.251.127: /* Server Code */</p>
<hr />
<div><br />
This tutorial covers how to make a password server and a password client. The server will host a Lua table of usernames and passwords and the client will remotely connect each start up, ask the user for their username and password and compare it to that on the password server.<br />
<br />
__TOC__<br />
<br />
== Setting Up ==<br />
As I'm sure you're aware, writing code directly into the computer terminal can be difficult, slow, and therefore annoying. I highly recommend writing/copying the following code into a Lua editor and then using FTP to transfer the documents across to the computer or disk. Please ensure you do not add the .lua extension.<br />
<br />
First craft two or more computers. One will act as your password server, any others will be connecting to that computer. Find the computer ID of your password server. This is important as we don't want to broadcast password requests and replies. In the following code I have also locked the requests to certain computers to ensure security but it is not a needed component of the code. If you decide to take the security as well, make a note of all of the ID's of the client computers. I found it useful to give each computer a label and then search for labels.txt on the server to map it out easily.<br />
<br />
Each computer you craft needs a [[modem|rednet modem]].<br />
<br />
== The Code ==<br />
<br />
=== Server Code ===<br />
<br />
os.pullEvent = os.pullEventRaw<br />
term.clear()<br />
term.setCursorPos(1,1)<br />
print("This is a password server. There is no user interaction here.")<br />
print("Please find a computer and login there.") <br />
local firstCycle = true<br />
local validSender = false<br />
local modemSide = "left" -- change to the side of the computer your modem is on<br />
local valid = false<br />
users = { username1, username2 } --make sure users and passwords line up<br />
passwords = { password1, password2 }<br />
senders = { 13, 14, 15, 17 } -- computer ID's of the computers you want to accept requests from<br />
function bootUp()<br />
rednet.open(modemSide)<br />
end <br />
while true do <br />
validSender = false<br />
if firstCycle then<br />
bootUp()<br />
firstCycle = false<br />
end<br />
senderId, message, distance = rednet.receive()<br />
for i,v in inpairs(senders) do<br />
if v == senderId then<br />
validSender = true<br />
break<br />
end<br />
end<br />
if validSender then<br />
for i,v in ipairs(users) do<br />
if message == v then<br />
valid = true<br />
password = passwords[i]<br />
else<br />
valid = false<br />
end<br />
end<br />
if valid then<br />
rednet.send(senderId, password)<br />
else<br />
rednet.send(senderId, "Not Valid")<br />
end<br />
end<br />
end<br />
<br />
=== Client Code ===<br />
<br />
os.pullEvent = os.pullEventRaw<br />
local locker = true<br />
local failed = true<br />
local attempted_login = true<br />
local password_server = 17 -- change to the ID of your password server computer<br />
rednet.open("left") -- change to the side your rednet modem is on<br />
while locker do<br />
attempted_login = false<br />
term.clear()<br />
term.setCursorPos(1,1)<br />
print("Welcome to a USERS PC : Roaming Profile Enabled")<br />
print("What would you like to do?")<br />
print("[1] Login (*)")<br />
print("[2] Shutdown")<br />
write("> ")<br />
local input = read()<br />
if input == "2" then<br />
os.shutdown()<br />
elseif input == "1" then<br />
attempted_login = true<br />
print("Please login...")<br />
write("Username: ")<br />
local username = read()<br />
write("Password: ")<br />
local password = read("*")<br />
rednet.send(password_server, username)<br />
senderId, message, distance = rednet.receive(5)<br />
if password == message then<br />
failed = false<br />
locker = false<br />
term.clear()<br />
term.setCursorPos(1,1)<br />
print("Welcome ", username)<br />
else<br />
print("Not authorised.")<br />
sleep(1)<br />
end<br />
else<br />
print("Command not recognised...")<br />
sleep(2)<br />
end<br />
end<br />
<br />
== Thoughts and Notes ==<br />
If you change the client code, to "startup" and put it in the root directory, it works very well as a login system.<br />
You may also want to put in the line <br />
os.pullEvent = os.pullEventRaw -- disables Ctrl+T<br />
as if it doesn't work first time it can be difficult to get out of the program.<br />
<br />
If you have either Railcraft or Additional Pipes, find the 'World Anchor' or 'Teleport Tether' block. If your server can afford the resources, place the block within a 3x3 grid of your password server. It will ensure that even if you leave the chunk, the password server still is operational. Otherwise you may find that you cannot login because you've left the chunk and the password server is no longer dealing with requests.<br />
<br />
This is a minor security flaw but it is only if someone has access to a computer that is 'whitelisted' to access the password server. The security flaw is that if they use the password Not Valid on that computer they are allowed access;<br />
<br />
if valid then<br />
--send client password<br />
else<br />
--send client Not Valid<br />
end<br />
<br />
to fix remove the else statement<br />
<br />
<br />
[[Category:User Management]]</div>188.223.251.127