1package otr3 2 3import "fmt" 4 5// SecurityEvent define the events used to indicate changes in security status. In comparison with libotr, this library does not take trust levels into concern for security events 6type SecurityEvent int 7 8const ( 9 // GoneInsecure is signalled when we have gone from a secure state to an insecure state 10 GoneInsecure SecurityEvent = iota 11 // GoneSecure is signalled when we have gone from an insecure state to a secure state 12 GoneSecure 13 // StillSecure is signalled when we have refreshed the security state but is still in a secure state 14 StillSecure 15) 16 17// SecurityEventHandler is an interface for events that are related to changes of security status 18type SecurityEventHandler interface { 19 // HandleSecurityEvent is called when a change in security status happens 20 HandleSecurityEvent(event SecurityEvent) 21} 22 23type dynamicSecurityEventHandler struct { 24 eh func(event SecurityEvent) 25} 26 27func (d dynamicSecurityEventHandler) HandleSecurityEvent(event SecurityEvent) { 28 d.eh(event) 29} 30 31func (c *Conversation) securityEvent(e SecurityEvent) { 32 if c.securityEventHandler != nil { 33 c.securityEventHandler.HandleSecurityEvent(e) 34 } 35} 36 37// String returns the string representation of the SecurityEvent 38func (s SecurityEvent) String() string { 39 switch s { 40 case GoneInsecure: 41 return "GoneInsecure" 42 case GoneSecure: 43 return "GoneSecure" 44 case StillSecure: 45 return "StillSecure" 46 default: 47 return "SECURITY EVENT: (THIS SHOULD NEVER HAPPEN)" 48 } 49} 50 51type combinedSecurityEventHandler struct { 52 handlers []SecurityEventHandler 53} 54 55func (c combinedSecurityEventHandler) HandleSecurityEvent(event SecurityEvent) { 56 for _, h := range c.handlers { 57 if h != nil { 58 h.HandleSecurityEvent(event) 59 } 60 } 61} 62 63// CombineSecurityEventHandlers creates a SecurityEventHandler that will call all handlers 64// given to this function. It ignores nil entries. 65func CombineSecurityEventHandlers(handlers ...SecurityEventHandler) SecurityEventHandler { 66 return combinedSecurityEventHandler{handlers} 67} 68 69func (c *Conversation) signalSecurityEventIf(cond bool, event SecurityEvent) { 70 if cond { 71 c.securityEvent(event) 72 } 73} 74 75// DebugSecurityEventHandler is a SecurityEventHandler that dumps all SecurityEvents to standard error 76type DebugSecurityEventHandler struct{} 77 78// HandleSecurityEvent dumps all security events 79func (DebugSecurityEventHandler) HandleSecurityEvent(event SecurityEvent) { 80 fmt.Fprintf(standardErrorOutput, "%sHandleSecurityEvent(%s)\n", debugPrefix, event) 81} 82