;==  Blacklist v0.1 ==


on *:LOAD: {
  echo -a Loaded: 7Blacklist v0.1 - by 6SkiPPeR
  if ( $exists($scriptdir $+ blistv.ini) == $true ) { echo 4 -a 13* 8[13Info8]13: File delle variabili blistv.ini trovato. Le variabili all'interno saranno usate, se invece vuoi usare il default, seleziona nel menù 'Reset Defaults'. }
  else default_vars
}
alias blacklist { dialog -m blist blist }

; Variables
alias -l readvar {
  var %x $readini blistv.ini vars $1-
  return %x
}
alias -l writevar {
  if ($2) writeini blistv.ini vars $1 $2-
}
alias -l remvar {
  remini blistv.ini vars $1
}
alias -l default_vars {
  writevar options_onjoin on | writevar options_ignore on | .enable #blacklist | writevar options_onop on
  writevar extra_remignore on | writevar options_banmasks 2 3 9
}

; Display what the script is doing in a custom window
alias -l dc {
  if ($readvar(extra_displayscript) == on) && ($1) {
    /window -n @mm_BlackList | /aline -p @mm_BlackList $timestamp * $1-
  }
}
; Adding users to blacklist (and ignore list if specified)
; Parameters:   /blacklist.add <nick|address> [reason]
alias blacklist.add {
  if ($1 == $null) { echo 4 -a 3* 7[3Info7]3: Parametri non validi, Usa: /blacklist.add <nick|address> [motivo] | halt }
  /dc 4Call to blacklist.add made ( blacklist.add $1 $2- $+ ).
  /dc Testing to see if $1 is an address.
  if ( *!*@* iswm $1 ) {
    /dc $1 seems to be an address. Adding $1 to blacklist user group.
    .auser blacklisted $1 $2-
    if ($readvar(options_ignore) == on) {
      /dc Adding $1 to ignore list.
      .ignore $readvar(options_ignoreswitches) $1
    }
  }
  else {
    if ($readvar(options_banmasks) == $null) {
      echo 4 -a 3* 7[3BL Errore7]3: Attenzione, nessun tipo mask è stata scelta!! vai nelle opzioni e scegline un tipo...
      halt
    }
    if ($server == $null) { 
      echo 4 -a 3* 7[3Info7]3: Attenzione, Non sei connesso al server! Non posso trovare l'indirizzo $1 $+  credi sia uno script telepatico??!!
      halt
    }
    /dc $1 sembra essere un nick. Aggiungendo $1 alla black list con il tipo di mask selezionata.
    var %i 1
    while ($gettok($readvar(options_banmasks),%i,32)) {
      .guser blacklisted $1 $gettok($readvar(options_banmasks),%i,32) $2-
      if ($readvar(options_ignore) == on) {
        if ($address($1,-1)) {
          .ignore $readvar(options_ignoreswitches) $address($1,$gettok($readvar(options_banmasks),%i,32))
        }
        else { /dc $1 is not in the IAL. Not added to ignore list. }
      }
      inc %i
    }
  }
  if ($dialog(blist)) { dialog.update }
}

; Remove users from blacklist (and ignore list if match)
;Params:     /blacklist.del <nick|address>
; Removes all matching addresses for nick or address
alias blacklist.del {
  if ($1 == $null) { echo 4 -a 3* 7[3Info7]3: Parametri non validi. Usa: /blacklist.del <nick|address> | halt }
  /dc 4Call to blacklist.del made ( $+ blacklist.del $1 $+ ).
  if (*!*@* iswm $1) {
    /dc Removing all matching addresses from blacklist for $1 $+ .
    var %i 1
    while ($ulist($1,blacklisted,%i)) {
      if ($1 iswm $ulist($1,blacklisted,%i)) {
        if ($readvar(extra_remignore) == on) && ($ignore($ulist($1,blacklisted,%i))) {
          /dc Removing $ulist($1,blacklisted,%i) from ignore list.
          .ignore -r $ignore($ulist($1,blacklisted,%i))
        }
        /dc Removing $ulist($1,blacklisted,%i) from blacklist.
        .ruser blacklisted $ulist($1,blacklisted,%i)
        var %i 0
      }
      inc %i
    }
  }
  else {
    if ($server == $null) {
      echo 4 -a 3* 7[3Info7]3: Attenzione. Non sei connesso al server! Non posso trovare l'indirizzo di $1 $+ 's pensi sia uno script telepatico??!!
      halt
    }
    /dc Attempting to remove $1 from blacklist using all possible masks.
    var %i -1
    while (%i <= 9) {
      .ruser blacklisted $1 %i
      if ($readvar(extra_remignore) == on) { .ignore -r $1 %i }
      inc %i
    }
  }
  if ($dialog(blist)) { dialog.update }
}
; Clear all users from blacklist (and ignore list if match)
;Params:     /blacklist.clear
alias -l blacklist.clear {
  if ($input(Are you sure you wish to remove ALL users from blacklist?,8,Warning) == $true) {
    /dc 4blacklist.clear called, clearing all users on blacklist...
    if ($readvar(extra_remignore) == on) {
      /dc Clearing blacklisted users from ignore list..
      var %i 1
      while ($ulist(*,blacklisted,%i)) {
        .ignore -r $ulist(*,blacklisted,%i)
        inc %i
      }
    }
    .rlevel blacklisted
  }
  if ($dialog(blist)) { dialog.update }
}



#blacklist on


;Deop/Ban/Kick of blacklisted user
;Params:    /blacklist.kb <channel> <nick> <matching blacklist address>
alias -l blacklist.kb {
  ;If banonly == on and the channel is not in the list, or you're not an op on channel, halt
  if ($readvar(options_banonly) == on) && ($istok($readvar(options_banonlylist),$1,32) != $true) || ($me !isop $1) { halt }
  /dc 4Call to blacklist.kb made, asking server to deop/ban/kick user from $1 $+ .
  if ($readvar(extra_kick) == on) {
    if ($readvar(extra_kick_opt) == 1) { raw -q mode $1 -o+b $2 $3 $+ $cr $+ kick $1 $2 : $+ $ulist($3,blacklisted,1).info (Blacklisted) (Kicks: $readvar(kscount)) }
    else { raw -q mode $1 -o+b $2 $3 $+ $cr $+ kick $1 $2 : $+ $ulist($3,blacklisted,1).info (Blacklisted) (Kicks: $readvar(kcount)) }
  }
  else { raw -q mode $1 -o+b $2 $3 $+ $cr $+ kick $1 $2 : $+ $ulist($3,blacklisted,1).info (Blacklisted) }
  ;** Increase script kick counter
  /writevar kscount $calc($readvar(kscount) + 1)
}
;Same as above but deop/kban even if user not on blacklist
;This is to put in nicklist popups basically
;Params:    /nicklist.kb <channel> <nick> [reason]
alias nicklist.kb {
  if ($1 == $null) || ($2 == $null) { echo 4 -a 13* 7[3Info7]13: Usa: /nicklist.kb <channel> <nick> [motivo] }
  if ($me !isop $1) { halt }
  /dc 4Call to nicklist.kb made, asking server to deop/ban/kick $2 from $1 $+ .
  if ($readvar(extra_kick) == on) {
    if ($readvar(extra_kick_opt) == 1) {
      mode $1 -o $2
      ban $1 $2 $gettok($readvar(options_banmasks),1,32)
      kick $1 $2 $3 (Blacklisted) (Kicks: $readvar(kscount))
    }
    else {
      mode $1 -o $2
      ban $1 $2 $gettok($readvar(options_banmasks),1,32)
      kick $1 $2 $3 (Blacklisted) (Kicks: $readvar(kcount))
    }
  }
  else { 
    mode $1 -o $2
    ban $1 $2 $gettok($readvar(options_banmasks),1,32)
    kick $1 $2 $3 (Blacklisted)
  }
  ;** Increase script kick counter
  /writevar kscount $calc($readvar(kscount) + 1)
}

;** When a blacklisted user joins:
on blacklisted:JOIN:#: {
  /dc 4A blacklisted user (Nick: $nick Matches Address: $maddress $+ ) has joined $chan $+ .
  if ($nick == $me) { /dc You are on your own blacklist... baka.. | halt }
  if ($me !isop $chan) {
    if ($readvar(extra_echo) == on) { /echo 4 -a 3* 7[3Info7]3: Ha joinato uno in black list $chan (Nick: $nick Matching Blacklist Address: $maddress $+ ) sono azzi sua. }
    /dc You're not an op. Nothing will be done... | halt
  }
  blacklist.kb $chan $nick $maddress
}
;** when you get opped in a channel, do a scan for blacklisted users.
on *:OP:#: {
  if ($opnick != $me) halt
  if ($readvar(options_onop) != on) halt
  /dc 4Opped on $chan $+, updating IAL...
  set %temp_who $addtok(%temp_who,$chan,32)
  /who $chan
}
; When doing a blacklist.scan, the ial must be updated/filled by doing a /who #channel
; Otherwise it won't scan everyone in a channel (only the ones in the ial)
; These raw events stop the status window filling up with server replies while updating the ial
; And only calls the scan after the ial for channel/s has been completely updated

