;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
multiconnect {
  window -ke @Status
  if ($1 == $null) { 
    set %server irc.webchat.net
    set %port 6667
  }
  else  {
    set %server $1
    if ($2 == $null) {
      set %port 6667
    } 
    else { 
      set %port $2 
    }
  }
  titlebar @Status : on %server $+ : $+ %port
  sockopen server %server %port
  aline -h @Status 2*** Connecting to %server ( $+ %port $+ )
  linesep @Status
  echo -e @Status 3Local host: $host ( $+ $ip $+ )
}

multiwrite {
  if (%debug == 1) {  
    echo 4 -i2t @Status >> $2- 
  }
  if ($sock(server) != $null) sockwrite -n $$1 $2-
}

multiserverprocesswrapper {
  multiserverprocess : $+ %server $1-
}

multiserverprocess {
  %envoyer = 0
  ;commandes servers

  if ($1 == ping) {
    set %commande PONG $right($1-,$mid($1-,6,0))
    set %envoyer 1
    echo -ei2t @Status PING? PONG!
  }
  elseif ($1 == error) {
    echo -et @Status  $+ $multifin($2-)
  }
  elseif ($left($1,1) != :) {
    multiserverprocesswrapper $1-
    halt
  }
  ;commandes users
  elseif ($2 == join) {
    set %nick $multiextractnick($1)
    set %host $multiextracthost($1)
    set %channel $remove($3,:)
    set %fenetre @ $+ $lower(%channel) $+ : $+ %server
    if (%nick == %me) {
      window -kel11S %fenetre
      if (%rejoin == $null) {
        echo 3 -i2t %fenetre *** Now talking in %channel
      }
      else {
        echo 3 -i2t %fenetre *** Rejoined channel %channel
        unset %rejoin
      }
    }
    else {
      aline -l %fenetre %nick
      echo 3 -t %fenetre *** %nick ( $+ %host $+ ) has joined %channel
    }
    unset %channel
    unset %fenetre
  }
  elseif ($2 == part) {
    set %nick $multiextractnick($1)
    set %host $multiextracthost($1)
    set %fenetre @ $+ $lower($3) $+ : $+ %server
    if (%nick == %me)  window -c %fenetre
    else {
      multideletenick %fenetre %nick
      echo 3 -t %fenetre *** %nick ( $+ %host $+ ) has left $3 ( $+ $multifin($4-) $+ )
    }
    unset %fenetre
    unset %nick
    unset %host
  }
  elseif ($2 == kick) {
    set %nick $multiextractnick($1)
    set %channel $lower($3)
    set %fenetre @ $+ %channel $+ : $+ %server
    set %strtemp ( $+ $multifin($5-) $+ )
    if ($4 == %me)  {
      echo 3 -i2t %fenetre *** You were kicked by %nick %strtemp
      echo 2 -i2t %fenetre *** Attempting to rejoin...
      echo -ei2t @Status  3*** You were kicked from %channel by %nick %strtemp
      window -c  @ $+ %channel $+ : $+ %server
      set %envoyer 1
      set %commande join %channel
      set %rejoin 1
    }
    else {
      multideletenick %fenetre $4
      echo 12 -i2t %fenetre *** $4 was kicked by %nick %strtemp
    }
    unset %fenetre
    unset %nick
    unset %channel
  }
  elseif ($2 == quit) {
    multiprocessquit %server $multiextractnick($1) $multiextracthost($1) $multifin($3-)
  }
  elseif ($2 == nick) {
    multiprocessnick %server $multiextractnick($1) $multifin($3-)
  }
  ;Names
  elseif ($2 == 353) {
    set %strtemp $multifin($6-)
    set %channel $lower($5)
    if (%names == $null) goto finnames

    set %cmpt $numtok(%strtemp,32)
    :debutnames
    aline -l @ $+ %channel $+ : $+ %server $gettok(%strtemp,%cmpt,32)
    dec %cmpt
    if (%cmpt > 0) goto debutnames
    unset %cmpt

    :finnames
    echo -i $+ $calc($len(%channel) + 1) $+ t @Status %channel %strtemp
    unset %strtemp
  }
  ;end of names
  elseif ($2 == 366) {
    echo -i2t @Status $4 End of /NAMES list.
    linesep @Status
    unset %names
  }
  elseif ($2 == privmsg) {
    multiprocessprivmsg %server $multiextractnick($1) $3 $multifin($4-)
  }
  elseif ($2 == notice) {
    multiprocessnotice %server $multiextractnick($1) $3 $multifin($4-)
  }
  elseif ($2 == mode) {
    multiprocessmode $1-
  }
  elseif ($2 == topic) {
    set %channel $lower($3)
    echo 2 -t @ $+ %channel $+ : $+ %server *** $multiextractnick($1) changes topic to ' $+ $multifin($4-) $+ '
  }
  ;Nickname already in use
  elseif ($2 == 433) {
    echo -i2t @Status $4 $multifin($5-)
    if ($3 == *) {
      set %me %me $+ _
      echo -i2t @Status  Trying %me
      set %commande nick %me
      set %envoyer 1
    }
  }
  ;whois-whowas stuff
  elseif ($2 == 311) {
    linesep @Status
    echo @Status $4 is $5 $+ @ $+ $6 $7 $multifin($8-)
    set %multiwhois 1
  }
  elseif ($2 == 319) {
    echo @Status $4 on $multifin($5-)
  }
  elseif ($2 == 312) {
    echo @Status $4 using $5 $multifin($6-)
  }
  elseif (($2 == 318) || ($2 == 369)) {
    echo @Status $4 $multifin($5-)
    linesep @Status
    unset %multiwhois
  }
  elseif ($2 == 317) {
    echo @Status $4 has been idle $duration($5)
  }

  elseif ($2 == 314) {
    linesep @Status
    echo @Status $4 was $5 $+ @ $+ $6 $7 $multifin($8-)
  }
  ; end whois-whowas

  ; is away
  elseif ($2 == 301) {
    if (%multiwhois == 1) echo @Status $4 is away: $multifin($5-)
    else echo -e @Status $4 is away: $multifin($5-)
  }

  ; no such nick channel
  elseif ($2 == 401) {
    linesep @Status
    echo @Status $4 $multifin($5-)
  }

  ;unknown command
  elseif ($2 == 421) {
    echo @Status $upper($4) $multifin($5-)
  }

  ; a la connection
  elseif ($2 == 375) {
    linesep @Status    
    aline -h @Status Message of the Day, $5
    linesep @Status
  }
  elseif (($2 == 372) || ($2 == 377)) {
    aline -h @Status $multifin($4-)
  }
  elseif (($2 == 255) || ($2 == 266)) {
    aline -h @Status $multifin($4-)
    linesep @Status
  }
  ;end of /MOTD
  elseif ($2 == 376) {
    aline -h @Status $multifin($4-)
    linesep @Status
    set %commande mode %me +i
    set %envoyer 1
  }
  elseif (($2 == 001) || ($2 == 002) || ($2 == 003)) {
    aline -h @Status $multifin($4-)
  }
  elseif ($2 == 004) {
    aline -h @Status $4-
  }
  elseif (($2 == 251) || ($2 == 265)) {
    linesep @Status
    aline -h @Status $multifin($4-)
  }
  elseif (($2 == 252) || ($2 == 253) || ($2 == 254)) {
    aline -h @Status $4 $multifin($5-)
  }
  ;end connection

  elseif (($2  == 307) || ($2 == 313) || ($2 == 310) || ($2 == 437)) {
    echo @Status $4 $multifin($5-)
  }

  ;topic
  elseif ($2 == 332) {
    set -u0 %channel $lower($4)
    set -u0 %window @ $+ %channel $+ : $+ %server
    echo -t %window *** Topic is ' $+ $multifin($5-) $+ '
    titlebar %window $multifin($5-)
  }
  elseif ($2 == 333) {
    set -u0 %channel $lower($4)
    set -u0 %window @ $+ %channel $+ : $+ %server
    echo -t %window *** Set by $5 on $asctime($6, ddd mmm dd HH:nn:ss)
  }
  ;You're not channel operator
  elseif ($2 == 482) {
    aline -h @Status $4 $multifin($5-)
  }

  ;No such channel
  elseif ($2 == 403) {
    linesep @Status
    aline -h @Status $4 $multifin($5-)
    linesep @Status
  }
  ;They aren't on that channel
  elseif ($2 == 441) {
    aline -h @Status $4 $5 $multifin($6-)
  }

  ;Can't change mode for other users (502) + bad username (468)
  elseif (($2 == 502) || ($2 == 468)) {
    aline -h @Status $multifin($4-)
  }

  else echo -t @Status $1-
  ;echo -t @Status $1-

  if (%envoyer == 1) {
    multiwrite server %commande
    unset %commande
  }
  unset %envoyer
}
multiuserprocess {
  if ($left($2,1) == /) {
    %commande = $remove($2-,/)
    if ($left(%commande,4) == join) {
      set %names 1
    }
    elseif ($left(%commande,1) == j) {
      set %names 1
      if ($left($3,1) == $chr(35)) set %commande join $3
      else set %commande join $chr(35) $+ $3
    }
    elseif ($left(%commande,4) == part) {
      window -c @ $+ $lower($3) $+ : $+ %server
    }
    elseif ($left(%commande,2) == me) {
      set %commande privmsg $multiextractchannel($1) :ACTION $3- $+ 
      echo -i2t $1 * %me $3-
    }
    elseif ($left(%commande,3) == msg) {
      if ($4 == $null) {
        multiwritewindow $1 2* /msg: insufficient parameters
        unset %commande
      }
      else {
        set %commande privmsg $3 : $+ $4-
        multiwritewindow $1 -> * $+ $3 $+ * $4-
      }
    }
    elseif ($left(%commande,8) == describe) {
      if ($4 == $null) {
        multiwritewindow $1 2* /describe: insufficient parameters
        unset %commande
      }
      else {
        set %commande privmsg $3 :ACTION $4- $+ 
      }
    }
    elseif ($left(%commande,6) == notice) {
      if ($4 == $null) {
        multiwritewindow $1 2* /notice: insufficient parameters
        unset %commande
      }
      else {
        set %commande notice $3 : $+ $4-
        multiwritewindow $1 -> - $+ $3 $+ - $4-
      }
    }
    elseif ($left(%commande,4) == quit) {
      set %commande quit : $+ $3-
    }
    elseif ($left(%commande,3) == say) {
      set %commande privmsg $multiextractchannel($1) : $+ [ [ $3- ] ]
      echo -i2t $1 < $+ %me $+ > [ [ $3- ] ]
    }
    elseif ($left(%commande,4) == ctcp) {
      if ($4 == ping) {
        set %commande privmsg $3 : $+ PING $ctime $+ 
        multiwritewindow $1 4-> 4[ $+ $3 $+ ] PING
      }
      elseif ($4 == dcc) {
        echo 2 -i2t $1 * /ctcp : use /dcc command
        unset %commande
      }
      else {
        if ($5 == $null) set %commande privmsg $3 : $+ $upper($4) $+ 
        else set %commande privmsg $3 : $+ $upper($4) $5- $+ 
        multiwritewindow $1 4-> 4[ $+ $3 $+ ] $upper($4) $5-
      }
    }
    elseif ($left(%commande,3) == dcc) {
      if ($numtok(%commande,32) == 1) {
        multiwritewindow $1 2* /dcc: insufficient parameters
        unset %commande
      }
      elseif ($3 == chat) multiprocessdccchat $1 $4
      elseif ($3 == send) multiprocessdccsend $1 $4-
      else {
        multiwritewindow $1 2* /dcc: invalid parameters
        unset %commande
      }
    }
    multiwrite server %commande
    unset %commande
  }
  elseif ($1 == @Status) {
    echo -ei2t @Status  2*** You're not on a channel
  }
  else {
    multiwrite server privmsg $multiextractchannel($1) : $+ $2-
    echo  -i2t $1 < $+ %me $+ > $2-
  }
}

