Zion Sphinx — File Browser

ZionSphinxv3.0 / ZionSphinxv3.0 / plugins / zion_friends.mrc

zion_friends.mrc — 25.68 KB — Download this file

; --------< Zion Plugin >------------------------------------------
; Name....: Friends
; Ver.....: 0.5b070407
; Author..: sprion
; Email...: sprion@lyniq.com
; Date....: 7 Apr 07
; URL.....: http://zion.lyniq.com
; Info....: A system to manage friends
; Changes.: Fully translatable
;---------------------------------------------------------------------
;translatable
; ---- BEGIN ----

; script unload signal
on *:signal:zion_unload:.unload -rs $+(",$script,")

; when LIST is created event. only happens once usually..
;on *:signal:zion_winmade:.timerzion.s.crcchk 1 5 z.crcchk | z.antiworms
on *:signal:zion_start:z.authdo zion_friends_start
on *:signal:zion_end:press.omode 1 -cf | _history end [me] $me | if ($hget($saveid)) hfree $saveid | _load.friends.hash | .timerzion.s.friends.refresh off


on *:signal:zion_deauth:hfree -w $+($saveid,_,$1,*) | if ($dialog(zion_friends)) xdialog -x zion_friends
/*
on *:signal:zion_connect2game:{
}
*/


; from zionle3
on *:signal:zion.le.ingame:if (($gettok($z.h.getvar(friends_tmode),1,32) != 5) && ($z3.findtog($saveid,omode) != 4)) { z.comms.send -iof $saveid omode $z3.findtog(auth,name) 5 $1- | z.h.setvar friends_tmode 5 $1- | _history omode [me] $me 5 $1- }
on *:signal:zion.le.endgame:if ($gettok($z.h.getvar(friends_tmode),1,32) = 5) { z.comms.send -of $saveid endgame $z3.findtog(auth,name) $1- | _history endgame [me] $me | press.omode $z3.findtog($saveid,omode,2) -cf }

; away
on *:signal:zion_check:{
  if (($away) && (!$z.h.getvar(friends_away))) { if ($z3.findtog($saveid,omode) != 4) z.comms.send -iof $saveid omode $z3.findtog(auth,name) 3 | z.h.setvar friends_away 1 | z.h.setvar friends_tmode 3 | _history omode [me] $me 3 }
  elseif ((!$away) && ($z.h.getvar(friends_away))) { press.omode $z3.findtog($saveid,omode,2) -cf | z.h.unsetvar friends_away }
}

alias zion_friends_start {
  z.comms.send -iof $saveid start $z3.findtog(auth,name)
  _history start [me] $me | press.omode $z3.findtog($saveid,omode,2) -cf | .timerzion.s.friends.refresh 1 300 _refresh
}
alias z3.aft {
  if ($1 = -skipnull) {
    var %n = 1
    if ($4- = $null) return
    tokenize 32 $2-
  }
  elseif ($1 = -nullto0) {
    var %n = 1
    if ($4- = $null) tokenize 32 $2-3 0
  }
  var %n $z3.findtog(auth,name),%v = $+(zion_friends_,%n,_,$1),%d = $3-,%l,%z = $+(",$z.sysfolder,%n,_friends.dat,")
  if (%d = $null) %d = nullstring
  if (!$hget(%v)) {
    hmake %v 5
    %l = $decompress(%z)
    hload -i %v %z $1
    %l = $compress(%z)
  }
  hadd %v $2 $z3.writecodes(%d)
  %l = $decompress(%z)
  writeini %z $1 $2 $z3.writecodes(%d)
  %l = $compress(%z)
}
alias z3.dft {
  var %n $z3.findtog(auth,name),%v = $+(zion_friends_,%n,_,$1),%d,%z = $+(",$z.sysfolder,%n,_friends.dat,")
  if ($hget(%v,$2)) hdel %v $2
  %d = $decompress(%z)
  remini %z $1 $2
  %d = $compress(%z)
}
alias z3.fft {
  var %n $z3.findtog(auth,name),%v = $+(zion_friends_,%n,_,$1),%r,%z = $+(",$z.sysfolder,%n,_friends.dat,")
  if (!$hget(%v)) {
    hmake %v 5
    var %d $decompress(%z)
    hload -i %v %z $1
    %d = $compress(%z)
  }
  %r = $z3.readcodes($hget(%v,$2))
  if (($3) && (%r = $null)) {
    z3.aft $1-
    return $3
  }
  if (%r = nullstring) %r = $null
  return %r
}

