All pastes #460516 Raw Edit

Anonymous

public text v1 · immutable
#460516 ·published 2007-04-27 02:49 UTC
rendered paste body
alias checkword {

  ; if a socket is already opened, close it
  if ($sock(checkword,0)) { sockclose checkword }

  ; open the socket and mark it with the word
  sockopen checkword dict.org 2628
  sockmark checkword $1 0

  ; set a 30 second timer to time the connection out, send the 'checking' signal
  .timercheckword 1 30 checkword.timeout
  .signal -n checkword.checking $1
}

alias checkword.timeout {

  ; send the timeout signal, close the socket
  .signal -n checkword.timeout
  sockclose checkword
}
on 1:sockopen:checkword:{

  ; send the error signal if there is an error, otherwise turn the timeout timer off
  if ($sockerr) { .signal -n checkword.error $sock($sockname).wsmsg }
  else { .timercheckword off }
}
on 1:sockread:checkword:{

  ; send the error signal if there is an error
  if ($sockerr) { .signal -n checkword.error $sock($sockname).wsmsg }
  :read
  sockread %checkword
  if (%checkword) {
    tokenize 32 %checkword

    ; send the DEFINE command and the word to the dict server
    if ($1 = 220) { sockwrite -n $sockname DEFINE ! $gettok($sock($sockname).mark,1,32) }

    ; if 150 is returned, an exact match was found, so the word is correct
    if ($1 = 150) { .signal -n checkword.correct $sock($sockname).mark | sockclose $sockname }

    ; if 552 is returned, no exact matches were found
    if ($1 = 552) {

      ; make sure the second part of the sockmark isnt 1 (to keep it from executing this code over and over)
      if ($gettok($sock($sockname).mark,2,32) != 1) {
        .signal -n checkword.incorrect $sock($sockname).mark

        ; request a list of possible spellings from the server based on levenshtein distance
        sockwrite -n $sockname MATCH web1913 lev $gettok($sock($sockname).mark,1,32)

        ; once the request is made, mark the socket to keep it from looping
        sockmark $sockname $puttok($sock($sockname).mark,1,2,32)
      }

      ; if we already requested suggestions and none were returned, send the nosuggestions signal and close the socket
      else { .signal -n checkword.nosuggestions $gettok($sock($sockname).mark,1,32) | sockclose $sockname }
    }
    if ($1 = web1913) { sockmark $sockname $sock($sockname).mark $remove($2,") }

    ; this sends the list of suggested spellings
    if ($1 = .) {
      tokenize 32 $sock($sockname).mark
      .signal -n checkword.suggestions $1 $3-
    }
    if ($1 = 250) { sockclose $sockname }
  }
  if ($sockbr) { goto read }
}