multiextractnick {
  if ($pos($1,!,0) == 0) return $multifin($1)
  else return $mid($1,2,$calc($pos($1,!)-2))
}

multiextracthost {
  return $right($1,$calc($len($1) - $pos($1,!)))
}

multiextractchannel {
  return $mid($1,2,$calc($pos($1,:) - 2)))
}
multiextractserver {
  return $right($1,$calc($len($1) - $pos($1,:)))
}

multideletenick {
  if ($fline($1,$2,0,1) != 0) set %num $fline($1,$2,1,1)
  else {
    set %nicktemp ? $+ $2
    set %num $fline($1,%nicktemp,1,1)
    unset %nicktemp
  }
  dline -l $1 %num
  unset %num
}

multiprocessquit {
  echo -et @Status  2*** $2 ( $+ $3 $+ ) Quit ( $+ $4- $+ )

  set -u0 %strtemp @#* $+ $1
  set -u0 %nbwind $window(%strtemp,0)
  if (%nbwind == 0) return
  set -u0 %cmpt 1

  :debut
  set -u0 %window $window(%strtemp,%cmpt)

  set %num -1
  if ($fline(%window,$2,0,1) != 0) set %num $fline(%window,$2,1,1)
  else {
    set %nicktemp ? $+ $2
    if ($fline(%window,%nicktemp,0,1) != 0)  set %num $fline(%window,%nicktemp,1,1)
    unset %nicktemp
  }
  if (%num != -1) {
    dline -l %window %num
    echo 2 -t %window *** $2 has quit IRC ( $+ $4- $+ )
  }
  unset %num

  if (%cmpt == %nbwind) return
  inc %cmpt
  goto debut
}

