PtokaX forum

Archive => Archived 5.0 boards => Request for scripts => Topic started by: sander815 on 19 April, 2005, 09:00:38

Title: recordbot in lua5
Post by: sander815 on 19 April, 2005, 09:00:38
can some1 convert this script to lua5?

--
-- [RecordBot] - v0.952
--
-- Simple bot which saves your hub's all-time share & user record
-- Written by bonki 2003
--
-- ABOUT:
--   It's first version was my first attempt of a script - written only to get in touch with the LUA language.
--   I know it's neither special nor very complex, I hope it's useful for someone, though. :)
--
--   If you downloaded this script as a packed archive there already exist a template
--   and a config file (in txt/), you can delete both if you don't need them.
--   Read the following if you want to know more about those files.
--
-- THANKS:
--   http://lua.bcs-solutions.de :)
--
-- USED FILES (don't forget to create the "scripts/txt" folder!!!):
--   Syntax:
--      options=value
--
--   o) txt/RecordBot.records.txt: File the bot saves the records to.
--                                 Don't create it yourself!
--
--      Options:
--        share = share record in bytes
--        users = user  record
--
--   o) txt/RecordBot.template.txt (optional): Template file where you can change the bot's messages.
--
--      Options:
--        *requestshare: used when user displays records - share record     (3)
--        *requestusers: used when user displays records - user  record     (2)
--        *newsharemain: main chat message               - new share record (1,3)
--        *newusersmain: main chat message               - new user  record (1,2)
--        *newsharepm:   PM chat message                 - new share record (1,3)
--        *newuserspm:   PM chat message                 - new user  record (1,2)
--
--      *Available variables/substitutions:
--        (1): [nick]  will be replaced with the user's nick
--        (2): [users] -||-                      new user record
--        (3): [bb]    -||-                      new share record (in B)
--             [mb.x]  -||-                      new share record (in MB, x digits after the decimal point)
--             [gb.x]  -||-                      new share record (in GB, x digits after the decimal point)
--             [tb.x]  -||-                      new share record (in TB, x digits after the decimal point)
--             [pb.x]  -||-                      new share record (in PB, x digits after the decimal point)
--
--      Example file:
--
--        requestshare=Our share record is: [tb.2]
--        requestusers=Our user record: [users]
--        newsharemain=[nick] has just raised the all-time share record to: [tb.3] :)
--        newusersmain=[nick] has just raised the all-time user record to: [users] users :)
--        newsharepm=Thanks, [nick]. You've just raised the all-time share record!
--        newuserspm=Thanks, [nick]. Yous've just raised the all-time user record!
--
--   o) txt/RecordBot.config.txt (optional): Config file
--
--      Options:
--        showmain: 0 enables the bot's main messages (the ones when a new record arises)
--                  1 disables the same
--        showpm:   same as above for the private messages
--
-- AVAILABLE COMMANDS:
--   !rb.help:                       Displays a help message.
--   !rb.show:                       Displays both records
--   !rb.showmain  : Enables/disables the bot's main messages when a new record arises
--   !rb.showpm    : Same as above with private messages
--   !rb.showlogin : Enables/disables the login report of both records
--                                   (messages looks exactly like as if requested by !rb.show)
--   !rb.reset:                      Sets both records back to 0
--
-- TODO:
--   Any suggestions? :)
--
-- BUGS:
--   It also counts bots.
--   Tell me!
--
-- CHANGELOG:
--   v0.952: Added:     Added a nice-looking tag to the bot showing both records
--           Optimized: Did some small code beautifications
--   v0.951: General:   Changed filenames, they will now always be Recordbot.*.* independent from the botname
--   v0.95:  Added:     Possibility to enable/disable the login report of both records
--           Optimized: Code partly rewritten/beautified/optimized, looks much better now :-)
--           Bugfix:    Removed bot's share because it was added to the total share
--                      DC++ displays which causes the displayed amount to be doubled...
--   v0.941: General:   Minor changes
--   v0.94:  Bugfix:    Fixed bot's share bug, now also works after records resetting
--           Added:     Possibility to enable/disable the bot's main/PM messages
--           General:   CHANGED FILE NAMES! (RecordBot.config.txt is now RecordBot.template.txt
--                      (sorry for the confusion, have a look at USED FILES for further details))
--   v0.93:  Optimized: Template file parsing code rewritten, have a look at USED FILES what has changed!
--   v0.921: Bugfix:    Fixed some minor bugs
--           Bugfix:    Fixed bot's share bug, now also works at startup
--   v0.92:  Bugfix:    Fixed some minor bugs
--           Added:     Bot's share = share record
--           Optimized: Template file is now optional
--           Added:     Improved template file support
--   v0.91:  General:   No info
--   v0.9:   General:   Initial release
--

sVer   = "0.952";
sName  = "--reCordboT--";
sAbout = "Logs and displays a hub's all time share and user record.";

sRecordFile   = "txt/RecordBot.records.txt";
sConfigFile   = "txt/RecordBot.config.txt";
sTemplateFile = "txt/RecordBot.template.txt";

sShowMain    = "1";
sShowPM      = "1";
sShowOnLogin = "1";

iShareCurrent = nil;
iUserCurrent  = nil;
iUserAllTime  = 0;
iShareAllTime = 0;

sNewShareResponse         = nil;
sNewUsersResponse         = nil;
sNewSharePMResponse       = nil;
sNewUsersPMResponse       = nil;
sShowRecordsShareResponse = nil;
sShowRecordsUsersResponse = nil;

sCrLf = "\r\n";

function Main()
  bot = { sBotName  = sName,
          sBotVer   = sVer,
          sBotEmail = "bonki@no-spam.net",
          sBotSpeed = "Sol",
          sBotDescr = "RecordBot v"..sVer.." logs our user/share records, type !rb.show to see them",
          sBotTag   = "<++ V:"..sVer..",P:bonki,S:[gb.0],U:[users]>",
          iBotShare = 0 };

  ReadConfig();
  ReadRecords();

  UpdateBotInfo();

  PrepareCommands();

  frmHub:RegBot(bot.sBotName);
end

function PrepareCommands()
  sOpHelpOutput = sCrLf..sCrLf..bot.sBotDescr..
                         sCrLf..sAbout..
                  sCrLf..sCrLf.."OP Commands:"..sCrLf..sCrLf;

  sHelpOutput   = sCrLf.."Commands:"..sCrLf..sCrLf;

  tCommands = {
                [ "!rb.help" ]      = { CmdShowHelp,       0, "\t\t\tDisplays this help message."                      },
                [ "!rb.show" ]      = { CmdShowRecords,    0, "\t\t\tShows this hub's all time share and user record." },
                [ "!rb.showmain" ]  = { CmdSetShowMain,    1, "\tmain message."                        },
                [ "!rb.showpm" ]    = { CmdSetShowPM,      1, "\tprivate message."                     },
                [ "!rb.showlogin" ] = { CmdSetShowOnLogin, 1, "\treport on login."                     },
                [ "!rb.reset" ]     = { CmdResetRecords,   1, "\t\t\tResets all records."                              },
              };

  for sCmd, tCmd in tCommands do
    if(tCmd[2] == 1) then
      sOpHelpOutput = sOpHelpOutput..sCmd.."\t "..tCmd[3]..sCrLf;
    else
      sHelpOutput   = sHelpOutput..sCmd.."\t "..tCmd[3]..sCrLf;
    end
  end
end

function CmdShowHelp(curUser)
  curUser:SendData(bot.sBotName, sOpHelpOutput..sHelpOutput);
end

function CmdShowRecords(curUser)
  ReadTemplate(curUser);
  curUser:SendData(bot.sBotName, sShowRecordsShareResponse);
  curUser:SendData(bot.sBotName, sShowRecordsUsersResponse);
end

function CmdSetShowMain(curUser, args)
  if (args == "enable") then
    sShowMain = "1";
    WriteConfig();
    curUser:SendData(bot.sBotName, "Enabled!");
  elseif (args == "disable") then
    sShowMain = "0";
    WriteConfig();
    curUser:SendData(bot.sBotName, "Disabled!");
  else
    curUser:SendData(bot.sBotName, "Syntax error!");
  end
end

function CmdSetShowPM(curUser, args)
  if (args == "enable") then
    sShowPM = "1";
    WriteConfig();
    curUser:SendData(bot.sBotName, "Enabled!");
  elseif (args == "disable") then
    sShowPM = "0";
    WriteConfig();
    curUser:SendData(bot.sBotName, "Disabled!");
  else
    curUser:SendData(bot.sBotName, "Syntax error!");
  end
end

function CmdSetShowOnLogin(curUser, args)
  if (args == "enable") then
    sShowOnLogin = "1";
    WriteConfig();
    curUser:SendData(bot.sBotName, "Enabled!");
  elseif (args == "disable") then
    sShowOnLogin = "0";
    WriteConfig();
    curUser:SendData(bot.sBotName, "Disabled!");
  else
    curUser:SendData(bot.sBotName, "Syntax error!");
  end
end

function CmdResetRecords(curUser)
  iShareAllTime = 0;
  iUserAllTime  = 0;
  WriteNewRecord(iShareAllTime, iUserAllTime);
  SendToAll(bot.sBotName, "Hub records reset!");
end

function DataArrival(curUser, sData)
  local _, _, cmd, args = strfind(sData, "%b<>%s+(%S+)%s*([^%|]*)%|$");

  if(cmd == nil) then
    return 0; end

  cmd = strlower(cmd);
  if (tCommands[cmd]) then
    curUser:SendData(sData);
    if (tCommands[cmd][2] == 0 or curUser.bOperator) then
      tCommands[cmd][1](curUser, strlower(args));
    else
      curUser:SendData(bot.sBotName, "You do not have sufficient rights to run that command!");
    end
    return 1;
  end

  return 0;
end

function CheckForRecords(curUser)
  iShareCurrent = frmHub:GetCurrentShareAmount();
  iUserCurrent  = frmHub:GetUsersCount();

  if (iShareCurrent > iShareAllTime) then
    iShareAllTime = iShareCurrent;
    WriteNewRecord(iShareAllTime, iUserCurrent);
    ReadTemplate(curUser);

    UpdateBotInfo();
    SendBotInfo();

    if (sShowPM == "1") then
      SendPmToNick(curUser.sName, bot.sBotName, sNewSharePMResponse); end;
    if (sShowMain == "1") then
      SendToAll(bot.sBotName, sNewShareResponse); end;
  end

Title:
Post by: sander815 on 19 April, 2005, 09:01:40
part 2


  if (iUserCurrent > iUserAllTime) then
    iUserAllTime = iUserCurrent;
    WriteNewRecord(iShareCurrent, iUserAllTime);
    ReadTemplate(curUser);

    UpdateBotInfo();
    SendBotInfo();

    if (sShowPM == "1") then
      SendPmToNick(curUser.sName, sNewUsersPMResponse); end;
    if (sShowMain == "1") then
      SendToAll(bot.sBotName, sNewUsersResponse); end;
  end
end

function NewUserConnected(curUser)
  SendBotInfo(curUser);

  CheckForRecords(curUser);

  if (sShowOnLogin == "1") then
    CmdShowRecords(curUser); end;
end

function OpConnected(curUser)
  SendBotInfo(curUser);

  CheckForRecords(curUser);

  if (sShowOnLogin == "1") then
    CmdShowRecords(curUser); end;
end

function FormatSize(sString, iVal)
  assert(type(sString) == "string", "FormatSize(): bad argument #1 (string expected)");
  assert(type(iVal)    == "number", "FormatSize(): bad argument #2 (numerical value expected)");

  local _, _, sFormat = strfind(sString, "%[[mgtp]b(%.%d+)%]");

  if (sFormat) then
    sFormat = "%"..sFormat.."f";
    sString = gsub(sString, "%[mb.*%]", format(sFormat, toMB(iVal)).." MB");
    sString = gsub(sString, "%[gb.*%]", format(sFormat, toGB(iVal)).." GB");
    sString = gsub(sString, "%[tb.*%]", format(sFormat, toTB(iVal)).." TB");
    sString = gsub(sString, "%[pb.*%]", format(sFormat, toPB(iVal)).." PB");
  else
    sString = gsub(sString, "%[bb%]",   iVal.." B");
  end

  return sString;
end

function WriteConfig()
  local flConfigFile = openfile(sConfigFile, "w+");

  if (flConfigFile) then
    write(flConfigFile, "showmain="..sShowMain.."\r\n");
    write(flConfigFile, "showpm="..sShowPM.."\r\n");
    write(flConfigFile, "showonlogin="..sShowOnLogin);

    closefile(flConfigFile);
  end
end

function ReadConfig()
  local flConfigFile = openfile(sConfigFile, "r");

  if (flConfigFile) then
    local line = read(flConfigFile);
    while(line) do
      if     (strfind(line, "^showmain=[01]$")) then    _, _, sShowMain    = strfind(line, "^showmain=([01])$");
      elseif (strfind(line, "^showpm=[01]$")) then      _, _, sShowPM      = strfind(line, "^showpm=([01])$");
      elseif (strfind(line, "^showonlogin=[01]$")) then _, _, sShowOnLogin = strfind(line, "^showonlogin=([01])$");
      end

      line = read(flConfigFile);
    end
    closefile(flConfigFile);
  end
end

function ReadTemplate(curUser)
  local flTemplateFile = openfile(sTemplateFile, "r");

  if (flTemplateFile) then
    local sShareFormat = nil;

    local line = read(flTemplateFile);
    while(line) do
      if (strfind(line, "^requestshare=.*$")) then
        _, _, sShowRecordsShareResponse = strfind(line, "^requestshare=(.*)$");
              sShowRecordsShareResponse = FormatSize(sShowRecordsShareResponse, iShareAllTime);

      elseif (strfind(line, "^requestusers=.*$")) then
        _, _, sShowRecordsUsersResponse = strfind(line, "^requestusers=(.*)$");
              sShowRecordsUsersResponse = gsub(sShowRecordsUsersResponse, "%[users%]", tostring(iUserAllTime));

      elseif (strfind(line, "^newsharemain=.*$")) then
        _, _, sNewShareResponse = strfind(line, "^newsharemain=(.*)$");
              sNewShareResponse   = gsub(sNewShareResponse, "%[nick%]", curUser.sName);
              sNewShareResponse   = FormatSize(sNewShareResponse, iShareAllTime);

      elseif (strfind(line, "^newusersmain=.*$")) then
        _, _, sNewUsersResponse = strfind(line, "^newusersmain=(.*)$");
              sNewUsersResponse   = gsub(sNewUsersResponse, "%[nick%]", curUser.sName);
              sNewUsersResponse   = gsub(sNewUsersResponse, "%[users%]", tostring(iUserAllTime));

      elseif (strfind(line, "^newsharepm=.*$")) then
        _, _, sNewSharePMResponse = strfind(line, "^newsharepm=(.*)$");
              sNewSharePMResponse = gsub(sNewSharePMResponse, "%[nick%]", curUser.sName);
              sNewSharePMResponse = FormatSize(sNewSharePMResponse, iShareAllTime);

      elseif (strfind(line, "^newuserspm=.*$")) then
        _, _, sNewUsersPMResponse = strfind(line, "^newuserspm=(.*)$");
              sNewUsersPMResponse = gsub(sNewUsersPMResponse, "%[nick%]", curUser.sName);
              sNewUsersPMResponse = gsub(sNewUsersPMResponse, "%[users%]", tostring(iUserAllTime));
      end

      line = read(flTemplateFile);
    end

    closefile(flTemplateFile);
  end

  if (not sShowRecordsShareResponse) then sShowRecordsShareResponse = "Share record: "..format("%.3f", toTB(iShareAllTime)).." TB"; end;
  if (not sShowRecordsUsersResponse) then sShowRecordsUsersResponse = "User record: "..iUserAllTime.." users"; end;
  if (not sNewShareResponse)         then sNewShareResponse         = curUser.sName.." has just raised the all-time share record to: "..format("%.3f", toTB(iShareAllTime)).." TB :)"; end;
  if (not sNewUsersResponse)         then sNewUsersResponse         = curUser.sName.." has just raised the all-time user record to: "..iUserAllTime.." users :)"; end;
  if (not sNewSharePMResponse)       then sNewSharePMResponse       = "Thanks, buddie. You've just raised the all-time share record!"; end;
  if (not sNewUsersPMResponse)       then sNewUsersPMResponse       = "Thanks, buddie. You've just raised the all-time user record!"; end;
end

function ReadRecords()
  local flRecordFile = openfile(sRecordFile, "r");

  if (flRecordFile) then
    _, _, iShareAllTime = strfind(read(flRecordFile), "^share=(%d+)$"); iShareAllTime = tonumber(iShareAllTime);
    _, _, iUserAllTime  = strfind(read(flRecordFile), "^users=(%d+)$"); iUserAllTime  = tonumber(iUserAllTime);

    closefile(flRecordFile);
  else
    iShareAllTime = 0;
    iUserAllTime  = 0;
  end
end

function WriteNewRecord(shareRecord, userRecord)
  local flRecordFile = openfile(sRecordFile, "w+");

  if (flRecordFile) then
    write(flRecordFile, "share="..shareRecord.."\r\n");
    write(flRecordFile, "users="..userRecord);

    closefile(flRecordFile);
  end
end

function UpdateBotInfo()
  local sBotTag = gsub(bot.sBotTag, "%[users%]", iUserAllTime);
  sBotTag = FormatSize(sBotTag, iShareAllTime);
  sBotTag = gsub(sBotTag, " (.B,)", "%1");
  sBotInfo = "$MyINFO $ALL "..bot.sBotName.." "..bot.sBotDescr..sBotTag.."$ $"..bot.sBotSpeed..strchar(1).."$"..bot.sBotEmail.."$"..bot.iBotShare.."$" ;
end

function SendBotInfo(curUser)
  if (curUser) then
    curUser:SendData(sBotInfo);
  else
    SendToAll(sBotInfo);
  end
end

function toMB(bytes)
  return (bytes / 1024 / 1024);
end

function toGB(bytes)
  return (bytes / 1024 / 1024 / 1024);
end

function toTB(bytes)
  return (bytes / 1024 / 1024 / 1024 / 1024);
end

function toPB(bytes)
  return (bytes / 1024 / 1024 / 1024 / 1024 / 1024);
end
Title:
Post by: jiten on 10 May, 2005, 15:40:14
Check this (http://board.univ-angers.fr/thread.php?threadid=4425&boardid=26&styleid=1&sid=6000fb61b119696ed0dceb9620a54867&page=1#1) thread.

Cheers
Title: ?
Post by: (-=TrIp-iN-SuN=-) on 10 May, 2005, 16:18:20
you don't finsh convert this
Title:
Post by: jiten on 10 May, 2005, 16:21:27
QuoteOriginally posted by (-=TrIp-iN-SuN=-)
you don't finsh convert this
Erm, have you checked that link? Guess not... :D

Cheers