File: handler.go

package info (click to toggle)
go-dlib 5.6.0.9%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 3,200 kB
  • sloc: ansic: 4,664; xml: 1,456; makefile: 20; sh: 15
file content (61 lines) | stat: -rw-r--r-- 1,494 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
package pam

import "sync"

// ConversationHandler is an interface for objects that can be used as
// conversation callbacks during PAM authentication.
type ConversationHandler interface {
	// RespondPAM receives a message style and a message string. If the
	// message Style is PromptEchoOff or PromptEchoOn then the function
	// should return a response string.
	RespondPAM(Style, string) (string, error)
}

// ConversationFunc is an adapter to allow the use of ordinary functions as
// conversation callbacks.
type ConversationFunc func(Style, string) (string, error)

func (f ConversationFunc) RespondPAM(s Style, msg string) (string, error) {
	return f(s, msg)
}

var globalHandlers struct {
	sync.Mutex
	m      map[handlerId]ConversationHandler
	nextId handlerId
}

type handlerId int

func init() {
	globalHandlers.m = make(map[handlerId]ConversationHandler)
	globalHandlers.nextId = 1
}

func addHandler(handler ConversationHandler) handlerId {
	globalHandlers.Lock()
	defer globalHandlers.Unlock()
	id := globalHandlers.nextId
	globalHandlers.nextId++
	globalHandlers.m[id] = handler
	return id
}

func getHandler(id handlerId) ConversationHandler {
	globalHandlers.Lock()
	defer globalHandlers.Unlock()
	v := globalHandlers.m[id]
	if v != nil {
		return v
	}
	panic("handler not found")
}

func deleteHandler(id handlerId) {
	globalHandlers.Lock()
	defer globalHandlers.Unlock()
	if _, ok := globalHandlers.m[id]; !ok {
		panic("handler not found")
	}
	delete(globalHandlers.m, id)
}