aIRC — File Browser

aIRCv0.8.69 / aIRCv0.8.69 / script / bots.mrc

bots.mrc — 15.87 KB — Download this file

;/**********************************************************************
; *
; * aIRC
; * Bots
; *
; **********************************************************************/

alias botprofiles { _dialog -m botprofiles botprofiles }
dialog botprofiles {
  title "Bot profiles"
  size -1 -1 147 164
  option dbu

  box "Profiles",10,5 5 137 50
  box "Commands",11,5 60 137 80

  combo 101,10 20 80 200,drop sort
  button "Add",102,95 20 19 10
  button "Del",103,117 20 19 10
  text "Bots use",104,10 37 30 8
  combo 105,40 35 50 50,drop

  text "Command:",150,10 75 160 8
  combo 151,10 85 80 100,drop sort
  button "Add",152,95 85 19 10
  button "Del",153,117 85 19 10
  combo 154,10 100 80 100,drop sort
  combo 155,95 100 42 100,drop sort
  edit "",156,10 112 128 11,autohs
  check "Requires level",157,10 127 45 8
  edit "",158,60 125 20 11,autohs

  button "&Ok",400,38 145 30 15,ok hide
  button "&Close",401,38 145 30 15,cancel
  button "&Help",402,78 145 30 15
}
ON *:DIALOG:botprofiles:*:*:{
  var %file = $_botprofilesfile,%profile = $did(101,$did(101).sel),%profilef = $replace(%profile,$chr(32),$chr(254)),%command = $did(151,$did(151).sel)
  if ($devent == init) {
    _mdx.init $_icofile(botprofiles.ico)
    did -a $dname 105 Flags
    did -a $dname 105 Levels
    did -a $dname 154 /ctcp
    did -a $dname 154 /ctcpreply
    did -a $dname 154 /describe
    did -a $dname 154 /msg
    did -a $dname 154 /notice
    did -a $dname 155 <BOT>
    did -a $dname 155 <CHAN>
    _botprofiles.loadprofiles
    _dummy $_lang.loaddialog($dname,_botprofiles,00086969,main.lang,$scriptdir)
  }
  if ($devent == sclick) {
    if (0) { }
    elseif ($did == 101) { _botprofiles.selectprofile %profile }
    elseif ($did == 102) { .timer -o 1 0 _botprofiles.addp }
    elseif ($did == 103) { _botprofiles.delp %profile }
    elseif ($did == 105) { write -s $+ %profilef $+ .flagsorlevels $"(%file) %profilef $+ .flagsorlevels $did(105).sel }
    elseif ($did == 151) { _botprofiles.selectcommand %command }
    elseif ($did == 152) { .timer -o 1 0 _botprofiles.addc %profile }
    elseif ($did == 153) { _botprofiles.delc %profile }
  }
  if ($did isnum 154-158) && (%command != $null) { _botprofiles.savecommand }
  _botprofiles.refresh
}
alias _botprofiles.savecommand {
  var %file = $_botprofilesfile,%d = botprofiles,%profile = $did(%d,101,$did(%d,101).sel),%profilef = $replace(%profile,$chr(32),$chr(254)),%command = $did(%d,151,$did(%d,151).sel)
  write -s $+ %profilef $+ ._ $+ %command $"(%file) %profilef $+ ._ $+ %command $did(%d,157).state $iif($gettok($did(%d,158),1,32) != $null,$gettok($did(%d,158),1,32),$null) $did(%d,154,$did(%d,154).sel) $did(%d,155,$did(%d,155).sel) $did(%d,156)
}
alias _botprofiles.loadprofiles {
  var %file = $_botprofilesfile,%d = botprofiles,%win = @__loadbotprofiles,%l
  did -r %d 101
  _botprofiles2win
  while ($line(%win,0)) {
    did -a %d 101 $line(%win,1)
    dline %win 1
  }
  close -@ %win
  if ($did(%d,101).lines) {
    did -c %d 101 1
    _botprofiles.selectprofile $did(%d,101,$did(%d,101).sel)
  }
}
alias _botprofiles2win {
  var %file = $_botprofilesfile,%d = botprofiles,%win1 = @__loadbotprofiles,%win2 = @__loadbotprofilestmp,%l
  _openwins %win1 %win2
  filter -fw $"(%file) %win2 *.isprofile 1
  while ($line(%win2,0)) {
    %l = $replace($gettok($line(%win2,1),1,32),$chr(254),$chr(32))
    if (*.isprofile iswm %l) { aline %win1 $deltok(%l,$numtok(%l,46),46) }
    dline %win2 1
  }
  close -@ %win2
}
alias _botprofiles.refresh {
  var %d = botprofiles
  did -o %d 157 1 Requires $iif($did(%d,105).sel == 1,Flag:,Level:)
  if ($did(%d,101).sel) {
    did -e %d 103,105,151,152
    if ($did(%d,151).sel) {
      did -e %d 153,154,155,156,157
      did $iif($did(%d,157).state,-e,-b) %d 158
    }
    else { did -b %d 153,154,155,156,157,158 }
  }
  else { did -b %d 103,105,151,152,153,154,155,156,157,158 }
}
alias _botprofiles.selectprofile {
  var %file = $_botprofilesfile,%d = botprofiles,%win = @__selectbotprofile,%l,%profile,%t,%line
  did -c %d 105 1
  did -u %d 157
  did -r %d 151,156,158
  _dialog.add %d 154 /msg
  _dialog.add %d 155 <BOT>
  if ($1- == $null) { return }
  _openwins %win
  filter -fw $"(%file) %win $replace($1-,$chr(32),$chr(254)) $+ .* *
  while ($line(%win,0)) {
    %l = $replace($gettok($line(%win,1),1,32),$chr(254),$chr(32))
    %profile = $deltok(%l,$numtok(%l,46),46)
    %t = $gettok(%l,$numtok(%l,46),46)
    %line = $gettok($line(%win,1),2-,32)
    if (%profile == $1-) {
      if (%t == flagsorlevels) { did -c %d 105 $iif(%line isnum 1-2,%line,1) }
      elseif (_* iswm %t) { did -a %d 151 $right(%t,-1) }
    }
    dline %win 1
  }
  close -@ %win
  if ($did(%d,151).lines) {
    did -c %d 151 1
    _botprofiles.selectcommand $did(%d,151,$did(%d,151).sel)
  }
  write -s $+ $replace($1-,$chr(32),$chr(254)) $+ .flagsorlevels $"(%file) $replace($1-,$chr(32),$chr(254)) $+ .flagsorlevels $did(%d,105).sel
  _botprofiles.refresh
}
alias _botprofiles.selectcommand {
  var %file = $_botprofilesfile,%d = botprofiles,%win = @__selectbotprofilecommand,%l,%profile,%profile2,%t,%line,%i
  did -u %d 157
  did -r %d 156,158
  if ($1- == $null) { return }
  _openwins %win
  %profile = $did(%d,101,$did(%d,101).sel)
  filter -fw $"(%file) %win $replace(%profile,$chr(32),$chr(254)) $+ ._ $+ $1 *
  while ($line(%win,0)) {
    %l = $replace($gettok($line(%win,1),1,32),$chr(254),$chr(32))
    %profile2 = $deltok(%l,$numtok(%l,46),46)
    %t = $gettok(%l,$numtok(%l,46),46)
    %line = $gettok($line(%win,1),2-,32)
    if (%profile == %profile2) && (_* iswm %t) && ($right(%t,-1) == $1) {
      tokenize 32 %line
      if ($5- != $null) { did -o %d 156 1 $5- }
      did $iif($1,-c,-u) %d 157
      if ($2 != $null) { did -o %d 158 1 $2 }
      _dialog.add %d 154 $3
      _dialog.add %d 155 $4
      break
    }
    dline %win 1
  }
  close -@ %win
  _botprofiles.refresh
}
alias _botprofilesfile { return $_chfile($_usrdir(botprofiles.ini)) }
alias _isbotprofile {
  var %file = $_botprofilesfile,%t = $read -ns $+ $replace($1-,$chr(32),$chr(254)) $+ .isprofile %file
  return $iif(%t,1,0)
}
alias _botprofiles.addp {
  var %file = $_botprofilesfile,%d = botprofiles
  tokenize 32 $replace($_parameter($null,1,Bot profiles [/botprofiles],normal,$null,Bot profile to add:),$chr(254),$chr(32))
  if ($1- == $null) || ($dialog(%d) == $null) { return }
  if ($_not($isbotprofile($1-))) { write $"(%file) $replace($1-,$chr(32),$chr(254)) $+ .isprofile 1 }
  _dialog.add %d 101 $1-
  _botprofiles.selectprofile $1-
}
alias _botprofiles.delp {
  var %file = $_botprofilesfile,%d = botprofiles,%profile = $1-,%profilef = $replace(%profile,$chr(32),$chr(254)),%command = $did(%d,151,$did(%d,151).sel),%sel,%win = @__botprofiledelprofile,%l,%profile2,%t,%line,%sel
  if ($dialog(%d) == $null) || (%profile == $null) { return }
  _openwins %win
  filter -fw $"(%file) %win %profilef $+ .* *
  while ($line(%win,0)) {
    %l = $replace($gettok($line(%win,1),1,32),$chr(254),$chr(32))
    %profile2 = $deltok(%l,$numtok(%l,46),46)
    %t = $gettok(%l,$numtok(%l,46),46)
    %line = $gettok($line(%win,1),2-,32)
    if (%profile == %profile2) { write -ds $+ " $+ $line(%win,1) $+ " $"(%file) }
    dline %win 1
  }
  %sel = $did(%d,101).sel
  did -d %d 101 %sel
  if ($did(%d,101).lines) {
    if (%sel <= $ifmatch) { did -c %d 101 $ifmatch }
    else { did -c %d 101 1 }
    _botprofiles.selectprofile $did(%d,101,$did(%d,101).sel)
  }
  close -@ %win
}
alias _botprofiles.addc {
  var %file = $_botprofilesfile,%d = botprofiles,%profile = $1-,%profilef = $replace(%profile,$chr(32),$chr(254)),%sel
  tokenize 32 $_selectedit($null,1,Bot profiles [/botprofiles],1,$null,32,$_bot.defaultcommands,Command to add:)
  if ($1- == $null) || ($dialog(%d) == $null) { return }
  %sel = $did(%d,151).sel
  _dialog.add %d 151 $1-
  _botprofiles.selectcommand $1-
  _botprofiles.savecommand
  _botprofiles.refresh
}
alias _botprofiles.delc {
  var %file = $_botprofilesfile,%d = botprofiles,%profile = $1-,%profilef = $replace(%profile,$chr(32),$chr(254)),%command = $did(%d,151,$did(%d,151).sel),%sel,%win = @__botprofiledelcommand,%l,%profile2,%t,%line,%sel
  if ($dialog(%d) == $null) || (%profile == $null) || (%command == $null) { return }
  _dummy $read -ns $+ %profilef $+ ._ $+ %command %file
  if ($readn) { write -dl $+ $ifmatch $"(%file) }
  %sel = $did(%d,151).sel
  did -d %d 151 %sel
  if ($did(%d,151).lines) {
    if (%sel <= $ifmatch) { did -c %d 151 $ifmatch }
    else { did -c %d 151 1 }
    _botprofiles.selectcommand $did(%d,151,$did(%d,151).sel)
  }
}
; op deop opme deopme voice voiceme devoice devoiceme
; kick kickban bankick
; invite
; unbanall unban ban
; mkick,masskick,mk mdeop,massdeop mdevoice,massdevoice
; topic mode
; say act
;
alias bot { _bot $1- }
alias _bot {
  var %title = Use bot [/bot]
  tokenize 32 $1-
  tokenize 32 $_selectedit($1,1,%title,1,$1,32,$_bot.defaultcommands,Command:) $2-
  if ($istok($_bot.defaultcommands.chan,$1,32)) {
    tokenize 32 $1 $_parameter($iif($_ischan($2),#$2,$chan),1,%title,ischan,$null,Channel:) $iif($_ischan($2),$3-,$2-)
    %title = Use bot [/bot] ( $+ $2 $+ )
    if ($3- == $null) { tokenize 32 $1-2 }
  }

  if ($1 == invite) && ($3 == $null) { tokenize 32 $1-2 $me }
  if ($1 == kick) { tokenize 32 $1-2 $_parameter($3,1,%title,1,$null,User to kick:) $4- }
  if ($1 == mode) { tokenize 32 $1-2 $_parameter($3-,1,%title,normal,$gettok($chan($2).mode,1,32),Channel mode:) }
  if ($1 == topic) { tokenize 32 $1-2 $_parameter($3-,1,%title,normal,$chan($2).topic,Channel topic:) }
  if ($istok(say act,$1,32)) { tokenize 32 $1-2 $_parameter($3-,1,%title,normal,$null,Message:) }
  if ($istok(op deop voice devoice,$1,32)) { tokenize 32 $1-2 $_parameter($3-,1,%title,normal,$null,User to [ $1 ] :) }
  if ($1 == unban) {
    if ($4 == $null) { tokenize 32 $1-2 $_parameter($3,1,%title,1,$null,Unban:) $4- }
  }
  if ($1 == ban) {
    if ($_not($_isint($3))) { tokenize 32 $1-2 0 $3- }
    tokenize 32 $1-3 $_parameter($4,1,%title,1,$null,Ban:) $5-
  }
  if ($istok(op invite unban voice,$1,32)) && ($3 == $me) { tokenize 32 $1 $+ me $2 $4- }
  _bot.use 1 $1-
}
alias _bot.defaultcommands { return $_bot.defaultcommands.chan $_bot.defaultcommands.bot }
alias _bot.defaultcommands.chan { return $_bot.defaultcommands.chan.isop $_bot.defaultcommands.chan.ison }
alias _bot.defaultcommands.chan.isop { return opme deopme op deop voice devoice voiceme devoiceme kick kickban bankick unbanall unbanme unban ban mkick masskick mk mdeop massdeop mdevoice massdevoice topic mode }
alias _bot.defaultcommands.chan.ison { return invite inviteme say act }
alias _bot.defaultcommands.bot { return auth login }

; returns 1 on error
alias _bot.use {
  var %showerrors = $1
  tokenize 32 $2-
  _bot.use.service $1-
  if ($result == done) { return 0 }
  _bot.use.bots $1-
  if ($result == done) { return 0 }
  if (%showerrors) { error.a No bot/service to use the command $^'($1) }
  return 1
}
; /_bot.use.bots2win <channel> <window>
alias _bot.use.bots2win {
  var %win = $1,%chan = $2,%handle,%file = $_userinifile,%i = $_countusers,%flags
  _openwins %win
  while (%i) {
    var %handle = $_ruini($ini(%file,%i),handle),%flags = $_nickflags(%handle,%chan)
    if (b isincs %flags) && (B isincs %flags) { aline %win %handle }
    dec %i
  }
}
alias _bot.use.bots {
  var %file = $_botprofilesfile,%win = @__botusebots,%handle,%chan = $2,%botnick,%profile,%profilef,%command = $1,%commandl,%reqflags,%flags,%uselevels,%rcommand,%rtarget,%rline,%botpass,%done = 0,%botison = 0,%botisop = 0,%the_end = 0,%me_ison = $iif($me ison %chan,1,0),%failed,%reqison = $istok($_bot.defaultcommands.chan.ison,%command,32),%reqisop = $istok($_bot.defaultcommands.chan.isop,%command,32),%dummy = $_bot.use.bots2win(%win,%chan),%reqchaninparm = $istok($_bot.defaultcommands.chan,%command,32)
  while ($line(%win,0)) {
    %handle = $line(%win,1)
    %profile = $_getuserfield(%handle,botprofile)
    %profilef = $replace(%profile,$chr(32),$chr(254))
    %botison = 0
    %botisop = 0
    %the_end = 0
    %failed = 0
    if ($_isbotprofile(%profile)) {
      %botnick = $_hand2nick(%handle,*,1)
      if (%botnick != $null) {
        if (%botnick ison %chan) { %botison = 1 }
        if (%botnick isop %chan) { %botisop = 1 }
        if (%botisop) { %the_end = 1 }
        elseif (%reqisop) { %failed = 1 }
        if (%botison) {
          if ($_not(%reqops)) { %the_end = 1 }
        }
        elseif (%reqison) { %failed = 1 }
        if (%me_ison == 0) || (%failed == 0) {
          %flags = $_getuserfield(%handle,$_nospaces(botaccess.,%chan)) $+ $_getuserfield(%handle,botaccess)
          if (%flags == $null) { var %flags }
          %uselevels = $read -ns $+ %profilef $+ .flagsorlevels %file
          %commandl = $read -ns $+ %profilef $+ ._ $+ %command %file
          if (%commandl != $null) && ( ($_not($gettok(%commandl,1,32))) || ($gettok(%commandl,2,32) == $null) || ( (%uselevels == 2) && ($gettok(%commandl,1,32)) && ($_isint(%flags)) && ($round(%flags,0) >= $round($gettok(%commandl,2,32),0)) ) || ( (%uselevels != 2) && ($gettok(%commandl,1,32)) && ($gettok(%commandl,2,32) != $null) && ($gettok(%commandl,2,32) isincs %flags) ) ) {
            %rcommand = $gettok(%commandl,3,32)
            %rtarget = $gettok(%commandl,4,32)
            %rline = $gettok(%commandl,5-,32)
            if (%rcommand == /ctcp) { %rcommand = _qctcp }
            elseif (%rcommand == /ctcpreply) { %rcommand = _qctcpreply }
            elseif (%rcommand == /describe) { %rcommand = _qaction }
            elseif (%rcommand == /notice) { %rcommand = _qnotice }
            else { %rcommand = _qprivmsg }

            if (%rtarget == <CHAN>) { %rtarget = %chan }
            else { %rtarget = %botnick }
            %botpass = $_decrypt($_getuserfield(%handle,botpass))
            if (<PASS> !isin %rline) || ((<PASS> isin %rline) && (%botpass != $null)) {
              if (%command == ban) { %rline = $replace(%rline,<DURATION>,$3,<BANMASK>,$4,<REASON>,$5-) }
              if (%command == unban) { %rline = $replace(%rline,<BANMASK>,$3,<NICK>,$4) }
              if (%command == unbanme) { %rline = $replace(%rline,<BANMASK>,$3,<NICK>,$me) }
              %rline = $replace(%rline,<BOT>,%botnick,<ME>,$me)
              if (%reqchaninparm) { %rline = $replace(%rline,<CHAN>,%chan,<NICK>,$3,<REASON>,$4-,<NICKS>,$3-,<PARMS>,$3-,<MSG>,$3-) }
              else { %rline = $replace(%rline,<NICK>,$2,<REASON>,$3-,<NICKS>,$2-,<PARMS>,$2-,<MSG>,$2-) }
              if ($show) { lechol.a $colour(info) $^bot(%botnick,%handle,%chan,%profile,$replace(%rline,<PASS>,$str(*,$len(%botpass)))) }
              %rline = $replace(%rline,<PASS>,%botpass)
              %rcommand %rtarget %rline
              if (%the_end) {
                close -@ %win
                return done
              }
              %done = 1
            }
          }
        }
      }
    }
    dline %win 1
  }
  close -@ %win
  if (%done) { return done }
  return
}
alias _bot.use.service {
  var %alias
  if (%.services != $null) {
    var %alias = _service. $+ %.services $+ .command
    if ($isalias(%alias)) {
      $iif($_not($show),.) $+ %alias $1-
      ;  ERR_nochan
      ;  ERR_nobot
      ;  ERR_nopass
      ;  ERR_command
      ;  ERR_invite2user user
      ;  ERR_banbadmask mask
      ;  ERR_unbanbadmask mask
      if ($result == $null) { return done }
    }
  }
  return
}
alias _usebotallchans {
  if ($_disconnected) { return }
  var %i = $chan(0),%chan,%t
  if (%i == 0) {
    error.a You are not in any channel
    return
  }
  while (%i) {
    var %chan = $chan(%i)
    if ($me !isop %chan) {
      _bot.use 0 $replace($1-,<CHAN>,%chan)
      if ($_not($result)) { %t = %t %chan }
    }
    dec %i
  }
  if (%t != $null) { info.a Requesting ops in: %t }
  else { error.a No bots found to request ops in any channel or you are already chanop }
}