/
interface.go
188 lines (160 loc) · 6.25 KB
/
interface.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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// mautrix-imessage - A Matrix-iMessage puppeting bridge.
// Copyright (C) 2022 Tulir Asokan
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package imessage
import (
"errors"
"fmt"
"os"
"path/filepath"
"time"
"github.com/rs/zerolog"
log "maunium.net/go/maulogger/v2"
"maunium.net/go/mautrix/id"
"go.mau.fi/mautrix-imessage/ipc"
)
var (
ErrNotLoggedIn = errors.New("you're not logged into iMessage")
)
type ContactAPI interface {
GetContactInfo(identifier string) (*Contact, error)
GetContactList() ([]*Contact, error)
SearchContactList(searchTerms string) ([]*Contact, error)
RefreshContactList() error
}
type ChatInfoAPI interface {
GetChatInfo(chatID, threadID string) (*ChatInfo, error)
GetGroupAvatar(chatID string) (*Attachment, error)
}
type VenturaFeatures interface {
UnsendMessage(chatID, targetGUID string, targetPart int) (*SendResponse, error)
EditMessage(chatID, targetGUID string, newText string, targetPart int) (*SendResponse, error)
}
type API interface {
Start(readyCallback func()) error
Stop()
GetMessagesSinceDate(chatID string, minDate time.Time, backfillID string) ([]*Message, error)
GetMessagesBetween(chatID string, minDate, maxDate time.Time) ([]*Message, error)
GetMessagesBeforeWithLimit(chatID string, before time.Time, limit int) ([]*Message, error)
GetMessagesWithLimit(chatID string, limit int, backfillID string) ([]*Message, error)
GetChatsWithMessagesAfter(minDate time.Time) ([]ChatIdentifier, error)
GetMessage(guid string) (*Message, error)
MessageChan() <-chan *Message
ReadReceiptChan() <-chan *ReadReceipt
TypingNotificationChan() <-chan *TypingNotification
ChatChan() <-chan *ChatInfo
ContactChan() <-chan *Contact
MessageStatusChan() <-chan *SendMessageStatus
BackfillTaskChan() <-chan *BackfillTask
ContactAPI
ChatInfoAPI
ResolveIdentifier(identifier string) (string, error)
PrepareDM(guid string) error
CreateGroup(guids []string) (*CreateGroupResponse, error)
SendMessage(chatID, text string, replyTo string, replyToPart int, richLink *RichLink, metadata MessageMetadata) (*SendResponse, error)
SendFile(chatID, text, filename string, pathOnDisk string, replyTo string, replyToPart int, mimeType string, voiceMemo bool, metadata MessageMetadata) (*SendResponse, error)
SendFileCleanup(sendFileDir string)
SendTapback(chatID, targetGUID string, targetPart int, tapback TapbackType, remove bool) (*SendResponse, error)
SendReadReceipt(chatID, readUpTo string) error
SendTypingNotification(chatID string, typing bool) error
SendMessageBridgeResult(chatID, messageID string, eventID id.EventID, success bool)
SendBackfillResult(chatID, backfillID string, success bool, idMap map[string][]id.EventID)
SendChatBridgeResult(guid string, mxid id.RoomID)
NotifyUpcomingMessage(eventID id.EventID)
PreStartupSyncHook() (StartupSyncHookResponse, error)
PostStartupSyncHook()
Capabilities() ConnectorCapabilities
}
var TempFilePermissions os.FileMode = 0640
var TempDirPermissions os.FileMode = 0700
func SendFilePrepare(filename string, data []byte) (string, string, error) {
dir, err := TempDir("mautrix-imessage-upload")
if err != nil {
return "", "", fmt.Errorf("failed to create temp dir: %w", err)
}
filePath := filepath.Join(dir, filename)
err = os.WriteFile(filePath, data, TempFilePermissions)
if err != nil {
return "", "", fmt.Errorf("failed to write data to temp file: %w", err)
}
return dir, filePath, err
}
type BridgeStatus struct {
StateEvent string `json:"state_event"`
Timestamp int64 `json:"timestamp"`
TTL int `json:"ttl"`
Source string `json:"source"`
Error string `json:"error,omitempty"`
Message string `json:"message,omitempty"`
UserID id.UserID `json:"user_id,omitempty"`
RemoteID string `json:"remote_id,omitempty"`
RemoteName string `json:"remote_name,omitempty"`
Info map[string]interface{} `json:"info,omitempty"`
}
type Bridge interface {
GetIPC() *ipc.Processor
GetLog() log.Logger
GetZLog() *zerolog.Logger
GetConnectorConfig() *PlatformConfig
PingServer() (start, serverTs, end time.Time)
SendBridgeStatus(state BridgeStatus)
ReIDPortal(oldGUID, newGUID string, mergeExisting bool) bool
GetMessagesSince(chatGUID string, since time.Time) []string
SetPushKey(req *PushKeyRequest)
}
var AppleEpoch = time.Date(2001, 1, 1, 0, 0, 0, 0, time.UTC)
var Implementations = make(map[string]func(Bridge) (API, error))
type PlatformConfig struct {
Platform string `yaml:"platform"`
IMRestPath string `yaml:"imessage_rest_path"`
IMRestArgs []string `yaml:"imessage_rest_args"`
ContactsMode string `yaml:"contacts_mode"`
HackySetLocale string `yaml:"hacky_set_locale"`
Environment []string `yaml:"environment"`
LogIPCPayloads bool `yaml:"log_ipc_payloads"`
UnixSocket string `yaml:"unix_socket"`
PingInterval int64 `yaml:"ping_interval_seconds"`
DeleteMediaAfterUpload bool `yaml:"delete_media_after_upload"`
BlueBubblesURL string `yaml:"bluebubbles_url"`
BlueBubblesPassword string `yaml:"bluebubbles_password"`
}
func (pc *PlatformConfig) BridgeName() string {
switch pc.Platform {
case "android":
return "Android SMS Bridge"
default:
return "iMessage Bridge"
}
}
func NewAPI(bridge Bridge) (API, error) {
cfg := bridge.GetConnectorConfig()
impl, ok := Implementations[cfg.Platform]
if !ok {
return nil, fmt.Errorf("no such platform \"%s\", available platforms: %+v", cfg.Platform, Implementations)
}
return impl(bridge)
}
func TempDir(name string) (string, error) {
dir := os.TempDir()
err := os.MkdirAll(dir, TempDirPermissions)
if err != nil {
return "", err
}
path, err := os.MkdirTemp(dir, name)
if err != nil {
return "", err
}
return path, os.Chmod(path, TempDirPermissions)
}