~emersion/soju

soju/db.go -rw-r--r-- 2.7 KiB
b609b86fSimon Ser Add support for account-tag 15 hours ago
                                                                                
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package soju

import (
	"fmt"
	"net/url"
	"strings"
	"time"
)

type Database interface {
	Close() error

	ListUsers() ([]User, error)
	GetUser(username string) (*User, error)
	StoreUser(user *User) error
	DeleteUser(id int64) error

	ListNetworks(userID int64) ([]Network, error)
	StoreNetwork(userID int64, network *Network) error
	DeleteNetwork(id int64) error
	ListChannels(networkID int64) ([]Channel, error)
	StoreChannel(networKID int64, ch *Channel) error
	DeleteChannel(id int64) error

	ListDeliveryReceipts(networkID int64) ([]DeliveryReceipt, error)
	StoreClientDeliveryReceipts(networkID int64, client string, receipts []DeliveryReceipt) error
}

type User struct {
	ID       int64
	Username string
	Password string // hashed
	Admin    bool
}

type SASL struct {
	Mechanism string

	Plain struct {
		Username string
		Password string
	}

	// TLS client certificate authentication.
	External struct {
		// X.509 certificate in DER form.
		CertBlob []byte
		// PKCS#8 private key in DER form.
		PrivKeyBlob []byte
	}
}

type Network struct {
	ID              int64
	Name            string
	Addr            string
	Nick            string
	Username        string
	Realname        string
	Pass            string
	ConnectCommands []string
	SASL            SASL
	Enabled         bool
}

func (net *Network) GetName() string {
	if net.Name != "" {
		return net.Name
	}
	return net.Addr
}

func (net *Network) URL() (*url.URL, error) {
	s := net.Addr
	if !strings.Contains(s, "://") {
		// This is a raw domain name, make it an URL with the default scheme
		s = "ircs://" + s
	}

	u, err := url.Parse(s)
	if err != nil {
		return nil, fmt.Errorf("failed to parse upstream server URL: %v", err)
	}

	return u, nil
}

func (net *Network) GetUsername() string {
	if net.Username != "" {
		return net.Username
	}
	return net.Nick
}

func (net *Network) GetRealname() string {
	if net.Realname != "" {
		return net.Realname
	}
	return net.Nick
}

type MessageFilter int

const (
	// TODO: use customizable user defaults for FilterDefault
	FilterDefault MessageFilter = iota
	FilterNone
	FilterHighlight
	FilterMessage
)

func parseFilter(filter string) (MessageFilter, error) {
	switch filter {
	case "default":
		return FilterDefault, nil
	case "none":
		return FilterNone, nil
	case "highlight":
		return FilterHighlight, nil
	case "message":
		return FilterMessage, nil
	}
	return 0, fmt.Errorf("unknown filter: %q", filter)
}

type Channel struct {
	ID   int64
	Name string
	Key  string

	Detached              bool
	DetachedInternalMsgID string

	RelayDetached MessageFilter
	ReattachOn    MessageFilter
	DetachAfter   time.Duration
	DetachOn      MessageFilter
}

type DeliveryReceipt struct {
	ID            int64
	Target        string // channel or nick
	Client        string
	InternalMsgID string
}