;352     RPL_WHOREPLY
raw 352:*: { 
  if ($istok(%temp_who,$2,32)) halt
}
;315     RPL_ENDOFWHO
raw 315:*: { 
  if ($istok(%temp_who,$2,32)) {
    /dc IAL update on $2 complete.
    blacklist.scan $2
    if ($remtok(%temp_who,$2,1,32)) { set %temp_who $remtok(%temp_who,$2,1,32) }
    else { unset %temp_who }
    halt
  }
}
; Scans channel for users in blacklist, bans any it comes across
; Note: ial should be updated before blacklist.scan called.
; Params    /blacklist.scan <channel>
alias -l blacklist.scan {
  /dc 4blacklist.scan called... scanning for users in blacklist on $1 $+ .
  var %i 1
  while ($nick($1,%i)) {
    if ($ulist($address($nick($1,%i),1),blacklisted)) {
      /dc $nick($1,%i) $+ 's address ( $+ $address($nick($1,%i),1) $+ ) matches $ulist($address($nick($1,%i),1),blacklisted,$ulist($address($nick($1,%i),1),blacklisted)) which is on the blacklist.
      blacklist.kb $1 $nick($1,%i) $ulist($address($nick($1,%i),1),blacklisted,$ulist($address($nick($1,%i),1),blacklisted))
      ; What a lovely simple to understand method to return the matching address for a nickname
    }
    inc %i
  }
  /dc Scan complete.
}


