[PATCH r16 v2] implement IRC-bot message integration
Export this patch
From: Alwinfy <20421383+Alwinfy@users.noreply.github.com>
Implemented suggestions from v1; an upgrade to an "allowed_bots" field
will be done later(tm) probably. Regex also cleaned up.
Also, this allows people to assume identities over the bridge;
internally their ID is "<bot snowflake>/<nick>".
Probably abusable? But impersonation is near unavoidable on IRC anyway.
---
frontends/discord.rkt | 40 ++++++++++++++++++++++++++++++++--------
scribblings/r16.scrbl | 1 +
2 files changed, 33 insertions(+), 8 deletions(-)
diff --git a/frontends/discord.rkt b/frontends/discord.rkt
index 4c1df54..3e7205b 100644
--- a/frontends/discord.rkt
+++ b/frontends/discord.rkt
@@ -87,6 +87,7 @@
(init-field bot-prefix)
(init-field trick-prefix)
(init-field delete-time-sec)
+ (init-field irc-bridge-bots)
(define with-typing-indicator ;; (_ proc)
(let ()
@@ -162,6 +163,28 @@
(list (string-append "\n:warning: stderr:\n" stderr))
null))))
+ (define (strip-irc-header original-sender contents)
+ (define match (regexp-match-positions #px"^<([^>]+)>\\s+" contents))
+ (and match
+ (cons
+ (string-append
+ original-sender
+ "/"
+ (substring contents (caadr match) (cdadr match)))
+ (substring contents (cdar match)))))
+
+ (define (get-message-author-and-contents message)
+ (define base-content (string-trim (hash-ref message 'content)))
+ (cond
+ [(set-member? irc-bridge-bots (message-author-id message))
+ (strip-irc-header base-content)]
+ [(message-from-bot? message) #f]
+ [else (cons (message-author-id message) base-content)]))
+
+ (define (get-message-author message)
+ (define author-and-contents (get-message-author-and-contents message))
+ (and author-and-contents (car author-and-contents)))
+
(define/public (response? v)
(http:attachment? v))
@@ -208,10 +231,9 @@
(define/public (get-enrich-context)
(define deleted-box (current-deleted-box))
(define message (current-message))
- (define message-contents (hash-ref message 'content))
+ (match-define (cons message-author message-contents) (get-message-author-and-contents message))
(define message-attachments (or (hash-ref message 'attachments #f) null))
(define reply-message-attachments (or (and~> message (hash-ref 'referenced_message #f) (hash-ref 'attachments #f)) null))
- (define message-author (message-author-id message))
(define channel-id (hash-ref message 'channel_id))
(define/contract (emote-lookup name)
@@ -263,7 +285,7 @@
(match type
['guild (cons 65536 'global)]
['channel (cons 8192 (string->symbol channel-id))]
- ['user (cons 2048 (string->symbol (message-author-id message)))]
+ ['user (cons 2048 (string->symbol message-author))]
[_ (cons 0 #f)]))
(define/contract ((read-storage trick) type)
@@ -394,11 +416,11 @@
[current-message message]
[current-deleted-box (box #f)]
[current-context-id (context-id message)])
- (define content (string-trim (hash-ref message 'content)))
- (define channel (hash-ref message 'channel_id))
- (unless (message-from-bot? message)
+ (define content (get-message-contents message))
+ (when content
(match-define (cons func func-args) (parse-command content))
(when func
+ (define channel (hash-ref message 'channel_id))
(define contents
(with-handlers
([exn?
@@ -516,7 +538,7 @@
(define result
(send (current-backend) register
name (strip-backticks body)
- (message-author-id (current-message))
+ (get-message-author (current-message))
(discord-timestamp-to-unix (hash-ref (current-message) 'timestamp))))
(result-case list error-response result))
@@ -675,6 +697,7 @@
(define bot-prefix (hash-ref config 'bot_prefix "!rkt "))
(define trick-prefix (hash-ref config 'trick_prefix "!!"))
(define delete-time-sec (hash-ref config 'delete_time 300))
+ (define irc-bridge-bots (list->set (hash-ref config 'irc_bridge_bots '())))
(define client
(rc:make-client
token
@@ -684,4 +707,5 @@
[client client]
[bot-prefix bot-prefix]
[trick-prefix trick-prefix]
- [delete-time-sec delete-time-sec]))
+ [delete-time-sec delete-time-sec]
+ [irc-bridge-bots irc-bridge-bots]))
diff --git a/scribblings/r16.scrbl b/scribblings/r16.scrbl
index 6653b8f..fd719ad 100644
--- a/scribblings/r16.scrbl
+++ b/scribblings/r16.scrbl
@@ -95,6 +95,7 @@ The @tt{frontend} object in the configuration file can have the following keys a
@item{@tt{trick_prefix} is a string specifying the bot's shorthand prefix. If not present, defaults to @code{"!!"}.}
@item{@tt{delete_time} is a nonnegative integer specifying a time in seconds. If any invocation of R16 is deleted within this time, then R16 will also delete its response.
This helps with spam due to incorrect trick invocations. If not present, defaults to 5 minutes (300 seconds). Set to 0 to disable this feature.}
+@item{@tt{irc_bridge_bots} is an array of snowflake strings representing a whitelist of bots which r16 should allow parsing IRC-style messages (of the format @code{<nickname> words...}) from. Defaults empty.}
]
@subsection{Discord Trick Environment Extensions}
--
2.34.1
r16/patches/linux_buildtest.yml: FAILED in 43s
[implement IRC-bot message integration][0] v2 from [][1]
[0]: https://lists.sr.ht/~williewillus/public-inbox/patches/54492
[1]: mailto:wenming.yi@gmail.com
✗ #1304879 FAILED r16/patches/linux_buildtest.yml https://builds.sr.ht/~williewillus/job/1304879