For SetProfileAttribute to declar a proper zone

This commit is contained in:
Sarah Jamie Lewis 2021-10-15 13:09:36 -07:00
parent d54567339d
commit 2f14966051
4 changed files with 39 additions and 36 deletions

6
go.mod
View File

@ -3,14 +3,10 @@ module git.openprivacy.ca/cwtch.im/libcwtch-go
go 1.15
require (
cwtch.im/cwtch v0.11.2
cwtch.im/cwtch v0.12.0
git.openprivacy.ca/openprivacy/connectivity v1.5.0
git.openprivacy.ca/openprivacy/log v1.0.3
golang.org/x/mobile v0.0.0-20210716004757-34ab1303b554 // indirect
golang.org/x/mod v0.5.0 // indirect
golang.org/x/sys v0.0.0-20210823070655-63515b42dcdf // indirect
)
replace (
cwtch.im/cwtch => /home/sarah/workspace/src/cwtch.im/cwtch
)

2
go.sum
View File

@ -1,5 +1,7 @@
cwtch.im/cwtch v0.11.2 h1:a38zZLSNsKJzLStBDOIoYKKIL56V9ueQvOZfnIEVc5I=
cwtch.im/cwtch v0.11.2/go.mod h1:QpTkQK7MqNt0dQK9/pBk5VpkvFhy6xuoxJIn401B8fM=
cwtch.im/cwtch v0.12.0 h1:hEMee2/2s4kUwukGCTBpGww/KfrsE84e9tOLnM8lM78=
cwtch.im/cwtch v0.12.0/go.mod h1:QpTkQK7MqNt0dQK9/pBk5VpkvFhy6xuoxJIn401B8fM=
filippo.io/edwards25519 v1.0.0-rc.1 h1:m0VOOB23frXZvAOK44usCgLWvtsxIoMCTBGJZlpmGfU=
filippo.io/edwards25519 v1.0.0-rc.1/go.mod h1:N1IkdkCkiLB6tki+MYJoSx2JTY9NUlxZE7eHn5EwJns=
git.openprivacy.ca/cwtch.im/tapir v0.4.9 h1:LXonlztwvI1F1++0IyomIcDH1/Bxzo+oN8YjGonNvjM=

28
lib.go
View File

