aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJesse Laprade <jesselaprade@gmail.com>2021-03-21 22:33:25 -0400
committerJesse Laprade <jesselaprade@gmail.com>2021-03-21 22:33:25 -0400
commit3b0dc64812588d2752524a0790bc980e86578026 (patch)
treed670c4c575362919ea81b4a28234637a531056c2
parent864d432dbdf66fdb4821c3c08c8652101aada1e8 (diff)
downloadruth-3b0dc64812588d2752524a0790bc980e86578026.tar.gz
ruth-3b0dc64812588d2752524a0790bc980e86578026.tar.bz2
ruth-3b0dc64812588d2752524a0790bc980e86578026.zip
WIP: Now parses nicknames and usernames in a cleaner and more organized way
-rw-r--r--src/ruth.rkt56
1 files changed, 27 insertions, 29 deletions
diff --git a/src/ruth.rkt b/src/ruth.rkt
index e84d63f..b514296 100644
--- a/src/ruth.rkt
+++ b/src/ruth.rkt
@@ -50,12 +50,6 @@
(define command-crisis "!crisis")
(define command-pluckafuck "!pluckafuck")
-;; Big shoutout to ~lucidiot for helping me out with regex
-(define regexp-nickname #rx"^:([A-Za-z0-9_-~]+)![A-Za-z0-9_-~]+@[A-Za-z0-9._-]+ [A-Za-z0-9_-]+ [#&][A-Za-z0-9_-]+ :.*$")
-(define regexp-username #rx"^:[A-Za-z0-9_-~]+!([A-Za-z0-9_-~]+)@[A-Za-z0-9._-]+ [A-Za-z0-9_-]+ [#&][A-Za-z0-9_-]+ :.*$")
-(define regexp-channel #rx"^:[A-Za-z0-9_-~]+![A-Za-z0-9_-~]+@[A-Za-z0-9._-]+ [A-Za-z0-9_-]+ ([#&][A-Za-z0-9_-]+) :.*$")
-(define regexp-message #rx"^:[A-Za-z0-9_-~]+![A-Za-z0-9_-~]+@[A-Za-z0-9._-]+ [A-Za-z0-9_-]+ [#&][A-Za-z0-9_-]+ :(.*)$")
-
;; ------------------------------------------------------------------------
;; Commands
;; ------------------------------------------------------------------------
@@ -69,7 +63,6 @@
bot-name
bot-name))
-
(define (cpu)
(shell-command
"ps -A -o pcpu | tail -n+2 | paste -sd+ | bc | awk '{print \"cpu used: \" $1 \"%\"}'"))
@@ -180,32 +173,37 @@
(send-message channel "fuuuuuuuuuuuuuccccccccccccccccccccccccckkkkkkkkkkk")
'hangout)))
-;; If regexp-match finds a match, it returns a two-item list when
-;; parentheses exist inside of the #rx expression. If regex-match
-;; doesn't find anything, it returns #f.
+(define (get-nickname split-message)
+ (let* ([nickname-block (list-ref split-message 0)] ;; :nickname!username@whatever
+ [colon-removed (substring nickname-block 1)] ;; nickname!username@whatever
+ [nickname (string-trim colon-removed #px"(!.+)$")])
+ nickname))
+
+(define (get-username split-message)
+ (let* ([username-block (list-ref split-message 0)] ;; :username!username@whatever
+ [username-matches (regexp-match #rx"![~]?(.+)@" username-block)] ;; '("!username@" "username")
+ [username (list-ref username-matches 1)])
+ username))
+
+(define (get-channel split-message)
+ ;; TODO
+ )
+
+(define (get-message split-message)
+ ;; TODO
+ )
;; This accepts a server string that looks something like:
+;; nick username channel message
;; :m455!m455@localhost PRIVMSG #tildetown :hmmm let me open up town's tmux
-(define (check-for-match server-data-string key)
- (case key
- ['nickname (regexp-match regexp-nickname server-data-string)]
- ['username (regexp-match regexp-username server-data-string)]
- ['channel (regexp-match regexp-channel server-data-string)]
- ['message (regexp-match regexp-message server-data-string)]))
-
(define (commands-listen server-data-string)
- (let* ([get-data (lambda (key)
- (let ([match-result? (check-for-match server-data-string key)])
- ;; The (when ...) prevents (second ...) from being called
- ;; on a #f if no matches are found
- (when match-result?
- (string-trim (second match-result?)))))]
- [nickname (get-data 'nickname)]
- [username (get-data 'username)]
- [channel (get-data 'channel)]
- [message (get-data 'message)])
- (when (string? message)
- (match (string-split message)
+ (let ([split-message (string-split message)])
+ (when (equal? (list-ref split-message 1) "PRIVMSG")
+ (let* ([nickname (get-nickname split-message)];;DONE
+ [username (get-username split-message)];;DONE
+ [channel (get-channel split-message)] ;;TODO
+ [message (get-message split-message)]);;TODO
+ (match message
;; 2-item matches
[(list (== command-water)
(== bot-name))