alias badword {
  var %syntax = * /badword: `badword <-,a|r|c|v|i|s> [word] [reason]
  tokenize 32 $1-
  if ($left($1,1) == -) {
    var %s = $right($1,-1)
    if ($2-) || ($istok(l c v s,%s,32)) { 
      badword.init
      if (%s == a) {  
        if (!$hget(badwords,$2)) { 
          hadd badwords $2 $iif($3-,$3-,Bad word)
          return * /baddword: Added $qt($2)
        }
        else return * /badword: $qt($2) is already listed
      }
      elseif (%s == r) {
        if ($hget(badwords,$2)) {
          hdel badwords $2
          return * /badword: Deleted $qt($2)
        }
        else return * /badword: $qt($2) was not found 
      }
      elseif (%s == c) {
        hfree badwords
        return * /badword: Cleared
      }
      elseif (%s == l) || (%s == v) {
        var %x = 1,%words
        while ($hget(badwords,%x).item) {
          var %words = %words $hget(badwords,%x).item
          inc %x 
        }
        return $iif(%words,%words,*list is empty*)
      }
      elseif (%s == i) {
        if ($hget(badwords,$2)) return $2 $+ : $hget(badwords,$2)
        else return * /badword: $qt($2) was not found
      }
      elseif (%s == s) {
        hsave badwords badwords.hsh
        return * /badword: Saved list
      }
      else return %syntax
    }
    else return %syntax
  }
  else return %syntax
}
alias suggestion {
  tokenize 32 $1-
  var %syntax = * /suggestion: `suggestion <-,a|c|r|s> [suggestion] 
  if ($left($1,1) == -) {
    var %s = $right($1,-1)
    if ($2-) || ($istok(c s,%s,32)) {
      if (%s == a) {
        write suggestions.txt $2-
        return * /suggestion: Wrote suggestion $+($chr(35),$lines(suggestions.txt))
      }
      if (%s == r) {
        if ($2 isnum $+(1-,$lines(suggestions.txt))) {
          write -dl $+ $2 suggestions.txt
          return * /suggestion: Removed line $+($chr(35),$2)
        }
        else {
          var %x = $read(suggestions.txt,sn,$2)
          if (%x) { write -dl $+ $readn suggestions.txt | return * /suggestion: Removed line $+($chr(35),$readn) }
          else return * /suggestion: Line not found beginning with $qt($2)
        }
      }
      if (%s == s) {
        if ($read(suggestions.txt,n,$2)) return $2 $+ . $read(suggestions.txt,$2)
        else { var %msg = $read(suggestions.txt,n),%n = $readn | return $iif(%msg,%n $+ . %msg,Empty) }
      }
      if (%s == c) {
        write -c suggestions.txt
        return * /suggestion: Cleared Suggestions.txt
      }
      else return %syntax
    }
    else return %syntax
  }
  else return * /suggestion: $lines(Suggestions.txt) line(s)
}

