/*
==========================================

==========================================
*/
alias -l wpadll dll $shortfn($scriptdirwpadding.dll) WinPadding $1 right 150
alias -l mas.wfx return $shortfn($scriptdirwindowfx.dll)
alias -l mas.ini return $shortfn($scriptdirmas.ini)
alias -l mas.noav return $shortfn($scriptdirnoavatar.png)
alias -l xgui.dll return $shortfn($scriptdirx-gui.dll)
alias -l mdc.dll return $shortfn($scriptdirmdc.dll)
alias -l dcod return $dll($shortfn($scriptdirzb64.dll),zdecode,$1)
alias -l ecod return $dll($shortfn($scriptdirzb64.dll),zencode,$1)
on *:signal:*: {
  if ($signal == wpadding) {
    if ($window($1)) {
      var %win $+(@mas*,$1)
      window %win $calc($2 + 1) 1 150 $3
      drawrect -rf %win $rgb(255,255,255) 1 0 0 1000 1000
      drawpic %win 13 25 $shortfn($scriptdirrect.png)
      drawpic %win 13 $calc($window(%win).h - 170) $shortfn($scriptdirrect.png)
      var %file = $shortfn($readini($mas.ini,nicks,$1))
      if ($isfile(%file) == $true) {
        if ($readini($mas.ini,avlog,$1)) drawpic -s %win 25 37 106 105 $mas.noav
        else { $iif($readini($mas.ini,nicks,$1),drawpic -s %win 25 37 106 105 %file,drawpic -s %win 25 37 106 105 $mas.noav) }
      }
      else drawpic -s %win 25 37 106 105 $mas.noav
      if (%no.avatar) { 
        drawpic %win 13 $calc($window(%win).h - 170) $shortfn($scriptdirrect.png)
        drawpic -s %win 25 $calc($window(%win).h - 158) 106 105 $mas.noav
      }
      else { $iif($readini($mas.ini,vars,$me),drawpic -s %win 25 $calc($window(%win).h - 158) 106 105 $qt($gettok($readini($mas.ini,vars,$me),-1,44)),drawpic -s %win 25 $calc($window(%win).h - 158) 106 105 $mas.noav) }
      drawtext -r %win $rgb(0,64,128) Tahoma 11 13 10 $1 
      drawtext -r %win $rgb(0,64,128) Tahoma 11 13 $calc($window(%win).h - 185) $me
      drawtext -r %win $rgb(0,64,128) Tahoma 11 30 $calc($window(%win).h - 40) Avatarımı Değiştir
    }
  }
  if ($signal == mdc) {
    if ($1 == ScrollWin) && ($2 == image) {
      did -u sel_av 8
      unset %no.avatar
      set %selected.file $gettok($readini($mas.ini,nicks,$me),$3,44)
      var %file = $gettok($readini($mas.ini,nicks,$me),$3,44)
      if (%file) && ($isfile(%file)) { set %sel.id $3 | did -g sel_av 5 $shortfn(%file) | did -e sel_av 6,3 }
    }
  }
}
menu @mas* {
  uclick: {
    if ($inrect($mouse.x,$mouse.y,30,$calc($window($+(@mas*,$active)).h - 40),$width(Avatarımı Değiştir,tahoma,11,0),13)) if (!$dialog(sel_av)) dialog -ma sel_av sel_av
  }
  rclick: window -a $remove($active,@mas*)
}
dialog sel_av {
  title "Görüntü Resimleri"
  size -1 -1 175 172
  option dbu
  text "Anlık İleti konuşmalarında diğerlerinin göreceği,sizi temsil edecek resmi seçiniz ßy OzeL.", 1, 4 3 160 15
  button "Gözat", 2, 118 34 37 12
  button "Kaldır", 3, 118 52 37 12, disable
  text "Önizleme:", 4, 114 71 24 8
  icon 5, 113 80 58 61, $shortfn($scriptdirnoavatar.png), 0
  button "Tamam", 6, 6 158 37 12, disable
  button "İptal", 7, 45 158 37 12, cancel
  check "Resim Gösterme", 8, 89 159 51 10
}
alias load.avatars {
  if (%no.avatar) { did -g $dname 5 $mas.noav | did -c $dname 8 }
  elseif ($isfile($shortfn($gettok($readini($mas.ini,nicks,$me),$readini($mas.ini,vars,av_id),44))) == $true) did -g sel_av 5 $shortfn($gettok($readini($mas.ini,nicks,$me),$readini($mas.ini,vars,av_id),44))
  window -Bhp +d @sav 0 0 64 64
  set %av.y -74
  set %sel_id $readini($mas.ini,vars,av_id)
  unset %av.id %tbox.id
  var %num = $numtok($readini($mas.ini,nicks,$me),44)
  var %i = 1
  while (%i <= %num) {
    inc %av.y 75
    inc %av.id
    inc %tbox.id
    var %file = $shortfn($gettok($readini($mas.ini,nicks,$me),%i,44))
    drawpic -s @sav 0 0 64 64 $shortfn(%file)
    drawsave -b32q100 @sav $qt($scriptdiravsave. $+ $gettok(%file,-1,46))
    dll $mdc.dll ScrollWin SetControl 10 image create %av.id 2 %av.y 64 64 > $shortfn($scriptdiravsave. $+ $gettok(%file,-1,46))
    dll $mdc.dll ScrollWin SetControl 10 text create %tbox.id 70 $calc(%av.y + 20) 100 15 > $remove($nopath(%file),$gettok($nopath(%file),-1,46),.)
    dll $mdc.dll ScrollWin SetControl 10 text font %tbox.id $rgb(0,0,0) 8 +n > Tahoma
    .remove $qt($scriptdiravsave. $+ $gettok(%file,-1,46))
    inc %i
  }
  window -c @sav
}

