;------------------------
;  nIRC Соединение (Мульти коннект)
;------------------------
;Version=1.0.0
;------------------------
alias nicks {
  if (!$dialog(nicks)) { dialog -ma nicks nicks } | else { dialog -av nicks }
}
alias nconnect {
  if (!$dialog(nconnect)) { dialog -ma nconnect nconnect } | else { dialog -av nconnect }
}
dialog nconnect {
  title "Подключение [/nconnect]"
  size -1 -1 206 195
  option dbu
  icon sys\icons\ $+ %icons $+ \1.ico
  box "Сервера", 1, 4 3 90 58
  list 2, 7 12 71 46, size
  box "Личная информация", 3, 97 3 106 58
  text "Имя:", 4, 100 13 15 8
  edit "", 5, 132 12 67 10, autohs
  text "E-mail:", 6, 100 25 31 8
  edit "", 7, 132 24 67 10, autohs
  text "Ник:", 8, 100 37 31 8
  edit "", 9, 132 36 67 10, autohs
  text "Запас. Ник:", 10, 100 49 31 8
  edit "", 11, 132 48 67 10, autohs
  button "5", 12, 80 13 11 8
  button "6", 13, 80 23 11 8
  box "Настройки", 14, 4 62 90 97
  text "Сеть (Н: DALNet):", 15, 8 71 53 8
  edit "", 16, 7 81 83 10, autohs
  check "Подключаться при старте", 19, 7 145 82 10
  box "Автоматизация", 20, 97 62 106 97
  edit "", 21, 100 71 100 85, multi return autohs autovs
  text "Сервер (Н: irc.tic-tac.ru):", 22, 8 119 77 8
  edit "", 23, 7 129 83 10, autohs
  button "Закрыть", 24, 166 162 37 12, ok
  button "Сохранить", 25, 4 162 37 12
  button "Удалить", 26, 43 162 37 12
  box "", 27, 4 176 199 15
  text "", 28, 6 181 194 8
  button "Подключиться", 29, 122 162 42 12
}
alias readconnect {
  did -u $dname 19
  did -r $dname 2,21
  .timer -m 1 300 did -ra $dname 28 - 
  var %q = 1 
  while (%q <= $lines(sys\txt\connect\servers.txt)) {
    did -i $dname 2 %q $read(sys\txt\connect\servers.txt,%q)
    inc %q
  }
  if (%up.temp2) { did -cf $dname 2 %up.temp2 | unset %up.temp2 | did -e $dname 12,13 }
  if ($did(2).sel == $lines(sys\txt\connect\servers.txt)) { did -b $dname 13 } 
  elseif ($did(2).sel == 1) { did -b $dname 12 }
  else { did -b $dname 12,13 }
  did -f $dname 5
}
alias nrc {
  if (!$isfile(sys\txt\connect\connect.ini)) { write -c sys\txt\connect\connect.ini }
  return $readini(sys\txt\connect\connect.ini,$$1,$$2)
}
alias nsc {
  if (!$isfile(sys\txt\connect\connect.ini)) { write -c sys\txt\connect\connect.ini }
  writeini sys\txt\connect\connect.ini $$1 $$2 $$3-
}
on *:dialog:nconnect:init:*:{
  mdxload
  mdx SetFont $dname 12,13 18 400 webdings
  did -fra $dname 28 -
  did -h $dname 26,29
  did -b $dname 12,13
  readconnect
}
on *:dialog:nconnect:sclick:*:{
  if ($did == 25) { addnewserver }
  if ($did == 2) {
    if ($did(2).seltext) {
      did -e $dname 12,13
      did -ra $dname 28 -
      did -r $dname 21
      var %userclick = $did(2).sel
      did -v $dname 26,29
      if (%userclick == $lines(sys\txt\connect\servers.txt)) { did -b $dname 13 } 
      if (%userclick == 1) { did -b $dname 12 }
      did -ra $dname 23 $did($dname,2).seltext
      did -ra $dname 5 $nrc($did(23).text,name)
      did -ra $dname 7 $nrc($did(23).text,mail)
      did -ra $dname 9 $nrc($did(23).text,nick) 
      did -ra $dname 11 $nrc($did(23).text,anick) 
      did -ra $dname 16 $nrc($did(23).text,network) 
      if ($nrc($did(23).text,connectonstart) == 1) { did -c $dname 19 }
      if ($isfile(sys\txt\connect\per $+ $did(16).text $+ .txt)) { loadperform per $+ $did(16).text $+ .txt }
    }
  }
  if ($did == 19) {
    if ($did($dname,19).state == 1 && $did(2).seltext) { nsc $did(23).text connectonstart 1 }
    if ($did($dname,19).state == 0 && $did(2).seltext) { nsc $did(23).text connectonstart 0 }
  }
  if ($did == 26) { 
    if ($did(2).seltext) {
      write -dl $+ $did(2).sel sys\txt\connect\servers.txt
      .remove sys\txt\connect\per $+ $did(16).text $+ .txt
      did -r $dname 21 
      remini sys\txt\connect\connect.ini $did(23).text
      did -r $dname 5,7,9,11,16,23
      did -h $dname 26
      did -ra $dname 28 Сервер удалён
      readconnect
    }
  }
  if ($did == 13) {
    var %userclick = $did(2).sel
    var %up.temp = $did($dname,2).sel
    var %up.temp.text = $did($dname,2).seltext
    set %up.temp2 $calc(%up.temp + 1)
    var %up.temp.text2 = $did($dname,2, $+ %up.temp2)
    write -l $+ %up.temp2 sys\txt\connect\servers.txt %up.temp.text
    write -l $+ %up.temp sys\txt\connect\servers.txt %up.temp.text2
    inc %userclick
    did -e $dname 13
    did -r $dname 5,7,9,11,16,23
    did -h $dname 26
    did -ra $dname 28 Сервер перемещён
    if (%userclick  == $lines(sys\txt\connect\servers.txt)) { did -b $dname 13 }
    readconnect
  }
  if ($did == 12) {
    var %userclick = $did(2).sel
    var %up.temp = $did($dname,2).sel
    var %up.temp.text = $did($dname,2).seltext
    set %up.temp2 $calc(%up.temp - 1)
    var %up.temp.text2 = $did($dname,2, $+ %up.temp2)
    write -l $+ %up.temp2 sys\txt\connect\servers.txt %up.temp.text
    write -l $+ %up.temp sys\txt\connect\servers.txt %up.temp.text2
    dec %userclick
    did -e $dname 13
    did -r $dname 5,7,9,11,16,23
    did -h $dname 26
    did -ra $dname 28 Сервер перемещён
    if (%userclick  == 1) { did -b $dname 12 }
    readconnect
  }
  if ($did == 29) {
    if ($did(2).seltext) {
      var %i = $did(2).sel
      if ($server != $null) { server -m $read($setdir $+ servers.txt,%i) | halt }
      if ($scon(0) == 1) { server $read($setdir $+ servers.txt,%i) }
      scon $scon(0) .fullname $readini($setdir $+ connect.ini,n,$read($setdir $+ servers.txt,%i),name) 
      scon $scon(0) .emailaddr $readini($setdir $+ connect.ini,n,$read($setdir $+ servers.txt,%i),mail) 
      scon $scon(0) .nick $readini($setdir $+ connect.ini,n,$read($setdir $+ servers.txt,%i),nick) 
      scon $scon(0) .anick $readini($setdir $+ connect.ini,n,$read($setdir $+ servers.txt,%i),anick)
    }
  }
}
alias addnewserver {
  if (!$did(5).text) { did -ra $dname 28 Введите имя | did -f $dname 5 | halt } 
  if (!$did(7).text || @ !isin $did(7).text) { did -ra $dname 28 Вы не верно ввели E-mail, либо поле пустое | did -f $dname 7 | halt } 
  if (!$did(9).text) { did -ra $dname 28 Введите ник | did -f $dname 9 | halt }
  if (!$did(11).text) { did -ra $dname 28 Введите запасной ник | did -f $dname 11 | halt }
  if (!$did(23).text) { did -ra $dname 28 Введите сервер, например: irc.netbynet.ru | did -f $dname 23 | halt }
  if (!$did(16).text) { did -ra $dname 28 Введите IRC сеть, например: NetByNet | did -f $dname 16 | halt }
  if ($did(2).sel) { goto next }
  if ($did(23).text != $read(sys\txt\connect\servers.txt, w, $did(23).text, 1)) {
    write sys\txt\connect\servers.txt $did(23).text
    :next
    if ($did(2).seltext && $did(2).seltext != $did(23).text) { write sys\txt\connect\servers.txt $did(23).text }
    nsc $did(23).text name $did(5).text
    nsc $did(23).text mail $did(7).text
    nsc $did(23).text nick $did(9).text
    nsc $did(23).text anick $did(11).text
    nsc $did(23).text network $did(16).text
    if ($did(21).text != $null) { writeperform }
    if ($did(21).text == $null && $isfile(sys\txt\connect\per $+ $did(16).text $+ .txt)) { .remove sys\txt\connect\per $+ $did(16).text $+ .txt }
    if ($did(19).state == 1) { nsc $did(23).text connectonstart 1 }
    did -r $dname 5,7,9,11,16,23
    readconnect
  }
  did -ra $dname 28 Сервер сохранён
}

