View Single Post
  #11  
Old 08-12-2010, 07:16 AM
Twinny Twinny is offline
My empire of dirt
Twinny's Avatar
Join Date: Mar 2006
Location: Australia
Posts: 2,422
Twinny is just really niceTwinny is just really nice
Send a message via AIM to Twinny
Had issues with the script Jerret put on Classic iPhone so made my own adaptation which provided some error checking. Depending on jerrets next version, will either improve this or implement changes to his next version.

This is based on what Jerret created but ended up starting from scratch using his as a reference point while scripting with style I am used to. Made my version as a backend, allowing for a simple frontend to be created. Provided example of NPC commands (RC) but can be adapted to be done anywhere.

Ultimately should move the hasright checking to DBNPC itself but that's for the next version =]

PHP Code:
function onCreated() {
  const 
ipbantick 60;
  
setTimer(1);
}

function 
onTimeout() {
  for (
ip this.ipban.getdynamicvarnames()) {
    
    if (
this.ipban.(@ip)[0] == "permanent")
      continue;
    
    
this.ipban.(@ip)[0] -= ipbantick;
    if (
this.ipban.(@ip)[0] <= 0)
      
unIPBan(ip,"Released by the system","system");  
  }
  
setTimer(ipbantick);
}

/* Add/Modify an IP ban */

public function IPBan(iptimecommentadminoverwrite) {
  
/* Work out ban time */
  
temp.ban banTime(time);
  if (
temp.ban[0] == 0)
    return 
"Invalid time specified: "@time;
  
  
/* Check IP given */
  
temp.check checkIP(ip);
  if (
temp.check[0] == 0)
    return 
temp.check[1];
  
  if (
comment "")
    return 
"Must provide a reason for the IP ban";

  
//Checks done: lets ban an IP!
  
temp.ipstring iptostring(ip);
  
  if (
this.ipban.(@ipstring).size() > 2)
  {
    if (!
overwrite)
    {
      
temp.ret format("%s has already been banned. Set overwrite as true to overwrite this entry",ip);
      return 
temp.ret;
    }
    else 
     
temp.modified true;
  }
  else
    
this.ipban.(@ipstring) = new[4];

  
/* set the ban */
  
this.ipban.(@ipstring) = {temp.ban[0], temp.commenttemp.admintemp.ban[0]};

  if (
temp.modified)
    
temp.ret format("%s modified the IP Ban for %s. Now banned for %s with reason: %s",
      
adminiptemp.ban[1], comment);
  else
    
temp.ret format("%s has IP banned %s for %s with reason: %s",
      
adminiptemp.ban[1], comment);
  
  
savelog2("ipbans.txt",temp.ret);
  
this.trigger("saveData","");
  
updateIPBans();  
  return 
temp.ret;
}

/* Remove an IP Ban */
public function unIPBan(ipcommentadmin) {
  if (
admin == "system")
    
temp.ipstring ip;
  else
    
temp.ipstring iptostring(ip);
    
  if (
this.ipban.(@ipstring).size() < 1)
    return 
"given IP address is not banned: "@ip;
  if (
comment == "")
    return 
"Must provide a reason to unban ip";
  
this.ipban.(@ipstring).clear();
  
this.trigger("saveData","");
  
updateIPBans();
  
temp.ret format("%s has unbanned the IP %s reason: %s",admin,ip,comment);
  
savelog2("ipbans.txt"temp.ret);
  return 
temp.ret;
}
  
public function 
updateIPBans() {
  
temp.lines = {
    
"###################################",
    
"#           Automatically generated IP Bans            #",
    
"###################################",
  };
  
  for (
ip this.ipban.getdynamicvarnames()) {
    if (
this.ipban.(@ip).size() < 1)
      continue;
    
temp.lines.add("# Ban Reason: "@this.ipban.(@ip)[1]);
    
temp.lines.add("# Banned for: "@this.ipban.(@ip)[3]);
    
temp.lines.add("# Banned by: "@this.ipban.(@ip)[2]);
    
temp.lines.add(stringtoip(ip));
    
temp.lines.add("");
  }
  
temp.lines.savelines("levels/ipbanned.txt"0);
  return 
1;
}
  
public function 
listBans() {
  for (
this.ipban.getdynamicvarnames()) {
    if (
this.ipban.(@t).size() < 1)
      continue;  
    
temp.list.add(stringtoip(t));
  }
  return 
temp.list;
}
  
  
/* Return with ban time*/

