2020-12-22 07:31:45 -07:00
|
|
|
package plugins
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
|
2022-02-04 09:21:21 -07:00
|
|
|
"github.com/ProtonMail/go-crypto/openpgp"
|
2020-12-22 07:31:45 -07:00
|
|
|
"github.com/matrix-org/gomatrix"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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 {
|
2021-01-04 21:05:15 -07:00
|
|
|
return `(?i)^pgp: (.+@.+\..+|[a-f0-9]+)$`
|
2020-12-22 07:31:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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())
|
2021-01-04 21:05:15 -07:00
|
|
|
return strings.ToUpper(re.ReplaceAllString(msg, "$1"))
|
2020-12-22 07:31:45 -07:00
|
|
|
}
|
|
|
|
|
2021-04-01 16:15:25 -06:00
|
|
|
func (p *PGP) Process(from, post string) string {
|
2020-12-22 07:31:45 -07:00
|
|
|
search := p.fix(post)
|
2021-01-04 21:05:15 -07:00
|
|
|
searchURL := "https://keys.openpgp.org//vks/v1/by-fingerprint/%s"
|
|
|
|
|
|
|
|
if strings.ContainsAny(search, "@") {
|
|
|
|
searchURL = "https://keys.openpgp.org//vks/v1/by-email/%s"
|
|
|
|
}
|
|
|
|
|
2020-12-22 07:31:45 -07:00
|
|
|
escSearch, err := url.Parse(search)
|
|
|
|
if err != nil {
|
2021-04-01 16:15:25 -06:00
|
|
|
return err.Error()
|
2020-12-22 07:31:45 -07:00
|
|
|
}
|
|
|
|
|
2021-01-04 21:05:15 -07:00
|
|
|
u := fmt.Sprintf(searchURL, escSearch)
|
2020-12-22 07:31:45 -07:00
|
|
|
|
|
|
|
resp, err := http.Get(u)
|
|
|
|
if err != nil {
|
2021-04-01 16:15:25 -06:00
|
|
|
return err.Error()
|
2020-12-22 07:31:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
kr, err := openpgp.ReadArmoredKeyRing(resp.Body)
|
|
|
|
if err != nil {
|
2021-04-01 16:15:25 -06:00
|
|
|
return err.Error()
|
2020-12-22 07:31:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
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[:])))
|
|
|
|
}
|
|
|
|
|
2021-04-01 16:15:25 -06:00
|
|
|
return fmt.Sprintf("%s\n\n%s",
|
2020-12-22 07:31:45 -07:00
|
|
|
strings.Join(ids, "\n"),
|
2021-04-01 16:15:25 -06:00
|
|
|
strings.Join(fps, "\n"))
|
|
|
|
}
|
|
|
|
|
|
|
|
// RespondText to looking up of PGP info
|
|
|
|
func (p *PGP) RespondText(c *gomatrix.Client, ev *gomatrix.Event, _, post string) error {
|
|
|
|
return SendMD(c, ev.RoomID, p.Process(ev.Sender, post))
|
2020-12-22 07:31:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Name PGP!
|
|
|
|
func (p *PGP) Name() string {
|
|
|
|
return "PGP"
|
|
|
|
}
|