on *:dialog:sel_av:init:*: { dll $mdc.dll ScrollWin create $dname 10 5 40 210 270 | load.avatars }
on *:dialog:sel_av:sclick:*: {
  if ($did == 2) {
    var %file = $dll($shortfn($scriptdiredialogs.dll),EDlg,l $+ $nofile(%file) $+ >Avatarınızı Seçin>|Resim Dosyası|*.jpg;);
    if (%file) {
      inc %av.y 75
      inc %av.id
      inc %tbox.id
      window -Bhp +d @sav 0 0 64 64
      drawpic -s @sav 0 0 64 64 $shortfn(%file)
      drawsave -b32q100 @sav $qt($scriptdiravsave. $+ $gettok(%file,-1,46))
      window -c @sav
      dll $mdc.dll ScrollWin SetControl 10 image create %av.id 2 %av.y 64 64 > $shortfn($scriptdiravsave. $+ $gettok(%file,-1,46))
      dll $mdc.dll ScrollWin SetControl 10 text create %tbox.id 70 $calc(%av.y + 20) 100 15 > $remove($nopath(%file),$gettok($nopath(%file),-1,46),.)
      dll $mdc.dll ScrollWin SetControl 10 text font %tbox.id $rgb(0,0,0) 8 +n > Tahoma
      writeini $mas.ini nicks $me $addtok($readini($mas.ini,nicks,$me),%file,44)
      .remove $qt($scriptdiravsave. $+ $gettok(%file,-1,46))
    }
  }
  if ($did == 8) {
    did -e $dname 6
    if ($did(8).state == 1) { set %no.avatar ok! | did -g $dname 5 $mas.noav }
    else { unset %no.avatar | if ($isfile($shortfn($gettok($readini($mas.ini,nicks,$me),$readini($mas.ini,vars,av_id),44))) == $true) did -g $dname 5 $shortfn($gettok($readini($mas.ini,nicks,$me),$readini($mas.ini,vars,av_id),44)) } 
  }
  if ($did == 3) {
    var %delfile = $deltok($readini($mas.ini,nicks,$me),%sel.id,44)
    $iif(%delfile,writeini $mas.ini nicks $me %delfile,remini $mas.ini nicks $me)
    did -g $dname 5 $shortfn($scriptdirnoavatar.png)
    dll $mdc.dll DeleteControls 10
    load.avatars
    did -b $dname 3,6
  }
  if ($did == 6) { 
    if (%no.avatar) {
      var %i = 1
      while (%i <= $query(0)) {
        .ctcp $query(%i) avlog noavatar
        var %win $+(@mas*,$query(%i))
        if ($window(%win)) {
          drawpic %win 13 $calc($window(%win).h - 170) $shortfn($scriptdirrect.png)
          drawpic -s %win 25 $calc($window(%win).h - 158) 106 105 $mas.noav
        }
        inc %i
      }
      dialog -x $dname
    }
    else {
      writeini $mas.ini vars av_id %sel.id
      if ($readini($mas.ini,uploaded,$shortfn(%selected.file))) {
        var %i = 1
        writeini $mas.ini vars $me %selected.file
        while (%i <= $query(0)) {
          .ctcp $query(%i) AVATARIM $ecod($readini($mas.ini,uploaded,%selected.file))
          var %win $+(@mas*,$query(%i))
          if ($window(%win)) {
            drawpic %win 13 $calc($window(%win).h - 170) $shortfn($scriptdirrect.png)
            drawpic -s %win 25 $calc($window(%win).h - 158) 106 105 $shortfn(%selected.file)
            inc %i
          }
        }
        dialog -x $dname
      }
      else {
        .copy -o $shortfn($gettok($readini($qt($scriptdirmas.ini),nicks,$me),%sel.id,44)) C:Program Files\Efsanesohbet mIRC\avatar\avatar\avatar.jpg | imgup | dialog -x $dname
        var %i = 1
        while (%i <= $query(0)) {
          writeini $mas.ini vars $me $gettok($readini($mas.ini,nicks,$me),%sel.id,44)
          var %win $+(@mas*,$query(%i))
          if ($window(%win)) {
            drawpic %win 13 $calc($window(%win).h - 170) $shortfn($scriptdirrect.png)
            drawpic -s %win 25 $calc($window(%win).h - 158) 106 105 C:Program Files\Efsanesohbet mIRC\avatar\avatar\avatar.jpg
          }
          inc %i
        }      
      }
    }
  }
}
on *:active:?: {
  if ($active != $me) {
    if ((!$readini($mas.ini,active,$active)) && ($window($active).type != channel) && (@mas !isin $active)) {
      if ($isfile($mas.ini) == $false) write -c $qt($mas.ini)
      if ($isdir($scriptdiravatar\) == $false) .mkdir $qt($scriptdiravatar\)
      writeini $mas.ini active $active ok!
      .ctcp $active kontrol
      var %nick $active
      var %win $+(@mas*,$active)
      wpadll $active
      window -hBp +d %win 0 0 1000 1000
      window -a %win
      dll $mas.wfx SetChild %nick > %win
      window -a %nick
    }
  }
}
ctcp ^*:kontrol: { 
  if (%no.avatar) .ctcpreply $nick kontrol noavatar
  else {
    $iif($readini($mas.ini,link,$me),.ctcpreply $nick kontrol $ecod($readini($mas.ini,link,$me)),.ctcp $nick kontrol noavatar) 
  }
  halt 
}
ctcp ^*:avlog: {
  var %win $+(@mas*,$nick)
  drawpic %win 13 25 $shortfn($scriptdirrect.png)
  drawpic -s %win 25 37 106 105 $mas.noav
  writeini $mas.ini avlog $nick ok!
  halt
}
on *:ctcpreply:*kontrol*: { 
  var %win $+(@mas*,$nick)
  if ($2 == noavatar) { 
    if ($window(%win)) {
      drawpic %win 13 25 $shortfn($scriptdirrect.png)
      drawpic -s %win 25 37 106 105 $mas.noav
    }
  }
  else {
    var %avatar $qt($scriptdiravatar\ $+ $gettok($2,-1,47))
    if ($isfile(%avatar) == $false) {
      .write avatar.txt $nick $+ $chr(20) $+ $dcod($2)
      $iif($read(avatar.txt) !== $null,.set %avatarnik $gettok($read(avatar.txt,1),1,20),.halt)
      avatarindir $gettok($read(avatar.txt,1),1,20) $gettok($read(avatar.txt,1),2,20) $nopath($gettok($gettok($read(avatar.txt,1),2,20),1,46)) $nopath($gettok($gettok($read(avatar.txt,1),2,20),4,46)) $gettok($gettok($read(avatar.txt,1),2,20),3-5,47)      
    }
    elseif ($window(%win)) {
      drawpic %win 13 25 $shortfn($scriptdirrect.png)
      drawpic -s %win 25 37 106 105 %avatar
    } 
    halt
  }
}
on *:close:?: {
  remini $mas.ini active $nick
}
on *:nick: {
  if ($readini($mas.ini,nicks,$nick)) {
    if ($window($+(@mas*,$nick))) window -c $+(@mas*,$nick)
    writeini $mas.ini nicks $newnick $readini($mas.ini,nicks,$nick)
    if ($nick == $me) { writeini $mas.ini vars $newnick $readini($mas.ini,vars,$nick) | writeini $mas.ini link $newnick $readini($mas.ini,link,$nick) }
  }
}
on *:part:*: {
  if ($nick != $me) && ($query($nick)) { window -c $nick | remini $mas.ini active $nick }
}
on *:quit: {   if ($nick != $me) && ($query($nick)) { window -c $nick | remini $mas.ini active $nick } }
alias -l about.mas {
  window -phdoBC +bL @about 0 0 250 150
  window -a @about
  drawpic @about 0 0 $shortfn($scriptdirabout.png)
}
menu @about {
  uclick: window -c @about
}
on *:load: {
  echo -a Zipli dosyanın içindeki tüm dosyaları "mas.mrc" dosyası ile aynı klasöre çıkartınız.(Bu işlemi gerçekleştirdiyseniz bu mesajı dikkate almayınız.)
  about.mas
}
/*
==================================
Image Uploading...

BY fjxokt
==================================
*/
alias -l imgshck_build {
  window -h @imgshck
  clear -@ @imgshck
  var %fname C:Program Files\Sohbetm mIRC\avatar\avatar\avatar.jpg , %a aline @imgshck , %b -----------------------------491299511942

  %a %b
  %a Content-Disposition: form-data; name="uploadtype"
  %a $cr
  %a on
  %a %b
  %a Content-Disposition: form-data; name="fileupload"; filename=" $+ $iif($1 == file,%fname) $+ "
  %a Content-Type: $iif($1 == file, image/ $+ $right(%fname,3),application/octet-stream)
  %a $cr
  %a $cr
  %a %b
  %a Content-Disposition: form-data; name="url"
  %a $cr
  %a $iif($1 == file,paste image url here,%fname)
  %a %b
  %a Content-Disposition: form-data; name="MAX_FILE_SIZE"
  %a $cr
  %a 3145728
  %a %b
  %a Content-Disposition: form-data; name="refer"
  %a $cr
  %a http://www.imageshack.us/
  %a %b
  %a Content-Disposition: form-data; name="brand"
  %a $cr
  %a $cr
  %a %b
  %a Content-Disposition: form-data; name="optsize"
  %a $cr
  %a 320x320
  %a $+(%b,--)
}
alias -l imgshck_send_infos {
  var %fname C:Program Files\Sohbetm mIRC\avatar\avatar\avatar.jpg , %i 0 , %b -----------------------------491299511942
  sw $+(%b,$crlf,Content-Disposition: form-data; name="url",$crlf,$crlf,paste image url here,$crlf,%b,$crlf,Content-Disposition: form-data; name="MAX_FILE_SIZE",$crlf)
  sw $+($crlf,$crlf,3145728,$crlf,%b,$crlf,Content-Disposition: form-data; name="refer",$crlf,$crlf,http://www.imageshack.us/,$crlf,%b,$crlf,Content-Disposition: form-data; name="brand",$str($crlf,3))
  sw $+(%b,$crlf,Content-Disposition: form-data; name="optsize",$crlf,$crlf)
  sw 320x320
  sw $+(%b,--,$crlf)
}
alias -l imgshck_get_length {
  var %f $file(C:Program Files\Sohbetm mIRC\avatar\avatar\avatar.jpg)
  var %s $qt(tmp.tmp)
  write -c %s
  savebuf @imgshck %s
  var %size $file(%s)
  .remove %s
  set %clength $calc(%f + %size - 12)
}
alias -l imgshck_fileupload {
  var %fname $qt($1-) , %s 8192 , %inc 1000 , %y = %inc
  if ($file(%fname) <= %s) { bread %fname 0 %s &data | sockwrite is3 &data }
  else {
    var %x 0, %size $file(%fname)
    while (%x <= %size) {
      .timerupload. $+ %y -mo 1 %y swr %x %s %fname
      inc %x %s
      inc %y %inc
    }
  }
  .timerupload. $+ %y -mo 1 %y imgshck_send_infos
}
alias -l swr {
  if ($sock(is3)) {
    if ($sock(is3).sq >= 16384) { echo -a Avatar upload edilemiyor.Tekrar deneyiniz. | sockclose is3 }
    bread $3- $1 $2 &data
    sockwrite is3 &data
  }
}
alias sw if ($sock(is3)) sockwrite -n is3 $1-
on *:sockopen:is3:{
  if ($sockerr) echo -a Avatar upload edilemedi!
  else {
    var %fname %filename , %i 0, %b -----------------------------491299511942
    sw POST / HTTP/1.1
    sw Host: imageshack.us $+ $crlf
    sw User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; fr; rv:1.8.0.4) Gecko/20060508 Firefox/1.5.0.4
    sw Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
    sw Accept-Language: fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3
    sw Accept-Encoding: gzip,deflate
    sw Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
    sw Keep-Alive: 300
    sw Connection: keep-alive
    sw Referer: http://imageshack.us/
    sw Content-Type: multipart/form-data; boundary= $+ $right(%b,-2)
    sw $+(Content-Length: %clength,$crlf,$crlf,%b,$crlf,Content-Disposition: form-data; name="uploadtype",$crlf,$crlf,on,$crlf,%b)
    sw $+(Content-Disposition: form-data; name="fileupload"; filename="C:Program Files\Sohbetm mIRC\avatar\avatar\avatar.jpg",$crlf,Content-Type: image/pjpeg,$crlf,$crlf)
    imgshck_fileupload C:Program Files\Sohbetm mIRC\avatar\avatar\avatar.jpg
  }
}
on *:sockread:is3: {
  sockread %s
  if (*<div id="forum1" style="display:none;vertical-align:top">* iswm %s) set %get ok!
  if (%get) && ([URL isin %s) { 
    if ($window(@imgshck)) window -c @imgshck
    var %i = 1
    sockclose is3 | unset %get 
    var %upfile $remove($gettok(%s,-1,32),[/URL],[URL],[IMG],[/IMG],value="[URL=http://imageshack.us,"/>,])
    writeini $mas.ini uploaded %selected.avatar %upfile
    writeini $mas.ini link $me %upfile
    while (%i <= $query(0)) {
      .ctcp $query(%i) AVATARIM $ecod(%upfile)
      inc %i
    }
  }
}
alias imgup {
  imgshck_build file
  imgshck_get_length
  set %fname $nopath(C:Program Files\Sohbetm mIRC\avatar\avatar\avatar.jpg)
  set %selected.avatar %selected.file
  sockclose is3
  sockopen is3 imageshack.us 80
}