alias badword.init {
  if (!$hget(badwords)) var %hmake = $true
  var %hloaded = $iif($hget(badwords,0) > 0,$true,$false) 
  if (%hmake) hmake badwords
  if (!%hloaded) && ($isfile(badwords.hsh)) hload badwords badwords.hsh
}
alias badword.check {
  tokenize 32 $1-
  var %y = 1
  badword.init
  while (%y <= $numtok($1-,0)) {
    var %phrase = $gettok($1-,%y,32)  
    if ($istok($badword(-l),%phrase,32)) return $hget(badwords,%phrase)
    inc %y
  }
  else return $false
}
alias op {
  if ($2) {
    if ($1 === -a) {
      .auser 25 $2
      return * [07!] $2 is now operator
    }
    elseif ($1 == -r) {
      if ($level($2) == 25) {
        .ruser 25 $2
        return * [07!] $2 was 04removed as operator
      }
      else return Invalid nickname/level
    }
  }
}
alias sop {
  if ($2) {
    if ($1 === -a) {
      .auser 50 $2 
      return * [12!] $2 is now super op
    }
    elseif ($1 == -r) {
      if ($level($2) == 50) {
        .ruser 50 $2
        return * [12!] $2 was 04removed as super op
      }
      else return Invalid nickname/level
    }
  }
}
alias admin {
  if ($2) {
    if ($1 === -a) {
      .auser 100 $2
      return * [08!] $2 is now admin 
    }
    elseif ($1 === -r) {
      if ($level($2) == 100) {
        .ruser 100 $2
        return * [08!] $2 was 04removed as admin
      }
      else return Invalid nickname/level
    }
  }
}
on *:TEXT:`*:#:{ 
  if ($level($nick) >= 100) {
    if ($1 == `chan) {
      if (!$2) { if (%chan) _msg $chan Current Channel: %chan }
      else { set %chan $iif($left($2,1) == $chr(35),$2,$chr(35) $+ $2) | _msg $chan Changed channel to: %chan }
    }
    if ($1 == `write) {
      if ($2-) {
        if (*.* iswm $2) {
          var %ext = $right($2,3)
          $iif(%ext == ini,writeini,write) $2 $3 $4 $iif(%ext == ini,$iif($5-,$5-,$false))
          _msg $chan * 10>EDIT> Wrote to $2 $+ . 
        }
        else _notice $nick * Error: invalid filename
      }
      else _notice $nick * Error: insufficient parameters
    }
  }
  if ($level($nick) >= 50) {
    if ($1 == `read) {
      if ($2-) {
        if (*.* iswm $2) {
          var %ext = $right($2,3)
          var %msg = $iif(%ext == ini,$readini($2,$3,$iif($4,$4,0)),$read($2,ns,$3))
          _msg $chan * 06<READ< From $2 $+ : %msg
        }
        else _notice $nick * Error: invalid filename
      }
      else _notice $nick * Error: insufficient parameters
    }
    if ($1 == `grant) {
      if ($2 == money) {
        if ($readini(user.ini,$3,register)) {
          if ($4 isnum $+(1-,$calc($readini(user.ini,bank,pot) * .45))) {
            writeini user.ini $3 money $calc($readini(user.ini,$3,money) + $4)
            writeini user.ini bank pot $calc($readini(user.ini,bank,pot) - $4) 
            _msg $chan * 03>GAVE> $chr(36) $+ $4 to $3 from the Pot
          }
          else _notice $nick * Error: Invalid money request 
        }
        else _notice $nick * Error: $3 is not registered
      }
    }
  }
  if ($level($nick) >= 25) {
    if ($1 == `badword) || ($1 == `bw) _notice $nick $badword($2,$3-)
    if ($1 == `suggest) || ($1 == `suggestion) {
      var %x = $numtok($2-,32),%words
      while (%x) {
        if ($badword.check($gettok($2-,%x,32))) var %words = %words $gettok($2-,%x,32)
        dec %x
      } 
      _notice $nick $iif(%words,* Error: badwords: %words,$suggestion($2,$3-)) 
    }
  }
  if ($1 == `add) {
    if ($2 == op) { 
      if ($level($nick) > 25) {
        _msg $chan $op(-a,$iif($3,$3,$nick))
      }
    }
    elseif ($2 == sop) {
      if ($level($nick) > 50) {
        _msg $chan $sop(-a,$iif($3,$3,$nick))
      }
    }
    elseif ($2 == admin) { 
      if ($level($nick) >= 100) {
        _msg $chan $admin(-a,$iif($3,$3,$nick))
      }
      else _notice $nick * Error: insufficient status
    }
  }
  if ($1 == `del) {
    if ($2 == op) {
      if ($level($nick) > 25) {
        _msg $chan $op(-r,$iif($3,$3,$nick))
      }
    }
    elseif ($2 == sop) {
      if ($level($nick) > 50) {
        _msg $chan $sop(-r,$iif($3,$3,$nick))
      }
    }
    elseif ($2 == admin) {
      if ($level($nick) >= 100) {
        _msg $chan $admin(-r,$iif($3,$3,$nick))
      }
    }
  }
  if ($1 == `help) {
    if ($ini(help.ini,$2)) {
      if ($readini(help.ini,$2,$3)) _notice $nick [HELP] $readini(help.ini,$2,$3)
      else {
        var %n = $ini(help.ini,$2),%topics
        while (%n) {
          var %topics = %topics $ini(help.ini,$2,%n)
          dec %n
        } 
        _notice $nick $iif(%topics,[HELP] Subjects to $2 $+ : %topics,[HELP] No subjects)
      }
    }
    else {
      var %n = 1,%topics
      while ($ini(help.ini,%n)) {
        var %topics = %topics $ini(Help.ini,%n)
        inc %n
      }
      _notice $nick $iif(%topics,[HELP] Topics: %topics,[HELP] No Topics)
    } 
  }
}
