Advertisement
dannysmc95

Blaze API (Email System)

Dec 19th, 2014
330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.30 KB | None | 0 0
  1. -- Blaze API
  2. -- Created By DannySMc
  3. -- Platform: Lua Virtual Machine
  4.  
  5. --[[ Blaze API: How to use, and contents
  6.  
  7.     CONTENTS:
  8.     ---------
  9.     Login -> Takes a username and password and will return true or false
  10.     Register -> Takes a username, password and email and returns true or false
  11.     Send -> Takes a username, password, recipient, subject, message and will return true or false
  12.     Delete -> Takea a username, password, and msgid, will then return true or false
  13.     Inbox -> Will take a username and password and return a nested array
  14.     WordWrap -> This will take a string and a character limit and will wrap the string to that character limit, returns a table
  15.     SHA256 -> This will take a string and return a hashed sum of it (The hashed sum is the stored password in the database)
  16.  
  17.     How To Use:
  18.     -----------
  19.     Login -> ec.login(username, password)
  20.     Simple. Give it a username and password and it will check it against the database, if that user exists then it will return the boolean value of true or false if no user exists. Always tell the user to register if they don't have a user account.
  21.  
  22.     Register -> ec.register(username, password, email)
  23.     This will take a username, password and email, the email needs to be real as when someone forgets their password (future update) you can send them a link through email to reset password. It returns a boolean true or false if it worked or not.
  24.  
  25.     Send -> ec.send(username, password, recipient, subject, message)
  26.     This needs to take a username, password, recipient, subject and message. The username and password allows it to check you're registered, and then the recipient, message and subject are all for the sending messsage. Make sure the subject is less than 20 characters and the message is less than 300 characters. Will return true or false on whether it worked or not, of course this may sometimes return false, when the message did send, so I wouldn't consider using the true or false values as the PHP script has it's moments.
  27.  
  28.     Delete -> ec.delete(username, password, msgid)
  29.     This takes a username and password to check your account and then takes the msgid, which is the first value in the nested array, for example to get the first message's id, (the table of all messages is called inbox): inbox[1][1] and this will return the id, if you want the second messages id then use inbox[2][1], etc. This will of course return true or false as a boolean result on whether it worked or not. Please note sometimes the message will delete but cause of an error it may delete the message but return false, so always make sure first as it is tempermental.
  30.  
  31.     Inbox -> ec.inbox(username, password)
  32.     This of course will return your whole inbox for the username and password arguments you give it, this may also be a slow process as the PHP script will loop through every single row of the table and add it to a lua nested array. So depending on internet speed and inbox count, so be wary to make this a function that is ONLY run when needed! Of cours to update the inbox etc. The format is as follows:
  33.     {
  34.         {
  35.             "Message ID",
  36.             "Message Sender",
  37.             "Recipeint",
  38.             "Subject",
  39.             "Message",
  40.         },
  41.         {
  42.             "Message 2 ID",
  43.             "Message 2 Sender",
  44.             "Recipient for message 2",
  45.             "Subject for message 2",
  46.             "Message for message 2",
  47.         },
  48.     }
  49.     so it is a nested table, so make sure when running through them that you get the right values from each message. Each message is it's own array inside another array, have any questions about this, email me on: danny@xigen.co.uk or PM me on the computercraft forums, my username is dannysmc95. Look at blaze for code snippets to display the contents but it is pretty simple as you can do a straight for loop on that array. for loops for nested tables work as so (this will get the second message in the table):
  50.     for _, v in ipairs(inbox[2]) do
  51.         print(v)
  52.     end
  53.     Like I said any questions PM me or email me.
  54.  
  55.     SHA256 -> ec.sha256(msg)
  56.     This is to be used with the database. All passwords should be hashed using this before they are stored or sent up, as this is what my Blaze Client uses to encrypt passwords and etc. Please use this like the following:
  57.     newPassword = ec.sha256(password)
  58.     and then use newPassword to send up (of course the variable names can be different if you want).
  59.  
  60.     Word Wrap -> ec.wordwrap(string, limit)
  61.     This is something I added which may be useful, in essence it will wrap the string variable to the limit (which should be a number) and return a table, so if you have a string that is like 300 characters and you wish to make it automatically wrap it to a computer you can use (msg being the 300 character string) ec.wordwrap(msg, 51) - always make sure you never send a wordwrapped string up as it will error, or at least if you try and get it back the inbox will be messed up!
  62.  
  63.     QUESTIONS:
  64.     ----------
  65.     Any questions PM me on computercraft forums: dannysmc95, or email me on danny@xigen.co.uk
  66.  
  67.     LICENSE:
  68.     --------
  69.     This is licensed under the Creative Commons (Attribution 3.0) license.
  70.     This declares you can:
  71.  
  72.     Share - Copy and redistribute the material in any medium or format
  73.     Adapt - Remix, transform, and build upon the material for any purpose, even commercially.
  74.  
  75.     AS LONG AS you follow the license terms which are as follows:
  76.  
  77.     Attribution - You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use.
  78.  
  79.     In other words you can use it as you need, but you have to give credit to me. To do this add me into a credits section as the hoster and creator of the API, or link to my computercraft forums profile or my website: http://dannysmc.com.
  80.  
  81.     I hope this is good and I hope you guys enjoy this, it is made so the email style of client can be standardized using this. So everyone can send emails to and fro without problems, with multiple programs created by many users but follow the same API to allow all the programs to work together!
  82.  
  83. ]]
  84.  
  85. blazeurls = {"http://dannysmc.com/files/php/emailsystem.php"}
  86. blazecmds = {"login", "getInbox", "register", "delete", "send",}
  87.  
  88. ec = {}
  89. ec.__index = ec
  90.  
  91. function ec.login(username, password)
  92.     local req = http.post(blazeurls[1], "command="..textutils.urlEncode(tostring(blazecmds[1])).."&".."username="..textutils.urlEncode(tostring(username)).."&".."password="..textutils.urlEncode(tostring(password)))
  93.     if req.readAll() == '"true"' then
  94.         return true
  95.     else
  96.         return false
  97.     end
  98. end
  99.  
  100. function ec.inbox(username, password)
  101.     local req = http.post(blazeurls[1], "command="..textutils.urlEncode(tostring(blazecmds[2])).."&".."username="..textutils.urlEncode(tostring(username)).."&".."password="..textutils.urlEncode(tostring(password)))
  102.     return req
  103. end
  104.  
  105. function ec.send(username, password, recipient, subject, message)
  106.     local req = http.post(blazeurls[1], "command="..textutils.urlEncode(tostring(blazecmds[5])).."&".."username="..textutils.urlEncode(tostring(username)).."&".."password="..textutils.urlEncode(tostring(password)).."&".."recipient="..textutils.urlEncode(tostring(recipient)).."&".."subject="..textutils.urlEncode(tostring(subject)).."&".."message="..textutils.urlEncode(tostring(message)))
  107.     if req.readAll() == '"true"' then
  108.         return true
  109.     else
  110.         return false
  111.     end
  112. end
  113.  
  114. function ec.delete(username, password, msgid)
  115.     local req = http.post(blazeurls[1], "command="..textutils.urlEncode(tostring(blazecmds[4])).."&".."username="..textutils.urlEncode(tostring(username)).."&".."password="..textutils.urlEncode(tostring(password)).."&".."messageid="..textutils.urlEncode(tostring(msgid)))
  116.     if req.readAll() == '"true"' then
  117.         return true
  118.     else
  119.         return false
  120.     end
  121. end
  122.  
  123. function ec.register(username, password, email)
  124.     local req = http.post(blazeurls[1], "command="..textutils.urlEncode(tostring(blazecmds[3])).."&".."username="..textutils.urlEncode(tostring(username)).."&".."password="..textutils.urlEncode(tostring(password)).."&".."email="..textutils.urlEncode(tostring(email)))
  125.     if req.readAll() == '"true"' then
  126.         return true
  127.     else
  128.         return false
  129.     end
  130. end
  131.  
  132. function ec.wordwrap(str, limit)
  133.   limit = limit or 72
  134.   local here = 1
  135.   local buf = ""
  136.   local t = {}
  137.   str:gsub("(%s*)()(%S+)()",
  138.   function(sp, st, word, fi)
  139.         if fi-here > limit then
  140.            --# Break the line
  141.            here = st
  142.            table.insert(t, buf)
  143.            buf = word
  144.         else
  145.            buf = buf..sp..word  --# Append
  146.         end
  147.   end)
  148.   --# Tack on any leftovers
  149.   if(buf ~= "") then
  150.         table.insert(t, buf)
  151.   end
  152.   return t
  153. end
  154.  
  155. local MOD = 2^32
  156. local MODM = MOD-1
  157. local function memoize(f)
  158.   local mt = {}
  159.   local t = setmetatable({}, mt)
  160.   function mt:__index(k)
  161.     local v = f(k)
  162.     t[k] = v
  163.     return v
  164.   end
  165.   return t
  166. end
  167. local function make_bitop_uncached(t, m)
  168.   local function bitop(a, b)
  169.     local res,p = 0,1
  170.     while a ~= 0 and b ~= 0 do
  171.       local am, bm = a % m, b % m
  172.       res = res + t[am][bm] * p
  173.       a = (a - am) / m
  174.       b = (b - bm) / m
  175.       p = p*m
  176.     end
  177.     res = res + (a + b) * p
  178.     return res
  179.   end
  180.   return bitop
  181. end
  182. local function make_bitop(t)
  183.   local op1 = make_bitop_uncached(t,2^1)
  184.   local op2 = memoize(function(a) return memoize(function(b) return op1(a, b) end) end)
  185.   return make_bitop_uncached(op2, 2 ^ (t.n or 1))
  186. end
  187. local bxor1 = make_bitop({[0] = {[0] = 0,[1] = 1}, [1] = {[0] = 1, [1] = 0}, n = 4})
  188. local function bxor(a, b, c, ...)
  189.   local z = nil
  190.   if b then
  191.     a = a % MOD
  192.     b = b % MOD
  193.     z = bxor1(a, b)
  194.     if c then z = bxor(z, c, ...) end
  195.     return z
  196.   elseif a then return a % MOD
  197.   else return 0 end
  198. end
  199. local function band(a, b, c, ...)
  200.   local z
  201.   if b then
  202.     a = a % MOD
  203.     b = b % MOD
  204.     z = ((a + b) - bxor1(a,b)) / 2
  205.     if c then z = bit32_band(z, c, ...) end
  206.     return z
  207.   elseif a then return a % MOD
  208.   else return MODM end
  209. end
  210. local function bnot(x) return (-1 - x) % MOD end
  211. local function rshift1(a, disp)
  212.   if disp < 0 then return lshift(a,-disp) end
  213.   return math.floor(a % 2 ^ 32 / 2 ^ disp)
  214. end
  215. local function rshift(x, disp)
  216.   if disp > 31 or disp < -31 then return 0 end
  217.   return rshift1(x % MOD, disp)
  218. end
  219. local function lshift(a, disp)
  220.   if disp < 0 then return rshift(a,-disp) end
  221.   return (a * 2 ^ disp) % 2 ^ 32
  222. end
  223. local function rrotate(x, disp)
  224.     x = x % MOD
  225.     disp = disp % 32
  226.     local low = band(x, 2 ^ disp - 1)
  227.     return rshift(x, disp) + lshift(low, 32 - disp)
  228. end
  229. local k = {
  230.   0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
  231.   0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
  232.   0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
  233.   0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
  234.   0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
  235.   0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
  236.   0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
  237.   0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
  238.   0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
  239.   0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
  240.   0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
  241.   0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
  242.   0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
  243.   0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
  244.   0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
  245.   0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
  246. }
  247. local function str2hexa(s)
  248.   return (string.gsub(s, ".", function(c) return string.format("%02x", string.byte(c)) end))
  249. end
  250. local function num2s(l, n)
  251.   local s = ""
  252.   for i = 1, n do
  253.     local rem = l % 256
  254.     s = string.char(rem) .. s
  255.     l = (l - rem) / 256
  256.   end
  257.   return s
  258. end
  259. local function s232num(s, i)
  260.   local n = 0
  261.   for i = i, i + 3 do n = n*256 + string.byte(s, i) end
  262.   return n
  263. end
  264. local function preproc(msg, len)
  265.   local extra = 64 - ((len + 9) % 64)
  266.   len = num2s(8 * len, 8)
  267.   msg = msg .. "\128" .. string.rep("\0", extra) .. len
  268.   assert(#msg % 64 == 0)
  269.   return msg
  270. end
  271. local function initH256(H)
  272.   H[1] = 0x6a09e667
  273.   H[2] = 0xbb67ae85
  274.   H[3] = 0x3c6ef372
  275.   H[4] = 0xa54ff53a
  276.   H[5] = 0x510e527f
  277.   H[6] = 0x9b05688c
  278.   H[7] = 0x1f83d9ab
  279.   H[8] = 0x5be0cd19
  280.   return H
  281. end
  282. local function digestblock(msg, i, H)
  283.   local w = {}
  284.   for j = 1, 16 do w[j] = s232num(msg, i + (j - 1)*4) end
  285.   for j = 17, 64 do
  286.     local v = w[j - 15]
  287.     local s0 = bxor(rrotate(v, 7), rrotate(v, 18), rshift(v, 3))
  288.     v = w[j - 2]
  289.     w[j] = w[j - 16] + s0 + w[j - 7] + bxor(rrotate(v, 17), rrotate(v, 19), rshift(v, 10))
  290.   end
  291.   local a, b, c, d, e, f, g, h = H[1], H[2], H[3], H[4], H[5], H[6], H[7], H[8]
  292.   for i = 1, 64 do
  293.     local s0 = bxor(rrotate(a, 2), rrotate(a, 13), rrotate(a, 22))
  294.     local maj = bxor(band(a, b), band(a, c), band(b, c))
  295.     local t2 = s0 + maj
  296.     local s1 = bxor(rrotate(e, 6), rrotate(e, 11), rrotate(e, 25))
  297.     local ch = bxor (band(e, f), band(bnot(e), g))
  298.     local t1 = h + s1 + ch + k[i] + w[i]
  299.     h, g, f, e, d, c, b, a = g, f, e, d + t1, c, b, a, t1 + t2
  300.   end
  301.   H[1] = band(H[1] + a)
  302.   H[2] = band(H[2] + b)
  303.   H[3] = band(H[3] + c)
  304.   H[4] = band(H[4] + d)
  305.   H[5] = band(H[5] + e)
  306.   H[6] = band(H[6] + f)
  307.   H[7] = band(H[7] + g)
  308.   H[8] = band(H[8] + h)
  309. end
  310. function ec.sha256(msg)
  311.   msg = preproc(msg, #msg)
  312.   local H = initH256({})
  313.   for i = 1, #msg, 64 do digestblock(msg, i, H) end
  314.   return str2hexa(num2s(H[1], 4) .. num2s(H[2], 4) .. num2s(H[3], 4) .. num2s(H[4], 4) ..
  315.     num2s(H[5], 4) .. num2s(H[6], 4) .. num2s(H[7], 4) .. num2s(H[8], 4))
  316. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement