This repository has been archived by the owner on Oct 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
messageHandlers.go
87 lines (73 loc) · 1.6 KB
/
messageHandlers.go
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
package main
import (
"fmt"
"strings"
"time"
"whapp-irc/util"
"whapp-irc/whapp"
)
// Message represents a WhatsApp message, with some basic formatting for IRC.
type Message struct {
From, To string
Body string
IsReply bool
Message *whapp.Message
}
// Quoted returns the quoted WhatsApp message.
func (msg *Message) Quoted() *whapp.Message {
return msg.Message.QuotedMessage
}
// MessageHandler represents a handler for a WhatsApp message to be sent to an
// IRC client.
type MessageHandler func(conn *Connection, msg Message) error
var handlerNormal = func(conn *Connection, msg Message) error {
lines := strings.Split(msg.Body, "\n")
time := msg.Message.Time()
if msg.IsReply {
line := "> " + lines[0]
if nRest := len(lines) - 1; nRest > 0 {
line = fmt.Sprintf(
"%s [and %d more %s]",
line,
nRest,
util.Plural(nRest, "line", "lines"),
)
}
return conn.irc.PrivateMessage(time, msg.From, msg.To, line)
}
for _, line := range lines {
if err := conn.irc.PrivateMessage(
time,
msg.From,
msg.To,
line,
); err != nil {
return err
}
}
return nil
}
var handlerAlternativeReplay = func(conn *Connection, msg Message) error {
if msg.IsReply {
return nil
}
for _, line := range strings.Split(msg.Body, "\n") {
util.LogMessage(msg.Message.Time(), msg.From, msg.To, line)
msg := fmt.Sprintf(
"(%s) %s->%s: %s",
msg.Message.Time().Format("2006-01-02 15:04:05"),
msg.From,
msg.To,
line,
)
if err := conn.irc.PrivateMessage(
time.Now(),
"replay",
conn.irc.Nick(),
msg,
); err != nil {
return err
}
}
return nil
}