Advertisement
hbar

Untitled

Jun 26th, 2013
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.32 KB | None | 0 0
  1. --[[
  2. Cleverbot client for ComputerCraft v1.0
  3. Based on https://code.google.com/p/chatter-bot-api/
  4.  
  5. This code uses a pure lua implementation of the MD5 hash algortihm by Adam Baldwin as presented
  6. in this Stack Overflow answer: http://stackoverflow.com/a/12292659
  7.  
  8. Usage:
  9. pastebin get Dv9x1ppc cleverbot
  10. cleverbot start
  11.  
  12. If a Chat Box (from Misc Peripherals) is connected to the computer the client uses it. Voice commands are activated by prepending the message with "Jarvis:" or "Jarvis," or just "Jarvis ". The name and delimiters can be customized by changing the relevant variables. Without a Chat Box the code responds to typed messages on the computer (no need to use the name, just type the message).
  13.  
  14. This same file can also be used as an API.
  15.  
  16. Example code:
  17. os.load("cleverbot")
  18.  
  19. bot = cleverbot.Cleverbot.new()
  20.  
  21. message = "Hi there!"
  22. response = bot:send(message) -- use of : is important here because of self reference
  23. print(response)
  24.  
  25. Notes:
  26. * Sending the message is pretty slow as the hashing takes time and cleverbot.com can sometimes take a while to respond. On slower computers this can sometimes lead to "too long without yielding" error.
  27.  
  28. Matti Vapa (2013)
  29. ]]--
  30.  
  31.  
  32. -- Here are the (slightly modified) LuaBit and MD5 implementations with original comments
  33.  
  34. --[[---------------
  35. LuaBit v0.4
  36. -------------------
  37. a bitwise operation lib for lua.
  38.  
  39. http://luaforge.net/projects/bit/
  40.  
  41. How to use:
  42. -------------------
  43. bit.bnot(n) -- bitwise not (~n)
  44. bit.band(m, n) -- bitwise and (m & n)
  45. bit.bor(m, n) -- bitwise or (m | n)
  46. bit.bxor(m, n) -- bitwise xor (m ^ n)
  47. bit.brshift(n, bits) -- right shift (n >> bits)
  48. bit.blshift(n, bits) -- left shift (n << bits)
  49. bit.blogic_rshift(n, bits) -- logic right shift(zero fill >>>)
  50.  
  51. Please note that bit.brshift and bit.blshift only support number within
  52. 32 bits.
  53.  
  54. 2 utility functions are provided too:
  55. bit.tobits(n) -- convert n into a bit table(which is a 1/0 sequence)
  56. -- high bits first
  57. bit.tonumb(bit_tbl) -- convert a bit table into a number
  58. -------------------
  59.  
  60. Under the MIT license.
  61.  
  62. copyright(c) 2006~2007 hanzhao ([email protected])
  63. --]]---------------
  64.  
  65. --do
  66.  
  67. ------------------------
  68. -- bit lib implementions
  69.  
  70. local function check_int(n)
  71. -- checking not float
  72. if(n - math.floor(n) > 0) then
  73. error("trying to use bitwise operation on non-integer!")
  74. end
  75. end
  76.  
  77. local function tbl_to_number(tbl)
  78. local n = #tbl
  79.  
  80. local rslt = 0
  81. local power = 1
  82. for i = 1, n do
  83. rslt = rslt + tbl[i]*power
  84. power = power*2
  85. end
  86.  
  87. return rslt
  88. end
  89.  
  90. local function expand(tbl_m, tbl_n)
  91. local big = {}
  92. local small = {}
  93. if(#tbl_m > #tbl_n) then
  94. big = tbl_m
  95. small = tbl_n
  96. else
  97. big = tbl_n
  98. small = tbl_m
  99. end
  100. -- expand small
  101. for i = #small + 1, #big do
  102. small[i] = 0
  103. end
  104.  
  105. end
  106.  
  107. local to_bits = function () end
  108.  
  109. local function bit_not(n)
  110. local tbl = to_bits(n)
  111. local size = math.max(#tbl, 32)
  112. for i = 1, size do
  113. if(tbl[i] == 1) then
  114. tbl[i] = 0
  115. else
  116. tbl[i] = 1
  117. end
  118. end
  119. return tbl_to_number(tbl)
  120. end
  121.  
  122.  
  123. to_bits = function (n)
  124. check_int(n)
  125. if(n < 0) then
  126. -- negative
  127. return to_bits(bit_not(math.abs(n)) + 1)
  128. end
  129. -- to bits table
  130. local tbl = {}
  131. local cnt = 1
  132. while (n > 0) do
  133. local last = math.fmod(n,2)
  134. if(last == 1) then
  135. tbl[cnt] = 1
  136. else
  137. tbl[cnt] = 0
  138. end
  139. n = (n-last)/2
  140. cnt = cnt + 1
  141. end
  142.  
  143. return tbl
  144. end
  145.  
  146.  
  147. local function bit_or(m, n)
  148. local tbl_m = to_bits(m)
  149. local tbl_n = to_bits(n)
  150. expand(tbl_m, tbl_n)
  151.  
  152. local tbl = {}
  153. local rslt = math.max(#tbl_m, #tbl_n)
  154. for i = 1, rslt do
  155. if(tbl_m[i]== 0 and tbl_n[i] == 0) then
  156. tbl[i] = 0
  157. else
  158. tbl[i] = 1
  159. end
  160. end
  161.  
  162. return tbl_to_number(tbl)
  163. end
  164.  
  165. local function bit_and(m, n)
  166. local tbl_m = to_bits(m)
  167. local tbl_n = to_bits(n)
  168. expand(tbl_m, tbl_n)
  169.  
  170. local tbl = {}
  171. local rslt = math.max(#tbl_m, #tbl_n)
  172. for i = 1, rslt do
  173. if(tbl_m[i]== 0 or tbl_n[i] == 0) then
  174. tbl[i] = 0
  175. else
  176. tbl[i] = 1
  177. end
  178. end
  179.  
  180. return tbl_to_number(tbl)
  181. end
  182.  
  183. local function bit_xor(m, n)
  184. local tbl_m = to_bits(m)
  185. local tbl_n = to_bits(n)
  186. expand(tbl_m, tbl_n)
  187.  
  188. local tbl = {}
  189. local rslt = math.max(#tbl_m, #tbl_n)
  190. for i = 1, rslt do
  191. if(tbl_m[i] ~= tbl_n[i]) then
  192. tbl[i] = 1
  193. else
  194. tbl[i] = 0
  195. end
  196. end
  197.  
  198. --table.foreach(tbl, print)
  199.  
  200. return tbl_to_number(tbl)
  201. end
  202.  
  203. local function bit_rshift(n, bits)
  204. check_int(n)
  205.  
  206. local high_bit = 0
  207. if(n < 0) then
  208. -- negative
  209. n = bit_not(math.abs(n)) + 1
  210. high_bit = 2147483648 -- 0x80000000
  211. end
  212.  
  213. for i=1, bits do
  214. n = n/2
  215. n = bit_or(math.floor(n), high_bit)
  216. end
  217. return math.floor(n)
  218. end
  219.  
  220. -- logic rightshift assures zero filling shift
  221. local function bit_logic_rshift(n, bits)
  222. check_int(n)
  223. if(n < 0) then
  224. -- negative
  225. n = bit_not(math.abs(n)) + 1
  226. end
  227. for i=1, bits do
  228. n = n/2
  229. end
  230. return math.floor(n)
  231. end
  232.  
  233. local function bit_lshift(n, bits)
  234. check_int(n)
  235.  
  236. if(n < 0) then
  237. -- negative
  238. n = bit_not(math.abs(n)) + 1
  239. end
  240.  
  241. for i=1, bits do
  242. n = n*2
  243. end
  244. return bit_and(n, 4294967295) -- 0xFFFFFFFF
  245. end
  246.  
  247. local function bit_xor2(m, n)
  248. local rhs = bit_or(bit_not(m), bit_not(n))
  249. local lhs = bit_or(m, n)
  250. local rslt = bit_and(lhs, rhs)
  251. return rslt
  252. end
  253. local md5={ff=tonumber(ffffffff,16),consts={}}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement