-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessages.go
More file actions
164 lines (151 loc) · 3.51 KB
/
messages.go
File metadata and controls
164 lines (151 loc) · 3.51 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package citadel
import (
"fmt"
"log"
"strconv"
"strings"
)
type msglist string
const (
LIST_ALL msglist = "all"
LIST_OLD msglist = "old"
LIST_NEW msglist = "new"
LIST_FIRST msglist = "first"
LIST_LAST msglist = "last"
LIST_GT msglist = "gt"
LIST_LT msglist = "lt"
LIST_SEARCH msglist = "search"
)
type MsgList struct {
Cmd msglist
FirstLast int
GtLt int
Find string
}
// Get list of all messages in current room
func (c *Citadel) MsgListAll() (list []string, ok bool) {
m := new(MsgList)
m.Cmd = LIST_ALL
list, ok = c.mlist(m)
return
}
// Get messages list for current room
// This command must be passed a single parameter: LIST_ALL, LIST_OLD, or LIST_NEW
func (c *Citadel) mlist(m *MsgList) (list []string, ok bool) {
if c.Request(fmt.Sprintf("MSGS %s", m.Cmd)) {
if c.Code == CODE_LISTING_FOLLOWS {
res := c.Responce()
no := len(res)
for i := 0; i < no; i++ {
list = append(list, res[i][0])
}
ok = true
}
}
return
}
// Del a list of messages from current room
func (c *Citadel) MsgsDel(list []string) (ok bool) {
var cmd string
no := len(list)
if no != 0 {
cmd = fmt.Sprintf("DELE %s", list[0])
if no > 1 {
for i := 1; i < no; i++ {
cmd = fmt.Sprintf("%s,%s", cmd, list[i])
}
}
ok = c.Request(cmd)
}
return
}
type Message struct {
EUID string
Mime string // text/vcard
UxTime int
Text string
}
type Dav struct {
UID string
REV string
FN string
Object string
}
// Get vcard from current room by MSG ID
func (c *Citadel) GetMessage(MsgID, Mime string) (msg *Message, ok bool) {
msg = new(Message)
msg.Mime = Mime
if c.Request(fmt.Sprintf("MSGP %v", Mime)) { // "MSGP text/vcard"
var err error
if c.Request(fmt.Sprintf("MSG4 %v", MsgID)) {
if c.Code == CODE_LISTING_FOLLOWS {
res := c.Responce()
no := len(res)
for i := 0; i < no; i++ {
//fmt.Println(res[i][0])
if ok == false {
if strings.HasPrefix(res[i][0], "msgn=") {
msg.EUID = TrimPrefix(res[i][0], "msgn=")
}
// Waitting on citadel support on this, what is exti=?
/*
if strings.HasPrefix(res[i][0], "exti=") {
msg.EUID = strings.TrimPrefix(res[i][0], "exti=")
}
*/
if strings.HasPrefix(res[i][0], "time=") {
ts := TrimPrefix(res[i][0], "time=")
if msg.UxTime, err = strconv.Atoi(ts); err != nil {
log.Println(err)
}
}
if res[i][0] == "text" {
ok = true
}
} else {
if strings.HasPrefix(res[i][0], "Content-type:") {
msg.Mime = TrimPrefix(res[i][0], "Content-type:")
}
msg.Text += res[i][0] + "\n"
}
}
}
}
}
return
}
func TrimPrefix(s, p string) string {
return strings.TrimSpace(strings.TrimPrefix(s, p))
}
func (dav *Dav) ParseDav(text string) {
list := strings.Split(text, "\n")
s := 0
for _, v := range list {
u := strings.ToUpper(v)
if u == "BEGIN:VCARD" || u == "BEGIN:VCALENDAR" || u == "BEGIN:VNOTE" && s == 0 {
v = u
s = 1
}
if s == 1 {
if strings.HasPrefix(v, "UID:") {
dav.UID = strings.TrimPrefix(v, "UID:")
}
if u == "END:VCARD" || u == "END:VCALENDAR" || u == "END:VNOTE" {
v = u
s = 2
}
dav.Object += v + "\n"
}
}
}
// Get vcard from current room by MSG ID
func (c *Citadel) GetDav(MsgID string, contentType string) (msg *Message, dav *Dav, ok bool) {
if contentType == "text/vcard" {
contentType = "text/vcard|text/x-vcard"
}
dav = new(Dav)
if msg, ok = c.GetMessage(MsgID, contentType); ok {
dav.ParseDav(msg.Text)
}
return
}