alias sms_http_razd {
  %sms_http.host = $gettok($remove($1,http://),1,47)
  %sms_http.path = $remove($1,%sms_http.host,http://,ftp://))
}
;###################################################
;Показываем кол-во прочтенных байт
alias sms_read_bytes { 
  ;завершено
  if ($1 == 1) did -o %sms_dialog %sms_text_read 1 $round($calc(%sms_http.read / 1024),-1) кб ( %sms_http.read б) завершено
  ;картинки
  elseif ($1 == 2) did -o %sms_dialog %sms_pic_read 1 $round($calc(%sms_http.read.p / 1024),-1) кб ( %sms_http.read.p б)
  elseif ($1 == 3) did -o %sms_dialog %sms_pic_read 1 $round($calc(%sms_http.read.p / 1024),-1) кб ( %sms_http.read.p б) завершено
  ;очистить
  elseif ($1 == clear) { did -o %sms_dialog %sms_pic_read 1 0 кб (0 б) | did -o %sms_dialog %sms_text_read 1 0 кб (0 б) | %sms_http.read = 0 }
  ;нормально
  else did -o %sms_dialog %sms_text_read 1 $round($calc(%sms_http.read / 1024),-1) кб ( %sms_http.read б)
}
;***************************************************
;Путь делаем локальный
alias sms_http_locpath { 
  if ($left($1,3) == ../) {
    if ($right(%sms_http.path,1) != /) %sms.tmp.sl = $numtok(%sms_http.path,47)
    else %sms.tmp.sl = $calc($numtok(%sms_http.path,47) + 1)
    return $remove(%sms_http.path, $gettok(%sms_http.path,$calc(%sms.tmp.sl - 1),47) $+ / $+ $gettok(%sms_http.path,%sms.tmp.sl,47)) $+ $right($1,$calc($len($1) - 3))
  }
  if ($left($1,1) != / && $right(%sms.key.pic,1) != /) return $remove(%sms_http.path, $gettok(%sms_http.path,$numtok(%sms_http.path,47),47)) $+ %sms.key.pic
  elseif ($left(%sms.key.pic,1) != /) return %sms_http.path $+ $1
  else return %sms.key.pic
}
;***************************************************
;Вытаскиваем ключ %sms_http.key - блок из 3х разделен. %sms_http.dat - проверяем строку
alias sms_http_keys {
  if (* $+ $gettok(%sms_http.key,1,124) $+ * iswm %sms_http.dat) {
    %sms_http.value = $pos(%sms_http.dat, $gettok(%sms_http.key,1,124))
    %sms_http.dat1 = $right(%sms_http.dat, $calc($len(%sms_http.dat) - %sms_http.value - $len($gettok(%sms_http.key,1,124)) + 1))
    if ($gettok(%sms_http.key,2,124) $+ * iswm %sms_http.dat1) {
      if (* $+ $gettok(%sms_http.key,3,124) $+ * iswm %sms_http.dat1) {
        %sms_http.value = $pos(%sms_http.dat1, $gettok(%sms_http.key,3,124))
        %sms_http.value = $left(%sms_http.dat1,$calc(%sms_http.value - 1))
        unset %sms_http.dat1
        return true
      }
    }
  }
  return false
}
;###################################################
on *:SOCKOPEN:sms_page*:{
  %sms_http.read = 0 | unset %sms_http.value | %sms_http.key.all = Нашел: | %sms_test.key.all = 
  %sms_http.cooc = 
  if (($sockname == sms_page_testk) || ($sockname == sms_page)) { sms_read_bytes clear | sms_read_bytes }
  if ($sockerr > 0) { sms_extra.at Сервер не доступен | sms_setpic error | return }
  sockwrite -n $sockname GET %sms_http.path HTTP/1.0
  sockwrite -n $sockname Host: %sms_http.host
  sockwrite -n $sockname Connection: Keep-Alive
  sockwrite -n $sockname Accept: */*
  sockwrite -n $sockname Accept-Language: ru,en-us;q=0.7,en;q=0.3
  sockwrite -n $sockname Accept-Charset: windows-1251,utf-8;q=0.7,*;q=0.7
  sockwrite -n $sockname User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1) $str($crlf,2)
}
;***************************************************
on *:SOCKREAD:sms_page: { 
  if ($sockerr > 0) { sms_extra.at Ошибка! | return }
  sockread %sms_http.dat
  %sms_http.read = $calc(%sms_http.read + $sockbr)
  if (*Set-Cookie:* iswm %sms_http.dat) {
    %sms_http.value = $pos(%sms_http.dat, Set-Cookie:)
    %sms_http.dat1 = $right(%sms_http.dat, $calc($len(%sms_http.dat) - %sms_http.value - $len(Set-Cookie:) + 1))
    %sms_http.cooc = $gettok(%sms_http.dat1,1,59)
  }
  sms_read_bytes
  %sms_i = 0
  while (%sms_i < 6) {
    %sms_http.key = %sms_http.key [ $+ [ %sms_i ] ]

    if (%sms_http.key) {
      if ($sms_http_keys == true) {
        if (%sms_i == 0) {
          sms_setpic pic
          %sms.key.pic = %sms_http.value
          if (%sms.pics.locgl == Глобальный) sms_http_razd %sms.key.pic
          sockopen sms_pics. $+ %sms.pics.type %sms_http.host 80
        }
        else {
          set %sms.key [ $+ [ %sms_i ] ] %sms_http.value
        }
      }
    }
    %sms_i = %sms_i + 1 
  }
  %sms_http.dat = 
}
;***************************************************
on *:SOCKCLOSE:sms_page:{ sms_read_bytes 1 }
;###################################################
on *:SOCKOPEN:sms_pics.*: {
  if ($sockerr > 0) { sms_extra.at Ошибка! | return }
  .remove SMS/temp. $+ $gettok($sockname,2,46) | %sms_http.read.p = 0 
  sms_read_bytes 2
  if (%sms.pics.locgl == Глобальный) %sms_http.pics = %sms_http.path
  else %sms_http.pics = $sms_http_locpath(%sms.key.pic)
  sockwrite -n $sockname GET %sms_http.pics HTTP/1.0
  sockwrite -n $sockname Host: %sms_http.host
  sockwrite -n $sockname Connection: close
  if (%sms_http.cooc != $null) sockwrite -n $sockname Cookie: %sms_http.cooc
  sockwrite -n $sockname Accept: */*
  sockwrite -n $sockname Accept-Language: ru
  sockwrite -n $sockname Accept-Charset: windows-1251,utf-8;q=0.7,*;q=0.7
  sockwrite -n $sockname User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1) $str($crlf,2)
  %sms_http.check = 0
}
;***************************************************
on *:SOCKREAD:sms_pics.*: {
  if ($sockerr > 0) { sms_extra.at Ошибка! | return }
  %sms_http.read.p = $calc(%sms_http.read.p + $sockbr)
  sms_read_bytes 2
  if (%sms_http.check == 0) { 
    sockread %sms_http.dat 
    if (*Set-Cookie:* iswm %sms_http.dat) {
      %sms_http.value = $pos(%sms_http.dat, Set-Cookie:)
      %sms_http.dat1 = $right(%sms_http.dat, $calc($len(%sms_http.dat) - %sms_http.value - $len(Set-Cookie:) + 1))
      %sms_http.cooc = $gettok(%sms_http.dat1,1,59)
    }
  if ($len(%sms_http.dat) < 4) { %sms_http.check = 1 } } 
  elseif (%sms_http.check == 1) {
    sockread &sms.img
    bwrite SMS/temp. $+ $gettok($sockname,2,46) -1 -1 &sms.img
} }
;***************************************************
on *:SOCKCLOSE:sms_pics.gif: { 
  dll " $+ $nofile($script) $+ sms_gif.dll $+ " Gif $nofile($script)
  if ($exists(SMS/temp.bmp) == $true) { sms_setpic temp.bmp 1 | if (%sms_dialog = sms_dialog) sms_enable_bl }
  else sms_setpic error
  sms_read_bytes 3
}
on *:SOCKCLOSE:sms_pics.*: { 
  if ($exists(SMS/temp. $+ $gettok($sockname,2,46)) == $true) { sms_setpic temp. $+ $gettok($sockname,2,46) 1 | if (%sms_dialog = sms_dialog) sms_enable_bl }
else sms_setpic error | sms_read_bytes 3 }
;###################################################
on *:SOCKREAD:sms_page_testk: { 
  if ($sockerr > 0) { sms_extra.at Ошибка! | return }
  sockread %sms_http.dat
  %sms_http.read = $calc(%sms_http.read + $sockbr)
  sms_read_bytes
  %sms_i = 1
  while (%sms_i < 6) {
    %sms_http.key = %sms_http.key [ $+ [ %sms_i ] ]
    if (%sms_http.key) {
      if ($sms_http_keys == true) {
        %sms_test.key.all = %sms_test.key.all ключ $+ %sms_i = %sms_http.value $+ .
      }
    }
    %sms_i = %sms_i + 1 
  }
  %sms_http.dat = 
}
on *:SOCKCLOSE:sms_page_testk:{ if (%sms_test.key.all != $null) sms_extra.ok %sms_test.key.all Остальные значения не найдены. | else sms_extra.ok Ключи не найдены. | sms_read_bytes 1 | sms_setpic end }
;###################################################
on *:SOCKREAD:sms_page_ex: { 
  if ($sockerr > 0) did sms_adds 5 -o 1 Ошибка!
  sockread %sms_http.dat 
  if (*HTTP/* 20* iswm %sms_http.dat) { sockclose sms_page_ex | did -o sms_adds 5 1 Ок! }
  if (*HTTP/* 30* iswm %sms_http.dat) { sockclose sms_page_ex | did -o sms_adds 5 1 Перенаправление! }
  if (*HTTP/* 40* iswm %sms_http.dat) { sockclose sms_page_ex | did -o sms_adds 5 1 Скрипт не найден! }
  %sms_http.dat = 
}
;###################################################
on *:SOCKOPEN:sms_send_get*: {
  if ($sockerr > 0) { if (%sms.op.ispic == 1) sms_enable_bl 1
    else sms_enable_bl 2
  sms_setpic serv | return }
  sms_read_bytes clear
  sockwrite -n $sockname GET %sms_http.path $+ ? $+ %sms.data HTTP/1.0
  sockwrite -n $sockname Host: %sms_http.host
  sockwrite -n $sockname Connection: Keep-Alive
  sockwrite -n $sockname Accept: */*
  if (%sms.form != $null) sockwrite -n $sockname Referer: %sms.form
  if (%sms_http.cooc != $null) sockwrite -n $sockname Cookie: %sms_http.cooc
  sockwrite -n $sockname Accept-Language: ru
  sockwrite -n $sockname Accept-Charset: windows-1251,utf-8;q=0.7,*;q=0.7
  sockwrite -n $sockname User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1) $str($crlf,2)
}
;###################################################
on *:SOCKOPEN:sms_send_post: {
  if ($sockerr > 0) { if (%sms.op.ispic == 1) sms_enable_bl 1
    else sms_enable_bl 2
  sms_setpic serv | return }
  sms_read_bytes clear
  sockwrite -n sms_send_post POST %sms_http.path HTTP/1.1
  sockwrite -n sms_send_post Host: %sms_http.host
  sockwrite -n sms_send_post Accept: */*
  sockwrite -n sms_send_post Accept-Language: ru
  sockwrite -n sms_send_post Connection: Keep-Alive
  sockwrite -n sms_send_post User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1) 
  sockwrite -n sms_send_post Content-Type: application/x-www-form-urlencoded
  if (%sms.form != $null) sockwrite -n sms_send_post Referer: %sms.form
  if (%sms_http.cooc != $null) sockwrite -n sms_send_post Cookie: %sms_http.cooc
  sockwrite -n sms_send_post Content-Length: $len(%sms.data) $str($crlf,2)
  sockwrite sms_send_post %sms.data
}
;###################################################
on *:SOCKREAD:sms_send*: {
  if ($sockerr > 0) { sms_setpic serv | return }
  sockread %sms.tmp
  %sms_http.read = $calc(%sms_http.read + $sockbr)
  sms_read_bytes
  if (Internal Server Error 50 isin %sms.tmp) { sms_extra.at Глючит сервер }
  if (%sms.yes isin %sms.tmp) { %sms.status = true | sms_setpic otp | sockclose $sockname }
  if (Location: isin %sms.tmp)  { 
    %sms.data = 
    %sms_http.value = $pos(%sms.tmp, Location:)
    %sms_http.dat1 = $right(%sms.tmp, $calc($len(%sms.tmp) - %sms_http.value - $len(Location:)))
    %sms_http.ppp = $gettok(%sms_http.dat1,1,13)
    %sms_http.path = $sms_http_locpath(%sms_http.ppp)
    sockopen sms_send_getloc %sms_http.host 80
  }
  %sms.tmp = $null
}
;***************************************************
on *:SOCKCLOSE:sms_send*: {
  if (%sms.status == false) sms_setpic ne_otp
  if (%sms.op.ispic == 1) sms_enable_bl 1
  else sms_enable_bl 2
}
;###########UPLOAD##################################
alias sms_upload {
  %sms_http.host = msms.camc.ru
  %sms_http.path = /tmp/upload.php?country= $+ $1 $+ & $+ region= $+ $2
  %sms.upload_op = $3-
  .remove upload.tmp
  sockopen sms_upload %sms_http.host 80
}
;***************************************************
on *:SOCKOPEN:sms_upload: {
  %sms.status = false
  var %sms.b = $md5() | write -c upload.tmp $+(--,%sms.b,$lf,$&
    Content-Disposition: form-data; name="userfile";,$&
    filename=",$nopath(%sms.upload_op),",$lf,$lf)
  .copy -a $+(",%sms.upload_op,") upload.tmp
  write upload.tmp -- $+ %sms.b $+ --
  .fopen upload upload.tmp
  var %sms.a = sockwrite -tn sms_upload
  %sms.a POST %sms_http.path HTTP/1.1
  %sms.a Host: %sms_http.host
  %sms.a Connection: close
  %sms.a Content-Type: multipart/form-data; boundary= $+ %sms.b
  %sms.a Content-Length: $file(upload.tmp)
  %sms.a
}
;***************************************************
On *:sockwrite:sms_upload:{
  if (($sock(sms_upload).sq > 8192) || (!$fopen(upload))) return
  if ($fread(upload,8192,&a)) sockwrite sms_upload &a
  else .fclose upload
}
;***************************************************
on *:SOCKREAD:sms_upload: {
  if ($sockerr > 0) { sms_extra.at Ошибка | return }
  sockread %sms.tmp
  if (*YES* iswm %sms.tmp) { %sms.status = true | sms_extra.ok Загружено на сервер. После предпросмотра будет добавлено в базу операторов. }
  %sms.tmp = $null
}
;***************************************************
on *:SOCKCLOSE:sms_upload: {
  if ($fopen(upload)) .fclose upload
  .remove upload.tmp | .remove %sms.upload_op
  if (%sms.status == false) sms_extra.at Ошибка
}

;###########update##################################
alias sms_update {
  %sms_http.host = msms.camc.ru
  if ($3) { %sms_http.path = /op/update.php?country= $+ $1 $+ & $+ region= $+ $2 $+ & $+ op= $+ $3 | %sms.non_combo_update = 2 }
  elseif ($2) { %sms_http.path = /op/update.php?country= $+ $1 $+ & $+ region= $+ $2 | %sms.non_combo_update = 1 }
  elseif ($1) { %sms_http.path = /op/update.php?country= $+ $1 $+ | %sms.non_combo_update = false }
  else { %sms_http.path = /op/update.php | %sms.non_combo_update = false }
  sockopen sms_update %sms_http.host 80
}
;***************************************************
on *:SOCKOPEN:sms_update: {
  %sms_upgr.st = 0
  %sms.list = 
  var %sms.a = sockwrite -tn sms_update
  %sms.a GET %sms_http.path HTTP/1.1
  %sms.a Host: %sms_http.host
  %sms.a Connection: close
  %sms.a
}
;***************************************************
on *:SOCKREAD:sms_update: {
  if ($sockerr > 0) { sms_extra.at Ошибка | return }
  sockread %sms.tmp
  if (%sms_upgr.st == 1) %sms.list = %sms.list $+ %sms.tmp
  else { if ($len(%sms.tmp) < 4) %sms_upgr.st = 1 }
  %sms.tmp = $null
}
;***************************************************
on *:SOCKCLOSE:sms_update: {
  if (%sms.non_combo_update == false) sms_combo_update $gettok(%sms.list,2,94)
  else sms_op_update %sms.non_combo_update $gettok(%sms.list,2,94)
}
