File: smp_events.go

package info (click to toggle)
golang-github-twstrike-otr3 0.0~git20161015.0.744856d-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,008 kB
  • ctags: 1,863
  • sloc: ansic: 127; makefile: 76
file content (100 lines) | stat: -rw-r--r-- 3,288 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package otr3

import "fmt"

// SMPEvent define the events used to indicate status of SMP to the UI
type SMPEvent int

const (
	// SMPEventError means abort the current auth and update the auth progress dialog with progress_percent. This event is only sent when we receive a message for another message state than we are in
	SMPEventError SMPEvent = iota
	// SMPEventAbort means update the auth progress dialog with progress_percent
	SMPEventAbort
	// SMPEventCheated means abort the current auth and update the auth progress dialog with progress_percent
	SMPEventCheated
	// SMPEventAskForAnswer means ask the user to answer the secret question
	SMPEventAskForAnswer
	// SMPEventAskForSecret means prompt the user to enter a shared secret
	SMPEventAskForSecret
	// SMPEventInProgress means update the auth progress dialog with progress_percent
	SMPEventInProgress
	// SMPEventSuccess means update the auth progress dialog with progress_percent
	SMPEventSuccess
	// SMPEventFailure means update the auth progress dialog with progress_percent
	SMPEventFailure
)

// SMPEventHandler handles SMPEvents
type SMPEventHandler interface {
	// HandleSMPEvent should update the authentication UI with respect to SMP events
	HandleSMPEvent(event SMPEvent, progressPercent int, question string)
}

type dynamicSMPEventHandler struct {
	eh func(event SMPEvent, progressPercent int, question string)
}

func (d dynamicSMPEventHandler) HandleSMPEvent(event SMPEvent, pp int, question string) {
	d.eh(event, pp, question)
}

func (c *Conversation) smpEvent(e SMPEvent, percent int) {
	if c.smpEventHandler != nil {
		c.smpEventHandler.HandleSMPEvent(e, percent, "")
	}
}

func (c *Conversation) smpEventWithQuestion(e SMPEvent, percent int, question string) {
	if c.smpEventHandler != nil {
		c.smpEventHandler.HandleSMPEvent(e, percent, question)
	}
}

func (s SMPEvent) String() string {
	switch s {
	case SMPEventError:
		return "SMPEventError"
	case SMPEventAbort:
		return "SMPEventAbort"
	case SMPEventCheated:
		return "SMPEventCheated"
	case SMPEventAskForAnswer:
		return "SMPEventAskForAnswer"
	case SMPEventAskForSecret:
		return "SMPEventAskForSecret"
	case SMPEventInProgress:
		return "SMPEventInProgress"
	case SMPEventSuccess:
		return "SMPEventSuccess"
	case SMPEventFailure:
		return "SMPEventFailure"
	default:
		return "SMP EVENT: (THIS SHOULD NEVER HAPPEN)"
	}
}

type combinedSMPEventHandler struct {
	handlers []SMPEventHandler
}

func (c combinedSMPEventHandler) HandleSMPEvent(event SMPEvent, progressPercent int, question string) {
	for _, h := range c.handlers {
		if h != nil {
			h.HandleSMPEvent(event, progressPercent, question)
		}
	}
}

// CombineSMPEventHandlers creates a SMPEventHandler that will call all handlers
// given to this function. It ignores nil entries.
func CombineSMPEventHandlers(handlers ...SMPEventHandler) SMPEventHandler {
	return combinedSMPEventHandler{handlers}
}

// DebugSMPEventHandler is an SMPEventHandler that dumps all SMPEvents to standard error
type DebugSMPEventHandler struct{}

// HandleSMPEvent dumps all SMP events
func (DebugSMPEventHandler) HandleSMPEvent(event SMPEvent, progressPercent int, question string) {
	fmt.Fprintf(standardErrorOutput, "%sHandleSMPEvent(%s, %d, %#v)\n", debugPrefix, event, progressPercent, question)
}