function banTime(time) {
  switch (
time) {
    case 
"minute":
      
temp.bantime 60;
      
temp.timereport "a minute (testing)";
      break;
    case 
"hour"
      
temp.bantime 3600
      
temp.timereport "an hour";
      break;
    case 
"day"
      
temp.bantime 3600 24
      
temp.timereport "a day";
      break;
    case 
"week"
      
temp.bantime 3600 24 7
      
temp.timereport "a week";
      break;
    case 
"month"
      
temp.bantime 3600 24 30
      
temp.timereport "a month";
      break;
    case 
"year"
      
temp.bantime 3600 24 365
      
temp.timereport "a year";
      break;
    case 
"permanent"
      
temp.bantime "permanent"
      
temp.timereport "a very long time";
      break;
    default:
      
temp.bantime int(time);      
      if (
temp.bantime 0)
        
temp.timereport "a custom time of " @time@" seconds";
      else
        
temp.bantime 0;
      break;
  }
  
temp.ret = {temp.bantimetemp.timereport};
  return 
temp.ret;
}

/* Verify whether given IP address can be banned */
function checkIP(ip) {
  
temp.tokens ip.tokenize(".");

  
// Check if IP address is private 
  
temp.private = {"10""127""172.16""192.168"};
  for (
temp.private) {
    if (
ip.starts(t)) {
      
temp.ret = {0format("IP Ban Error: %s is in a private address class"ip)};
      return 
temp.ret;
    }
  }
  
  
// Not enough octets: needs to be x.x.x.x
  
if (temp.tokens.size() != 4) {
    
temp.ret = {0format("IP Ban Error: Invalid IP address given: %s. 4 octets expected (x.x.x.x)",ip)};
    return 
temp.ret;
  }
  
  for (
i=0i<temp.tokens.size(); i++) {
    
    if (
temp.tokens[i] == "*")
      continue;
      
    if (
temp.tokens[i] != int(temp.tokens[i])) {
      
temp.ret = {0format("IP Ban Error: Invalid IP address given: %s. An IP address can only contain numbers!",ip)};
      return 
temp.ret;
    }
    
    
// An octet needs to be above 0 and less than 255
    
if (temp.tokens[i] > 255 || temp.tokens[i] < 0) {
      
temp.ret = {0format("IP Ban Error: Invalid IP address given: %s. An octet value must be between 0 and 255",ip)}; 
      return 
temp.ret;
    }
  }
  
  return 
1;
}

public function 
iptostring(ip) {
  
temp.toks ip.tokenize(".");
  
temp.ipstring toks[0] @"_"toks[1] @"_"toks[2] @"_"toks[3];
  return 
temp.ipstring;
}

public function 
stringtoip(ip) {
  
temp.toks ip.tokenize("_");
  
temp.ipstring toks[0] @"."toks[1] @"."toks[2] @"."toks[3];
  return 
temp.ipstring;


Example of my NC commands

PHP Code:
    case "ipban": {
      if (!
player.hasright("rw""levels/ipbanned.txt")) {
        
sendtorc("You do not have IP Ban rights, "@player.account);
        return;
      }
      
temp.ret IPBans.ipBan(params[1], params[2], params[3], player.accountparams[4]);
      
sendtorc(temp.ret);
      break;
    };
    
    case 
"listipbans": {
      if (!
player.hasright("rw""levels/ipbanned.txt")) {
        
sendtorc("You do not have IP Ban rights, "@player.account);
        return;
      }
      
temp.ret IPBans.listBans();
      
sendtorc(temp.ret);
      break;
    };
    
    case 
"unipban": {
      if (!
player.hasright("rw""levels/ipbanned.txt")) {
        
sendtorc("You do not have IP Ban rights, "@player.account);
        return;
      }
      
temp.ret IPBans.unIPBan(params[1],params[2],player.account);
      
sendtorc(temp.ret);
      break;
    };
   
   case 
"rebuildipbanlist": {
      if (!
player.hasright("rw""levels/ipbanned.txt")) {
        
sendtorc("You do not have IP Ban rights, "@player.account);
        return;
      }
      
temp.ret IPBans.updateIPBans();
      if (
temp.ret)
        
sendtorc("IP Bans list has been regenerated");
      else
        
sendtorc("Issues generating IP bans list");
      break;
   }; 

Last edited by Twinny; 08-12-2010 at 07:59 AM..
Reply With Quote