diff --git a/main.go b/main.go index 4e13299..1309bef 100644 --- a/main.go +++ b/main.go @@ -17,6 +17,7 @@ import ( "sync" "cwtch.im/cwtch/peer/connections" "math/rand" + "encoding/base32" ) func driftoff() { @@ -25,9 +26,15 @@ func driftoff() { } func addContactEntry(peer libpeer.CwtchPeer, name string, onion string) { + decodedPub, err := base32.StdEncoding.DecodeString(strings.ToUpper(onion[:56])) + if err != nil { + fmt.Printf("couldn't extract public key from %v.onion\n", onion) + os.Exit(1) + } + pp := model.PublicProfile{ name, - nil, + decodedPub[:32], false, false, onion} @@ -39,6 +46,7 @@ func addContactEntry(peer libpeer.CwtchPeer, name string, onion string) { peer.GetProfile().Contacts[onion] = &pp peer.GetProfile().SetCustomAttribute(onion+"_name", name) peer.GetProfile().SetCustomAttribute(name+"_onion", onion) + peer.GetProfile().TrustPeer(onion) } func exitOnEnter() { @@ -139,7 +147,7 @@ func main() { contacts := peer.GetContacts() for i := range contacts { attr, _ := peer.GetProfile().GetCustomAttribute(contacts[i] + "_name") - fmt.Printf("%v <%v>\n", attr, contacts[i]) + fmt.Printf("%v <%v> %v\n", attr, contacts[i], peer.GetContact(contacts[i]).Trusted) } case "add": if len(os.Args) != 4 { @@ -230,7 +238,7 @@ func main() { secret = strings.Join(suggestedSecret, " ") } - fmt.Println("okay, now please wait a moment while some magic happens...") + fmt.Println("please wait a moment while some magic happens...") ephemeralPeer, err := libpeer.NewCwtchPeer("Alice", "alicepass", "") if err != nil { fmt.Printf("couldn't create an ephemeral onion address: %v\n", err) @@ -340,7 +348,7 @@ func main() { for i := range groups { g := peer.GetGroup(groups[i]) groupName, _ := peer.GetProfile().GetCustomAttribute(g.GroupID + "_groupname") - fmt.Printf("%v <%v@%v>\n", groupName, g.GroupID, g.GroupServer) + fmt.Printf("%v <%v@%v> %v\n", groupName, g.GroupID, g.GroupServer, g.Accepted) } case "create": if len(os.Args) != 4 { @@ -461,6 +469,77 @@ func main() { m := <-group.NewMessage fmt.Printf("%v\n", m.Message) } + case "invite": + if len(os.Args) != 5 { + fmt.Println("example: sendafriend group invite [friendname] [groupname]") + os.Exit(1) + } + + onion, exists := peer.GetProfile().GetCustomAttribute(os.Args[3] + "_onion") + if !exists { + fmt.Fprintf(os.Stderr, "hmm you don't have a contact named %v\n", os.Args[3]) + os.Exit(1) + } + + groupId, exists := peer.GetProfile().GetCustomAttribute(os.Args[4] + "_groupid") + if !exists { + fmt.Printf("you don't seem to have a group called %v\n", os.Args[4]) + os.Exit(1) + } + + fmt.Printf("attempting to connect to %v...\n", os.Args[3]) + peer.PeerWithOnion(onion) + mp := peer.GetPeers() + for ; mp[onion] != connections.AUTHENTICATED; mp = peer.GetPeers() { + time.Sleep(time.Millisecond * 500) + } + + err = peer.InviteOnionToGroup(onion, groupId) + if err != nil { + fmt.Printf("Trusted: %v\n", peer.GetContact(onion).Trusted) + fmt.Printf("failed to invite %v to %v: %v\n", os.Args[3], os.Args[4], err) + os.Exit(1) + } + case "accept": + if len(os.Args) != 5 { + fmt.Println("example: sendafriend group accept [friendname] [groupname]") + os.Exit(1) + } + + onion, exists := peer.GetProfile().GetCustomAttribute(os.Args[3] + "_onion") + if !exists { + fmt.Fprintf(os.Stderr, "hmm you don't have a contact named %v\n", os.Args[3]) + os.Exit(1) + } + + _, exists = peer.GetProfile().GetCustomAttribute(os.Args[4] + "_groupid") + if exists { + fmt.Printf("oops, you already have a group called %v\n", os.Args[4]) + os.Exit(1) + } + + s := len(peer.GetGroups()) + go peer.Listen() + fmt.Println("okay, have a friend run \"sendafriend group invite [yourname] [groupname]\"") + for { + n := len(peer.GetGroups()) + if n == s { + time.Sleep(time.Millisecond * 500) + continue + } + + groups := peer.GetGroups() + for i := range groups { + if peer.GetGroup(groups[i]).Owner == onion && !peer.GetGroup(groups[i]).Accepted { + fmt.Printf("adding group %v!\n", os.Args[4]) + peer.AcceptInvite(groups[i]) + peer.GetProfile().SetCustomAttribute(os.Args[4] + "_groupid", groups[i]) + peer.GetProfile().SetCustomAttribute(groups[i] + "_groupname", os.Args[4]) + peer.Save() + os.Exit(0) + } + } + } } }