Part of Slepp's ProjectsPastebinTURLImagebinFilebin
Feedback -- English French German Japanese
Create Upload Newest Tools Donate
Sign In | Create Account

Advertising

Paste Description for Someone

Jabber bot

Someone
Wednesday, November 29th, 2006 at 3:46:41pm MST 

  1. BotName = "John";
  2. BotJID = "mattj@jabber.org";
  3. BotPass = "********";
  4.  
  5. activejids = {};
  6.  
  7. function onMessage(msg)
  8.         command, param = msg.body:match("^!(%a*)%s*(.*)");
  9.        
  10.         if (string.find(msg.body:upper(), BotName:upper())) ~= nil and GetResource(msg.fromFull) ~= BotName or msg.type == "chat" or string.find(msg.body:upper(),"HI ALL") then
  11.                 print("Refreshed active status of "..msg.fromFull.."\n");
  12.                 activejids[msg.fromFull] = os.time();
  13.         end
  14.         --print(msg.fromFull.." wrote: "..msg.body.."\n");
  15.         if command == "time" then
  16.                 return Reply(msg, "The time (in the UK) is "..os.date());
  17.         elseif command == "Jabber" then
  18.                 return Reply(msg, "http://getjabber.no-ip.info/");
  19.         elseif command == "slap" then
  20.                 if (string.match(param:upper(),".*%s*M[%p%s]*A[%p%s]*T[%p%s]*T[%p%s]*.*")) == nil then
  21.                         return Reply(msg, "/me slaps "..param.." for "..GetResource(msg.fromFull));
  22.                 end
  23.                 return Reply(msg, "No way I'm touching him!");
  24.         elseif command == "ping" then
  25.                 return Reply(msg, "pong");
  26.         elseif command == "xep" then
  27.                 return Reply(msg, GetXEPInfo(param));
  28.         elseif command == "join" and GetResource(msg.fromFull) == "MattJ" then
  29.                 JoinMUC(param);
  30.         elseif command == "reload" then
  31.                 dofile("bot.lua");
  32.                 Reply(msg, "Reloaded ok");
  33.         elseif command == "run" then
  34.                 return Reply(msg, "/me runs");
  35.         end
  36.        
  37.         if os.difftime(os.time(),activejids[msg.fromFull]) < 120 then
  38.                 Sleep(math.random(3, 6));
  39.                 activejids[msg.fromFull] = os.time();
  40.                 return MatchPattern(msg);
  41.         else
  42.                 print(msg.fromFull.." is not an active JID\n");
  43.                 return nil;
  44.         end
  45. end
  46.  
  47. function Reply(msg, text)
  48.                 reply = {};
  49.                 reply.body=text;
  50.                 if msg.type == MSG_GROUPCHAT then
  51.                         print("Replying to conference\n");
  52.                         reply.to = msg.fromBare; -- Reply to conference, not the participant
  53.                         reply.type = "groupchat";
  54.                 else
  55.                         print("Replying to individual\n");
  56.                         reply.to = msg.fromFull;
  57.                         reply.type = "chat"; -- Standard message
  58.                 end
  59.                 return reply;
  60. end
  61.  
  62. function GetResource(jid)
  63.         return jid:match("^.*/(.*)$");
  64. end
  65.  
  66. function GetXEPInfo(xep)
  67.         xep_num = tostring(tonumber(xep));
  68.         xep_num = string.rep("0", 4-xep_num:len())..xep_num;
  69.         if xep_num == "0000" or tonumber(xep_num) == nil or tonumber(xep_num) > 200 then
  70.                 return "Sorry, the XEP number is invalid. Try again..."; end
  71.         i = 0;
  72.         for line in io.lines("xeps2.txt") do
  73.                 i = i +1;
  74.                 if i == tonumber(xep_num) then
  75.                         XEP, Title, Type, Status, Date = string.match(line, "^(%d*)%s*,%s*(.*)%s*,%s*(.*)%s*,%s*(.*)%s*,%s*(.*)");
  76.                         if XEP == nil then return "Unable to grab XEP info"; end
  77.                         return "XEP-"..XEP.."\nName: "..Title.."\nType: "..Type.."\nStatus: "..Status.."\nDate: "..Date.."\nLink: http://xmpp.org/extensions/xep-"..xep_num..".html";
  78.                 end
  79.         end
  80.         return "Unable to find the XEP you specified, sorry."
  81. end
  82.  
  83. function onConnect()
  84.         print("JoinMUC is a "..type(JoinMUC));
  85.         print("Attempting to join...\n");
  86.         JoinMUC("test@conference.jabber.org/"..BotName);
  87. end
  88.  
  89. function MatchPattern(msg)
  90.         print("Fallen to pattern matcher...\n");
  91.         msgin = msg.body:match("^"..BotName.."[:,]*%s*(.*)%s*$") or msg.body; -- Strip prefixed nick, if any
  92.         print("Looking for matches for '"..msgin.."'\n");
  93.         for line in io.lines("ai.txt") do
  94.                 --print("Testing: "..line.."\n");
  95.                 patterns, actions = line:match("^(.*])({.*)$");
  96.                 if patterns == nil then print(line.." is an invalid line, it seems\n"); end
  97.                 --print("Patterns: "..patterns.."\n");
  98.                 for pattern in patterns:gmatch("%b[]") do
  99.                                 if (msgin:lower():match(pattern:sub(2,-2):lower())) then
  100.                                         print("Match for '"..msgin.."' and '"..pattern:sub(2,-2).."'\n");
  101.                                         return Reply(msg, ChooseAction(actions));
  102.                                 else
  103.                                         -- print(msgin.." does not match "..pattern:sub(2,-2).."\n");
  104.                                 end
  105.                 end
  106.                 --print("No match on line: "..line.."\n");
  107.         end
  108.         print("No match in file\n");
  109.         AddToBotLog(msg);
  110. end
  111.  
  112. function ChooseAction(actionlist)
  113.         actions = {};
  114.         for action in actionlist:gmatch("%b{}") do
  115.                 table.insert(actions, action);
  116.         end
  117.         return string.sub(actions[math.random(1,#actions)],2,-2);
  118. end
  119.  
  120. function AddToBotLog(m)
  121.         log = io.open("log.txt","a+");
  122.         log:write("From: "..m.fromFull.." Body: "..m.body.."\n");
  123.         log:close();
  124. end

advertising

Update the Post

Either update this post and resubmit it with changes, or make a new post.

You may also comment on this post.

update paste below
details of the post (optional)

Note: Only the paste content is required, though the following information can be useful to others.

Save name / title?

(space separated, optional)



Please note that information posted here will expire by default in one month. If you do not want it to expire, please set the expiry time above. If it is set to expire, web search engines will not be allowed to index it prior to it expiring. Items that are not marked to expire will be indexable by search engines. Be careful with your passwords. All illegal activities will be reported and any information will be handed over to the authorities, so be good.

worth-right