Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2013
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scheme 2.91 KB | None | 0 0
  1. #lang racket
  2. ;bind ports, set buffer mode.
  3. (define-values (input-port output-port) (tcp-connect "laserblue.org" 6667))
  4. (file-stream-buffer-mode output-port 'block)
  5.  
  6.  
  7. ;initial send initial connection information.
  8. (write-bytes #"USER Kaylin * laserblue.org :Kaylin\r\n" output-port)
  9. (write-bytes #"NICK Kayscript\r\n" output-port)
  10. (flush-output output-port)
  11.  
  12.  
  13. (define (irc-connect input-port output-port)
  14.   (if (regexp-match-peek #rx".*?\r\n" input-port);if it's a full line
  15.       (cond
  16.         ;reply to ping commands (send the wrong reply so I get an error message)
  17.         [(regexp-match-peek #rx"^PING.*?\r\n" input-port 0 512) (write-bytes (regexp-replace #"PING" (car (regexp-match #rx"^PING.*?\r\n" input-port)) #"WRONGREPLY") output-port)
  18.                                                                 (flush-output output-port)
  19.                                                                 (printf "Replied to server PING.\r\n")
  20.                                                                 (irc-connect input-port output-port)]
  21.         ;login when prompted
  22.         [(regexp-try-match #rx"^:NickServ.*?msg NickServ identify <password>.*?\r\n" input-port 0 512) (write-bytes #"PRIVMSG NickServ :identify notreallymypw\r\n" output-port)
  23.                                                                                                        (flush-output output-port)(printf "Identified to NickServ.\r\n")
  24.                                                                                                        (write-bytes #"JOIN #laserblue\r\n" output-port)
  25.                                                                                                        (flush-output output-port)(printf "Joined #laserblue.\r\n")(irc-connect input-port output-port)]
  26.         ;if the server disconnects the client...
  27.         [(regexp-try-match #rx"^ERROR.*?\r\n" input-port 0 512) (close-output-port output-port) #|paddy.mahoney:Notice that output-port here refers to the binding made in line 32, rather than the enclosing lambda. |#
  28.                                                                 (close-input-port input-port)
  29.                                                                 (printf "Server closed the connection.\r\nAttempting to reconnect...\r\n")
  30.                                                                 (sleep 30)
  31.                                                                 (define-values (input-port output-port) (tcp-connect "laserblue.org" 6667))
  32.                                                                 (irc-connect input-port output-port) #|ignore the fact that this does not relog it merely connects to the server |#]
  33.        
  34.        
  35.         ;discard all other lines.
  36.         [(regexp-match #rx"^.*?\r\n" input-port 0 512)(irc-connect input-port output-port)]
  37.         [else (irc-connect input-port output-port)])
  38.       (error "Not a full line.")))
  39.  
  40. (irc-connect input-port output-port)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement