mirror of
https://github.com/D-Modem/bbs.git
synced 2026-11-28 17:38:04 -08:00
Merge pull request #32 from D-Modem/claude/project-thread-2u91ij
AOL host: keywords and mail
This commit is contained in:
8 files changed
+597
-9
No files matched your search
@@ -30,9 +30,14 @@ client's `AmericaOnline.aol.com`) and by modem.
|
||||
as `AB`, comings and goings as `CA`/`CB`, `Aa` said; `iS`/`is` IMs as
|
||||
BBS private messages (kept for one away), shown in 32-223 (4.0's
|
||||
32-6736); `iO` asks who is on.
|
||||
- **Tokens**, next: mail (`m4`, MIP), keywords (`k1`/`f1`) to areas, the
|
||||
room list (`LP`); each answered with forms compiled from source. The
|
||||
client asks for art it lacks (`fh`): none is served yet.
|
||||
- **Keywords and mail** (done, untried with a real client): `Kk` from
|
||||
the Keyword window (de_data) or a host button (the word as it is) to
|
||||
Help, Chat, Mail, Who; `m4` MIP mail (header and text in one stream or
|
||||
two) to BBS mail; the mailbox, a mail and Who are host windows whose
|
||||
buttons send keywords; new mail counted on the Welcome window.
|
||||
- **Tokens**, next: the client's own Read Mail list (`m7` and its MIP
|
||||
answers), the room list (`LP`) and other rooms, message areas as
|
||||
boards; the client asks for art it lacks (`fh`): none is served yet.
|
||||
- **Modem callers**: the client's CCL script expects a network's prompts
|
||||
(SprintNet, Tymnet) before P3; emulate one, or ship a CCL that dials
|
||||
straight in.
|
||||
|
||||
+25
-1
@@ -2,7 +2,8 @@ The BBS can be an AOL host for AOL's own Windows clients (3.0, 4.0 and
|
||||
later) over TCP. A caller signs on in the client with their BBS name and
|
||||
password, and the BBS draws its Welcome window. People Connection is the
|
||||
BBS's chat, and Instant Messages are its private messages, with callers on
|
||||
every other route. This is early work: mail and keywords are still to come.
|
||||
every other route, mail is BBS mail, and keywords go to each of these.
|
||||
This is early work, not yet tried with a real client.
|
||||
|
||||
```ini
|
||||
[aol]
|
||||
@@ -52,6 +53,29 @@ told. Messages to the client open its own Instant Message window, one for
|
||||
each person. Screen names are compared without their spaces, so
|
||||
`CarolAnn` is the user Carol Ann (AOL's screen names have no spaces).
|
||||
|
||||
## Mail
|
||||
|
||||
Compose Mail in the client sends BBS mail to each name it is addressed
|
||||
to, by screen name as for IMs. 4.0's formatted mail arrives as plain text.
|
||||
The Welcome window says how much new mail is waiting, and new mail while
|
||||
signed on is shown in a message box.
|
||||
|
||||
The keyword Mail opens the BBS's mailbox window, newest first with new
|
||||
mail marked `*`. A button reads a mail, which marks it read, and the mail's
|
||||
window can delete it. The client's own Read Mail and Old Mail lists are
|
||||
not served yet.
|
||||
|
||||
## Keywords
|
||||
|
||||
The client's Keyword window (Ctrl+K) takes:
|
||||
|
||||
| Keyword | Goes to |
|
||||
|---|---|
|
||||
| `Chat` | People Connection |
|
||||
| `Mail` | the mailbox |
|
||||
| `Who` | who is on now, by any route |
|
||||
| `Help` | the list of keywords |
|
||||
|
||||
## How it is built
|
||||
|
||||
- `internal/aol/p3`: P3, the client's link layer (packets, checksums,
|
||||
|
||||
+2
-1
@@ -5,7 +5,8 @@
|
||||
// own that draw windows and run actions.
|
||||
//
|
||||
// The client signs on with a BBS account (the Welcome form's screen name
|
||||
// and password, token Dd), and signs off with pE. The client's own
|
||||
// and password, token Dd), and signs off with pE; keywords (Kk) and mail
|
||||
// (m4) go to the BBS's chat, mail and Who. The client's own
|
||||
// database has most of its windows; the host draws its own Welcome.
|
||||
//
|
||||
// Sources for what the client expects: Dialtone (MIT) and other hosts
|
||||
|
||||
+143
-1
@@ -3,6 +3,7 @@ package aol
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
"path/filepath"
|
||||
@@ -293,7 +294,8 @@ func TestLongStreams(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestForms(t *testing.T) {
|
||||
for _, src := range []string{onlineForm, alertForm(`"Quotes" and \ and é`), welcomeForm("BBS", "Zoë", 3)} {
|
||||
for _, src := range []string{onlineForm, alertForm(`"Quotes" and \ and é`), welcomeForm("BBS", "Zoë", 3, 1),
|
||||
windowForm("50-1-1", "Mailbox", "a\r\nb", 4, []button{{"Read", "read mail 1"}})} {
|
||||
if _, err := fdo.Compile(src); err != nil {
|
||||
t.Errorf("%v:\n%s", err, src)
|
||||
}
|
||||
@@ -475,3 +477,143 @@ func TestIM(t *testing.T) {
|
||||
t.Errorf("iO:\n%s", s)
|
||||
}
|
||||
}
|
||||
|
||||
// mailForm is a mail as the Write Mail form sends it (m4): its header and
|
||||
// text, in one stream or in two.
|
||||
func mailForm(to, subject, text string, split bool) [][]byte {
|
||||
head := " mip_addressee <" + fdo.Quote(to) + ">\n mip_subject <" + fdo.Quote(subject) + ">\n"
|
||||
body := " mip_text <" + fdo.Quote(text) + ">\n mip_end_message\n"
|
||||
st := func(src string) []byte {
|
||||
return append([]byte{0, 7}, fdo.MustCompile("uni_start_stream\n"+src+"uni_end_stream\n")...)
|
||||
}
|
||||
if split {
|
||||
return [][]byte{st(head), st(body)}
|
||||
}
|
||||
return [][]byte{st(head + body)}
|
||||
}
|
||||
|
||||
// button0 is the keyword the button labelled so in a host window sends
|
||||
// (sm_send_token_raw <"Kk", keyword>, read from the compiled stream).
|
||||
func button0(t *testing.T, src, label string) string {
|
||||
t.Helper()
|
||||
b := fdo.MustCompile(src)
|
||||
i := bytes.Index(b, append([]byte(label), 0x02)) // its title, then its first atom
|
||||
if i < 0 {
|
||||
t.Fatalf("no button %q:\n%s", label, src)
|
||||
}
|
||||
j := bytes.Index(b[i:], []byte("Kk"))
|
||||
if j < 0 {
|
||||
t.Fatalf("button %q sends nothing", label)
|
||||
}
|
||||
rest := b[i+j+2:]
|
||||
n := 0
|
||||
for n < len(rest) && rest[n] >= 0x20 && rest[n] < 0x7F {
|
||||
n++
|
||||
}
|
||||
return string(rest[:n])
|
||||
}
|
||||
|
||||
func TestMail(t *testing.T) {
|
||||
srv, _ := newServer(t)
|
||||
if _, err := srv.B.Store.CreateUser("Carol Ann", "password1", "There", false); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
a := signedOn(t, srv, "alice")
|
||||
|
||||
// alice writes to Carol Ann, in one stream; then in two
|
||||
a.send("m4", mailForm("CarolAnn", "Lunch", "Are you free?\r\nNoon?", false)[0])
|
||||
if s := a.stream(); !strings.Contains(s, "mip_message_accepted") {
|
||||
t.Errorf("sent:\n%s", s)
|
||||
}
|
||||
parts := mailForm("CarolAnn", "Again", "<html><b>Bold</b> & plain<br>line 2</html>", true)
|
||||
a.send("m4", parts[0])
|
||||
if s := a.stream(); !strings.Contains(s, "mip_header_accepted") {
|
||||
t.Errorf("header:\n%s", s)
|
||||
}
|
||||
a.send("m4", parts[1])
|
||||
if s := a.stream(); !strings.Contains(s, "mip_message_accepted") {
|
||||
t.Errorf("sent:\n%s", s)
|
||||
}
|
||||
u, _ := srv.B.Store.UserByName("Carol Ann")
|
||||
inbox, err := srv.B.Store.Inbox(u.ID)
|
||||
if err != nil || len(inbox) != 2 {
|
||||
t.Fatalf("inbox %v %v", inbox, err)
|
||||
}
|
||||
if m := inbox[0]; m.From != "alice" || m.Subject != "Lunch" || m.Body != "Are you free?\nNoon?" {
|
||||
t.Errorf("first %+v", m)
|
||||
}
|
||||
if m := inbox[1]; m.Body != "Bold & plain\nline 2" {
|
||||
t.Errorf("4.0's HTML: %q", m.Body)
|
||||
}
|
||||
// to no one here
|
||||
a.send("m4", mailForm("nobody", "Hi", "x", false)[0])
|
||||
if s := a.stream(); strings.Contains(s, "mip_message_accepted") {
|
||||
t.Errorf("to nobody:\n%s", s)
|
||||
}
|
||||
if s := a.stream(); !strings.Contains(s, "not sent to") || !strings.Contains(s, "nobody") {
|
||||
t.Errorf("to nobody:\n%s", s)
|
||||
}
|
||||
|
||||
// Carol is told at sign-on, and reads it by keyword and buttons
|
||||
c := connect(t, srv)
|
||||
c.send("Dd", signOnForm("CarolAnn", "password1"))
|
||||
c.stream()
|
||||
if s := c.stream(); !strings.Contains(s, "You have 2 new messages") {
|
||||
t.Errorf("welcome:\n%s", s)
|
||||
}
|
||||
c.send("Kk", deData("Mail"))
|
||||
c.stream() // the last mailbox closed
|
||||
box := c.stream()
|
||||
if !strings.Contains(box, "You have 2 messages, 2 new") {
|
||||
t.Errorf("mailbox:\n%s", box)
|
||||
}
|
||||
kw := button0(t, box, "* "+inbox[0].Created.Format("Jan 2")+" alice: Lunch")
|
||||
c.send("Kk", []byte(kw))
|
||||
c.stream()
|
||||
letter := c.stream()
|
||||
if !strings.Contains(letter, "Subject: Lunch") || !strings.Contains(letter, `Are you free?\r\nNoon?`) {
|
||||
t.Errorf("letter:\n%s", letter)
|
||||
}
|
||||
if m, _ := srv.B.Store.Mail(u.ID, inbox[0].ID); !m.Read {
|
||||
t.Error("not marked read")
|
||||
}
|
||||
c.send("Kk", []byte(button0(t, letter, "Delete")))
|
||||
c.stream() // the letter closed
|
||||
c.stream() // the last mailbox closed
|
||||
if s := c.stream(); !strings.Contains(s, "You have 1 message, 1 new") {
|
||||
t.Errorf("after delete:\n%s", s)
|
||||
}
|
||||
// no one reads another's mail
|
||||
a.send("Kk", []byte(fmt.Sprintf("read mail %d", inbox[1].ID)))
|
||||
if s := a.stream(); !strings.Contains(s, "not in your mailbox") {
|
||||
t.Errorf("another's:\n%s", s)
|
||||
}
|
||||
}
|
||||
|
||||
func TestKeywords(t *testing.T) {
|
||||
srv, _ := newServer(t)
|
||||
a := signedOn(t, srv, "alice")
|
||||
a.send("Kk", deData(" HELP "))
|
||||
a.stream()
|
||||
if s := a.stream(); !strings.Contains(s, "Mail: your mailbox") || button0(t, s, "Chat") != "chat" {
|
||||
t.Errorf("help:\n%s", s)
|
||||
}
|
||||
a.send("Kk", []byte("who"))
|
||||
a.stream()
|
||||
if s := a.stream(); !strings.Contains(s, "1 caller on now") || !strings.Contains(s, "alice, by") {
|
||||
t.Errorf("who:\n%s", s)
|
||||
}
|
||||
a.send("Kk", []byte("chat"))
|
||||
if s := a.stream(); !strings.Contains(s, "man_close") {
|
||||
t.Errorf("chat:\n%s", s)
|
||||
}
|
||||
if s := a.stream(); !strings.Contains(s, "uni_invoke_local <32-117>") {
|
||||
t.Errorf("chat:\n%s", s)
|
||||
}
|
||||
a.stream()
|
||||
a.chatLine()
|
||||
a.send("Kk", deData("nowhere"))
|
||||
if s := a.stream(); !strings.Contains(s, `There is no keyword \"nowhere\" here`) {
|
||||
t.Errorf("unknown:\n%s", s)
|
||||
}
|
||||
}
|
||||
+10
-2
@@ -23,7 +23,7 @@ func alertForm(text string) string {
|
||||
}
|
||||
|
||||
// welcomeForm is the BBS's Welcome window.
|
||||
func welcomeForm(bbsName, user string, online int) string {
|
||||
func welcomeForm(bbsName, user string, online, unread int) string {
|
||||
others := "You are the only one here."
|
||||
switch {
|
||||
case online == 2:
|
||||
@@ -31,17 +31,25 @@ func welcomeForm(bbsName, user string, online int) string {
|
||||
case online > 2:
|
||||
others = fmt.Sprintf("%d other callers are here.", online-1)
|
||||
}
|
||||
mail := "You have no new mail."
|
||||
if unread > 0 {
|
||||
mail = fmt.Sprintf("You have %s. The keyword Mail opens your mailbox.", count(unread, "new message", "new messages"))
|
||||
}
|
||||
text := strings.Join([]string{
|
||||
fmt.Sprintf("Welcome to %s, %s!", bbsName, user),
|
||||
"",
|
||||
"You are signed on with your BBS account. " + others,
|
||||
"",
|
||||
mail,
|
||||
"",
|
||||
"The keyword Help lists where you can go.",
|
||||
}, "\r\n") // the client drops what follows a bare CR
|
||||
return fmt.Sprintf(`uni_start_stream
|
||||
man_start_object <ind_group, %s>
|
||||
mat_orientation <vcf>
|
||||
mat_position <center_center>
|
||||
man_start_object <view, "">
|
||||
mat_size <40, 6>
|
||||
mat_size <40, 9>
|
||||
man_append_data <%s>
|
||||
man_end_data
|
||||
man_end_object
|
||||
|
||||
@@ -0,0 +1,146 @@
|
||||
package aol
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/D-Modem/bbs/internal/aol/fdo"
|
||||
)
|
||||
|
||||
// Keywords: the client's Keyword window sends Kk with the word typed as
|
||||
// de_data, and a button of the host's sends it with the word as it is
|
||||
// (sm_send_token_raw <"Kk", "...">). Each takes the user to a part of
|
||||
// the BBS.
|
||||
|
||||
// keywords are the ones HELP lists, with what each does.
|
||||
var keywords = []struct{ word, what string }{
|
||||
{"Chat", "People Connection, the BBS's main chat room"},
|
||||
{"Mail", "your mailbox"},
|
||||
{"Who", "who is on the BBS now"},
|
||||
{"Help", "this list"},
|
||||
}
|
||||
|
||||
// The host's other windows, by global id.
|
||||
const (
|
||||
whoWindow = "50-2-1"
|
||||
helpWindow = "50-2-2"
|
||||
)
|
||||
|
||||
// keyword takes the user where a keyword goes.
|
||||
func (ss *session) keyword(kw string) {
|
||||
kw = strings.Join(strings.Fields(kw), " ")
|
||||
k := strings.ToLower(kw)
|
||||
if verb, arg, ok := strings.Cut(k, " mail "); ok {
|
||||
id, err := strconv.ParseInt(arg, 10, 64)
|
||||
switch {
|
||||
case err != nil:
|
||||
case verb == "read":
|
||||
ss.readMail(id)
|
||||
return
|
||||
case verb == "delete":
|
||||
ss.deleteMail(id)
|
||||
return
|
||||
}
|
||||
}
|
||||
switch k {
|
||||
case "":
|
||||
ss.send(waitOff)
|
||||
case "chat", "people connection", "pc", "lobby", "people":
|
||||
ss.enterChat()
|
||||
case "mail", "read mail", "new mail", "mailbox", "check mail", "email", "e-mail":
|
||||
ss.mailbox()
|
||||
case "who", "online", "who's online", "whos online", "members":
|
||||
ss.who()
|
||||
case "help", "keyword", "keywords", "keyword list":
|
||||
ss.help()
|
||||
default:
|
||||
ss.notice(fmt.Sprintf("There is no keyword \"%s\" here. The keyword Help lists those there are.", kw))
|
||||
}
|
||||
}
|
||||
|
||||
// keywordOf is the word in a Kk: the first field a form sent, or the
|
||||
// text a button sent as it is.
|
||||
func keywordOf(body []byte) string {
|
||||
if isText(body) {
|
||||
return fdo.FromLatin1(body)
|
||||
}
|
||||
_, atoms := splitStream("Kk", body)
|
||||
if f := texts(atoms); len(f) > 0 {
|
||||
return f[0]
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// isText is whether b is plain text, not a stream id and atoms.
|
||||
func isText(b []byte) bool {
|
||||
if len(b) == 0 {
|
||||
return false
|
||||
}
|
||||
for _, c := range b {
|
||||
if c < 0x20 && c != '\t' {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// who lists the callers on now.
|
||||
func (ss *session) who() {
|
||||
var lines []string
|
||||
for _, n := range ss.s.B.Nodes() {
|
||||
name := n.User
|
||||
if name == "" {
|
||||
name = "(signing on)"
|
||||
}
|
||||
line := fmt.Sprintf("%s, by %s, %dm", name, n.Via, int(time.Since(n.Since).Minutes()))
|
||||
if n.Activity != "" {
|
||||
line += ": " + n.Activity
|
||||
}
|
||||
lines = append(lines, line)
|
||||
}
|
||||
text := fmt.Sprintf("%s on now:\r\n\r\n%s", count(len(lines), "caller", "callers"), strings.Join(lines, "\r\n"))
|
||||
ss.send(closeForm(whoWindow))
|
||||
ss.send(windowForm(whoWindow, "Who's Online", text, 12, []button{{"Chat", "chat"}}))
|
||||
}
|
||||
|
||||
// help lists the keywords.
|
||||
func (ss *session) help() {
|
||||
var lines []string
|
||||
var buttons []button
|
||||
for _, k := range keywords {
|
||||
lines = append(lines, k.word+": "+k.what)
|
||||
if k.word != "Help" {
|
||||
buttons = append(buttons, button{k.word, strings.ToLower(k.word)})
|
||||
}
|
||||
}
|
||||
text := "Type a keyword in the Keyword window (Ctrl+K) to go there:\r\n\r\n" + strings.Join(lines, "\r\n")
|
||||
ss.send(closeForm(helpWindow))
|
||||
ss.send(windowForm(helpWindow, "Keywords", text, 8, buttons))
|
||||
}
|
||||
|
||||
// button is a button in a host window that sends a keyword.
|
||||
type button struct{ label, keyword string }
|
||||
|
||||
// windowForm is a host window: its title, a view of text, and its
|
||||
// buttons.
|
||||
func windowForm(gid, title, text string, rows int, buttons []button) string {
|
||||
var sb strings.Builder
|
||||
fmt.Fprintf(&sb, "uni_start_stream\n man_start_object <ind_group, %s>\n mat_object_id <%s>\n"+
|
||||
" mat_orientation <vcf>\n mat_position <center_center>\n", fdo.Quote(title), gid)
|
||||
fmt.Fprintf(&sb, " man_start_object <view, \"\">\n mat_size <50, %d>\n man_append_data <%s>\n"+
|
||||
" man_end_data\n man_end_object\n", rows, fdo.Quote(text))
|
||||
for _, b := range buttons {
|
||||
fmt.Fprintf(&sb, " man_start_object <trigger, %s>\n act_replace_select_action\n <\n"+
|
||||
" uni_start_stream\n sm_send_token_raw <\"Kk\", %s>\n uni_end_stream\n >\n"+
|
||||
" man_end_object\n", fdo.Quote(b.label), fdo.Quote(b.keyword))
|
||||
}
|
||||
sb.WriteString(" man_end_object\n man_update_display\n uni_wait_off\nuni_end_stream\n")
|
||||
return sb.String()
|
||||
}
|
||||
|
||||
// notice is a message box that also ends the client's wait.
|
||||
func (ss *session) notice(text string) {
|
||||
ss.send(fmt.Sprintf("uni_start_stream\n async_alert <info, %s>\n uni_wait_off\nuni_end_stream\n", fdo.Quote(text)))
|
||||
}
|
||||
@@ -0,0 +1,250 @@
|
||||
package aol
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"html"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/D-Modem/bbs/internal/aol/fdo"
|
||||
"github.com/D-Modem/bbs/internal/bbs"
|
||||
"github.com/D-Modem/bbs/internal/store"
|
||||
)
|
||||
|
||||
// Mail: the BBS's private mail. The client's own Write Mail form sends
|
||||
// m4 with a MIP stream: the addressees (mip_addressee), the subject
|
||||
// (mip_subject) and the text (mip_text, or mip_data in pieces; HTML from
|
||||
// 4.0), to mip_end_message. A client may send the header in a stream of
|
||||
// its own and wait for mip_header_accepted before it sends the text; the
|
||||
// host answers a whole message with mip_message_accepted.
|
||||
//
|
||||
// The mailbox and each mail are the host's own windows (mailForm), whose
|
||||
// buttons send keywords: "mail", "read mail N", "delete mail N".
|
||||
|
||||
// The host's mail windows, by global id.
|
||||
const (
|
||||
mailboxWindow = "50-1-1"
|
||||
letterWindow = "50-1-2"
|
||||
)
|
||||
|
||||
// mailboxMax is the most mail the mailbox lists.
|
||||
const mailboxMax = 20
|
||||
|
||||
// draft is a mail the client has sent the header of.
|
||||
type draft struct {
|
||||
to []string
|
||||
subject string
|
||||
text strings.Builder
|
||||
}
|
||||
|
||||
// sendMail is m4: (part of) a mail from the client's Write Mail form.
|
||||
func (ss *session) sendMail(atoms []byte) {
|
||||
frames, err := fdo.Decode(atoms)
|
||||
if err != nil {
|
||||
ss.logf("m4: %v", err)
|
||||
ss.notice("Your mail could not be read, and was not sent.")
|
||||
return
|
||||
}
|
||||
ss.mu.Lock()
|
||||
d := ss.draft
|
||||
if d == nil {
|
||||
d = &draft{}
|
||||
ss.draft = d
|
||||
}
|
||||
ss.mu.Unlock()
|
||||
header, end := false, false
|
||||
for _, f := range frames {
|
||||
switch f.Name() {
|
||||
case "mip_addressee":
|
||||
for _, to := range strings.FieldsFunc(fdo.FromLatin1(trimNul(f.Data)), func(r rune) bool { return r == ',' }) {
|
||||
if to = strings.TrimSpace(to); to != "" {
|
||||
d.to = append(d.to, to)
|
||||
}
|
||||
}
|
||||
header = true
|
||||
case "mip_subject":
|
||||
d.subject = strings.TrimSpace(fdo.FromLatin1(trimNul(f.Data)))
|
||||
header = true
|
||||
case "mip_text", "mip_data":
|
||||
d.text.WriteString(fdo.FromLatin1(trimNul(f.Data)))
|
||||
case "mip_end_message":
|
||||
end = true
|
||||
}
|
||||
}
|
||||
if !end {
|
||||
if d.text.Len() > 64<<10 {
|
||||
ss.mu.Lock()
|
||||
ss.draft = nil
|
||||
ss.mu.Unlock()
|
||||
ss.notice("Your mail is too long, and was not sent.")
|
||||
return
|
||||
}
|
||||
if header {
|
||||
ss.send("uni_start_stream\n mip_header_accepted\n uni_wait_off\nuni_end_stream\n")
|
||||
} else {
|
||||
ss.send(waitOff)
|
||||
}
|
||||
return
|
||||
}
|
||||
ss.mu.Lock()
|
||||
ss.draft = nil
|
||||
ss.mu.Unlock()
|
||||
ss.deliver(d)
|
||||
}
|
||||
|
||||
// deliver sends a mail the client has sent all of, to each addressee.
|
||||
func (ss *session) deliver(d *draft) {
|
||||
if len(d.to) == 0 {
|
||||
ss.notice("Your mail has no one to send it to.")
|
||||
return
|
||||
}
|
||||
subject := d.subject
|
||||
if subject == "" {
|
||||
subject = "(no subject)"
|
||||
}
|
||||
body := bbs.WrapBody(mailText(d.text.String()))
|
||||
var sent, failed []string
|
||||
for _, to := range d.to {
|
||||
name := to
|
||||
if u := ss.member(to); u != nil {
|
||||
name = u.Name
|
||||
}
|
||||
if err := ss.n.SendMail(name, subject, body, 0); err != nil {
|
||||
failed = append(failed, fmt.Sprintf("%s: %v.", to, err))
|
||||
continue
|
||||
}
|
||||
sent = append(sent, name)
|
||||
}
|
||||
accepted := ""
|
||||
if len(sent) > 0 {
|
||||
accepted = " mip_message_accepted\n"
|
||||
}
|
||||
ss.send("uni_start_stream\n" + accepted + " uni_wait_off\nuni_end_stream\n")
|
||||
if len(failed) > 0 {
|
||||
msg := "Your mail was not sent to:\r\n" + strings.Join(failed, "\r\n")
|
||||
if len(sent) > 0 {
|
||||
msg = "Your mail was sent to " + strings.Join(sent, ", ") + ", but not to:\r\n" + strings.Join(failed, "\r\n")
|
||||
}
|
||||
ss.alert(msg)
|
||||
}
|
||||
}
|
||||
|
||||
var (
|
||||
htmlBreak = regexp.MustCompile(`(?i)<\s*(br|/p|p|/div)\b[^>]*>`)
|
||||
htmlTag = regexp.MustCompile(`<[^>]*>`)
|
||||
)
|
||||
|
||||
// mailText is a mail's text as the BBS keeps it: lines ended by LF, and
|
||||
// 4.0's HTML made plain.
|
||||
func mailText(s string) string {
|
||||
s = strings.ReplaceAll(s, "\r\n", "\n")
|
||||
s = strings.ReplaceAll(s, "\r", "\n")
|
||||
if strings.Contains(s, "<") && strings.Contains(s, ">") {
|
||||
s = htmlBreak.ReplaceAllString(s, "\n")
|
||||
s = htmlTag.ReplaceAllString(s, "")
|
||||
s = html.UnescapeString(s)
|
||||
s = strings.ReplaceAll(s, " ", " ")
|
||||
}
|
||||
return strings.Trim(s, "\n")
|
||||
}
|
||||
|
||||
// mailbox is the user's mail, newest first, each a button that reads it.
|
||||
func (ss *session) mailbox() {
|
||||
all, err := ss.s.B.Store.Inbox(ss.u.ID)
|
||||
if err != nil {
|
||||
ss.logf("inbox: %v", err)
|
||||
ss.notice("Your mailbox can't be opened just now.")
|
||||
return
|
||||
}
|
||||
unread := 0
|
||||
for _, m := range all {
|
||||
if !m.Read {
|
||||
unread++
|
||||
}
|
||||
}
|
||||
var text string
|
||||
switch {
|
||||
case len(all) == 0:
|
||||
text = "You have no mail."
|
||||
case unread == 0:
|
||||
text = fmt.Sprintf("You have %s, all read.", count(len(all), "message", "messages"))
|
||||
default:
|
||||
text = fmt.Sprintf("You have %s, %d new (marked *).", count(len(all), "message", "messages"), unread)
|
||||
}
|
||||
if len(all) > mailboxMax {
|
||||
text += fmt.Sprintf(" The newest %d are here.", mailboxMax)
|
||||
}
|
||||
text += "\r\n\r\nTo write one, use Compose Mail."
|
||||
var buttons []button
|
||||
for i := len(all) - 1; i >= 0 && len(buttons) < mailboxMax; i-- {
|
||||
m := all[i]
|
||||
mark := " "
|
||||
if !m.Read {
|
||||
mark = "*"
|
||||
}
|
||||
buttons = append(buttons, button{
|
||||
label: fmt.Sprintf("%s %s %s: %s", mark, m.Created.Format("Jan 2"), m.From, clip(m.Subject, 40)),
|
||||
keyword: fmt.Sprintf("read mail %d", m.ID)})
|
||||
}
|
||||
ss.send(closeForm(mailboxWindow))
|
||||
ss.send(windowForm(mailboxWindow, "Mailbox", text, 4, buttons))
|
||||
}
|
||||
|
||||
// readMail shows a mail of the user's, and marks it read.
|
||||
func (ss *session) readMail(id int64) {
|
||||
m, err := ss.s.B.Store.Mail(ss.u.ID, id)
|
||||
if err != nil {
|
||||
if !errors.Is(err, store.ErrNotFound) {
|
||||
ss.logf("mail %d: %v", id, err)
|
||||
}
|
||||
ss.notice("That mail is not in your mailbox.")
|
||||
return
|
||||
}
|
||||
if !m.Read {
|
||||
ss.s.B.Store.MarkMailRead(ss.u.ID, m.ID)
|
||||
}
|
||||
text := strings.Join([]string{
|
||||
"From: " + m.From,
|
||||
"To: " + m.To,
|
||||
"Date: " + m.Created.Format("Jan 2, 2006 15:04"),
|
||||
"Subject: " + m.Subject,
|
||||
"",
|
||||
strings.ReplaceAll(m.Body, "\n", "\r\n"),
|
||||
}, "\r\n")
|
||||
ss.send(closeForm(letterWindow))
|
||||
ss.send(windowForm(letterWindow, m.Subject, text, 16, []button{
|
||||
{"Delete", fmt.Sprintf("delete mail %d", m.ID)},
|
||||
{"Mailbox", "mail"},
|
||||
}))
|
||||
}
|
||||
|
||||
// deleteMail deletes a mail of the user's, and shows what is left.
|
||||
func (ss *session) deleteMail(id int64) {
|
||||
if err := ss.s.B.Store.DeleteMail(ss.u.ID, id); err != nil {
|
||||
if !errors.Is(err, store.ErrNotFound) {
|
||||
ss.logf("delete mail %d: %v", id, err)
|
||||
}
|
||||
ss.notice("That mail is not in your mailbox.")
|
||||
return
|
||||
}
|
||||
ss.send(closeForm(letterWindow))
|
||||
ss.mailbox()
|
||||
}
|
||||
|
||||
// count is n and a noun, singular or plural.
|
||||
func count(n int, one, many string) string {
|
||||
if n == 1 {
|
||||
return "1 " + one
|
||||
}
|
||||
return fmt.Sprintf("%d %s", n, many)
|
||||
}
|
||||
|
||||
// clip is s cut to n characters, with "..." if it was longer.
|
||||
func clip(s string, n int) string {
|
||||
r := []rune(s)
|
||||
if len(r) <= n {
|
||||
return s
|
||||
}
|
||||
return string(r[:n-3]) + "..."
|
||||
}
|
||||
+13
-1
@@ -31,6 +31,7 @@ type session struct {
|
||||
slots map[string]byte // the room window's people, by lower-case name
|
||||
nextSlot byte
|
||||
ims map[string]int // IM windows, by lower-case partner
|
||||
draft *draft // the mail the client is sending
|
||||
|
||||
parts map[partKey][]byte // streams the client has sent part of
|
||||
}
|
||||
@@ -83,6 +84,12 @@ func (ss *session) signedOn() bool {
|
||||
|
||||
// token handles one token from the client.
|
||||
func (ss *session) token(tok string, body []byte) {
|
||||
if tok == "Kk" && isText(body) { // from a button: the word, as it is
|
||||
if ss.signedOn() {
|
||||
ss.keyword(keywordOf(body))
|
||||
}
|
||||
return
|
||||
}
|
||||
sid, atoms := splitStream(tok, body)
|
||||
if atoms = ss.whole(tok, sid, atoms); atoms == nil {
|
||||
return // more to come
|
||||
@@ -104,6 +111,10 @@ func (ss *session) token(tok string, body []byte) {
|
||||
ss.sendIM(atoms)
|
||||
case tok == "iO":
|
||||
ss.available(atoms)
|
||||
case tok == "Kk":
|
||||
ss.keyword(keywordOf(append(sid, atoms...)))
|
||||
case tok == "m4":
|
||||
ss.sendMail(atoms)
|
||||
default:
|
||||
ss.logf("token %q (not served yet)", tok)
|
||||
// never leave the client waiting on an answer that won't come
|
||||
@@ -275,7 +286,8 @@ func (ss *session) signOn(atoms []byte) {
|
||||
ss.mu.Unlock()
|
||||
b.Log.Printf("node %d: %s on AOL (%s, %s)", n.Num, u.Name, ss.host, ss.init)
|
||||
ss.send(onlineForm)
|
||||
ss.send(welcomeForm(b.Conf().Name, u.Name, len(b.Nodes())))
|
||||
unread, _ := b.Store.UnreadMail(u.ID)
|
||||
ss.send(welcomeForm(b.Conf().Name, u.Name, len(b.Nodes()), unread))
|
||||
for _, e := range n.Kept() {
|
||||
ss.event(e)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user