on *:load:{
  z.note $z.head(plugin) $ $nopath($script) $z.lang(sys,loaded)
  .signal -n zion_plugin_load $script
  z.echo $z.head(notice) $z.lang(friends,loadmsg)
  z.authdo zion_friends_load
}
alias zion_friends_load {
  var %g $z3.fft(main,gno)
  if (%g = $null) _group.defaults
}
on *:unload:if ($dialog(zion_friends)) xdialog -x zion_friends | .timerzion.s.friends.refresh off | .signal -n zion_plugin_unload $script | hfree -w $+($saveid,*)

; provide userinfo information & cmd of this plugin
on *:signal:zion_userinfo.requestver:zion.user.adddetails $z.fileinfo($script,Name) $ $z.fileinfo($script,Ver)
on *:signal:zion_userinfo.showplug:{
  if ($3- = $z.fileinfo($script,Name)) {
    var %v = $z3.fft(friends,$1)
    if (!%v) .signal zion_userinfo.addbutton $z.lang(friends,addfriend) $ zion.addfriend $2
    else .signal -n zion_userinfo.setname $gettok(%v,2,36)
  }
}

;gamelist3
on *:signal:zion_gamelist3_made:{
  .timer 1 0 z.agl.addbutton 2 6 $z.fileinfo($script,Name)
}
on *:signal:zion_gamelist3_hitbutton:{
  if ($2- = $z.fileinfo($script,Name)) z.authdo zion_friends
}
; END plugin conforming.


alias -l saveid return zion_friends

alias -l _refresh {
  z.comms.send -iof $saveid refresh $z3.findtog(auth,name)
  if ($dialog(zion_friends)) xdialog -x zion_friends
  .timerzion.s.friends.refresh 1 300 _refresh
}
alias zion_friends {
  if ($dialog(zion_friends)) { xdialog -rs zion_friends | _sb.update }
  else dialog -m zion_friends zion_friends_table
}

dialog zion_friends_table {
  title $z.lang(friends,wintitle)
  size -1 -1 167 248
}

on *:dialog:zion_friends:*:*: {
  if ($devent == init) {
    dll_mc SetIcon -w $dialog($dname).hwnd 13 $z.icl
    dcx Mark $dname zion_friends_cb
    xdialog -b $dname +ntyz
    
    ;// Call initilisation alias
    zion_friends_init_dcx
  }
}

alias -l zion_friends_init_dcx {
  var %d $dname
  ;  var %bg2_ $z.options.bg2_
  var %bg2_ $rgb(255,255,255)
  ;// Initialising control: (Toolbar 1)
  xdialog -c $dname 1 toolbar 0 0 195 28 top tooltips arrows tabstop list
  xdid -l $dname 1 16
  xdid -m $dname 1 0
  
  ; adding icons
  xdid -w %d 1 +nhd 12 $z.icl
  xdid -w %d 1 +nhd 15 $z.icl
  xdid -w %d 1 +nhd 16 $z.icl
  xdid -w %d 1 +nhd 21 $z.icl
  
  
  ; adding items
  xdid -a %d 1 0 +lbc 22 2 0 $chr(9) $z.lang(friends,listall)
  xdid -a %d 1 0 +dlbc 22 1 0 $chr(9) $z.lang(friends,listonline)
  xdid -a %d 1 0 +dlbc 22 3 0 $chr(9) $z.lang(friends,listgaming)
  xdid -a %d 1 0 +lbc 22 4 0 $chr(9) $z.lang(friends,showhistory)
  
  
  ;  xdid -a %d 1 0 +abc 0 1 0 hello $chr(9) hello
  ;  xdid -a %d 1 0 +abc 0 2 0 world $chr(9) hello
  
  
  ;// Initialising control: (TreeView 2)
  xdialog -c $dname 2 treeview 0 28 175 197 tabstop nohscroll notooltips editlabel
  
  xdid -i %d 2 +b $rgb(18,61,102)
  xdid -f %d 2 +a ansi 8 Tahoma
  
  ;linesatroot hasbuttons
  xdid -l $dname 2 16
  
  xdid -w %d 2 +ns 13 $z.icl
  xdid -w %d 2 +ns 12 $z.icl
  xdid -w %d 2 +ns 17 $z.icl
  xdid -w %d 2 +ns 14 $z.icl
  xdid -w %d 2 +ns 16 $z.icl
  xdid -w %d 2 +ns 18 $z.icl
  xdid -w %d 2 +ns 19 $z.icl
  xdid -w %d 2 +ns 20 $z.icl
  
  
  ;// Initialising control: (Statusbar 3)
  xdialog -c $dname 3 statusbar 0 225 163 23 grip tooltips
  
  xdid -w %d 3 + 13 $z.icl
  xdid -w %d 3 + 12 $z.icl
  xdid -w %d 3 + 17 $z.icl
  xdid -w %d 3 + 14 $z.icl
  
  xdid -l %d 3 80 -1
  
  
  xdid -t %d 3 2 +p 0 0/0 Online $chr(9) haha
  
  
  xdialog -l $dname root $chr(9) +pv 0 0 0 0
  
  ;  xdialog -l $dname cell root $chr(9) +pv 0 1 0 0
  
  
  ; adding into top pane 1
  xdialog -l $dname cell root $chr(9) +fvi 1 0 0 28
  
  xdialog -l $dname cell root $chr(9) +li 2 1 0 0
  
  xdialog -l $dname cell root $chr(9) +fvi 3 0 0 23
  ;  xdialog -l $dname space 1 1 $chr(9) + 5 0 0 0
  
  .timer 1 0 xdialog -l $dname update
  
  
  _loadsettings $dname
}

