Introduction to newLISP/The Internet

The internetEdit

HTTP and networkingEdit

Most networking tasks are possible with newLISP's networking functions:

  • base64-dec decode a string from BASE64 format
  • base64-enc encode a string to BASE64 format
  • delete-url delete a URL
  • get-url read a file or page from the web
  • net-accept accept a new incoming connection
  • net-close close a socket connection
  • net-connect connect to a remote host
  • net-error return the last error
  • net-eval evaluate expressions on multiple remote newLISP servers
  • net-interface define the default network interface
  • net-listen listen for connections to a local socket
  • net-local local IP and port number for a connection
  • net-lookup the name for an IP number
  • net-peek number of characters ready to read
  • net-peer remote IP and port for a net-connect
  • net-ping send a ping packet (ICMP echo request) to one or more addresses
  • net-receive read data on a socket connection
  • net-receive-from read a UDP datagram on an open connection
  • net-receive-udp read a UDP datagram on and closes connection
  • net-select check a socket or list of sockets for status
  • net-send send data on a socket connection
  • net-send-to send a UDP datagram on an open connection
  • net-send-udp send a UDP datagram and closes connection
  • net-service translate a service name to a port number
  • net-sessions return a list of currently open connections
  • post-url post info to a URL address
  • put-url upload a page to a URL address.
  • xml-error return last XML parse error
  • xml-parse parse an XML document
  • xml-type-tags show or modify XML type tags

With these networking functions you can build all kinds of network-capable applications. With functions like net-eval you can start newLISP as a daemon on a remote computer and then use on a local computer to send newLISP code across the network for evaluation.

Accessing web pagesEdit

Here's a very simple example using get-url. Given the URL of a web page, obtain the source and then use replace and its list-building ability to generate a list of all the JPEG images on that page:

(set 'the-source (get-url ""))
(replace {src="(http\S*?jpg)"} the-source (push $1 images-list -1) 0)
(println images-list)

A simple HTML FormEdit

The simplest search form is probably something like this.

(load "cgi.lsp")
(println (string "Content-type: text/html\r\n\r\n"
    [text]<!doctype html>

(set 'search-string (CGI:get "userinput"))

(println (format [text]
        <form name="form" class="dialog" method="GET">
                 <input type="text" value="search" name="userinput" >
                 <input type="submit" style="display:none"/>

(unless (nil? search-string)
    (println " I couldn't be bothered to search for \"" search-string "\""))

(println [text]

A simple IRC clientEdit

The following code implements a simple IRC (Internet Relay Chat) client, and it shows how the basic network functions can be used. The script logs in to the server using the given username, and joins the # newlisp channel. Then the script divides into two threads: the first thread displays any channel activity in a continuous loop, while the second thread waits for input at the console. The only communication between the two threads is through the shared connected flag.

(set 'server (net-connect "" 6667)) 
(net-send server "USER newlispnewb 0 * :XXXXXXX\r\n") 
(net-send server "NICK newlispnewb \r\n") 
(net-send server "JOIN #newlisp\r\n") 

(until (find "366" buffer)
  (net-receive server buffer 8192 "\n")
  (print buffer))

(set 'connected (share))
(share connected true)

    (while (share connected)
        ((net-select server "read" 1000) ; read the latest
            (net-receive server buffer 8192 "\n")
            ; ANSI colouring: output in yellow then switch back
            (print "\n\027[0;33m" buffer "\027[0;0m"))
        ((regex {^PING :(.*)\r\n} buffer) ; play ping-pong
            (net-send server (append "PONG :" (string $1 ) "\r\n"))
            (sleep 5000))
        ((net-error) ; error
            (println "\n\027[0;34m" "UH-OH: " (net-error) "\027[0;0m")
            (share connected nil)))
     (sleep 1000)))
(while (share connected) 
   (sleep 1000)
   (set 'message (read-line))
     ((starts-with message "/")  ; a command?
          (net-send server (append (rest message) "\r\n"))
            (net-select server "read" 1000)
                (net-receive server buffer 8192 "\n") 
                (print "\n\027[0;35m" buffer "\027[0;0m"))))
     ((starts-with message "quit") ; quit
            (share connected nil))
     (true  ; send input as message
          (net-send server (append "PRIVMSG #newlisp :" message "\r\n")))))

(println "finished; closing server")
(close server)