155 lines
3.9 KiB
Go
155 lines
3.9 KiB
Go
package meowlib
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"image"
|
|
"image/color"
|
|
"image/png"
|
|
"log"
|
|
"math"
|
|
"os"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/makiuchi-d/gozxing"
|
|
"github.com/makiuchi-d/gozxing/qrcode"
|
|
)
|
|
|
|
type Contact struct {
|
|
Name string `json:"name,omitempty"`
|
|
ContactPublicKey string `json:"contact_public_key,omitempty"`
|
|
EncryptionPublicKey string `json:"encryption_public_key,omitempty"`
|
|
LookupPublicKey string `json:"lookup_public_key,omitempty"`
|
|
PullServers []Server `json:"pull_servers,omitempty"`
|
|
}
|
|
|
|
type Peer struct {
|
|
Name string `json:"name,omitempty"`
|
|
Me KeyPair `json:"me,omitempty"`
|
|
Contact Contact `json:"contact,omitempty"`
|
|
Visible bool `json:"visible,omitempty"`
|
|
VisiblePassword string `json:"visible_password,omitempty"`
|
|
PasswordType string `json:"password_type,omitempty"`
|
|
Blocked bool `json:"blocked,omitempty"`
|
|
MessageNotification string `json:"message_notification,omitempty"`
|
|
OnionMode bool `json:"onion_mode,omitempty"`
|
|
Conversation []InternalMessage `json:"convdersation,omitempty"`
|
|
LastMessage time.Time `json:"last_message,omitempty"`
|
|
EncryptionKp KeyPair `json:"conversation_kp,omitempty"`
|
|
LookupKp KeyPair `json:"lookup_kp,omitempty"`
|
|
CommUrl string `json:"comm_url,omitempty"`
|
|
net Network
|
|
}
|
|
|
|
type PeerList []Peer
|
|
|
|
type Group struct {
|
|
Name string `json:"name,omitempty"`
|
|
Members []Contact `json:"members,omitempty"`
|
|
}
|
|
|
|
func (pl *PeerList) GetFromPublicKey(publickey string) *Peer {
|
|
for _, peer := range *pl {
|
|
if peer.Contact.ContactPublicKey == publickey {
|
|
return &peer
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (pl *PeerList) GetFromName(name string) *Peer {
|
|
for _, peer := range *pl {
|
|
if peer.Contact.Name == name {
|
|
return &peer
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (peer *Peer) SetCommInterface(url string) {
|
|
if strings.HasPrefix(url, "http://") {
|
|
var https *Https
|
|
https.url = url
|
|
peer.net = Network(https)
|
|
}
|
|
peer.net = nil
|
|
}
|
|
|
|
func (peer *Peer) SendText(text string) {
|
|
im := CreateText(*peer, text)
|
|
fmt.Println(im.MessageData.Destination)
|
|
}
|
|
|
|
func (contact *Contact) WritePng(filename string) {
|
|
jsonContact, _ := json.Marshal(contact)
|
|
//imgdata := base64.StdEncoding.EncodeToString(jsonContact)
|
|
size := int(math.Sqrt(float64(len(jsonContact))/3)) + 1
|
|
println(size)
|
|
|
|
// Create a colored i mage of the given width and height.
|
|
img := image.NewNRGBA(image.Rect(0, 0, size, size))
|
|
|
|
for y := 0; y < size; y++ {
|
|
for x := 0; x < size*3; x = x + 3 {
|
|
p1 := uint8(jsonContact[x+y])
|
|
p2 := uint8(jsonContact[x+y+1])
|
|
p3 := uint8(jsonContact[x+y+2])
|
|
img.Set(x/3, y, color.NRGBA{
|
|
R: p1,
|
|
G: p2,
|
|
B: p3,
|
|
A: 255,
|
|
})
|
|
}
|
|
}
|
|
|
|
f, err := os.Create(filename)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
if err := png.Encode(f, img); err != nil {
|
|
f.Close()
|
|
log.Fatal(err)
|
|
}
|
|
|
|
if err := f.Close(); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
}
|
|
|
|
func (contact *Contact) WriteQr(filename string) {
|
|
jsonContact, _ := json.Marshal(contact)
|
|
qwriter := qrcode.NewQRCodeWriter()
|
|
code, err := qwriter.Encode(string(jsonContact), gozxing.BarcodeFormat_QR_CODE, 512, 512, nil)
|
|
if err != nil {
|
|
println(err.Error())
|
|
}
|
|
file, _ := os.Create("barcode.png")
|
|
defer file.Close()
|
|
|
|
// *BitMatrix implements the image.Image interface,
|
|
// so it is able to be passed to png.Encode directly.
|
|
_ = png.Encode(file, code)
|
|
|
|
}
|
|
|
|
func ReadQr(fielname string) Contact {
|
|
var contact Contact
|
|
// open and decode image file
|
|
file, _ := os.Open("qrcode.jpg")
|
|
img, _, _ := image.Decode(file)
|
|
|
|
// prepare BinaryBitmap
|
|
bmp, _ := gozxing.NewBinaryBitmapFromImage(img)
|
|
|
|
// decode image
|
|
qrReader := qrcode.NewQRCodeReader()
|
|
result, _ := qrReader.Decode(bmp, nil)
|
|
|
|
fmt.Println(result)
|
|
return contact
|
|
}
|