alias -l _loadsettings {
  var %v
  
  %v = $z3.findtog($saveid,dmode,a)
  if (%v = a) press.dmode $1 1
  elseif (%v = o) press.dmode $1 2
  elseif (%v = g) press.dmode $1 3
  
  press.omode $z3.findtog($saveid,omode,2) -f
  
  ;people
  .timer 1 0 _load.friends.hash
  
}
alias -l press.dmode {
  xdid -t $1 1 1 +d
  xdid -t $1 1 2 +d
  xdid -t $1 1 3 +d
  xdid -t $1 1 $2 +p
  
  if ($3 = -p) {
    if ($2 = 1) z3.addtog $saveid dmode a
    elseif ($2 = 2) z3.addtog $saveid dmode o
    elseif ($2 = 3) z3.addtog $saveid dmode g
  }
}

alias -l _group.defaults {
  var %a z3.aft main
  %a gno 4
  %a g1 id1
  %a g2 id2
  %a g3 id3
  %a g4 id4
  %a id1 General
  %a id2 $z.lang(friends,classmates)
  %a id3 $z.lang(friends,coworkers)
  %a id4 $z.lang(friends,clanmates)
  if ($1) $1-
}
alias -l _load.friends.hash {
  var %g $z3.fft(main,gno),%n 1,%d zion_friends,%bg2_ $rgb(255,255,255),%f $z3.fft(friends,0),%z <[!@#]>
  if (!$dialog(%d)) return
  if (%g = $null) { _group.defaults _load.friends.hash | return }
  xdid -r %d 2
  while (%n <= %g) {
    dll_wfix WhileFix
    if ($z3.fft(main,$+(g,%n))) {
      var %id $ifmatch,%idn = $z3.fft(main,%id)
      xdid -a %d 2 $+(%n,$chr(9),+bce 8 8 0 0 0 %bg2_ 0 %idn,$chr(9),<group> %idn %id $+(g,%n))
    }
    inc %n
  }
  
  _sb.update
  
  
  %n = 1
  
  if (!%f) z3.aft friends 0 %z
  
  %f = $hget($+(zion_friends_,$z3.findtog(auth,name),_,friends),0).item
  
  while (%n <= %f) {
    dll_wfix WhileFix
    var %i = $hget($+(zion_friends_,$z3.findtog(auth,name),_,friends),%n).item,%da = $hget($+(zion_friends_,$z3.findtog(auth,name),_,friends),%i)
    if (%da != %z) _tv.addfriend $iif($hget($saveid,%i),-mode $ifmatch) $hget($+(zion_friends_,$z3.findtog(auth,name),_,friends),%n).item %da
    ;xdid -a %d 2 $+($_tv.findidpath($gettok(%da,1,36)) -1,$chr(9),+c 1 1 0 0 0 %bg2_ $gettok(%da,2,36),$chr(9),$hget($+(zion_pvars_,$+($saveid,_h)),%n).item %da)
    inc %n
  }
  
}
alias _sb.update {
  if (!$dialog(zion_friends)) return
  var %t $calc($hget($+(zion_friends_,$z3.findtog(auth,name),_,friends),0).item -1),%n $hget($saveid,0).item,%i,%o 0
  while (%n > 0) {
    dll_wfix WhileFix
    %i = $hget($saveid,%n).item
    if ((_nick !isin %i) && (_inIP !isin %i)) {
      if ($hget($saveid,%i) = 2) inc %o
    }
    dec %n
  }
  xdid -t zion_friends 3 2 +p 0 $+(%o,/,%t) Online $chr(9) haha
}
alias _hash.addfriend {
  var %n = $z3.fft(friends,$1),%d zion_friends
  if (%n) { z.echo $z.head(notice) $z.lang(friends,useralreadyon) | return }
  z3.aft friends $1 $+(id1,$chr(36),$2-)
  if ($dialog(%d)) _tv.addfriend $1 $+(id1,$chr(36),$2-)
}
alias -l _tv.addfriend {
  var %m 1
  if ($1 = -mode) { %m = $2 | tokenize 32 $3- }
  xdid -a zion_friends 2 $+($_tv.findidpath($gettok($2-,1,36)) -1,$chr(9),+c %m %m 0 0 0 $rgb(255,255,255) 0 $gettok($2-,2,36) $iif($hget($saveid,$+($1,_nick)),$+($chr(40),$ifmatch,$chr(41))),$chr(9),$1-)
}
alias _tv.findidpath {
  var %d zion_friends,%n = $xdid(%d,2,root).num
  while (%n > 0) {
    if ($gettok($xdid(%d,2,%n).tooltip,3,32) = $1) return %n
    dec %n
  }
  return -1
}
;// Callback alias for zion_friends
alias zion_friends_cb {
  
  /*
  if ($2 != mouse) {
    echo $color(info) -s * zion_friends_cb: $1-
  }
  */
  
  if ($2 = sclick) {
    if ($3 = 1) {
      if ($4 isnum 1-3) {
        press.dmode $1 $4 -p
      }
      elseif ($4 = 4) zion_friends_history
    }
    elseif ($3 = 3) {
      if ($4 = 1) z.friends.sb.menumake
    }
  }
  elseif ($2 = rclick) {
    if ($3 = 2) {
      if ($4) {
        var %t $xdid($1,2,$4-).tooltip
        if ($left(%t,7) = <group>) z.friends.tv.g.menumake
        else z.friends.tv.f.menumake
      }
    }
  }
  elseif ($2 = expand) {
    if ($3 = 2) {
      xdid -j $1 $3 $4 $chr(9) 8 8
    }
  }
  elseif ($2 = collapse) {
    if ($3 = 2) {
      xdid -j $1 $3 $4 $chr(9) 7 7
    }
  }
  elseif ($2 = labelbegin) {
    if ($3 = 2) {
      var %t $xdid($1,2,$xdid($1,2).selpath).tooltip
      tokenize 32 %t
      if (($1 != <group>) || ($3 = id1)) return noedit
    }
  }
  elseif ($2 = labelend) {
    if ($3 = 2) {
      var %t $xdid($1,2,$xdid($1,2).selpath).tooltip,%d $4-
      tokenize 32 %t
      if (%d = General) return noedit
      z3.aft main $3 %d
    }
  }
  elseif ($2 = close) {
    if ($z.started) {
      xdialog -h $1
      return noclose
    }
  }
  
}



alias z.friends.sb.menumake {
  var %v zion.friends.sb
  if (!$xpopup(%v).ismenu) {
    xpopup -c %v normal
    xpopup -i %v 13 $z.icl
    xpopup -i %v 12 $z.icl
    xpopup -i %v 17 $z.icl
    xpopup -i %v 14 $z.icl
    
    
    ;    xpop -a %v 1 $chr(9) + 1 1 $modename(1)
    xpop -a %v 2 $chr(9) + 2 2 $modename(2)
    ;    xpop -a %v 3 $chr(9) + 3 3 $modename(3)
    xpop -a %v 4 $chr(9) + 4 4 $modename(4)
    
  }
  ; modify which checked
  press.menu %v
  
  ;  xpopup -s %v +mlt $calc($dialog(zion_friends).x + 3) $calc(153 + $dialog(zion_friends).y)
  xpopup -s %v +mlt $calc($dialog(zion_friends).x + 3) $calc(197 + $dialog(zion_friends).y)
}

on *:signal:xpopup-zion.friends.sb:{
  if ($1 isnum 1-4) press.omode $1 -cs
}
alias -l press.menu {
  var %n 2,%s $calc($z3.findtog($saveid,omode,2) /2)
  while (%n > 0) {
    xpop -s $1 %n $chr(9) $iif(%n = %s,+c,+)
    dec %n
  }
}
/*
alias -l press.menu {
  var %n 4,%s $z3.findtog($saveid,omode,2)
  while (%n > 0) {
    xpop -s $1 %n $chr(9) $iif(%n = %s,+c,+)
    dec %n
  }
}
*/
alias -l modename {
  if ($1 = 1) return $z.lang(friends,offline)
  elseif ($1 = 2) return $z.lang(friends,online)
  elseif ($1 = 3) return $z.lang(friends,na)
  elseif ($1 = 4) return $z.lang(friends,invisible)
  elseif ($1 = 5) return $z.lang(friends,ingame)
}
alias -l press.omode {
  var %o = $z3.findtog($saveid,omode,2),%d zion_friends,%m
  if ($1 = 4) {
    if ($z3.findtog(personal,inv)) z.reg.ret
    else { %o = $input($z.lang(err,1),odw,$z.lang(err,2)) | return }
  }
  if ((-*f* !iswm $2) && (%o = $1)) return
  if (-*s* iswm $2) { z3.addtog $saveid omode $1 | %o = $1 }
  if (-*c* iswm $2) { %m = $replace($1,4,1) | z.comms.send -iof $saveid $iif(-*r* iswm $2,rmode,omode) $z3.findtog(auth,name) %m $z3.findtog(auth,name) | z.h.setvar friends_tmode %m | if (-*r* !iswm $2) _history omode [me] $me %m }
  if ($dialog(%d)) xdid -t %d 3 1 +p $1 $modename($1) $chr(9) haha
}
alias -l _tv.findhpath {
  var %d zion_friends,%l = $z3.fft(friends,$1),%id $gettok(%l,1,36),%n $gettok(%l,2,36),%t,%p
  ; finding friend item
  %t = $xdid(%d,2, $chr(9) $+(%n,*) $chr(9), W, 0,$_tv.findidpath(%id)).find
  while (%t > 0) {
    %p = $xdid(%d,2, $chr(9) $+(%n,*) $chr(9), W,%t,$_tv.findidpath(%id)).find
    ; if found ..
    if ($gettok($xdid(%d,2,%p).tooltip,1,32) = $1) return %p
    dec %t
  }
  return $false
}
alias -l _set.mode {
  var %d zion_friends,%h $4-,%id $gettok(%h,1,36),%n $gettok(%h,2,36),%p
;  hadd -m $saveid $1 $2
;300
  hadd -mu330 $saveid $1 $2
;  hadd -m $saveid $+($1,_nick) $3
  hadd -mu330 $saveid $+($1,_nick) $3
  
  if ($dialog(%d)) {
    %p = $_tv.findhpath($1)
    if (%p) {
      xdid -j %d 2 %p $chr(9) $2 $2
      xdid -v %d 2 %p $chr(9) %n $+($chr(40),$3,$chr(41))
    }
    else _tv.addfriend -mode $2 $1 $+(%id,$chr(36),%n)
  }
  
  _sb.update
  ;  _set.nick -p %p $ $1 $3
}
alias -l _hash.name {
  if ($1 = [me]) return $z3.findtog(auth,name)
  elseif ($gettok($z3.fft(friends,$1),2,36)) return $ifmatch
  else return 0
}
alias -l _set.nick {
  var %p 0,%d zion_friends
  if ($1 = -p) { %p = $gettok($2-,1,36) | tokenize 32 $gettok($2-,2,36) }
  
  hadd -mu330 $saveid $+($1,_nick) $2
  if ($dialog(%d)) {
    if (!%p) %p = $_tv.findhpath($1)
    if (%p) xdid -v %d 2 %p $chr(9) $gettok($gettok($xdid(%d,2,%p).tooltip,2-,32),2,36) $+($chr(40),$2,$chr(41))
  }
}
alias -l _history {
  var %f $+(",$scriptdir,history.dat,"),%d
  %d = $decompress(%f)
  if ((($1 = start) && ($2 = [me])) || ($date != $z.h.getvar(friends_lastday))) {
    write %f zstart $time $day $date
  }
  write %f $timestamp $1-
  z.h.setvar friends_lastday $date
  %d = $compress(%f)
}
on *:signal:zion_comms:{
  if ($2 = $saveid) {
    var %h,%r
    if (($3 = omode) || ($3 = rmode)) {
      %h = $z3.fft(friends,$4)
      if (%h) {
        if ($6-) {
          if ($5 = 5) {
            hadd -m $saveid $+($4,_inIP) $6
            z.h.setvar friends_gameips $addtok($z.h.getvar(friends_gameips),$6,36)
            if ($3 = omode) zion.gui.gnote -b $rgb(255,235,101) $z.lang(friends,header) $ $gettok(%h,2,36) ( $+ $1 $+ ) $z.lang(friends,isingame) $ 5
          }
          else {
            %h = $puttok(%h,$6-,2,36)
            z3.aft friends $4 %h
          }
        }
        _set.mode $4 $5 $1 %h
        if ($3 = omode) { _history $3 $4 $1 $5 $6- | if ($5 = 2) zion.gui.gnote -b $rgb(125,255,125) $z.lang(friends,header) $ $gettok(%h,2,36) ( $+ $1 $+ ) $z.lang(friends,isonline) $ 5 }
      }
    }
    elseif (($3 = start) || ($3 = refresh)) {
;      %r = $iif($3 = start,omode,rmode)
      %r = rmode
      %h = $z3.fft(friends,$4)
      if (%h) {
        if ($gettok($z.h.getvar(friends_tmode),1,32) = 5) {
          z.comms.send -of $saveid %r $z3.findtog(auth,name) $z.h.getvar(friends_tmode)
        }
        else z.comms.send -of $saveid %r $z3.findtog(auth,name) $z.h.getvar(friends_tmode) $z3.findtog(auth,name)
        if ($3 = start) {
          _history $3 $4 $1
          z.splay snd_fonline
          zion.gui.gnote -b $rgb(125,255,125) $z.lang(friends,header) $ $gettok(%h,2,36) ( $+ $1 $+ ) $z.lang(friends,isonline) $ 5
        }
      }
      
    }
    elseif ($3 = endgame) {
      %h = $z3.fft(friends,$4)
      if (%h) {
        hdel $saveid $+($4,_inIP)
        z.h.setvar friends_gameips $remtok($z.h.getvar(friends_gameips),$5-,1,36)
        _history $3 $4 $1
      }
    }
    
  }
}

alias z.friends.tv.g.menumake {
  var %v zion.friends.tv.g
  if (!$xpopup(%v).ismenu) {
    xpopup -c %v office2003
    ;    xpopup -i %v 13 $z.icl
    
    
    xpop -a %v 1 $chr(9) +g 1 0 $z.lang(friends,msggroup)
    xpop -a %v 2 $chr(9) + 0 0 -
    xpop -a %v 3 $chr(9) +g 2 0 $z.lang(friends,moveup)
    xpop -a %v 4 $chr(9) +g 3 0 $z.lang(friends,movedown)
    xpop -a %v 5 $chr(9) + 0 0 -
    xpop -a %v 6 $chr(9) + 4 0 $z.lang(friends,delgroup)
    xpop -a %v 7 $chr(9) + 0 0 -
    xpop -a %v 8 $chr(9) + 5 0 $z.lang(friends,createnewgroup)
    
    
  }
  ; modify which checked
  ;  press.menu %v
  
  xpopup -s %v +mlt $mouse.dx $mouse.dy
}

on *:signal:xpopup-zion.friends.tv.g:{
  if ($1 = 4) {
    _tv.select -del
  }
  elseif ($1 = 5) _tv.select -add
}

alias -l _tv.select {
  var %d zion_friends,%p = $xdid(%d,2).selpath,%t = $xdid(%d,2,%p).tooltip,%c $1,%i
  tokenize 32 %t
  if (%c = -del) {
    if ($3 = id1) { %c = $input($z.lang(friends,groupreqprompt),doh,$z.lang(friends,header_groupreqprompt)) | return }
    if ($input($z.lang(friends,delgrp) $+(',$2,') $z.lang(friends,delgrp2),dywuv,$z.lang(options,confirm)) = $yes) {
      z3.dft main $3
      _tv.moveup $+(g,$calc($right($4,-1) +1)) -all
    }
  }
  elseif (%c = -add) {
    %i = $$input($z.lang(friends,entergrpname),eoq,$z.lang(friends,createnewgroup))
    if (%i) {
      var %tmp 2
      while ($z3.fft(main,$+(id,%tmp)) != $null) {
        dll_wfix WhileFix
        inc %tmp
      }
      z3.aft main $+(id,%tmp) %i
      %i = $z3.fft(main,gno)
      inc %i
      z3.aft main $+(g,%i) $+(id,%tmp)
      z3.aft main gno %i
    }
  }
  _load.friends.hash
  xpopup -d zion.friends.tv.f
}

alias -l _tv.moveup {
  var %m $right($1,-1),%a $calc(%m -1),%g $z3.fft(main,gno)
  if (%m = 1) return
  if ($2 = -all) {
    while (%m <= %g) {
      dll_wfix WhileFix
      z3.aft main $+(g,%a) $z3.fft(main,$+(g,%m))
      inc %a
      inc %m
    }
    z3.dft main $+(g,%g)
    z3.aft main gno $calc(%g -1)
  }
}

alias z.friends.tv.f.menumake {
  var %v zion.friends.tv.f
  if (!$xpopup(%v).ismenu) {
    xpopup -c %v office2003
    ;    xpopup -i %v 13 $z.icl
    
    
    xpop -a %v 1 $chr(9) +g 1 0 $z.lang(friends,msgfriend)
    xpop -a %v 2 $chr(9) + 0 0 -
    
    xpop -a %v 3 $chr(9) + 2 0 $z.lang(friends,joinfriend)
    
    xpop -a %v 4 $chr(9) + 3 0 $z.lang(friends,viewinfo)
    xpop -a %v 5 $chr(9) + 4 0 $z.lang(friends,moveto)
    xpop -a %v 6 $chr(9) + 5 0 $z.lang(friends,deletefriend)
    
    xpop -c %v 5
    var %g $z3.fft(main,gno),%n 1
    if (%g = $null) { _group.defaults _load.friends.hash | return }
    
    while (%n <= %g) {
      dll_wfix WhileFix
      if ($z3.fft(main,$+(g,%n))) {
        xpop -a %v 5 %n $chr(9) + $calc(100 + %n) 0 $z3.fft(main,$ifmatch)
      }
      inc %n
    }
    
  }
  
  ; modify which checked
  ;  press.menu %v
  
  xpopup -s %v +mlt $mouse.dx $mouse.dy
}

on *:signal:xpopup-zion.friends.tv.f:{
  if ($1 = 5) _tv.select.f -del
  elseif ($1 = 3) _tv.select.f -userinfo
  elseif ($1 = 2) _tv.select.f -joingame
  if ($1 > 100) {
    _tv.select.f -move $+(g,$calc($1 -100))
  }
}

alias -l _tv.select.f {
  var %d zion_friends,%p = $xdid(%d,2).selpath,%t = $xdid(%d,2,%p).tooltip,%c $1,%n,%pr $2-
  tokenize 32 %t
  if (%c = -del) {
    %n = $gettok($2,2,36)
    if ($input($z.lang(friends,removeprompt) $+(',%n,'?),dywuv,$z.lang(options,confirm)) = $yes) {
      z3.dft friends $1
      xdid -d %d 2 %p
      ;      _tv.moveup $+(g,$calc($right($4,-1) +1)) -all
    }
  }
  elseif (%c = -move) {
    var %id = $z3.fft(main,%pr)
    if (%id = $gettok($2,1,36)) return
    %n = $+(%id,$chr(36),$gettok($2,2,36))
    z3.aft friends $1 %n
    _load.friends.hash
    ;    xdid -o %d 2 %p $chr(9) %n
    ;    xdid -m %d 2 %p $chr(9) $_tv.findidpath(%id) -1
  }
  elseif (%c = -userinfo) {
    if ($hget($saveid,$1) = 2) { if ($hget($saveid,$+($1,_nick))) zion.userinfo $ifmatch }
    else z.echo $z.head(err) $z.lang(friends,useroffline)
  }
  elseif (%c = -joingame) {
    if ($hget($saveid,$+($1,_inIP))) z.connect $ifmatch
    else z.echo $z.head(err) $z.lang(friends,usernotingame)
  }
  ;  _load.friends.hash
}


alias zion.addfriend {
  if ($timer(zion.s.friendtimeout)) {
    z.echo $z.head(err) $z.lang(friends,toofast)
    return
  }
  var %k $r(999,999999)
  z.h.setvar $+($saveid,_,$1) %k
  .ctcp $1 ZION-FRIEND-REQ $z.b64($me $z3.findtog(auth,name) %k $z3.findtog(auth,name))
  .timerzion.s.friendtimeout 1 10 return
}


ctcp *:ZION-FRIEND-REQ:?:{
  var %v $1 $z.64b($2-)
  tokenize 32 %v
  if ($2 != $nick) return
  
  .timer 1 0 _prompt $nick $1-
  haltdef
}

alias -l _prompt {
  if (($z3.findtog(personal,frdprmpt)) && ($isalias(z.reg.friends.p))) z.reg.friends.p $1-
  else {
    .ctcpreply $1 $2 $z.b64($me $z3.findtog(auth,name) $5 $z3.findtog(auth,name))
    .timerzion.s.fr.ann 1 3 zion.friends.tellmode
    _hash.addfriend $4 $6-
  }
}
alias zion.friends.tellmode {
  var %r $z.h.getvar(friends_tmode)
  if ($gettok(%r,1,32) = 5) z.comms.send -iof $saveid rmode $z3.findtog(auth,name) %r
  else press.omode %r -crf
}
on *:CTCPREPLY:ZION-FRIEND-REQ*:{
  var %v $1 $z.64b($2-),%e $+($saveid,_,$nick)
  tokenize 32 %v
  if ($2 != $nick) return
  if ($4 = $z.h.getvar(%e)) {
    .timerzion.s.fr.ann 1 3 zion.friends.tellmode
    _hash.addfriend $3 $5-
    %v = $z.pop($z.lang(friends,added) $nick $+($chr(40),$5-,$chr(41)) $z.lang(friends,added2),$z.lang(friends,addfriend),4,64,0,0)
    z.h.unsetvar %e
  }
  haltdef
}


alias zion_friends_history {
  if (!$isfile($+(",$scriptdir,history.dat,"))) var %v = $input($z.lang(friends,nohistory),oi,$z.lang(err,oops))
  elseif (!$dialog(zion_friends_history)) dialog -ma zion_friends_history zion_friends_history_table
}

dialog zion_friends_history_table {
  title $z.lang(friends,historytitle)
  size -1 -1 396 298
}

on *:dialog:zion_friends_history:*:*: {
  if ($devent == init) {
    dcx Mark $dname zion_friends_history_cb
    xdialog -b $dname +oty
    window -hn @z.friends.history 5 5 385 250
    font @z.friends.history -8 Tahoma
    zion_friends_history_init_dcx
  }
}

alias -l zion_friends_history_init_dcx {
  ;  xdialog -c $dname 1 richedit 5 5 385 250 multi vsbar tabstop left hsbar
  xdialog -c $dname 1 window 5 5 385 250 @z.friends.history
  xdid -h $dname 1
  ;  xdid -f $dname 1 +a ansi 10 Tahoma
  
  xdialog -c $dname 3 button 160 265 75 25 tabstop
  xdid -t $dname 3 $z.lang(friends,clearlog)
  
  xdialog -c $dname 2 button 315 265 75 25 tabstop
  xdid -t $dname 2 $z.lang(sys,close)
  
  xdialog -c $dname 4 text 170 130 75 25
  xdid -t $dname 4 $z.lang(friends,loading)
  xdid -f $dname 4 +ba ansi 10 Tahoma
  
  .timer 1 0 _loadhistory $dname
}
alias -l _echohistory {
  dll_wfix WhileFix
  var %d aline @z.friends.history,%t,%t2,%bs 0,0 $str($chr(160),20),%s 0,0 $str($chr(160),3),%ms 0,0 $str($chr(160),5)
  tokenize 32 $1-
  ;  %d $1-
  %t = 1,15 $+ $1 $+ 
  %t2 = 15,15 $+ $1 $+ 
  %t3 = 15,15 $+ $timestamp $+ 
  if ($1 = zstart) {
    if (!%zion.s.friends.spaced) %d %t3
    %d 0,12 $2- $+ $str($chr(160),200)
    %d %t3
    %zion.s.friends.spaced = 1
  }
  elseif ($2 = start) {
    if (!%zion.s.friends.spaced) %d %t3
    %d %t %s 10››› %ms 14 $_hash.name($3) $z.lang(friends,startzion)
    %d %t2 %bs 14 irc: $4-
    %d %t3
    %zion.s.friends.spaced = 1
  }
  elseif ($2 = endgame) {
    %d %t %s 12!1 $_hash.name($3) $+ 7 $+($chr(40),$4,$chr(41)) $z.lang(friends,disconnected)
    unset %zion.s.friends.spaced
    ;      %d $crlf
  }
  
  elseif ($2 = omode) {
    if (($5 != 5) && ($5 != 1)) { %d %t %s 12!1 $_hash.name($3) $+ 13 $+($chr(40),$4,$chr(41)) changes mode to $modename($5) $+ . | unset %zion.s.friends.spaced }
    elseif ($5 = 5) { %d %t %s 12!1 $_hash.name($3) $+ 7 $+($chr(40),$4,$chr(41)) $z.lang(friends,nowplaying) $6- | unset %zion.s.friends.spaced }
    elseif ($5 = 1) {
      if (!%zion.s.friends.spaced) %d %t3
      %d %t %s 13‹‹‹ %ms 14 $_hash.name($3) $z.lang(friends,gooffline)
      %d %t2 %bs 14 irc: $4
      %d %t3
      %zion.s.friends.spaced = 1
    }
    ;      %d $crlf
  }
}
alias -l _loadhistory {
  var %d $1-,%f $+(",$scriptdir,history.dat,"),%t $decompress(%f),%l $lines(%f),%n 1,%r,%h zion_friends_history
  if (%l = 0) {   xdid -t %d 4 No history. | window -c @z.friends.history | return }
  filter -fk %f _echohistory *
  xdid -s %h 1
  xdid -h %h 4
  %t = $compress(%f)
}
alias zion_friends_history_cb {
  /*
  if ($2 != mouse) {
    echo $color(info) -s * zion_friends_history_cb: $1-
  }
  */
  if ($2 = sclick) {
    if ($3 = 2) dialog -x $1
    elseif ($3 = 3) { var %f $+(",$scriptdir,history.dat,") | if ($isfile(%f)) .remove %f | clear @z.friends.history }
  }
  if ($2 = close) window -c @z.friends.history
}