Advertisement
killer64

Untitled

Oct 15th, 2013
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.25 KB | None | 0 0
  1. local socket=require("socket")
  2. local http=require("socket.http")
  3. local url=require("socket.url")
  4. local lfs=require("lfs")
  5. local ltn12=require("ltn12")
  6. local config
  7. do
  8. local oprint=print
  9. function print(txt)
  10. local m=txt:gsub("\27","")
  11. oprint(m)
  12. end
  13. local file=io.open("tBot-config.txt","r")
  14. local defconfig='vernum=0,\nversion="0",\nnsuser=nil,\nnick="tBot",\nchan="#tbot",\nnetwork="178.79.153.80",\nnetport="6667",'
  15. if file then
  16. config=loadstring("return {"..file:read("*a").."}")()
  17. file:close()
  18. if not config.nsuser then
  19. print("Edit config!")
  20. os.exit()
  21. end
  22. else
  23. file=io.open("tBot-config.txt","w")
  24. file:write(defconfig)
  25. file:close()
  26. print("Edit config!")
  27. os.exit()
  28. end
  29. end
  30. local sv
  31. local cnick=config.nick
  32. local function connect()
  33. print("connecting")
  34. if sv then
  35. sv:close()
  36. end
  37. while true do
  38. sv=socket.connect(config.network,config.netport)
  39. if sv then
  40. print("connected")
  41. sv:send("NICK "..config.nick.."\r\nUSER tBot tBot tBot :ping's bot\r\n")
  42. return
  43. end
  44. socket.sleep(5)
  45. print("retrying")
  46. end
  47. end
  48. local function savecfg()
  49. file=io.open("tBot-config.txt","w")
  50. for k,v in pairs(config) do
  51. file:write(k.."=")
  52. if type(v)=="string" then
  53. file:write("\""..string.format("%q",v).."\",\n")
  54. else
  55. file:write(tostring(v)..",\n")
  56. end
  57. end
  58. file:close()
  59. end
  60. local parsecmd
  61. do
  62. local cmdat
  63. local function say(txt)
  64. if cmdat.chan==cnick then
  65. sv:send("PRIVMSG "..cmdat.nick.." :"..txt.."\r\n")
  66. else
  67. sv:send("PRIVMSG "..cmdat.chan.." :"..txt.."\r\n")
  68. end
  69. end
  70. local short={
  71. ["s"]="server",
  72. ["w"]="wiki",
  73. }
  74. local cmds={
  75. ["help"]=function()
  76. say("Commands: help, server, wiki | Test commands in PM first!")
  77. end,
  78. ["ping"]=function()
  79. say("pong")
  80. end,
  81. ["server"]=function(dat)
  82. local m={}
  83. for ma in dat:gmatch("%S+") do
  84. m[#m+1]=ma
  85. end
  86. if #m>2 or #m<1 then
  87. say(cmdat.nick..", Usage: ~server <ip> [port]")
  88. return
  89. end
  90. m[2]=tonumber(m[2] or "7777")
  91. if not m[2] then
  92. say(cmdat.nick..", Invalid port!")
  93. return
  94. end
  95. if m[2]>65535 or m[2]<1024 then
  96. say(cmdat.nick..", Port out of allowed range!")
  97. return
  98. end
  99. local n=socket.dns.toip(m[1])
  100. if not n then
  101. say(cmdat.nick..", Could not resolve address!")
  102. return
  103. end
  104. local s=socket.tcp()
  105. s:settimeout(1)
  106. local sv=s:connect(n,m[2])
  107. if not sv then
  108. say(cmdat.nick..", Could not connect!")
  109. return
  110. end
  111. local pa="\1Terraria"..config.vernum
  112. s:send(string.char(#pa).."\0\0\0"..pa)
  113. local pk=s:receive(5)
  114. if not pk then
  115. say(cmdat.nick..", Could not connect!")
  116. return
  117. end
  118. pk=pk:sub(5)
  119. if pk=="\3" then
  120. say(cmdat.nick..", Server running and updated to "..config.version)
  121. elseif pk=="\2" then
  122. say(cmdat.nick..", Server running but outdated (<"..config.version..")")
  123. else
  124. say(cmdat.nick..", Unknown response code: 0x"..string.format("%X",string.byte(pk)))
  125. end
  126. end,
  127. ["setversion"]=function(dat)
  128. local m={}
  129. for ma in dat:gmatch("%S+") do
  130. m[#m+1]=ma
  131. end
  132. if #m~=2 then
  133. say(cmdat.nick..", Usage: ~setversion <number> <name> ; Example: 71 1.2.0.3.1")
  134. return
  135. end
  136. m[1]=tonumber(m[1])
  137. if not m[1] then
  138. say(cmdat.nick..", Invalid version number!")
  139. return
  140. end
  141. config.vernum=m[1]
  142. config.version=m[2]
  143. savecfg()
  144. say(cmdat.nick..", Version set!")
  145. end,
  146. ["wiki"]=function(dat)
  147. dat=dat:gsub("%s+"," ")
  148. if dat==" " or #dat==0 then
  149. say(cmdat.nick..", Usage: ~wiki <search>")
  150. return
  151. end
  152. dat=url.escape(dat)
  153. local t={}
  154. local r=http.request({
  155. url="http://terraria.gamepedia.com/"..dat,
  156. headers={["User-Agent"]="tBot"},
  157. sink=ltn12.sink.table(t),
  158. })
  159. local c=table.concat(t)
  160. if not c:find('<div class="noarticletext">\n<p>There is currently no text in this page.') then
  161. local ur=c:match('<meta name="og:url" content="http://terraria%.gamepedia%.com/(.-)" />')
  162. if ur then
  163. say("http://terraria.gamepedia.com/"..ur)
  164. return
  165. end
  166. end
  167. t={}
  168. r=http.request({
  169. url="http://terraria.gamepedia.com/api.php?limit=1&action=opensearch&search="..dat,
  170. headers={["User-Agent"]="tBot"},
  171. sink=ltn12.sink.table(t),
  172. })
  173. if r then
  174. local _,m=table.concat(t):match("%[\"(.+)\",%[\"(.+)\"%]%]")
  175. if m then
  176. say("http://terraria.gamepedia.com/"..m:gsub(" ","_"))
  177. else
  178. say("No results.")
  179. end
  180. end
  181. end
  182. }
  183. for k,v in pairs(short) do
  184. cmds[k]=cmds[v]
  185. end
  186. local spam={}
  187. function parsecmd(tb,dat)
  188. cmdat=tb
  189. local cmd=dat:match("^%S+")
  190. if cmds[cmd] then
  191. spam[tb.chan]=spam[tb.chan] or {}
  192. local sp=spam[tb.chan]
  193. sp[tb.host]=sp[tb.host] or 0
  194. if os.time()<sp[tb.host] then
  195. sv:send("NOTICE "..tb.nick.." :Please wait "..sp[tb.host]-os.time().." seconds before sending another command")
  196. if tb.chan==cnick then
  197. sv:send(".\r\n")
  198. else
  199. sv:send(" in "..tb.chan.."\r\n")
  200. end
  201. return
  202. end
  203. if tb.chan==cnick or cmd=="help" then
  204. sp[tb.host]=os.time()+5
  205. else
  206. sp[tb.host]=os.time()+30
  207. end
  208. cmds[cmd](dat:sub(#cmd+2))
  209. end
  210. end
  211. end
  212. local resp={
  213. ["^PING (.+)"]=function(r)
  214. sv:send("PONG "..r.."\r\n")
  215. end,
  216. ["^PING$"]=function()
  217. sv:send("PONG\r\n")
  218. end,
  219. ["^:(.+) 433 (.+)"]=function()
  220. cnick=cnick.."_"
  221. sv:send("NICK "..cnick.."\r\n")
  222. end,
  223. [function() return "^:"..cnick.." MODE "..cnick.." :%+ix" end]=function()
  224. print("joined")
  225. if cnick~=config.nick then
  226. sv:send("PRIVMSG NickServ :identify "..config.nsuser.."\r\n")
  227. else
  228. sv:send("JOIN "..config.chan.."\r\n")
  229. end
  230. end,
  231. ["^:NickServ!NickServ@services%. NOTICE (.-) :(.+)"]=function(ch,msg)
  232. if msg:sub(1,22)=="You are now identified" then
  233. sv:send("PRIVMSG NickServ :ghost "..config.nick.."\r\n")
  234. elseif msg=="\2"..config.nick.."\2 has been ghosted." then
  235. cnick=config.nick
  236. sv:send("NICK "..config.nick.."\r\nJOIN "..config.chan.."\r\n")
  237. end
  238. end,
  239. ["^:(.-)!(.-) PRIVMSG (.-) :(.+)"]=function(ni,hn,ch,msg)
  240. local m=msg:match("^\1(.+)\1$")
  241. if m and ch==cnick then
  242. if m:sub(1,7)=="ACTION " then
  243. print("["..ch.."] * "..ni.." "..m:sub(8))
  244. else
  245. print("-CTCP- <"..ni.."> "..m)
  246. end
  247. if m=="VERSION" then
  248. sv:send("NOTICE "..ni.." :Potato v49.51.51.55\r\n")
  249. elseif m:sub(1,5)=="PING " or m=="PING" then
  250. sv:send("NOTICE "..ni.." :PONG"..m:sub(5).."\r\n")
  251. end
  252. else
  253. if m then
  254. if m:sub(1,7)=="ACTION " then
  255. print("["..ch.."] * "..ni.." "..m:sub(8))
  256. else
  257. print("["..ch.."] <"..ni.."> "..msg)
  258. end
  259. else
  260. print("["..ch.."] <"..ni.."> "..msg)
  261. end
  262. if ch==cnick then
  263. parsecmd({chan=ch,nick=ni,host=hn},msg)
  264. elseif msg:sub(1,1)=="." then
  265. parsecmd({chan=ch,nick=ni,host=hn},msg:sub(2))
  266. end
  267. end
  268. end,
  269. ["^:NickServ!NickServ@services%. KILL "..config.nick.." (.+)"]=function()
  270. os.exit()
  271. end
  272. }
  273. connect()
  274. while true do
  275. local s,e=sv:receive()
  276. if e=="closed" then
  277. connect()
  278. end
  279. while s do
  280. for k,v in pairs(resp) do
  281. if type(k)=="function" then
  282. k=k()
  283. end
  284. local m={s:match(k)}
  285. if m[1] then
  286. v(unpack(m))
  287. end
  288. end
  289. s,e=sv:receive()
  290. if e=="closed" then
  291. connect()
  292. end
  293. end
  294. socket.select({sv})
  295. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement