[request] RecordBot convert Lua 4 to Lua 5
 

News:

29 December 2022 - PtokaX 0.5.3.0 (20th anniversary edition) released...
11 April 2017 - PtokaX 0.5.2.2 released...
8 April 2015 Anti child and anti pedo pr0n scripts are not allowed anymore on this board!
28 September 2015 - PtokaX 0.5.2.1 for Windows 10 IoT released...
3 September 2015 - PtokaX 0.5.2.1 released...
16 August 2015 - PtokaX 0.5.2.0 released...
1 August 2015 - Crowdfunding for ADC protocol support in PtokaX ended. Clearly nobody want ADC support...
30 June 2015 - PtokaX 0.5.1.0 released...
30 April 2015 Crowdfunding for ADC protocol support in PtokaX
26 April 2015 New support hub!
20 February 2015 - PtokaX 0.5.0.3 released...
13 April 2014 - PtokaX 0.5.0.2 released...
23 March 2014 - PtokaX testing version 0.5.0.1 build 454 is available.
04 March 2014 - PtokaX.org sites were temporary down because of DDOS attacks and issues with hosting service provider.

Main Menu

[request] RecordBot convert Lua 4 to Lua 5

Started by Psycho_Chihuahua, 08 May, 2005, 15:50:27

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Psycho_Chihuahua

Could someone be so kind to convert this script to Lua 5 please?
sVer   = "KryFinal";
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.." written by bonki 2003",
          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

  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
PtokaxWiki ?PtokaX Mirror + latest Libs

01100001011011000111001101101111001000000110101101101110011011110111011101101110001000000110000101110011001000000101010001101111011010110110111101101100011011110111001101101000

jiten

Check this thread.

Cheers

Psycho_Chihuahua

PtokaxWiki ?PtokaX Mirror + latest Libs

01100001011011000111001101101111001000000110101101101110011011110111011101101110001000000110000101110011001000000101010001101111011010110110111101101100011011110111001101101000

jiten

yw :]
Btw, I didn't test it much so, I'll need to know if there are any bugs...

Cheers

SMF spam blocked by CleanTalk