/*
==========================
Avatar Download 

BY OzeL
==========================
*/
ctcp ^1:AVATARIM:*:{ 
  var %get.av $gettok($2,-1,47) , %win $+(@mas*,$nick)
  if ($readini($mas.ini,downloaded,%get.av)) && ($isfile($scriptdiravatar\ $+ %get.av) == $true) {
    if ($window(%win)) {
      drawpic %win 13 25 $shortfn($scriptdirrect.png)
      drawpic -s %win 25 37 106 105 $shortfn($scriptdiravatar\ $+ %get.av)
    }
  }
  else {
    .write avatar.txt $nick $+ $chr(20) $+ $dcod($2)
    $iif($read(avatar.txt) !== $null,.set %avatarnik $gettok($read(avatar.txt,1),1,20),.halt)
    avatarindir $gettok($read(avatar.txt,1),1,20) $gettok($read(avatar.txt,1),2,20) $nopath($gettok($gettok($read(avatar.txt,1),2,20),1,46)) $nopath($gettok($gettok($read(avatar.txt,1),2,20),4,46)) $gettok($gettok($read(avatar.txt,1),2,20),3-5,47)
  }
  halt
}
alias avatarindir {
  .set %socknik $1
  .set $+(%,avatar,.,$1) $1
  .set $+(%,avatar,.,$1,.,url) $2
  .set $+(%,avatar,.,$1,.,uzanti) $4
  .set $+(%,avatar,.,$1,.,ilkurl) $3
  .set $+(%,avatar,.,$1,.,orta) $5
  sockclose %socknik
  sockopen %socknik $($+(%,avatar,.,$1,.,ilkurl),2) $+ .imageshack.us 80
}
on *:sockopen:%socknik: {
  if $sockerr {
    .echo -a Avatar alınamıyor
    .write -dl1 avatar.txt
    .timer 1 2 avatarvarmi
  }
  else {
    sockwrite -n $sockname GET / $+ $($+(%,avatar,.,$sockname,.,orta),2) HTTP/1.0
    sockwrite -n $sockname Host: $($+(%,avatar,.,$sockname,.,ilkurl),2) $+ . $+ imageshack.us $+ $crlf $+ $crlf
  }
}
on *:sockread:%socknik: {
  var %x
  sockread %x
  if (Length isin %x) set %len $gettok(%x,2,32)
  if (* !iswm %x) { 
    set %halt $sock($sockname).rcvd
    .set %sockniks $r(1000,9999) $+ . $+ $($+(%,avatar,.,$sockname),2) 
    .set %dosyaismi $gettok($($+(%,avatar,.,$sockname,.,orta),2),3,47)
    .sockclose %sockniks
    sockrename %socknik %sockniks
    write -c $qt($scriptdiravatar $+ \ $+ %dosyaismi)
  }
}
on *:sockread:%sockniks: {
  sockread &pdata
  bwrite $shortfn($scriptdiravatar $+ \ $+ %dosyaismi) -1 -1 &pdata
  var %prog $round($calc(100 * ($sock($sockname).rcvd - %halt) / %len),1)
  if (%prog == 100) {
    .write -dl1 avatar.txt
    .timer 1 2 avatarvarmi
    .unset %socknik*
    var %nick $gettok($sockname,-1,46)
    writeini $mas.ini downloaded %dosyaismi ok!
    writeini $mas.ini nicks %nick $shortfn($scriptdiravatar\ $+ %dosyaismi)
    var %win = @mas* $+ %nick
    remini $mas.ini avlog %nick
    if ($window(%win)) {
      drawpic %win 13 25 $shortfn($scriptdirrect.png)
      var %fl = $shortfn($scriptdiravatar\ $+ %dosyaismi)
      if (!$pic(%fl).width) {
        echo -a Avatar yüklenemedi.
        drawpic -s %win 25 37 106 105 $mas.noav
      }
      else drawpic -s %win 25 37 106 105 $shortfn($scriptdiravatar\ $+ %dosyaismi)
    }
  }
}
alias avatarvarmi {
  if $read(avatar.txt) !== $null {
    .set %avatarnik $read(avatar.txt,1)
    .avatarindir $gettok($read(avatar.txt,1),1,20) $gettok($read(avatar.txt,1),2,20) $nopath($gettok($gettok($read(avatar.txt,1),2,20),1,46)) $nopath($gettok($gettok($read(avatar.txt,1),2,20),4,46)) $gettok($gettok($read(avatar.txt,1),2,20),3-5,47)
  }
  else {
    .remove avatar.txt
    .unset %dosyaismi
    .unset %avatar*
  }
}
