From 6888be615ac5ed3db612651829b6c5c076f8eaba Mon Sep 17 00:00:00 2001 From: Aaron Bieber Date: Tue, 22 Dec 2020 07:31:45 -0700 Subject: [PATCH] +pgp key lookup tool --- plugins/pgp.go | 87 ++++++++++++++++++++++++++++++++++++++++++++++ plugins/plugins.go | 3 +- 2 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 plugins/pgp.go diff --git a/plugins/pgp.go b/plugins/pgp.go new file mode 100644 index 0000000..1463063 --- /dev/null +++ b/plugins/pgp.go @@ -0,0 +1,87 @@ +package plugins + +import ( + "encoding/hex" + "fmt" + "net/http" + "net/url" + "regexp" + "strings" + + "github.com/matrix-org/gomatrix" + "golang.org/x/crypto/openpgp" +) + +// PGP is our plugin type +type PGP struct { +} + +// SetStore is the setup function for a plugin +func (p *PGP) SetStore(s PluginStore) { +} + +// Descr describes this plugin +func (p *PGP) Descr() string { + return "Queries keys.openpgp.org" +} + +// Re is what our pgp request matches +func (p *PGP) Re() string { + return `(?i)^pgp: (.+@.+\..+)$` +} + +// Match checks for "pgp: " messages +func (p *PGP) Match(_, msg string) bool { + re := regexp.MustCompile(p.Re()) + return re.MatchString(msg) +} + +func (p *PGP) fix(msg string) string { + re := regexp.MustCompile(p.Re()) + return 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) + escSearch, err := url.Parse(search) + if err != nil { + return err + } + + u := fmt.Sprintf("https://keys.openpgp.org//vks/v1/by-email/%s", + escSearch) + + resp, err := http.Get(u) + if err != nil { + _ = SendText(c, ev.RoomID, fmt.Sprintf("Can't search keys.openpgp.org: %s", err)) + return err + } + + defer resp.Body.Close() + + kr, err := openpgp.ReadArmoredKeyRing(resp.Body) + if err != nil { + _ = SendText(c, ev.RoomID, fmt.Sprintf("Can't parse key ring: %s", err)) + return err + } + + var ids []string + var fps []string + for _, entity := range kr { + for _, i := range entity.Identities { + ids = append(ids, fmt.Sprintf("- %q", i.Name)) + } + fps = append(fps, fmt.Sprintf("**Fingerprint**: %s", + hex.EncodeToString(entity.PrimaryKey.Fingerprint[:]))) + } + + return SendMD(c, ev.RoomID, fmt.Sprintf("%s\n\n%s", + strings.Join(ids, "\n"), + strings.Join(fps, "\n"))) +} + +// Name PGP! +func (p *PGP) Name() string { + return "PGP" +} diff --git a/plugins/plugins.go b/plugins/plugins.go index c6d1515..5e5a617 100644 --- a/plugins/plugins.go +++ b/plugins/plugins.go @@ -243,13 +243,14 @@ var Plugs = Plugins{ &Hi{}, &LoveYou{}, &OpenBSDMan{}, + &PGP{}, &Palette{}, + &RFC{}, &Snap{}, &Source{}, &Thanks{}, &Toki{}, &Version{}, &Wb{}, - &RFC{}, &Weather{}, }