#blacklist end


; Increase kick counter
on *:KICK:#: {
  if ($nick == $me) { /writevar kcount $calc($readvar(kcount + 1)) }
}
on *:EXIT: {
  unset %temp_who
}
alias -l dialog.update {
  /dc Updating blacklist dialog.
  if ($did(blist,9).lines) /did -r blist 9
  var %i 1
  while ($ulist(*,blacklisted,%i)) {
    /did -a blist 9 $ulist(*,blacklisted,%i) $ulist(*,blacklisted,%i).info
    inc %i
  }
  if ($did(blist,9).lines == 0) {
    did -b blist 10
    did -b blist 11
    did -b blist 14
    did -b blist 15
  }
  elseif ($did(blist,10).enabled == $false) {
    did -e blist 10
    did -e blist 11
    did -e blist 14
    did -e blist 15
  }
}
dialog blist {
  title "Blacklist Settings"
  size -1 -1 195 188
  option dbu
  radio "Abilita" 2, 5 0 35 10, Group
  radio "Disabilita" 3, 40 0 35 10,
  button "OK" 4, 45 175 25 10, Ok  
  button "Cancel" 5, 80 175 25 10, Cancel  
  button "ATTIVA" 6, 115 175 25 10,
  link "BlackList 0.1 by SkiPPeR" 7, 125 2 90 20,
  tab "Control" 8, 0 15 195 155
  combo 9, 5 45 185 110, tab 8
  button "Del" 10, 80 155 35 10, tab 8
  button "Del All" 11, 150 155 35 10, tab 8
  text "Blacklist" 12, 5 35 95 10, tab 8
  button "Add" 13, 10 155 35 10, tab 8
  button "Add Info" 14, 45 155 35 10, tab 8
  button "Del Wildcard" 15, 115 155 35 10, tab 8
  tab "Options" 17,
  check "Aggiungi anche all'ignore list" 18, 105 65 75 10, tab 17
  check "private" 19, 110 75 35 10, tab 17
  check "channel" 20, 145 75 35 10, tab 17
  check "notice" 21, 110 85 35 10, tab 17
  check "ctcp" 22, 145 85 35 10, tab 17
  check "invite" 23, 110 95 35 10, tab 17
  check "all" 24, 145 95 35 10, tab 17
  text "Aggiungi alla blacklist/ignore list usando queste masks:" 25, 5 35 80 15, tab 17
  check "*!*@host.domain" 26, 10 50 70 10, tab 17
  check "*!*user@*.domain" 27, 10 60 70 10, tab 17
  check "*!*@*.domain" 28, 10 70 70 10, tab 17
  check "nick!*@*.domain" 29, 10 80 70 10, tab 17
  combo 30, 10 110 80 40, tab 17
  check "" 31, 5 95 10 15, tab 17
  button "Add" 32, 20 155 20 10, tab 17
  button "Del" 33, 40 155 20 10, tab 17
  button "Clr" 34, 60 155 20 10, tab 17
  check "" 35, 105 115 10 25, tab 17
  text "Quando ti oppano, controlla se ci sono users nella tua blacklist, se sono presenti nel canale, bannali." 36, 115 115 70 25, tab 17
  text "Banna gli users della blacklist solo su questi canali:" 38, 15 95 75 15, tab 17
  text "Messaggio di default blacklist:" 39, 105 35 75 10, tab 17
  edit "" 40, 110 45 75 10, AutoHs tab 17
  tab "Extra" 41,
  check "Mostra i dettagli dei ban in una window." 42, 5 35 160 10, tab 41
  check "Mostra kick counter alla fine del kick message." 43, 5 50 160 10, tab 41
  radio "Numero kicks effettuato dal script." 44, 15 60 115 10, group, tab 41
  radio "Numero ToTaLe di kicks effettuati." 45, 15 70 115 10, tab 41
  check "" 46, 5 85 10 15, tab 41
  text "Se non sei operatore del canale, attiva una finestra di avviso nel caso joina un user presente nella tua blacklist." 47, 15 85 170 15, tab 41
  check "Quando rimuovi gli utenti dalla blacklist, rimuovili anche dalla ignore list." 48, 5 105 170 10, tab 41
}
; The dialog code was created using Ymar The Leprechaun's Dialog Creator (ldc), downloaded from mircscripts.org.

