recordbot in lua5
 

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

recordbot in lua5

Started by sander815, 19 April, 2005, 09:00:38

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

sander815

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 someonethough. :)
--
--   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
--
--   
otxt/RecordBot.records.txtFile 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:
--        *
requestshareused when user displays records share record     (3)
--        *
requestusersused when user displays records user  record     (2)
--        *
newsharemainmain chat message               - new share record (1,3)
--        *
newusersmainmain 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  <enable|disable>: Enables/disables the bot's main messages when a new record arises
--   !rb.showpm    <enable|disable>: Same as above with private messages
--   !rb.showlogin <enable|disable>: 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.941General:   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 confusionhave a look at USED FILES for further details))
--   
v0.93:  OptimizedTemplate file parsing code rewrittenhave a look at USED FILES what has changed!
--   
v0.921Bugfix:    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
--           OptimizedTemplate 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 };

  
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"<enable/disable>\tmain message."                        },
                [ 
"!rb.showpm" ]    = { CmdSetShowPM,      1"<enable/disable>\tprivate message."                     },
                [ 
"!rb.showlogin" ] = { CmdSetShowOnLogin1"<enable/disable>\treport on login."                     },
                [ 
"!rb.reset" ]     = { CmdResetRecords,   1"\t\t\tResets all records."                              },
              };

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

function CmdShowHelp(curUser)
  
curUser:SendData(bot.sBotNamesOpHelpOutput..sHelpOutput);
end

function CmdShowRecords(curUser)
  
ReadTemplate(curUser);
  
curUser:SendData(bot.sBotNamesShowRecordsShareResponse);
  
curUser:SendData(bot.sBotNamesShowRecordsUsersResponse);
end

function CmdSetShowMain(curUserargs)
  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(curUserargs)
  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(curUserargs)
  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(iShareAllTimeiUserAllTime);
  
SendToAll(bot.sBotName"Hub records reset!");
end

function DataArrival(curUsersData)
  
local __cmdargs strfind(sData"%b<>%s+(%S+)%s*([^%|]*)%|$");

  if(
cmd == nilthen
    
return 0end

  cmd 
strlower(cmd);
  if (
tCommands[cmd]) then
    curUser
:SendData(sData);
    if (
tCommands[cmd][2] == or curUser.bOperatorthen
      tCommands
[cmd][1](curUserstrlower(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 iShareAllTimethen
    iShareAllTime 
iShareCurrent;
    
WriteNewRecord(iShareAllTimeiUserCurrent);
    
ReadTemplate(curUser);

    
UpdateBotInfo();
    
SendBotInfo();

    if (
sShowPM == "1"then
      SendPmToNick
(curUser.sNamebot.sBotNamesNewSharePMResponse); end;
    if (
sShowMain == "1"then
      SendToAll
(bot.sBotNamesNewShareResponse); end;
  
end


sander815

part 2


  
if (iUserCurrent iUserAllTimethen
    iUserAllTime 
iUserCurrent;
    
WriteNewRecord(iShareCurrentiUserAllTime);
    
ReadTemplate(curUser);

    
UpdateBotInfo();
    
SendBotInfo();

    if (
sShowPM == "1"then
      SendPmToNick
(curUser.sNamesNewUsersPMResponse); end;
    if (
sShowMain == "1"then
      SendToAll
(bot.sBotNamesNewUsersResponse); 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(sStringiVal)
  
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 (
sFormatthen
    sFormat 
"%"..sFormat.."f";
    
sString gsub(sString"%[mb.*%]"format(sFormattoMB(iVal)).." MB");
    
sString gsub(sString"%[gb.*%]"format(sFormattoGB(iVal)).." GB");
    
sString gsub(sString"%[tb.*%]"format(sFormattoTB(iVal)).." TB");
    
sString gsub(sString"%[pb.*%]"format(sFormattoPB(iVal)).." PB");
  else
    
sString gsub(sString"%[bb%]",   iVal.." B");
  
end

  
return sString;
end

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

  if (
flConfigFilethen
    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 (
flConfigFilethen
    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 (
flTemplateFilethen
    local sShareFormat 
nil;

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

      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(sNewShareResponseiShareAllTime);

      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(sNewSharePMResponseiShareAllTime);

      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 sShowRecordsShareResponsethen sShowRecordsShareResponse "Share record: "..format("%.3f"toTB(iShareAllTime)).." TB"end;
  if (
not sShowRecordsUsersResponsethen 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 (
flRecordFilethen
    _
_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(shareRecorduserRecord)
  
local flRecordFile openfile(sRecordFile"w+");

  if (
flRecordFilethen
    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(sBotTagiShareAllTime);
  
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 (
curUserthen
    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

jiten

Check this thread.

Cheers

(-=TrIp-iN-SuN=-)

you don't finsh convert this

jiten

QuoteOriginally posted by (-=TrIp-iN-SuN=-)
you don't finsh convert this
Erm, have you checked that link? Guess not... :D

Cheers

SMF spam blocked by CleanTalk