--这是一个简易的拆分函数,用于拆分信息 function split(data, pat) local ret = {} for i in string.gmatch(data,pat) do table.insert(ret,i) end return ret end --配置 local nickname = "myircbot" local channel = "#mybotchannel" local net = require("internet") local con = net.open("irc.esper.net",6667) --在此处设定服务器与端口,将会连接到服务器 if(con) then local line,png,linesplt,msgfrom = "" while(true) do line = con:read() --从套接字中读取一行 print(line) linesplt = split(line,"[^:]+") if #linesplt >= 2 and string.find(linesplt[2], "No Ident response") ~= nil then print("JOIN") con:write("USER " .. nickname .. " 0 * :" .. nickname .. "\r\n") --con:write(msg)用于发送信息,con:read()会读取一行 con:write("NICK " .. nickname .. "\r\n") --对IRC而言,请记住在所有信息末尾追加\r\n con:write("JOIN :" .. channel .. "\r\n") elseif linesplt[1] == "PING" or linesplt[1] == "PING " then print("PING") png = split(line,"[^:]+") con:write("PONG :"..png[#png].."\r\n") --对ping作出响应,以保证不会断联 elseif string.find(linesplt[1], "PRIVMSG #") ~= nil then msgfrom = split(linesplt[1],"[^ ]+") msgfrom = msgfrom[3] con:write("PRIVMSG "..msgfrom.." :"..linesplt[2].."\r\n") end end else print("Connection failed.") end