From 26647bc2358ce1e4eb54ce3caefb2c81525e32b4 Mon Sep 17 00:00:00 2001 From: Aaron Bieber Date: Mon, 4 Jan 2021 21:05:15 -0700 Subject: [PATCH] extend pgp plugin to check email and fingerprint --- README.md | 2 +- plugins/pgp.go | 13 +++++++++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 6ff7154..1f20414 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ |Hi|`(?i)^hi|hi$`|Friendly bots say hi.| |LoveYou|`(?i)i love you`|Spreading love where ever we can by responding when someone shows us love.| |OpenBSDMan|`(?i)^man: ([1-9][p]?)?\s?(.+)$`|Produces a link to man.openbsd.org.| -|PGP|`(?i)^pgp: (.+@.+\..+)$`|Queries keys.openpgp.org| +|PGP|`(?i)^pgp: (.+@.+\..+|[a-f0-9]+)$`|Queries keys.openpgp.org| |Palette|`(?i)^#[a-f0-9]{6}$`|Creates an solid 56x56 image of the color specified.| |RFC|`(?i)^rfc\s?([0-9]+)$`|Produces a link to tools.ietf.org.| |Snap|`(?i)^snap:$`|checks the current build date of OpenBSD snapshots.| diff --git a/plugins/pgp.go b/plugins/pgp.go index 1463063..5a8a5cb 100644 --- a/plugins/pgp.go +++ b/plugins/pgp.go @@ -27,7 +27,7 @@ func (p *PGP) Descr() string { // Re is what our pgp request matches func (p *PGP) Re() string { - return `(?i)^pgp: (.+@.+\..+)$` + return `(?i)^pgp: (.+@.+\..+|[a-f0-9]+)$` } // Match checks for "pgp: " messages @@ -38,19 +38,24 @@ func (p *PGP) Match(_, msg string) bool { func (p *PGP) fix(msg string) string { re := regexp.MustCompile(p.Re()) - return re.ReplaceAllString(msg, "$1") + return strings.ToUpper(re.ReplaceAllString(msg, "$1")) } // RespondText to looking up of PGP info func (p *PGP) RespondText(c *gomatrix.Client, ev *gomatrix.Event, _, post string) error { search := p.fix(post) + searchURL := "https://keys.openpgp.org//vks/v1/by-fingerprint/%s" + + if strings.ContainsAny(search, "@") { + searchURL = "https://keys.openpgp.org//vks/v1/by-email/%s" + } + escSearch, err := url.Parse(search) if err != nil { return err } - u := fmt.Sprintf("https://keys.openpgp.org//vks/v1/by-email/%s", - escSearch) + u := fmt.Sprintf(searchURL, escSearch) resp, err := http.Get(u) if err != nil {