@ -230,14 +230,14 @@ func ReconnectCwtchForeground() {
}
// populate profile list
peerList := application.ListPeers()
for onion := range peerList {
peerList := application.ListProfiles()
for _, onion := range peerList {
eventHandler.Push(event.NewEvent(event.NewPeer, map[event.Field]string{event.Identity: onion, event.Created: event.False, "Reload": event.True}))
}
settings := utils.ReadGlobalSettings()
for profileOnion := range peerList {
for _,profileOnion := range peerList {
// fix peerpeercontact message counts
contactList := application.GetPeer(profileOnion).GetContacts()
for _, handle := range contactList {
@ -307,7 +307,7 @@ func SendAppEvent(eventJson string) {
// Group Experiment Refresh
groupHandler, err := groups.ExperimentGate(utils.ReadGlobalSettings().Experiments)
if err == nil {
for profileOnion := range application.ListPeers() {
for _,profileOnion := range application.ListProfiles() {
serverListForOnion := groupHandler.GetServerInfoList(application.GetPeer(profileOnion))
serversListBytes, _ := json.Marshal(serverListForOnion)
eventHandler.Push(event.NewEvent(groups.UpdateServerInfo, map[event.Field]string{"ProfileOnion": profileOnion, groups.ServerList: string(serversListBytes)}))
@ -317,11 +317,11 @@ func SendAppEvent(eventJson string) {
// Explicitly toggle blocking/unblocking of unknown connections for profiles
// that have been loaded.
if utils.ReadGlobalSettings().BlockUnknownConnections {
for onion := range application.ListPeers() {
for _,onion := range application.ListProfiles() {
application.GetPeer(onion).BlockUnknownConnections()
}
} else {
for onion := range application.ListPeers() {
for _,onion := range application.ListProfiles() {
application.GetPeer(onion).AllowUnknownConnections()
}
}
@ -384,7 +384,7 @@ func SendProfileEvent(onion string, eventJson string) {
eventHandler.Push(event.NewEvent(event.AppError, map[event.Field]string{event.Data: err.Error()}))
// DEPRECATED: use SetProfileAttribute()
case event.SetAttribute:
peer.SetAttribute(new_event.Data[event.Key], new_event.Data[event.Data])
log.Errorf("SetAttribute is deprecated.")
// DEPRECATED: use SetContactAttribute()
case event.SetPeerAttribute:
peer.SetContactAttribute(new_event.Data[event.RemotePeer], new_event.Data[event.Key], new_event.Data[event.Data])
@ -865,10 +865,20 @@ func c_SetProfileAttribute(profile_ptr *C.char, profile_len C.int, key_ptr *C.ch
SetProfileAttribute(profileOnion, key, value)
}
// SetProfileAttribute provides a wrapper around profile.SetAttribute
// SetProfileAttribute provides a wrapper around profile.SetScopedZonedAttribute
// Key must have the format zone.key where Zone is defined in Cwtch. Unknown zones are not permitted.
func SetProfileAttribute(profileOnion string, key string, value string) {
profile := application.GetPeer(profileOnion)
profile.SetAttribute(key, value)
zone,key := attr.ParseZone(key)
if zone != attr.UnknownZone {
// TODO: We only allow Locally scoped attributes to be set here.
// To set a publicly scoped zone you need to write a dedicated handler/functionality
// And this probably belongs in Cwtch
profile.SetScopedZonedAttribute(attr.LocalScope, zone, key, value)
} else {
log.Errorf("attempted to set an attribute with an unknown zone: %v", key)
}
}
//export c_SetContactAttribute

View File

@ -5,9 +5,10 @@ import (
"cwtch.im/cwtch/app/plugins"
"cwtch.im/cwtch/model"
"cwtch.im/cwtch/model/attr"
"cwtch.im/cwtch/model/constants"
"cwtch.im/cwtch/protocol/connections"
"encoding/json"
"git.openprivacy.ca/cwtch.im/libcwtch-go/constants"
constants2 "git.openprivacy.ca/cwtch.im/libcwtch-go/constants"
"git.openprivacy.ca/cwtch.im/libcwtch-go/features/groups"
"git.openprivacy.ca/openprivacy/log"
"strconv"
@ -68,7 +69,7 @@ func (eh *EventHandler) handleAppBusEvent(e *event.Event) string {
switch e.EventType {
case event.ACNStatus:
if e.Data[event.Progress] == "100" {
for onion := range eh.app.ListPeers() {
for _, onion := range eh.app.ListProfiles() {
// launch a listen thread (internally this does a check that the protocol engine is not listening)
// and as such is safe to call.
eh.app.GetPeer(onion).Listen()
@ -83,22 +84,18 @@ func (eh *EventHandler) handleAppBusEvent(e *event.Event) string {
eh.startHandlingPeer(onion)
}
tag, isTagged := profile.GetAttribute(app.AttributeTag)
if isTagged {
e.Data[app.AttributeTag] = tag
} else {
// Assume encrypted for non-tagged profiles - this isn't always true, but all post-beta profiles
// are tagged on creation.
e.Data[app.AttributeTag] = constants.ProfileTypeV1Password
}
// CwtchPeer will always set this now...
tag,_ := profile.GetScopedZonedAttribute(attr.LocalScope, attr.ProfileZone, constants.Tag)
e.Data[constants.Tag] = tag
if e.Data[event.Created] == event.True {
name, _ := profile.GetScopedZonedAttribute(attr.LocalScope, attr.ProfileZone, constants.Name)
profile.SetScopedZonedAttribute(attr.PublicScope, attr.ProfileZone, constants.Name, name)
profile.SetScopedZonedAttribute(attr.PublicScope, attr.ProfileZone, constants.Picture, ImageToString(NewImage(RandomProfileImage(onion), TypeImageDistro)))
profile.SetScopedZonedAttribute(attr.PublicScope, attr.ProfileZone, constants2.Picture, ImageToString(NewImage(RandomProfileImage(onion), TypeImageDistro)))
}
if e.Data[event.Status] != event.StorageRunning || e.Data[event.Created] == event.True {
profile.SetScopedZonedAttribute(attr.LocalScope, attr.ProfileZone, constants.PeerOnline, event.False)
profile.SetScopedZonedAttribute(attr.LocalScope, attr.ProfileZone, constants2.PeerOnline, event.False)
eh.app.AddPeerPlugin(onion, plugins.CONNECTIONRETRY)
eh.app.AddPeerPlugin(onion, plugins.NETWORKCHECK)
@ -124,7 +121,7 @@ func (eh *EventHandler) handleAppBusEvent(e *event.Event) string {
nick = onion
}
picVal, ok := profile.GetScopedZonedAttribute(attr.PublicScope, attr.ProfileZone, constants.Picture)
picVal, ok := profile.GetScopedZonedAttribute(attr.PublicScope, attr.ProfileZone, constants2.Picture)
if !ok {
picVal = ImageToString(NewImage(RandomProfileImage(onion), TypeImageDistro))
}
@ -134,12 +131,10 @@ func (eh *EventHandler) handleAppBusEvent(e *event.Event) string {
}
picPath := GetPicturePath(pic)
//tag, _ := profile.GetAttribute(app.AttributeTag)
online, _ := profile.GetAttribute(attr.GetLocalScope(constants.PeerOnline))
online, _ := profile.GetScopedZonedAttribute(attr.LocalScope, attr.ProfileZone, constants2.PeerOnline)
e.Data[constants.Name] = nick
e.Data[constants.Picture] = picPath
e.Data[constants2.Picture] = picPath
e.Data["Online"] = online
var contacts []Contact
@ -166,7 +161,7 @@ func (eh *EventHandler) handleAppBusEvent(e *event.Event) string {
if !set {
saveHistory = event.DeleteHistoryDefault
}
isArchived, set := contactInfo.GetAttribute(attr.GetLocalScope(constants.Archived))
isArchived, set := contactInfo.GetAttribute(attr.GetLocalScope(constants2.Archived))
if !set {
isArchived = event.False
}
@ -201,7 +196,7 @@ func (eh *EventHandler) handleAppBusEvent(e *event.Event) string {
if group.Accepted {
authorization = model.AuthApproved
}
isArchived, set := group.GetAttribute(attr.GetLocalScope(constants.Archived))
isArchived, set := group.GetAttribute(attr.GetLocalScope(constants2.Archived))
if !set {
isArchived = event.False
}
@ -266,12 +261,12 @@ func (eh *EventHandler) handleProfileEvent(ev *EventProfileEnvelope) string {
// only needs contact nickname and picture, for displaying on popup notifications
ev.Event.Data["Nick"] = ph.GetNick(ev.Event.Data["RemotePeer"])
ev.Event.Data["Picture"] = ph.GetProfilePic(ev.Event.Data["RemotePeer"])
peer.SetContactAttribute(ev.Event.Data["RemotePeer"], attr.GetLocalScope(constants.Archived), event.False)
peer.SetContactAttribute(ev.Event.Data["RemotePeer"], attr.GetLocalScope(constants2.Archived), event.False)
case event.NewMessageFromGroup:
// only needs contact nickname and picture, for displaying on popup notifications
ev.Event.Data["Nick"] = ph.GetNick(ev.Event.Data[event.GroupID])
ev.Event.Data["Picture"] = ph.GetProfilePic(ev.Event.Data[event.GroupID])
peer.SetGroupAttribute(ev.Event.Data[event.GroupID], attr.GetLocalScope(constants.Archived), event.False)
peer.SetGroupAttribute(ev.Event.Data[event.GroupID], attr.GetLocalScope(constants2.Archived), event.False)
case event.PeerAcknowledgement:
// No enrichement required
case event.PeerCreated:
@ -314,7 +309,7 @@ func (eh *EventHandler) handleProfileEvent(ev *EventProfileEnvelope) string {
if cxnState == connections.AUTHENTICATED {
// if known and authed, get vars
peer.SendScopedZonedGetValToContact(ev.Event.Data[event.RemotePeer], attr.PublicScope, attr.ProfileZone, constants.Name)
peer.SendScopedZonedGetValToContact(ev.Event.Data[event.RemotePeer], attr.PublicScope, attr.ProfileZone, constants.Picture)
peer.SendScopedZonedGetValToContact(ev.Event.Data[event.RemotePeer], attr.PublicScope, attr.ProfileZone, constants2.Picture)
}
}