~williewillus/public-inbox

This thread contains a patchset. You're looking at the original emails, but you may wish to use the patch review UI. Review patch
1

[PATCH v3] implement IRC-bot message integration

Details
Message ID
<20240816045925.742962-1-wenming.yi@gmail.com>
DKIM signature
pass
Download raw message
Patch: +36 -9
From: Alwinfy <20421383+Alwinfy@users.noreply.github.com>

fix a use-define oopsies.

also destructure the regex match because "people don't like caadrs winfy".
1984.

---
 frontends/discord.rkt | 44 ++++++++++++++++++++++++++++++++++---------
 scribblings/r16.scrbl |  1 +
 2 files changed, 36 insertions(+), 9 deletions(-)

diff --git a/frontends/discord.rkt b/frontends/discord.rkt
index 4c1df54..fe85694 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,30 @@
                (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
        (match-let ([(list (cons _ message-start) (cons nick-start nick-end)) match])
          (cons
            (string-append
              original-sender
              "/"
              (substring contents nick-start nick-end))
            (substring contents message-start)))))

    (define (get-message-author-and-contents message)
      (define base-content (string-trim (hash-ref message 'content)))
      (define author (message-author-id message))
      (cond
        [(set-member? irc-bridge-bots author)
         (strip-irc-header author base-content)]
        [(message-from-bot? message) #f]
        [else (cons author 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 +233,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 +287,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 +418,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)
          (match-define (cons func func-args) (parse-command content))
        (define author-and-content (get-message-author-and-contents message))
        (when author-and-content
          (match-define (cons func func-args) (parse-command (cdr content)))
          (when func
            (define channel (hash-ref message 'channel_id))
            (define contents
              (with-handlers
                ([exn?
@@ -516,7 +540,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 +699,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 +709,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
Details
Message ID
<87o75s58gz.fsf@vincent-lee.net>
In-Reply-To
<20240816045925.742962-1-wenming.yi@gmail.com> (view parent)
DKIM signature
pass
Download raw message
Pushed with a small compile fix

wenming.yi@gmail.com writes:

> From: Alwinfy <20421383+Alwinfy@users.noreply.github.com>
>
> fix a use-define oopsies.
>
> also destructure the regex match because "people don't like caadrs winfy".
> 1984.
>
> ---
>  frontends/discord.rkt | 44 ++++++++++++++++++++++++++++++++++---------
>  scribblings/r16.scrbl |  1 +
>  2 files changed, 36 insertions(+), 9 deletions(-)
>
> diff --git a/frontends/discord.rkt b/frontends/discord.rkt
> index 4c1df54..fe85694 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,30 @@
>                  (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
> +        (match-let ([(list (cons _ message-start) (cons nick-start nick-end)) match])
> +          (cons
> +            (string-append
> +              original-sender
> +              "/"
> +              (substring contents nick-start nick-end))
> +            (substring contents message-start)))))
> +
> +    (define (get-message-author-and-contents message)
> +      (define base-content (string-trim (hash-ref message 'content)))
> +      (define author (message-author-id message))
> +      (cond
> +        [(set-member? irc-bridge-bots author)
> +         (strip-irc-header author base-content)]
> +        [(message-from-bot? message) #f]
> +        [else (cons author 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 +233,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 +287,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 +418,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)
> -          (match-define (cons func func-args) (parse-command content))
> +        (define author-and-content (get-message-author-and-contents message))
> +        (when author-and-content
> +          (match-define (cons func func-args) (parse-command (cdr content)))
>            (when func
> +            (define channel (hash-ref message 'channel_id))
>              (define contents
>                (with-handlers
>                  ([exn?
> @@ -516,7 +540,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 +699,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 +709,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}
Reply to thread Export thread (mbox)