--[[ "etc_offlineusers.lua" by Motnahp - on parameter 'show' it posts you a table with all users who have been offline for a set time periode - on parameter 'addexception' you can add an user to exceptions, this means he can't be deleted - on parameter 'removeexception' you can delete a users ecxeption - on parameter 'showexceptions' you can see all protected ( users in exceptions) users - on parameter 'delete' you can delete all users with the numbers you enterd , example: '+offline delete 13 24 27' deletes user 13, 24 and 27 of the given table - on parameter 'showdeleted' it posts you a table with all deleted users - on parameter 'autoclean' it deletes all users who are longer offline as allowed ( depends on t_settings ) - on parameter 'showsettings' it shows you all settings - on parameter 'set' and additional parameter 'toggleautoclean' to enable periodic auto clean 'maxlvl' you may set the maximum level to check for methode getofflineusers( ) 'days' you may set the maximum days to be apart for methode getofflineusers( ) on parameter 'help' it shows an overview of all parameters Go and edit t_settings.tbl for autoclean!! only Users who have at least been online once will be deleted v0.4.3 - cleanup v0.4.2 - added parameter autoclean v0.4.1 - added timer for shedule autoclean ( disabled on default ) - added import of opchat to feed deleted users - added parameter showsettings - added parameterset to set settings - added parameter help - changed methode MinutesToTime( ) to SecondsToTime( ) // changed something before and it caused incompatibility -.- v0.3 - fixed ucmd v0.2 - fixed methode getofflineusers( ) v0.1 - adds command offline with parameters show, addexception, removeexception, showexception, delete and showdeleted - adds help - adds ucmd - adds language support ]]-- --[[ Settings ]]-- -- nicht Editieren -- do not edit -- local scriptname = "etc_offlineusers" local scriptversion = "0.4.3" local hub_bot = hub.getbot() -- cmd -- local cmd = "offline" -- parameters -- local prm1 = "show" local prm1_1 = "check" local prm2 = "addexception" local prm3 = "removeexception" local prm4 = "showexceptions" local prm5 = "delete" local prm6 = "showdeleted" local prm7 = "autoclean" local prm8 = "showsettings" local prm9 = "set" local prm10 = "help" local prm9_1 = "maxlvl" local prm9_2 = "days" local prm9_3 = "toggleautoclean" -- includes // renames -- local opchat = hub.import "bot_opchat" local hubcmd local os_time = os.time local os_difftime = os.difftime local utf_match = utf.match local utf_format = utf.format local table_remove = table.remove local util_savearray = util.savearray local util_savetable =util.savetable local hub_getuser = hub.getuser local hub_getusers = hub.getusers local hub_getregusers = hub.getregusers local hub_isnickonline = hub.isnickonline local hub_bot = hub.getbot( ) local start = os_time( ) --local tabel and storage path -- local exceptions_path = "scripts/etc_offlineusers/t_exceptions.tbl" local t_exceptions = util.loadtable( exceptions_path ) or { } local settings_path = "scripts/etc_offlineusers/t_settings.tbl" local t_settings = util.loadtable( settings_path ) or { } local backup_path = "scripts/etc_offlineusers/t_backup.tbl" local t_backup = util.loadtable( backup_path ) or { } -- load the left ones local lastfound = { } -- load lang file local scriptlang = cfg.get "language" local lang, err = cfg.loadlanguage( scriptlang, scriptname ); lang = lang or { }; err = err and hub.debug( err ) -- functions -- local getofflineusers local showexcluded local showdeleted local addexception local removeexception local deleteuser local getusertbl local filterusertbl local autocleanusers -- implementieren local SecondsToTime local changesettings local showsettings local returnhelp local day = 60 * 60 * 24 -->> nachfolgende Settings sind editierbar -->> you may edit the following settings -->> -- permissions -- local min_level_owner = 100 -- to use all commands local min_level = 60 -- according permissions, used to send ucmd and hub help local permission = { -- who is allowed to use "show", "check", "add/remove/show exceptions", "show deleted" [ 0 ] = false, -- unreg [ 10 ] = false, -- guest [ 20 ] = false, -- reg [ 30 ] = false, -- vip [ 40 ] = false, -- svip [ 50 ] = false, -- server [ 60 ] = false, -- operator [ 70 ] = false, -- supervisor [ 80 ] = true, -- admin [ 90 ] = false, -- emty [ 100 ] = true, -- hubowner } -- ucmd_menu -- local ucmd_menu_addexception1 = lang.ucmd_menu_addexception1 or { "Hub", "Offline Users", "exclude User" } local ucmd_menu_addexception2 = lang.ucmd_menu_addexception2 or { "Hub", "Offline Users", "exclude this User" } local ucmd_menu_removeexception1 = lang.ucmd_menu_removeexception1 or { "Hub", "Offline Users", "include User" } local ucmd_menu_removeexception2 = lang.ucmd_menu_removeexception2 or { "Hub", "Offline Users", "include this User" } local ucmd_menu_showexception = lang.ucmd_menu_showexception or { "Hub", "Offline Users", "show excluded Users" } local ucmd_menu_show = lang.ucmd_menu_show or { "Hub", "Offline Users", "Manual clean", "show offline Users" } local ucmd_menu_delete = lang.ucmd_menu_delete or { "Hub", "Offline Users", "Manual clean", "delete Users" } local ucmd_menu_showdeleted = lang.ucmd_menu_showdeleted or { "Hub", "Offline Users", "show deleted Users" } local ucmd_menu_auto_check = lang.ucmd_menu_auto_check or { "Hub", "Offline Users", "Automatic clean", "check" } local ucmd_menu_auto_clean = lang.ucmd_menu_auto_clean or { "Hub", "Offline Users", "Automatic clean", "clean" } local ucmd_menu_showsettings = lang.ucmd_menu_showsettings or { "Hub", "Offline Users", "show settings" } local ucmd_menu_help = lang.ucmd_menu_help or { "Hub", "Offline Users", "help" } --settings-- local ucmd_menu_set_check_below = lang.ucmd_menu_set_check_below or { "Hub", "Offline Users", "Settings - manual clean", "set max level to check" } local ucmd_menu_set_max_offlinedays_manual = lang.ucmd_menu_set_max_offlinedays_manual or { "Hub", "Offline Users", "Settings - manual clean", "set max offline days " } local ucmd_menu_set_toggle = lang.ucmd_menu_set_toggle or { "Hub", "Offline Users", "Settings - automatic clean", "toggle full automatic" } --msgs- local ucmd_level = lang.ucmd_level or "Enter level: " local ucmd_days = lang.ucmd_days or "Enter days: " local ucmd_numbers = lang.ucmd_numbers or "Enter numbers of users to delete. seperate with whitespace ' '" local ucmd_who = lang.ucmd_who or "Enter user nick: " local ucmd_why = lang.ucmd_why or "Reason: " -- help -- local help_title = lang.help_title or "Offline Users" local help_usage = lang.help_usage or "[+!#]offline [show|addexception|removeexceptions|showexceptions|delete [a, b, c, ...] | showdeleted | autoclean | showsettings | set [ toggleautoclean | maxlvl | days ] | help]" local help_desc = lang.help_desc or "Allows you to [ shows all users offline for to long | add an exception | remove an exception | delete users by number| shows all by this script deleted users | start autoclean | show settings | set [ toggle auto clean shedule | max level to check (manual clean) | max offline days (manual clean)] ]." -- error msgs -- local help_err = lang.help_err or "You are not allowed to use this command." local help_err_wrong_id = lang.help_err_wrong_id or "\n\t\t You have entered one or more wrong parameters, try one of those: \n\n\t\t %s \n\t\t %s " local help_err_in = lang.help_err_in or "The user %s is already in the exceptions tbl" local help_err_out = lang.help_err_out or "The user %s is not in the exceptions tbl" local help_err_off = lang.help_err_off or "User %s was not found." -- msgs -- local showmsg = lang.showmsg or "\n All users who have been offline for more than %s days:" local addexceptionmsg = lang.addexceptionmsg or "Added user %s to exceptions. Reason: %s " local removeexceptionmsg = lang.removeexceptionmsg or "Removed user %s from exceptions." local showexceptedmsg = lang.showexceptedmsg or "\n All excepted users at the moment:" local deleteusermsg = lang.deleteusermsg or "[[ Offline User Script | %s ]]->> User %s was removed from userlist" local delete_error = lang.delete_error or "Number %s does not exist" local delete_errorprotect = lang.delete_errorprotect or "User %s is protected" local showdeletedmsg = lang.showdeletedmsg or "\n All users who have been deleted: \n[Nick] [Pw] [Level] [Date] " local changesettingsmsg = lang.changesettingsmsg or "%s has been set to %s" local showsettingsmsgprt1 = lang.showsettingsmsgprt1 or "\nThe settings: \n\tThe maximum offline days per level for AUTOMATIC clean:\n\t".. " Full auto clean: %s\n\t" local showsettingsmsgprt2 = lang.showsettingsmsgprt2 or " Level %s: %s days\n\t" local showsettingsmsgprt3 = lang.showsettingsmsgprt3 or "\n\tThe settings for MANUAL clean:\n\t".. " Offline days to see a user: %s\n\t".." Check all users below level: %s\n" local helpmsg = lang.helpmsg or [[. An Overview of all parameters of "etc_offlineuser.lua" usage is [+!#]offlineusers - on parameter 'show' it posts you a table with all users who have been offline for a set time periode - on parameter 'addexception' you can add an user to exceptions, this means he can't be deleted - on parameter 'removeexception' you can delete a users ecxeption - on parameter 'showexceptions' you can see all protected ( users in exceptions) users - on parameter 'delete' you can delete all users with the numbers you enterd , example: '+offline delete 13 24 27' deletes user 13, 24 and 27 of the given table - on parameter 'showdeleted' it posts you a table with all deleted users - on parameter 'autoclean' it deletes all users who are longer offline as allowed ( depends on t_settings ) - on parameter 'showsettings' it shows you all settings - on parameter 'set' and additional parameter 'toggleautoclean' to enable periodic auto clean 'maxlvl' you may set the maximum level to check for methode getofflineusers( ) 'days' you may set the maximum days to be apart for methode getofflineusers( ) on parameter 'offlineusershelp' it shows this msg ]] --<< ende des editierbaren Teils --<< end of editable settings --<< --[[ Code ]]-- local onbmsg = function( user, adccmd, parameters) local local_prms = parameters.." " local user_level = user:level( ) if not permission[user_level] then user:reply( help_err, hub_bot ) return PROCESSED else local id, others = utf_match( local_prms, "^(%S+) (.*)") -- local nick = utf_match( others, "(%S+)" ) if id == prm1 then -- shows users longer offline than 't_settings.max_offlinedays_manual' user:reply( getofflineusers( false), hub_bot, hub_bot ) return PROCESSED end if id == prm1_1 then -- shows users longer offline than 't_settings.max_offlinedays_manual' user:reply( getofflineusers( true ), hub_bot, hub_bot ) return PROCESSED end if id == prm2 then -- add exception -- only online users atm user:reply( addexception( others ), hub_bot ) return PROCESSED end if id == prm3 then -- remove exception -- only online users atm user:reply( removeexception( others ), hub_bot ) return PROCESSED end if id == prm4 then -- show exception user:reply( showexcluded( ), hub_bot, hub_bot ) return PROCESSED end if id == prm5 then -- delete users with numbers if user_level < min_level_owner then user:reply( help_err, hub_bot ) else opchat.feed( deleteuser( others ) ) end return PROCESSED end if id == prm6 then -- show deleted users user:reply( showdeleted( ), hub_bot, hub_bot ) return PROCESSED end if id == prm7 then -- autoclean if user_level < min_level_owner then user:reply( help_err, hub_bot ) else opchat.feed( autocleanusers( false ) ) end return PROCESSED end if id == prm8 then -- shows all settings user:reply( showsettings( ), hub_bot, hub_bot ) return PROCESSED end if id == prm9 then -- change settings if user_level < min_level_owner then user:reply( help_err, hub_bot ) else user:reply( changesettings( others, user ), hub_bot) end return PROCESSED end if id == prm10 then -- show help user:reply( returnhelp( others, user ), hub_bot, hub_bot) return PROCESSED end user:reply( utf_format( help_err_wrong_id, help_usage, help_desc ), hub_bot ) -- if no id hittes return PROCESSED end end hub.setlistener( "onStart", { }, function( ) local help = hub.import "cmd_help" if help then help.reg( help_title, help_usage, help_desc, min_level ) -- reg help end local ucmd = hub.import "etc_usercommands" -- add usercommand if ucmd then ucmd.add( ucmd_menu_show, cmd, { prm1 }, { "CT1" }, min_level ) ucmd.add( ucmd_menu_delete, cmd, { prm5, "%[line:" .. ucmd_numbers .. "]" }, { "CT1" }, min_level_owner ) ucmd.add( ucmd_menu_auto_check, cmd, { prm1_1 }, { "CT1" }, min_level_owner ) ucmd.add( ucmd_menu_auto_clean, cmd, { prm7 }, { "CT1" }, min_level_owner ) ucmd.add( ucmd_menu_showexception, cmd, { prm4 }, { "CT1" }, min_level ) ucmd.add( ucmd_menu_showdeleted, cmd, { prm6 }, { "CT1" }, min_level ) ucmd.add( ucmd_menu_showsettings, cmd, { prm8}, { "CT1" }, min_level ) ucmd.add( ucmd_menu_addexception1, cmd, { prm2, "%[line:" .. ucmd_who .. "]", 1, "%[line:" .. ucmd_why .. "]" }, { "CT1" }, min_level ) ucmd.add( ucmd_menu_addexception2, cmd, { prm2, "%[userNI]", 2, "%[line:" .. ucmd_why .. "]" }, { "CT2" }, min_level ) ucmd.add( ucmd_menu_removeexception1, cmd, { prm3, "%[line:" .. ucmd_who .. "]", 1 }, { "CT1" }, min_level ) ucmd.add( ucmd_menu_removeexception2, cmd, { prm3, "%[userNI]", 2 }, { "CT2" }, min_level ) ucmd.add( ucmd_menu_set_check_below, cmd, { prm9, prm9_1, "%[line:" .. ucmd_level .. "]" }, { "CT1" }, min_level_owner ) ucmd.add( ucmd_menu_set_max_offlinedays_manual, cmd, { prm9, prm9_2, "%[line:" .. ucmd_days .. "]" }, { "CT1" }, min_level_owner ) ucmd.add( ucmd_menu_set_toggle, cmd, { prm9, prm9_3, 100 }, { "CT1" }, min_level_owner ) ucmd.add( ucmd_menu_help, cmd, { prm10 }, { "CT1" }, min_level ) end hubcmd = hub.import "etc_hubcommands" -- add hubcommand assert( hubcmd ) assert( hubcmd.add( cmd, onbmsg ) ) return nil end ) hub.setlistener( "onTimer", { }, function( ) if os_difftime( os_time( ) - start ) >= ( day ) then if t_settings.autoclean then hub.broadcast( autocleanusers( true ),hub_bot ) start = os_time( ) end end return nil end ) -- functions -- getofflineusers = function ( mode ) local profile, protected, gonefor local msg = utf_format( showmsg, t_settings.max_offlinedays_manual ).."\n" local regusers, reggednicks, reggedcids = hub.getregusers( ) lastfound = { } -- get data -- lastfound = getusertbl( ) -- kick good ones -- if mode then lastfound = filterusertbl( lastfound, t_settings.check_below, true ) else lastfound = filterusertbl( lastfound, t_settings.check_below, false ) end -- sort data -- table.sort( lastfound, function(a, b) return (a.lastentry > b.lastentry) end ) -- format output -- msg = msg.."[ # ]\t[ nick ]\t\t[ level_nr ]\t[ level_name ]\t[ ever been connected ]\t[ protected ]\t[ offlinezeit ]" for i, user in ipairs( lastfound ) do -- format output local protected, nick, cid, hash = false, user.nick, user.cid, user.hash for i, excepttbl in ipairs( t_exceptions ) do -- is user in t_exceptions? if excepttbl.user_nick == nick then protected = true -- to check if he is in the list break end end gonefor = SecondsToTime( user.lastentry ) local tabs = string.len( user.nick ) if tabs > 8 then tabs = "\t" else tabs = "\t\t" end msg = msg.."\n["..tostring(i).."]\t".."["..user.nick.."]"..tabs.."["..tostring( user.level_nr ).."]\t\t".."["..user.level_name.."]\t\t\t".."["..tostring( user.enter ).."]\t\t".."["..tostring( protected ).."]\t\t".."["..gonefor.."]" end return msg end addexception = function ( others ) local user, ct, reason = utf_match( others, "(%S+) (%d+) (.+)" ) local inlist = false local nick if tonumber(ct) == 1 then nick = user else user = hub_isnickonline( user ) nick = user:firstnick( ) end local msg, key, except for i, excepttbl in ipairs( t_exceptions ) do -- is user in t_exceptions? key = i except = excepttbl if except.user_nick == nick then inlist = true -- to check if he is in the list and want to leave break end end if not reason then reason = "none" end if not inlist then t_exceptions[ #t_exceptions + 1 ] = { user_nick = nick, reason = reason } util_savearray( t_exceptions, exceptions_path ) msg = utf_format( addexceptionmsg, nick, reason ) else msg = utf_format( help_err_in, nick ) end return msg end removeexception = function ( others ) local user, ct = utf_match( others, "(%S+) (%d+)" ) local inlist = false local nick if tonumber(ct) == 1 then nick = user else user = hub_isnickonline( user ) nick = user:firstnick( ) end local msg, key, except for i, excepttbl in ipairs( t_exceptions ) do -- is user in t_exceptions? key = i except = excepttbl if except.user_nick == nick then inlist = true -- to check if he is in the list and want to leave break end end if inlist then -- to check if he is in the list yet, if yes remove him of t_exceptions table_remove( t_exceptions, key ) util_savearray( t_exceptions, exceptions_path ) msg = utf_format( removeexceptionmsg, nick ) else msg = utf_format( help_err_out, nick ) end return msg end showexcluded = function ( ) local msg = showexceptedmsg for i, excepttbl in ipairs( t_exceptions ) do msg = msg.."\n "..( excepttbl.user_nick or " ").." - "..( excepttbl.reason or " " ) end return msg end showdeleted = function ( ) local msg = showdeletedmsg for i, backuptbl in ipairs( t_backup ) do msg = msg.."\n "..backuptbl.nick .." - "..backuptbl.password.." - "..( backuptbl.level or "20" ).." - "..backuptbl.date end return msg end deleteuser = function ( numbers ) local msg ="" -- split numbers in single digits -- local digits = { } for k, v in string.gmatch(numbers, "%w+") do digits[#digits + 1 ] = { [1] = k } end -- check if the insert digits are in the list and ready to delete; also formats a return string-- for j, digitstbl in pairs( digits ) do msg = msg.."\n" if ( #lastfound >= tonumber( digitstbl[1] ) ) then local protected = false local number = tonumber( digitstbl[1] ) local nick = lastfound[number].nick local level = lastfound[number].level_nr -- is user protected? -- for i, excepttbl in ipairs( t_exceptions ) do -- is user in t_exceptions? if excepttbl.user_nick == nick then protected = true -- to check if he is in the list break end end -- delete if not protected -- if not protected then local password = lastfound[number].password t_backup[ #t_backup +1 ] = { nick = nick, password = password, level = level, date = os.date( "%d.%m.%y" ) } util_savearray( t_backup, backup_path ) hub.delreguser( nick ) msg = msg..utf_format( deleteusermsg, "Manual-Clean", nick ) else msg = msg..utf_format( delete_errorprotect, nick ) end else msg = msg..utf_format( delete_error, tostring( digitstbl[1] ) ) end end return msg end SecondsToTime = function(iSeconds, bSmall) -- Build table with time fields local T = os.date("!*t", tonumber(iSeconds)); -- Format to string local sTime = string.format("%i y, %i m, %i d, %i h, %i min", (T.year-1970), T.month-1, T.day-1, T.hour, T.min) -- Small stat? return sTime end changesettings = function( others ) -- check id and change -- local id2, level = utf_match( others, "(%S+) (%d+)") level = tonumber( level) if id2 == prm9_1 then t_settings.check_below = level elseif id2 == prm9_2 then t_settings.max_offlinedays_manual = level elseif id2 == prm9_3 then if t_settings.autoclean then t_settings.autoclean = false level = "false" else t_settings.autoclean = true level = "true" end end -- save settings -- util_savetable( t_settings, "t_settings", settings_path ) return utf_format( changesettingsmsg, id2, level ) end showsettings = function ( ) -- build output -- local msg = utf_format( showsettingsmsgprt1, tostring ( t_settings.autoclean )) -- check levels and value -- for levels, value in pairs( t_settings.max_offline_days_auto ) do msg = msg..utf_format(showsettingsmsgprt2, tostring ( levels), tostring ( value)) end msg = msg..utf_format(showsettingsmsgprt3, t_settings.max_offlinedays_manual, t_settings.check_below ) return msg end autocleanusers = function ( mode ) local msg if mode then msg = "full-automatic clean\n" else msg = "semi-automatic clean\n" end lastfound = { } -- get data -- lastfound = getusertbl( ) -- kick good ones -- lastfound = filterusertbl( lastfound, t_settings.check_below, true ) -- sort data -- table.sort( lastfound, function(a, b) return (a.lastentry > b.lastentry) end ) -- ggf noch einfügen wieviele nicht gelöscht wurden.. for var = #lastfound, 1, -1 do -- only users who have been online will be deleted -- if lastfound[var].enter then msg = msg..deleteuser(var) end end return msg end returnhelp = function( ) return helpmsg end getusertbl = function( ) local regusers, reggednicks, reggedcids = hub.getregusers( ) local lastconnect, bot local maxofflinetime = t_settings.max_offlinedays_manual * day local tbl = { } for i, user in ipairs( regusers ) do lastconnect = user.lastconnect if ( user.is_bot ) then bot = true else bot = false end if lastconnect then tbl[ #tbl + 1 ] = { nick = user.nick, lastentry = os.difftime( os.time(), lastconnect), enter = true, level_nr = user.level, level_name = cfg.get( "levels" )[ tonumber( user.level ) ] or "Unreg", password = user.password, bot = bot } else tbl[ #tbl + 1 ] = { nick = user.nick, lastentry = ( maxofflinetime * 10 ), enter = false, level_nr = user.level, level_name = cfg.get( "levels" )[ tonumber( user.level ) ] or "Unreg", password = user.password, bot = bot } end end return tbl end filterusertbl = function( tbl, below, mode ) local offline_max, check_if_lower offline_max = t_settings.max_offlinedays_manual * day if mode then check_if_lower = 100 else check_if_lower = below end for var = #tbl, 1, -1 do local last_connect = tbl[var].lastentry local lvl local removed = false if tonumber( tbl[var].level_nr ) then lvl = tonumber( tbl[var].level_nr ) else lvl = 100 end if mode then offline_max = t_settings.max_offline_days_auto[lvl] * day or t_settings.max_offlinedays_manual * day end if ( last_connect < offline_max ) then table_remove( tbl, var ) removed = true elseif ( lvl >= check_if_lower ) then table_remove( tbl, var ) removed = true elseif tbl[var].is_bot then table_remove( tbl, var ) removed = true elseif not removed then for sid, onlineuser in pairs( hub_getusers( ) ) do if tbl[var].nick == onlineuser:firstnick( ) then table_remove( tbl, var ) break end end end end return tbl end hub.debug( "** Loaded " .. scriptname.." "..scriptversion.. ".lua **" ) --[[ End ]]--