multiprocessnick {
  if ($2 == %me) {
    echo -ei2t @Status  3*** Your nick is now $3
    set %me $3
  }
  set -u0 %strtemp @#* $+ $1
  set -u0 %nbwind $window(%strtemp,0)
  if (%nbwind == 0) goto suite
  set -u0 %cmpt 1

  :debut
  set -u0 %window $window(%strtemp,%cmpt)

  if ($fline(%window,$2,0,1) != 0) {
    rline -l %window $fline(%window,$2,1,1) $3
    echo 3 -i2t %window *** $2 is now known as $3
  }
  else {
    set %nicktemp ? $+ $2
    if ($fline(%window,%nicktemp,0,1) != 0)  {
      set -u0 %num $fline(%window,%nicktemp,1,1)
      set %char $left($line(%window,%num,1),1)
      rline -l %window %num %char $+ $3
      echo 3 -i2t %window *** $2 is now known as $3
      unset %char
    }
    unset %nicktemp
  }

  if (%cmpt == %nbwind) goto suite
  inc %cmpt
  goto debut

  :suite
  set -u0 %window1 @ $+ $lower($2) $+ : $+ %server
  set -u0 %window2 @ $+ $lower($3) $+ : $+ %server
  if ($window(%window1,0) == 0) return
  renwin %window1 %window2 
}