alias writeperform {
  write -c sys\txt\connect\per $+ $did(16).text $+ .txt
  var %q = 1
  while (%q <= $did(21).lines) {
    .write sys\txt\connect\per $+ $did(16).text $+ .txt $did(21,%q)
    inc %q
  }
}
alias loadperform {
  did -r $dname 21
  var %q = 1
  while (%q <= $lines(sys\txt\connect\ $+ $1)) {
    did -i $dname 21 %q $read(sys\txt\connect\ $+ $1,n,%q)
    inc %q
  }
}
alias setdir return sys\txt\connect\
on 1:start: {
  if ($lines($setdir $+ servers.txt) != 0) {
    var %i = 1
    while (%i <= $lines($setdir $+ servers.txt)) {
      if ($readini($setdir $+ connect.ini,n,$read($setdir $+ servers.txt,%i),connectonstart) == 1) { 
        if (%i == 1) { server $read($setdir $+ servers.txt,%i) }
        if (%i != 1) { server -m $read($setdir $+ servers.txt,%i) }
        scon $scon(0) .fullname $readini($setdir $+ connect.ini,n,$read($setdir $+ servers.txt,%i),name) 
        scon $scon(0) .emailaddr $readini($setdir $+ connect.ini,n,$read($setdir $+ servers.txt,%i),mail) 
        scon $scon(0) .nick $readini($setdir $+ connect.ini,n,$read($setdir $+ servers.txt,%i),nick) 
        scon $scon(0) .anick $readini($setdir $+ connect.ini,n,$read($setdir $+ servers.txt,%i),anick)
      }
      inc %i
    }  
  }
}
on *:connect: {
  if ($read(sys\txt\connect\per $+ $network $+ .txt) != $null) { doperform $network } 
  var %q = 1
  while (%q <= $lines(sys\txt\connect\servers.txt)) {
    if ($readini(sys\txt\connect\connect.ini,n,$read(sys\txt\connect\servers.txt,%q),network) == $network) { /join $readini(sys\txt\connect\connect.ini,n,$read(sys\txt\connect\servers.txt,%q),ajoin) }
    inc %q
  }
}
alias doperform {
  var %q = 1 
  while (%q <= $lines(sys\txt\connect\per $+ $1 $+ .txt)) {
    $read(sys\txt\connect\per $+ $1 $+ .txt,%q)
    inc %q
  }
}