;-- Dialog Initialisation --
on *:dialog:blist:init:0: {
  if ($group(#blacklist) == on) { /did -c blist 2 }
  else /did -c blist 3
  if ( $istok($readvar(options_banmasks),2,32) == $true ) { /did -c blist 26 }
  if ( $istok($readvar(options_banmasks),3,32) == $true ) { /did -c blist 27 }
  if ( $istok($readvar(options_banmasks),4,32) == $true ) { /did -c blist 28 }
  if ( $istok($readvar(options_banmasks),9,32) == $true ) { /did -c blist 29 }
  if ( $readvar(options_banonly) == on ) { /did -c blist 31 }
  if ($readvar(options_banonlylist)) { /didtok blist 30 32 $readvar(options_banonlylist) }
  if ( $readvar(options_msg) ) { /did -a blist 40 $readvar(options_msg) }
  if ( $readvar(options_ignore) == on ) { /did -c blist 18 }
  if ( $readvar(options_ignoreswitches) == $null ) { /did -c blist 24 }
  else {
    if ( p isin $readvar(options_ignoreswitches) ) { /did -c blist 19 }
    if ( c isin $readvar(options_ignoreswitches) ) { /did -c blist 20 }
    if ( n isin $readvar(options_ignoreswitches) ) { /did -c blist 21 }
    if ( t isin $readvar(options_ignoreswitches) ) { /did -c blist 22 }
    if ( i isin $readvar(options_ignoreswitches) ) { /did -c blist 23 }
  }
  if ( $readvar(options_onop) == on ) { /did -c blist 35 }
  if ( $readvar(extra_displayscript) == on ) { /did -c blist 42 }
  if ( $readvar(extra_kick) == on ) { /did -c blist 43 }
  if ($readvar(extra_kick_opt) == 1) { /did -c blist 44 }
  else /did -c blist 45
  if ( $readvar(extra_echo) == on ) { /did -c blist 46 }
  if ( $readvar(extra_remignore) == on ) { /did -c blist 48 }
  dialog.update
}
;-- Save options --
alias -l save_vars {
  if ($did(blist,2).state == 1) { .enable #blacklist }
  else .disable #blacklist
  /remvar options_banmasks
  if ( $did(blist,26).state == 1 ) { /writevar options_banmasks $addtok($readvar(options_banmasks),2,32) }
  if ( $did(blist,27).state == 1 ) { /writevar options_banmasks $addtok($readvar(options_banmasks),3,32) } 
  if ( $did(blist,28).state == 1 ) { /writevar options_banmasks $addtok($readvar(options_banmasks),4,32) }
  if ( $did(blist,29).state == 1 ) { /writevar options_banmasks $addtok($readvar(options_banmasks),9,32) }
  if ( $did(blist,31).state == 1 ) { /writevar options_banonly on }
  else /remvar options_banonly
  if ($didtok(blist,30,32)) { /writevar options_banonlylist $didtok(blist,30,32) }
  else /remvar options_banonlylist
  if ($did(blist,40)) { /writevar options_msg $did(blist,40) }
  else /remvar options_msg
  if ( $did(blist,18).state == 1 ) { /writevar options_ignore on }
  else /remvar options_ignore
  var %s -
  if ( $did(blist,19).state == 1) { %s = %s $+ p }
  if ( $did(blist,20).state == 1) { %s = %s $+ c }
  if ( $did(blist,21).state == 1) { %s = %s $+ n }
  if ( $did(blist,22).state == 1) { %s = %s $+ t }
  if ( $did(blist,23).state == 1) { %s = %s $+ i }
  if ( $did(blist,24).state == 1) { %s = $null }
  if ( %s == $null ) { /remvar options_ignoreswitches }
  else /writevar options_ignoreswitches %s
  if ( $did(blist,35).state == 1 ) { /writevar options_onop on }
  else /remvar options_onop
  if ( $did(blist,42).state == 1 ) { /writevar extra_displayscript on }
  else /remvar extra_displayscript
  if ( $did(blist,43).state == 1 ) { /writevar extra_kick on }
  else /remvar extra_kick
  if ( $did(blist,44).state == 1 ) { /writevar extra_kick_opt 1 }
  else /remvar extra_kick_opt
  if ( $did(blist,46).state == 1 ) { /writevar extra_echo on }
  else /remvar extra_echo
  if ( $did(blist,48).state == 1 ) { /writevar extra_remignore on }
  else /remvar extra_remignore
}

;-- OK Button --
on *:dialog:blist:sclick:4:save_vars

;-- Apply Button --
on *:dialog:blist:sclick:5:save_vars


;-- Blacklist Add Button --
on *:dialog:blist:sclick:13: {
  var %s $input(Add address (eg. *!*@*.aol.com) o nick. Nick presente in IRC.,1,Aggiungilo alla blacklist)
  if (%s) {
    blacklist.add %s
    did -d blist 9 0
    dialog.update
  }
}
;-- Edit Info --
on *:dialog:blist:sclick:14: {
  if ($did(blist,9).seltext) {
    var %i 2
    while ($gettok($did(blist,9).seltext,%i,32)) {
      var %info = %info $gettok($did(blist,9).seltext,%i,32)
      inc %i
    }
    if (%info == $null) { var %info = - }
    var %s $input(Edit Info,1,Edit Info (space for nothing), $+ %info $+ )
    if (%s) { .iuser $gettok($did(blist,9).seltext,1,32) %s | /dc 4Info of blacklisted entry edited. | dialog.update }
  }
  else { return $input(Seleziona un blacklist entry nell'edit.,4,Baka detected.) }
}
;-- Del --
on *:dialog:blist:sclick:10: {
  if ($did(blist,9).seltext) {
    /dc 4Removing selected address from blacklist.
    .ruser blacklisted $gettok($did(blist,9).seltext,1,32)
    if ($readvar(extra_remignore) == on) {
      /dc Removing address from ignore list.
      .ignore -r $gettok($did(blist,9).seltext,1,32)
    }
    dialog.update
  }
  else { return $input(SEleziona un blacklist entry da cancellare.,4,Baka detected.) }
}
;-- Del Wildcard --
on *:dialog:blist:sclick:15: {
  var %s $input(Enter Wildcard address (eg. *!*@*.aol.com). o inserisci un nickname. Il Nickname presente in IRC.,1,indirizzo o nick rimosso dalla Wildcard.)
  if (%s) {
    /dc 4Calling "/blacklist.del %s $+ "
    blacklist.del %s
    dialog.update
  }
}
;-- Clear All --
on *:dialog:blist:sclick:11: {
  blacklist.clear | dialog.update
}

;-- Options Chan List Add button --
on *:dialog:blist:sclick:32: {
  if ( $did(blist,30).text == $null ) || ( $left($did(blist,30).text,1) != $chr(035) ) {
    return $input(Invalid Channel,4,Error)
  }
  /did -a blist 30 $did(blist,30).text
  /did -d blist 30 0
}
;-- Del button --
on *:dialog:blist:sclick:33: {
  if ($did(blist,30).sel) {
    /did -d blist 30 $did(blist,30).sel
    /did -d blist 30 0
  }
}
;-- Clear button --
on *:dialog:blist:sclick:34: {
  /did -r blist 30
}