multiprocessmode {
  if ($left($3,1) == $chr(35)) {
    set %nick $multiextractnick($1)
    set -u0 %window @ $+ $lower($3) $+ : $+ %server

    set %cmpt 1
    set %currp 5

    :loop
    set %curr $mid($4,%cmpt,1)
    if (%curr == +) { set %add 1 }
    elseif (%curr == -) { set %add  0 }
    elseif (%curr == b) { inc %currp }
    elseif (%curr == o) {
      set %opped $gettok($1-,%currp,32)
      if (%add == 1) {
        set %opped2 @ $+ %opped
      } 
      else { 
        set %opped2 %opped 
      }
      rline -l %window $multifindnick(%window,%opped) %opped2
      inc %currp
      unset %opped*
    }
    elseif (%curr == v) {
      set %opped $gettok($1-,%currp,32)
      set %num $multifindnick(%window,%opped)
      if ($left($line(%window,%num,1),1) != @) {
        if (%add == 1) {
          set %opped2 + $+ %opped
        } 
        else { 
          set %opped2 %opped 
        }
        rline -l %window %num %opped2
      }
      inc %currp
      unset %num
      unset %opped*
    }
    elseif (%curr == l) { inc %currp }
    elseif (%curr == k) { inc %currp }
    if (%cmpt < $len($4)) {
      inc %cmpt
      goto loop
    }
    unset %curr
    unset %currp
    unset %cmpt
    echo 3 -i2t %window *** %nick sets mode: $4 $5-
  }
  else { echo 3 -ei2t @Status *** $3 sets mode: $multifin($4) }

}

