All pastes #339249 Raw Edit

Whiteboard

public text v1 · immutable
·published 2007-02-03 23:25 UTC
rendered paste body

; ============================================================================================
;
;  This is the Shared Whiteboard addon for mIRC
;
;  For updates and more information, visit http://www3.nbnet.nb.ca/pacman/scripts.html
;  This script was written by MarkP (markp@dal.net), and is Copyright 1997-1998 of Mark Paciga
;  No part of this file may be copied or modified without prior consent of the author.
;
; ============================================================================================

alias WhiteboardVersion return 2.0
alias WhiteboardVersionBase return 2.0
alias WhiteboardProtocolVersion return 1.1

on 1:load {
  .remote on
  .ial on
}

on 1:start {
  if ($wbSetting(GraphicsFolder,$null) = $null)  {
    writeini $scriptdir $+ mscripts.ini Whiteboard GraphicsFolder $mircdir $+ graphics\
    if ($exists( [ $mircdir $+ graphics\ ] ) = $false) {
      mkdir $mircdir $+ graphics
    }
    if ($exists(c:\windows\clouds.bmp)) { copy c:\windows\clouds.bmp $wbSetting(GraphicsFolder) }
  }

  if ($wbSetting(Status) = $null) { whiteboard -on }
  wbCheckState

  if (none = $mSetting(Global,ShowGreeting,whiteboard)) { return }
  if ($version < 5.3) { echo $colour(info) -s *** You do not have the correct version of mIRC to use the Whiteboard addon. Please download the latest version of mIRC from http://www.mirc.co.uk }
  else { echo $colour(info) -is *** Started MarkP's Whiteboard $WhiteboardVersion $+ . 10http://www3.nbnet.nb.ca/pacman }
}


; =================== Misc Procedures =====

; --- translation for $sockerr codes

alias txtSockErr {
  if ($sockerr = 3) { return Unable to connect }
  elseif ($sockerr = 4) { return Unable to resolve server }
  elseif ($sockerr > 0) { return Unknown error $sockerr }
  else { return $1- }
}

; --- $wbSetting(name,default)

alias wbSetting {
  set %tmpSetting $readini -n $scriptdir $+ mscripts.ini Whiteboard $1
  if (%tmpSetting = $null) { return $2- }
  else { return %tmpSetting }
}

; ==========

alias whiteboard {

  if ($1 = -on) || ($1 = -off) || ($1 = -auto) {
    writeini $scriptdir $+ mscripts.ini Whiteboard Status $1
    wbCheckState
    return 
  }

  ; choose window name
  if ($2 != $null) { set %tempWbWin $2 }
  else { set %tempWbWin Main }

  if ($window( [ @Whiteboard: $+ [ %tempWbWin ] ] ) != $null) { window -a @Whiteboard: $+ %tempWbWin }
  else {
    ; show the toolbar
    set %WhiteboardColour 1
    set %WhiteboardWidth 1
    if (%WhiteboardTool != $null) { .disable %WhiteboardTool }
    set %WhiteboardTool #WhiteboardPaintbrush
    .enable #WhiteboardPaintbrush
    window -p +b @Whiteboard-Toolbar 0 0 $window(-1).w 22 @wbSelectTool
    WhiteboardDrawToolbar

    window -ape @Whiteboard: $+ %tempWbWin 10 22 608 300 @wbTool
  }  

  wbCheckState %tempWbWin

  if ($1 != $null) && ($1 != -l) {
    ; somehow, find the address to connect to
    titlebar @Whiteboard: $+ %tempWbWin $window( [ @Whiteboard: $+ [ %tempWbWin ] ] ).title - Connecting to $1
    if (. isin $1) { set -u2 %tempHost $1 }
    else { set -u2 %tempHost $ial( [ $1 $+ !*@* ] ,1).host }
    if (%tempHost != $null) { sockopen @wbDraw: $+ %tempWbWin $+ : $+ %tempHost %tempHost 831 }
    else { .enable #WhiteboardGetAddress | set %wbTempWin %tempWbWin | .quote userhost $1 }
  }
  unset %tempWbWin
}

; look for address in USERHOST result
#WhiteboardGetAddress on
raw 302:* {
  set %tempHost $gettok($2,2,64)
  if (%tempHost = $null) { echo $colour(info) -is * /Whiteboard: Can't find address of this nick | halt }
  titlebar @Whiteboard: $+ %wbTempWin $window( [ @Whiteboard: $+ [ %wbTempWin ] ] ).title - Connecting to %tempHost
  if ($sock( [ @wbDraw: $+ [ %wbTempWin ] $+ : $+ [ %tempHost ] ] ) = $null) { sockopen @wbDraw: $+ %wbTempWin $+ : $+ %tempHost %tempHost 831 }
  ; else: in use
  unset %wbTempWin
  .disable #WhiteboardGetAddress
  halt
}
#WhiteboardGetAddress end


alias WhiteboardDrawToolbar {
  set %tw @Whiteboard-Toolbar
  drawrect -fn %tw 0 1 320 0 80 20
  drawrect -fn %tw 15 1 400 0 180 20
  drawrect -fn %tw 0 1 580 0 20 20
  drawline -n %tw 14 1 0 20 $window(%tw).w 20
  drawline -n %tw 1 1 0 21 $window(%tw).w 21

  ; draw the colour selectors
  set %tempColour 0
  :nextColour
  drawrect -fn %tw %tempColour 1 $calc(20 * %tempColour) 0 19 19
  inc %tempColour
  if (%tempColour < 16) { goto nextColour }

  ; draw empty boxes for rest of tools
  :nextBox
  drawline -n %tw 1 1 $calc(20 * %tempColour) 0 $calc(20 * %tempColour) 20
  inc %tempColour
  if (%tempColour < 31) { goto nextBox }
  unset %tempColour

  ; draw line width selectors
  drawline -fn %tw 1 0 323 10 338 10
  drawrect -fn %tw 1 1 343 9 14 1
  drawrect -fn %tw 1 1 363 9 14 2
  drawrect -fn %tw 1 1 383 8 14 4

  ; paintbrush (400-420)
  drawline -n %tw 2 1 404 8 404 6 404 5 405 4 405 3 406 3 407 3 407 4 408 4 408 5 409 5 409 6 410 6 411 6 412 6 412 7 412 8 410 10 410 11 409 11 409 12 408 12 407 12 407 13 407 14 408 14 409 14 410 14 411 14 411 15 412 15 413 14 413 13 414 13 415 12 416 12 416 11 415 11 414 11 414 10

  ; dots (420-440)
  drawdot -n %tw 2 3 428 7
  drawdot -n %tw 2 3 435 10
  drawdot -n %tw 2 3 431 16

  ; line (440-460)
  drawline -n %tw 2 2 444 17 457 4

  ; rectagle (460-480)
  ;  drawrect -n %tw 2 1 463 4 14 10
  drawrect -n %tw 2 1 464 5 12 9

  ; filled rectagle (480-500)
  ;  drawrect -fn %tw 2 483 4 14 10
  drawrect -fn %tw 2 1 484 5 12 9

  ; circle (500-520)
  ;  drawdot -n %tw 2 7 512 11
  drawrect -e %tw 2 1 504 3 12 13

  ; fill  (520-540)
  drawrect -fn %tw 2 1 520 5 19 14  

  ; text  (540-560)
  drawtext -on %tw 2 "Times New Roman" 20 544 0 A

  ; picture  (560-580)
  drawtext -on %tw 2 "Times New Roman" 15 563 2 Pic

  ; clear all (580-600)
  drawline -n %tw 14 2 584 17 598 3
  drawline -n %tw 14 2 584 3 598 17

  ; show currently highlighted tools
  WhiteboardHighlightTool %WhiteboardColour
  WhiteboardHighlightTool $WhiteboardToolbarID(%WhiteboardWidth)
  WhiteboardHighlightTool $WhiteboardToolbarID(%WhiteboardTool)

  ; refresh display
  drawdot %tw
  unset %tw
}


; ===== highlights the specified toolbar ID

alias WhiteboardHighlightTool {
  if ($1 < 16) { drawrect -i @Whiteboard-Toolbar 1 1 $calc($1 * 20 + 1) 1 17 17 }
  ;lse { drawrect -i @Whiteboard-Toolbar 1 1 $calc($1 20 + 1) 0 18 19 }
  else { drawrect -i @Whiteboard-Toolbar 1 1 $calc($1 * 20 + 2) 1 16 17 }
}


; ===== this function translates toolbar ID's to and from their associated line widths and tool names

alias WhiteboardToolbarID {
  ; --- line widths
  if ($1 = 16) { return 1 }
  elseif ($1 = 17) { return 2 }
  elseif ($1 = 18) { return 3 }
  elseif ($1 = 19) { return 5 }

  elseif ($1 = 1) { return 16 }
  elseif ($1 = 2) { return 17 }
  elseif ($1 = 3) { return 18 }
  elseif ($1 = 5) { return 19 }

  ; --- drawing tools
  elseif ($1 = 20) { return #WhiteboardPaintbrush }
  elseif ($1 = 21) { return #WhiteboardDots }
  elseif ($1 = 22) { return #WhiteboardLine }
  elseif ($1 = 23) { return #WhiteboardRectangle }
  elseif ($1 = 24) { return #WhiteboardFilled }
  elseif ($1 = 25) { return #WhiteboardCircle }
  elseif ($1 = 26) { return #WhiteboardFill }
  elseif ($1 = 27) { return #WhiteboardText }
  elseif ($1 = 28) { return #WhiteboardPicture }

  elseif ($1 = #WhiteboardPaintbrush) { return 20 }
  elseif ($1 = #WhiteboardDots) { return 21 }
  elseif ($1 = #WhiteboardLine) { return 22 }
  elseif ($1 = #WhiteboardRectangle) { return 23 }
  elseif ($1 = #WhiteboardFilled) { return 24 }
  elseif ($1 = #WhiteboardCircle) { return 25 }
  elseif ($1 = #WhiteboardFill) { return 26 }
  elseif ($1 = #WhiteboardText) { return 27 }
  elseif ($1 = #WhiteboardPicture) { return 28 }
}


; ===== toolbar choice handler

menu @wbSelectTool {
  sclick {

    ; ---chose a colour
    if ($mouse.x < $calc(16 * 20)) {
      WhiteboardHighlightTool %WhiteboardColour
      set %WhiteboardColour $int($calc($mouse.x / 20))
      WhiteboardHighlightTool %WhiteboardColour
    }

    ; --- chose a line width
    elseif ($mouse.x < $calc(20 * 20)) {
      WhiteboardHighlightTool $WhiteboardToolbarID(%WhiteboardWidth)
      set %WhiteboardWidth $WhiteboardToolbarID($int($calc($mouse.x / 20)))
      WhiteboardHighlightTool $WhiteboardToolbarID(%WhiteboardWidth)
    }

    ; --- chose a drawing tool
    elseif ($mouse.x < $calc(29 * 20)) {
      .disable %WhiteboardTool
      WhiteboardHighlightTool $WhiteboardToolbarID(%WhiteboardTool)

      set %WhiteboardTool $WhiteboardToolbarID($int($calc($mouse.x / 20)))
      .enable %WhiteboardTool
      WhiteboardHighlightTool $WhiteboardToolbarID(%WhiteboardTool)
    }

    ; --- others: clearing screen, invalid item...
    elseif ($mouse.x < 600) { window -n @Whiteboard-Toolbar | WhiteboardDo clear $active | .timer -m 1 1 window -r @Whiteboard-Toolbar }
    else { beep 1 }
    halt
  }
}

; ========== enable a different menu depending on the active tool ==========

#WhiteboardPaintbrush on
menu @wbTool {
  mouse {
    if ($mouse.key = 1) {
      WhiteboardDo drawline -h $active %WhiteboardColour %WhiteboardWidth %WhiteboardLastX %WhiteboardLastY $mouse.x $mouse.y
      set %WhiteboardLastX $mouse.x
      set %WhiteboardLastY $mouse.y
    }
    else { unset %WhiteboardLastX %WhiteboardLastY }
  }
  sclick {
    WhiteboardDo drawline -h $active %WhiteboardColour %WhiteboardWidth $mouse.x $mouse.y $mouse.x $mouse.y
    set %WhiteboardLastX $mouse.x
    set %WhiteboardLastY $mouse.y
  }
}
#WhiteboardPaintbrush end

#WhiteboardDots off
menu @wbTool {
  mouse {
    ;    if ($mouse.y < $calc(20 + %WhiteboardWidth)) { return }
    if ($mouse.y < $calc(6 + $WhiteboardToolbarID(%WhiteboardWidth))) { return }
    if ($mouse.key = 1) {
      WhiteboardDo drawdot -h $active %WhiteboardColour %WhiteboardWidth $mouse.x $mouse.y
    }
  }
  sclick {
    if ($mouse.y < $calc(6 + $WhiteboardToolbarID(%WhiteboardWidth))) { return }
    WhiteboardDo drawdot -h $active %WhiteboardColour %WhiteboardWidth $mouse.x $mouse.y
  }
}
#WhiteboardDots end

#WhiteboardLine off
menu @wbTool {
  mouse {
    if ($mouse.key = 1) && (%WhiteboardOriginX != $null) {
      drawline -i $active 1 %WhiteboardWidth %WhiteboardOriginX %WhiteboardOriginY %WhiteboardLastX %WhiteboardLastY
      drawline -i $active 1 %WhiteboardWidth %WhiteboardOriginX %WhiteboardOriginY $mouse.x $mouse.y
      set %WhiteboardLastX $mouse.x
      set %WhiteboardLastY $mouse.y
    }
    elseif (%WhiteboardOriginX != $null) {
      drawline -i $active 1 %WhiteboardWidth %WhiteboardOriginX %WhiteboardOriginY %WhiteboardLastX %WhiteboardLastY
      unset %WhiteboardOriginX %WhiteboardOriginY %WhiteboardLastX %WhiteboardLastY
    }
  }
  sclick {
    set %WhiteboardOriginX $mouse.x
    set %WhiteboardOriginY $mouse.y
    set %WhiteboardLastX $mouse.x
    set %WhiteboardLastY $mouse.y
    drawline -i $active 1 %WhiteboardWidth %WhiteboardOriginX %WhiteboardOriginY %WhiteboardLastX %WhiteboardLastY
  }
  drop {
    if (%WhiteboardOriginX != $null) {
      drawline -i $active 1 %WhiteboardWidth %WhiteboardOriginX %WhiteboardOriginY %WhiteboardLastX %WhiteboardLastY
      WhiteboardDo drawline -h $active %WhiteboardColour %WhiteboardWidth %WhiteboardOriginX %WhiteboardOriginY $mouse.x $mouse.y
      unset %WhiteboardOriginX %WhiteboardOriginY %WhiteboardLastX %WhiteboardLastY
    }
  }
}
#WhiteboardLine end

#WhiteboardRectangle off
menu @wbTool {
  mouse {
    if ($mouse.key = 1) {
      drawrect -i $active 1 %WhiteboardWidth %WhiteboardOriginX %WhiteboardOriginY $calc(%WhiteboardLastX - %WhiteboardOriginX) $calc(%WhiteboardLastY - %WhiteboardOriginY)
      drawrect -i $active 1 %WhiteboardWidth %WhiteboardOriginX %WhiteboardOriginY $calc($mouse.x - %WhiteboardOriginX) $calc($mouse.y - %WhiteboardOriginY)
      set %WhiteboardLastX $mouse.x
      set %WhiteboardLastY $mouse.y
    }
    elseif (%WhiteboardOriginX != $null) {
      drawrect -i $active 1 %WhiteboardWidth %WhiteboardOriginX %WhiteboardOriginY $calc(%WhiteboardLastX - %WhiteboardOriginX) $calc(%WhiteboardLastY - %WhiteboardOriginY)
      unset %WhiteboardOriginX %WhiteboardOriginY %WhiteboardLastX %WhiteboardLastY
    }
  }
  sclick {
    set %WhiteboardOriginX $mouse.x
    set %WhiteboardOriginY $mouse.y
    set %WhiteboardLastX $mouse.x
    set %WhiteboardLastY $mouse.y
    drawrect -i $active 1 %WhiteboardWidth %WhiteboardOriginX %WhiteboardOriginY $calc(%WhiteboardLastX - %WhiteboardOriginX) $calc(%WhiteboardLastY - %WhiteboardOriginY)
  }
  drop {
    drawrect -i $active 1 %WhiteboardWidth %WhiteboardOriginX %WhiteboardOriginY $calc(%WhiteboardLastX - %WhiteboardOriginX) $calc(%WhiteboardLastY - %WhiteboardOriginY)
    WhiteboardDo drawrect -h $active %WhiteboardColour %WhiteboardWidth %WhiteboardOriginX %WhiteboardOriginY $calc($mouse.x - %WhiteboardOriginX) $calc($mouse.y - %WhiteboardOriginY)
    unset %WhiteboardOriginX %WhiteboardOriginY %WhiteboardLastX %WhiteboardLastY
  }
}
#WhiteboardRectangle end

#WhiteboardFilled off
menu @wbTool {
  mouse {
    if ($mouse.key = 1) {
      drawrect -if $active 1 %WhiteboardWidth %WhiteboardOriginX %WhiteboardOriginY $calc(%WhiteboardLastX - %WhiteboardOriginX) $calc(%WhiteboardLastY - %WhiteboardOriginY)
      drawrect -if $active 1 %WhiteboardWidth %WhiteboardOriginX %WhiteboardOriginY $calc($mouse.x - %WhiteboardOriginX) $calc($mouse.y - %WhiteboardOriginY)
      set %WhiteboardLastX $mouse.x
      set %WhiteboardLastY $mouse.y
    }
    elseif (%WhiteboardOriginX != $null) {
      drawrect -if $active 1 %WhiteboardWidth %WhiteboardOriginX %WhiteboardOriginY $calc(%WhiteboardLastX - %WhiteboardOriginX) $calc(%WhiteboardLastY - %WhiteboardOriginY)
      unset %WhiteboardOriginX %WhiteboardOriginY %WhiteboardLastX %WhiteboardLastY
    }
  }
  sclick {
    set %WhiteboardOriginX $mouse.x
    set %WhiteboardOriginY $mouse.y
    set %WhiteboardLastX $mouse.x
    set %WhiteboardLastY $mouse.y
    drawrect -if $active 1 %WhiteboardWidth %WhiteboardOriginX %WhiteboardOriginY $calc(%WhiteboardLastX - %WhiteboardOriginX) $calc(%WhiteboardLastY - %WhiteboardOriginY)
  }
  drop {
    drawrect -if $active 1 %WhiteboardWidth %WhiteboardOriginX %WhiteboardOriginY $calc(%WhiteboardLastX - %WhiteboardOriginX) $calc(%WhiteboardLastY - %WhiteboardOriginY)
    WhiteboardDo drawrect -hf $active %WhiteboardColour %WhiteboardWidth %WhiteboardOriginX %WhiteboardOriginY $calc($mouse.x - %WhiteboardOriginX) $calc($mouse.y - %WhiteboardOriginY)
    unset %WhiteboardOriginX %WhiteboardOriginY %WhiteboardLastX %WhiteboardLastY
  }
}
#WhiteboardFilled end

#WhiteboardCircle off
menu @wbTool {
  mouse {
    if ($mouse.key = 1) {
      drawrect -ie $active 1 %WhiteboardWidth %WhiteboardOriginX %WhiteboardOriginY $calc(%WhiteboardLastX - %WhiteboardOriginX) $calc(%WhiteboardLastY - %WhiteboardOriginY)
      drawrect -ie $active 1 %WhiteboardWidth %WhiteboardOriginX %WhiteboardOriginY $calc($mouse.x - %WhiteboardOriginX) $calc($mouse.y - %WhiteboardOriginY)
      set %WhiteboardLastX $mouse.x
      set %WhiteboardLastY $mouse.y
    }
    elseif (%WhiteboardOriginX != $null) {
      drawrect -ie $active 1 %WhiteboardWidth %WhiteboardOriginX %WhiteboardOriginY $calc(%WhiteboardLastX - %WhiteboardOriginX) $calc(%WhiteboardLastY - %WhiteboardOriginY)
      unset %WhiteboardOriginX %WhiteboardOriginY %WhiteboardLastX %WhiteboardLastY
    }
  }
  sclick {
    set %WhiteboardOriginX $mouse.x
    set %WhiteboardOriginY $mouse.y
    set %WhiteboardLastX $mouse.x
    set %WhiteboardLastY $mouse.y
    drawrect -ie $active 1 %WhiteboardWidth %WhiteboardOriginX %WhiteboardOriginY $calc(%WhiteboardLastX - %WhiteboardOriginX) $calc(%WhiteboardLastY - %WhiteboardOriginY)
  }
  drop {
    drawrect -ie $active 1 %WhiteboardWidth %WhiteboardOriginX %WhiteboardOriginY $calc(%WhiteboardLastX - %WhiteboardOriginX) $calc(%WhiteboardLastY - %WhiteboardOriginY)
    WhiteboardDo drawrect -he $active %WhiteboardColour %WhiteboardWidth %WhiteboardOriginX %WhiteboardOriginY $calc($mouse.x - %WhiteboardOriginX) $calc($mouse.y - %WhiteboardOriginY)
    unset %WhiteboardOriginX %WhiteboardOriginY %WhiteboardLastX %WhiteboardLastY
  }
}
#WhiteboardCircle end

#WhiteboardFill off
menu @wbTool {
  sclick {
    WhiteboardDo drawfill -h $active %WhiteboardColour 1 $mouse.x $mouse.y
  }
}
#WhiteboardFill end

#WhiteboardText off
menu @wbTool {
  sclick {
    WhiteboardDo drawtext -ho $active %WhiteboardColour "Times New Roman" $$?="Enter size of text" $mouse.x $mouse.y $$?="Enter text:"
  }
}
#WhiteboardText end

#WhiteboardPicture off
menu @wbTool {
  sclick {
    set %tempFile $$dir="Choose a picture file" $wbSetting(GraphicsFolder) $+ [ \*.bmp ]
    ; if the file isn't in the cache, copy it there for requests
    if ($exists( [ $wbSetting(GraphicsFolder) $+ [ $gettok(%tempFile,$gettok(%tempFile,0,92),92) ] ] ) = $false) { copy %tempFile $wbSetting(GraphicsFolder) }

    drawpic -h $active $mouse.x $mouse.y %tempFile
    if ($sock(@wbDraw*,0) > 0) { sockwrite -n @wbDraw* drawpic -h $active $mouse.x $mouse.y $gettok(%tempFile,$gettok(%tempFile,0,92),92) }

    unset %tempFile %c
  }
}
#WhiteboardPicture end

; ============================== Between-Client Communication ==============================

alias wbCheckState {
  set %tw $window(0)
  :nxt
  if (%tw = 0) { window -c @Whiteboard-Toolbar }
  elseif (@Whiteboard:* !iswm $window(%tw)) { dec %tw | goto nxt }

  if ($wbSetting(Status) = -off) || (($wbSetting(Status) = -auto) && ($window(@Whiteboard-Toolbar) = $null)) { sockclose @wbListen }
  elseif ($portfree(831)) { socklisten @wbListen 831 }

  unset %tw
  WhiteboardInfoRefresh $1
}


on 1:socklisten:@wbListen {
  ; --- accept the connection to get the IP
  set -u2 %tmps @wbLogon $+ $ticks
  sockaccept %tmps

  ; --- do some basic flood control  
  set -u8 %wbRequests $+ $sock(%tmps).ip $calc( [ %wbRequests [ $+ [ $sock(%tmps).ip ] ] ] + 1)
  if ( [ %wbRequests [ $+ [ $sock(%tmps).ip ] ] ] > 3) { echo $colour(info) -is *** Too many whiteboard requests too fast from $sock(%tmps).ip | sockclose %tmps | halt }

  ; --- check for address on the ignore list  
  if ( *!*@ $+ [ $sock(%tmps).ip ] isignore) {
    echo $colour(info) -is *** Whiteboard request from ignored address $sock(%tmps).ip
    sockclose %tmps
  }
}

on 1:sockopen:@wbDraw* {
  .timer 1 1 WhiteboardInfoRefresh $gettok($sockname,2,58)
  echo $colour(info) -s *** Whiteboard $txtsockerr(connected to [ $sock($sockname).ip ] )
  if ($sockerr > 0) { return }
  sockwrite -n $sockname WHITEBOARD $WhiteboardProtocolVersion [ [ [ $host ] ] ] [ [ [ $me ] ] ] $gettok($sockname,2,58)
}

on 1:sockread:@wbLogon* {
  sockread %temp
  set %1 $gettok(%temp,1,32)  

  if (%1 = WHITEBOARD) {
    ; return handshake
    sockwrite -n $sockname WHITEBOARD $WhiteboardProtocolVersion [ [ [ $host ] ] ] [ [ [ $me ] ] ] <unknown>

    ; does the connection already exist?
    if ($sock( [ @wbDraw: $+ [ $gettok(%temp,5,32) $] + : $+ [ $sock($sockname).ip ] ] ) != $null) { sockclose $sockname }

    ; inform of existing connections
    set %tc $sock( [ @wbDraw: $+ [ $gettok(%temp,5,32) ] $+ :* ] ,0)
    :nxtc
    if ($sock( [ @wbDraw: $+ [ $gettok(%temp,5,32) ] $+ :* ] ,%tc) = $null) || (%tc = 0) { goto cont }
    sockwrite -n $sockname WhiteboardConnectedTo $sock( [ @wbDraw: $+ [ $gettok(%temp,5,32) ] $+ :* ] ,%tc).ip
    dec %tc
    goto nxtc
    :cont
    unset %tc

    ; if the window requested isn't open already, open it
    if ($window( [ @Whiteboard: $+ [ $gettok(%temp,5,32) ] ] ) = $null) {
      if ($wbSetting(Status) = -on) { whiteboard -l $gettok(%temp,5,32) }
      else { sockclose $sockname | halt }
    }

    echo $colour(info) -is *** Whiteboard ' $+ $gettok(%temp,5,32) $+ ' connection from $sock($sockname).ip ( $+ $gettok(%temp,3,32) $+ ) is $gettok(%temp,4,32)
    sockmark $sockname $gettok(%temp,4,32)
    if ($gettok($gettok(%temp,2,32),1,46) != $gettok($WhiteboardProtocolVersion,1,46)) {
      echo $colour(info) -is *** Whiteboard user $gettok(%temp,4,32) is using a newer version than you. Unfortunately, these two versions are not compatible. Please upgrade to the newer version, available at http://www3.nbnet.nb.ca/pacman/scripts.html.  The connection will now be closed.
      sockclose $sockname
      return
    }
    elseif ($gettok(%temp,2,32) != $WhiteboardProtocolVersion) {
      echo $colour(info) -is *** Whiteboard user $gettok(%temp,4,32) is using a newer version than you. While the connection should work without many problems, it is advised that you upgrade to the newer version, available at http://www3.nbnet.nb.ca/pacman/scripts.html
    }
    sockrename $sockname @wbDraw: $+ $gettok(%temp,5,32) $+ : $+ $sock($sockname).ip
    WhiteboardInfoRefresh $gettok(%temp,5,32)
  }

  elseif (%1 = WhiteboardGetPic) {
    sockmark $sockname $wbSetting(GraphicsFolder) $+ $gettok(%temp,2-,32)
    ; send first packet
    bread $sock($sockname).mark 0 4096 &temp
    sockwrite $sockname &temp
    ; rename it to store the length of file
    sockrename $sockname @wbSendPic $+ $ticks $+ \ $+ $lof($sock($sockname).mark)
    WhiteboardInfoRefresh
    ; rest of file send handled below
  }

  else {
    echo $colour(info) -is *** Whiteboard rejected unknown connection attempt from $sock($sockname).ip
    sockclose $sockname
  }
}


; ============================== Now that we're connected... ==============================

; ===== incoming

on 1:sockread:@wbDraw* {
  sockread %temp
  set %1 $gettok(%temp,1,32)  

  if ((%1 = drawdot) || (%1 = drawline) || (%1 = drawrect) || (%1 = drawfill) || (%1 = drawtext) || (%1 = drawscroll) || (%1 = drawcopy)) && ($window($gettok(%temp,3,32)) != $null) && ($left($gettok(%temp,3,32),12) = @Whiteboard:) {
    %temp
    return
  }

  elseif ((%1 = clear) && ($window($gettok(%temp,2,32)) != $null) && ($left($gettok(%temp,2,32),12) = @Whiteboard:)) {
    %temp
  }

  elseif (%1 = drawpic) {
    ; if we have the picture in the cache, draw it. Otherwise, request a copy
    if ($exists( [ $wbSetting(GraphicsFolder) $+ [ $gettok(%temp,6-,32) ] ] )) { $gettok(%temp,1-5,32) $wbSetting(GraphicsFolder) $+ $gettok(%temp,6-,32) }

    else {
      set -u2 %tempSock @wbGetPic $+ $ticks
      sockopen %tempSock $sock($sockname).ip 831
      ; save the original command in the sockmark
      sockmark %tempSock %temp
    }
  }

  elseif (%1 = WhiteboardMessage) {
    WhiteboardChatWindow $gettok($sockname,2,58)
    echo -i2 @Whiteboard-Chat: $+ $gettok($sockname,2,58) < $+ $sock($sockname).mark $+ > $gettok(%temp,2-,32)
  }

  elseif (%1 = WhiteboardAction) {
    WhiteboardChatWindow $gettok($sockname,2,58)
    echo $colour(action) -i2 @Whiteboard-Chat: $+ $gettok($sockname,2,58) * $sock($sockname).mark $gettok(%temp,2-,32)
  }

  elseif (%1 = WhiteboardConnectedTo) {
    if ($sock( [ @wbDraw: $+ [ $gettok($sockname,2,58) ] $+ : $+ [ $gettok(%temp,2,32) ] ] ) = $null) { echo $colour(info) -is *** $sock($sockname).mark has another participant on this whiteboard. To connect this person, type /whiteboard $gettok(%temp,2,32) $gettok($sockname,2,58) }
  }

  elseif (%1 = WHITEBOARD) {
    echo $colour(info) -is *** Whiteboard ' $+ $gettok($sockname,2,58) $+ ' connection to $sock($sockname).ip ( $+ $gettok(%temp,3,32) $+ ) is $gettok(%temp,4,32)
    sockmark $sockname $gettok(%temp,4,32)
    WhiteboardInfoRefresh $gettok($sockname,2,58)
  }
}

; ===== outgoing

alias WhiteboardDo {
  $1-
  if ($sock( [ @wbDraw: $+ [ $gettok($active,2,58) ] $+ :* ] ,0) > 0) { sockwrite -n @wbDraw: $+ $gettok($active,2,58) $+ :* $1- }
}


; ===== when the window is closed, shut down all user connections and the listening connection

on 1:close:@Whiteboard-*:return

on 1:close:@Whiteboard* {
  echo $colour(info) -s *** Whiteboard ' $+ $gettok($target,2,58) $+ ' connections closed
  sockclose @wbDraw: $+ $gettok($target,2,58) $+ :*
  ; check if all whiteboard windows are closed
  .timer 1 1 wbCheckState
}

on 1:sockclose:@wbDraw* {
  echo $colour(info) -s *** Whiteboard ' $+ $gettok($sockname,2,58) $+ ' connection with $sock($sockname).mark ( $+ $sock($sockname).ip $+ ) closed
  .timer 1 1 WhiteboardInfoRefresh $gettok($sockname,2,58)
}


; ==================== PICTURE SENDING

; --- packet was received, go on.

on 1:sockwrite:@wbSendPic* {
  if ($sockerr > 0) { halt }
  ; if (position >= lof)
  if ($sock($sockname).sent >= $gettok($sockname,2,92)) {
    ; send complete
    sockclose $sockname
    WhiteboardInfoRefresh
    return
  }
  ;bread filename position length variable
  bread $sock($sockname).mark $sock($sockname).sent 4096 &temp
  sockwrite $sockname &temp
}


; ===== PICTURE RECEIVING

on 1:sockopen:@wbGetPic* {
  ;  1:drawpic 2:-h 3:$active 4:$mouse.x 5:$mouse.y 6:$gettok(%tempFile,$gettok(%tempFile,0,92),92)
  sockwrite -n $sockname WhiteboardGetPic $gettok($sock($sockname).mark,6-,32)
  WhiteboardInfoRefresh
}

on 1:sockread:@wbGetPic* {
  set %tempFile $wbSetting(GraphicsFolder) $+ [ $gettok($sock($sockname).mark,6-,32) ]
  :NextPacket
  sockread &temp
  bwrite %tempFile -1 &temp
  if ($sock($sockname).rq > 0) goto NextPacket
}

on 1:sockclose:@wbGetPic* {
  ; if we now have the picture in the cache, draw it. Otherwise, there was an error
  if ($exists( [ $wbSetting(GraphicsFolder) $+ [ $gettok($sock($sockname).mark,6-,32) ] ] )) { $gettok($sock($sockname).mark,1-5,32) $wbSetting(GraphicsFolder) $+ $gettok($sock($sockname).mark,6-,32) }

  else { echo $colour(info) -s *** Error receiving picture $gettok($sock($sockname).mark,6-,32) from $sock($sockname).ip }

  .timer 1 1 WhiteboardInfoRefresh
}

; ==================== Chatting

on 1:input:@Whiteboard* {
  if ($left($1,1) = /) && ($1 != /me) { return }

  WhiteboardChatWindow $gettok($active,2,58)
  if ($1 = /me) {
    if ($sock(@wbDraw*,0) > 0) { sockwrite -n @wbDraw: $+ $gettok($active,2,58) $+ :* WhiteboardAction $2- }
    echo $colour(action) @Whiteboard-Chat: $+ $gettok($active,2,58) * $me $2-
  }
  else {
    if ($sock(@wbDraw*,0) > 0) { sockwrite -n @wbDraw: $+ $gettok($active,2,58) $+ :* WhiteboardMessage $1- }
    echo @Whiteboard-Chat: $+ $gettok($active,2,58) < $+ $me $+ > $1-
  }
  halt
}

alias WhiteboardChatWindow {
  if ($window( [ @Whiteboard-Chat: $+ [ $1 ] ] ) != $null) { return }

  set %tempPos $readini $mircini Windows wquery
  set %tempPos $gettok($replace(%tempPos,$chr(44),$chr(32)),1-4,32)
  set %tempPos $gettok(%tempPos,1,32) $gettok(%tempPos,3,32) $gettok(%tempPos,2,32) $gettok(%tempPos,4,32)
  set %tempMenu $readini $mircini pfiles n2
  set %tempFont $readini $mircini Fonts fquery
  set %tempFont $gettok(%tempFont,1,44)
  window -e @Whiteboard-Chat: $+ $1 %tempPos %tempFont
  unset %tempPos %tempMenu %tempFont
}


; ============================== CONNECTION MANAGER ==============================

alias whiteboardInfo {
  window -l -t12,22,37,45,53,61 @Whiteboard-Info 50 50 306 180 @Whiteboard-Info
  WhiteboardInfoRefresh
}

alias WhiteboardInfoRefresh {
  if ($1 != $null) { titlebar @Whiteboard: $+ $1 - $sock( [ @wbDraw: $+ [ $1 ] $+ :* ] ,0) connections }  

  if ($window(@Whiteboard-Info) = $null) { return }

  if ($sock(@wbListen) = $null) { titlebar @Whiteboard-Info - not listening }
  else { titlebar @Whiteboard-Info }

  clear @Whiteboard-Info
  aline @Whiteboard-Info Double-Click here to add new connection...
  aline @Whiteboard-Info $str(_,79)
  aline @Whiteboard-Info Nick	Using	IP	Sent	Received	SendIdle	RecvIdle
  aline @Whiteboard-Info $str($chr(0175),79)
  set %temp 1
  :n1
  if ($sock(@wbDraw*,%temp) = $null) { goto pics }
  aline @Whiteboard-Info $sock(@wbDraw*,%temp).mark $+ $chr(9) $+ $gettok($sock(@wbDraw*,%temp),2,58) $+ $chr(9) $+ $sock(@wbDraw*,%temp).ip $+ $chr(9) $+ $sock(@wbDraw*,%temp).sent $+ $chr(9) $+ $sock(@wbDraw*,%temp).rcvd $+ $chr(9) $+ $gettok($duration($sock(@wbDraw*,%temp).ls),1,32) $+ $chr(9) $+ $gettok($duration($sock(@wbDraw*,%temp).lr),1,32)
  inc %temp
  goto n1

  :pics
  aline @Whiteboard-Info $str(_,79)
  aline @Whiteboard-Info File	IP	Transferred
  aline @Whiteboard-Info $str($chr(0175),79)
  set %temp 1
  :n2
  if ($sock(@wbGetPic*,%temp) = $null) { goto more }
  aline @Whiteboard-Info $gettok($sock(@wbGet*,%temp).mark,6-,32) $+ $chr(9) $+ $sock(@wbGet*,%temp).ip $+ $chr(9) $+ $sock(@wbGet*,%temp).rcvd rcvd
  inc %temp
  goto n2

  :more
  set %temp 1
  :n3
  if ($sock(@wbSend*,%temp) = $null) { goto end }
  aline @Whiteboard-Info $gettok($sock(@wbSend*,%temp).mark,$gettok($sock(@wbSend*,%temp).mark,0,92),92) $chr(9) $+ $sock(@wbSend*,%temp).ip $+ $chr(9) $+ $sock(@wbSend*,%temp).sent sent
  inc %temp
  goto n3
  :end
  unset %temp
}

menu @Whiteboard-Info {
  dclick {
    if ($sline(@Whiteboard-Info,1).ln = 1) { whiteboard $$?="Enter nickname, IP, or host to connect to:" }
  }
  &Disconnect User: {
    if ($sline(@Whiteboard-Info,1).ln < 5) { return }

    echo $colour(info) -s *** Whiteboard connection with $gettok($sock(@wbDraw*,$calc( [ $sline(@Whiteboard-Info,1).ln ] - 2)),2,46) ( $+ $gettok($sock(@wbDraw*,$calc( [ $sline(@Whiteboard-Info,1).ln ] - 2)),3-,46) $+ ) closed
    sockclose $sock(@wbDraw*,$calc( [ $sline(@Whiteboard-Info,1).ln ] - 4))
    .timer 1 1 WhiteboardInfoRefresh
  }
  -
  &Refresh:WhiteboardInfoRefresh
}


; --- End of File ---