multiprocessprivmsg {
  if (($left($4-,1) == ) && ($4 != ACTION)) {
    if ($pos($4-,,0) == 1) {
      set -u0 %str $multifin($4-)
    }
    else {
      set -u0 %str $mid($4-,2,$calc($pos($4-,,2) - 2))
    }

    set -u0 %type $upper($gettok(%str,1,32))
    set -u0 %str $deltok(%str,1,32)

    if (%type == VERSION) {
      set  %reply $version
    }
    if (%type == PING) {
      set %reply %str
      unset %str
    }
    if (%type == TIME) {
      set %reply $fulldate
    }
    if (%type == FINGER) {
      set %reply %ircname ( $+ $email $+ ) Idle $idle seconds
    }
    if (%type == DCC) {
      ;      multidccprocess %str
      set %reply DCC not implemented yed
    }
    echo -ei2t @Status  4[ $+ $2 %type $+ ] %str
    if (%reply != $null) {
      set %commande notice $2 : $+  $+ %type %reply $+ 
      set %envoyer 1
      unset %reply
    }
  }
  else {
    if ($4 == ACTION) {
      if ($pos($4-,,0) == 1) {
        set %n $mid($4-,9,0)
      }
      else {
        set %n $calc($pos($4-,,2) - 9)
      }
      set %msg 6* $2 $mid($4-,9,%n)
      unset %n
    }
    else {
      set %msg < $+ $2 $+ > $4-
    }
    if ($left($3,1) == $chr(35)) {
      ; $3 est le channel ou le gars parle
      echo -i2t @ $+ $lower($3) $+ : $+ $1 %msg
    }
    else {
      ; $2 est celui qui envoie le msg
      set %window @ $+ $lower($2) $+ : $+ $1
      if ($window(%window,0) == 0) {
        window -ken %window
      }
      echo -i2t %window %msg
    }
    unset %window
    unset %msg
  }
}

multiprocessnotice {
  if ($left($4-,1) == ) {
    ; ctcp reply
    if ($pos($4-,,0) == 1) {
      set -u0 %str $multifin($4-)
    }
    else {
      set -u0 %str $mid($4-,2,$calc($pos($4-,,2) - 2))
    }
    set -u0 %type $upper($gettok(%str,1,32))
    set -u0 %str $deltok(%str,1,32)
    if (%type == PING) {
      set -u0 %str $duration($calc($ctime - %str))
    }
    echo -ei2t @Status  4[ $+ $2 %type reply]: %str
  }
  ; notice
  else {
    if ($left($3,1) == $chr(35)) {
      ; notice dans un channel
      set -u0 %channel $lower($3)
      echo 5 -i2t @ $+ %channel $+ : $+ $1 - $+ $2 $+ : $+ %channel $+ - $4-
    }
    elseif ($4 == DCC) {
      echo -ei2t @Status  5- $+ $2 $+ - $4-
    }
    elseif (($left($active,1) == @) && ($multiextractserver($active) == %server) && ($multifindnick($active,$2) != $null)) {
      ; notice d'un user sur le channel actif
      echo 5 -i2t $active - $+ $2 $+ - $4-
    }
    else {
      ; notice d'un user => recherche de tous les channels ou il est...
      set -u0 %strtemp @#* $+ $1
      set -u0 %nbwind $window(%strtemp,0)
      set -u0 %ok 0
      if (%nbwind != 0) {
        set -u0 %cmpt 1

        :debut
        set -u0 %window $window(%strtemp,%cmpt)

        if ($multifindnick(%window,$2) != $null) {
          echo 5 -i2t %window - $+ $2 $+ - $4-
          set -u0 %ok 1
        }

        if (%cmpt == %nbwind) goto fin
        inc %cmpt
        goto debut
        :fin
      }
      ; si aucun alors affichage sur @Status
      if (%ok == 0) echo -ei2t @Status  5- $+ $2 $+ - $4-
    }
    beep
  }
}

multidccprocess {
  dialog -m multidccchat multidccchat
}

multiprocessdccchat {
  echo 2 -i2t $1 * /dcc chat Not yet implemented
  unset %commande
}

multiprocessdccsend {
  echo 2 -i2t $1 * /dcc send Not yet implemented
  unset %commande
}

multifindnick {
  if ($fline($1,$2,0,1) != 0) set -u0 %num $fline($1,$2,1,1)
  else {
    set %nicktemp ? $+ $2
    set -u0 %num $fline($1,%nicktemp,1,1)
    unset %nicktemp
  }
  return %num
}

multifin {
  set -u0 %result $right($1-,$calc($len($1-) - 1))
  if (%result == $null) set -u0 %result 2-
  return %result
}

multiwritewindow {
  if ($1 == @Status) echo -ei2t @Status  $2-
  else echo -i2t $1 $2-
}

multiversion {
  return 0.12
}

multiselectednick {
  set -u0 %tmp $sline($1,1)
  if ($pos(%tmp,@,1) == 1 || $pos(%tmp,+,1) == 1) {
    set %tmp $multifin(%tmp)
  }
  return %tmp
}
multiaffichedefaut {
  set %fin $findtok($1-,$wildtok($1-,:*,1,32),1,32)
  echo [ $ $+ %fin $+ - ]
  return $gettok($1-,[ %fin $+ - ],32) 
}
