Skip to content

Commit 2bb41c0

Browse files
added support for new 'start-dm' command based on phone or email
1 parent 9541fd0 commit 2bb41c0

File tree

3 files changed

+93
-2
lines changed

3 files changed

+93
-2
lines changed

commands.go

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// mautrix-imessage - A Matrix-iMessage puppeting bridge.
2+
// Copyright (C) 2022 Tulir Asokan
3+
//
4+
// This program is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Affero General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// This program is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Affero General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Affero General Public License
15+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
17+
package main
18+
19+
import (
20+
"maunium.net/go/mautrix/bridge/commands"
21+
)
22+
23+
var (
24+
HelpSectionChatManagement = commands.HelpSection{Name: "Chat management", Order: 11}
25+
)
26+
27+
type WrappedCommandEvent struct {
28+
*commands.Event
29+
Bridge *IMBridge
30+
User *User
31+
Portal *Portal
32+
}
33+
34+
func (br *IMBridge) RegisterCommands() {
35+
proc := br.CommandProcessor.(*commands.Processor)
36+
proc.AddHandlers(
37+
cmdStartDm,
38+
)
39+
}
40+
41+
func wrapCommand(handler func(*WrappedCommandEvent)) func(*commands.Event) {
42+
return func(ce *commands.Event) {
43+
user := ce.User.(*User)
44+
var portal *Portal
45+
if ce.Portal != nil {
46+
portal = ce.Portal.(*Portal)
47+
}
48+
br := ce.Bridge.Child.(*IMBridge)
49+
handler(&WrappedCommandEvent{ce, br, user, portal})
50+
}
51+
}
52+
53+
var cmdStartDm = &commands.FullHandler{
54+
Func: wrapCommand(fnStartDm),
55+
Name: "start-dm",
56+
Help: commands.HelpMeta{
57+
Section: HelpSectionChatManagement,
58+
Description: "Creates a new DM with the specified number.",
59+
},
60+
RequiresPortal: false,
61+
RequiresLogin: false,
62+
}
63+
64+
func fnStartDm(ce *WrappedCommandEvent) {
65+
ce.Bridge.ZLog.Trace().Interface("args", ce.Args).Str("cmd", ce.Command).Msg("fnStartDm")
66+
67+
_, err := ce.Bridge.WebsocketHandler.StartChat(*&StartDMRequest{
68+
Identifier: ce.RawArgs,
69+
Force: false,
70+
ActuallyStart: true,
71+
})
72+
73+
if err != nil {
74+
ce.Reply("Failed to start DM: %s", err)
75+
} else {
76+
ce.Reply("Created!")
77+
}
78+
}
79+
80+
// TODO: potentially add the following commands
81+
// contact-search (search contact for addresses so they can be used to start a dm)
82+
// start-group-chat

imessage/bluebubbles/api.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -1037,12 +1037,18 @@ func (bb *blueBubbles) SendTypingNotification(chatID string, typing bool) error
10371037

10381038
func (bb *blueBubbles) ResolveIdentifier(identifier string) (string, error) {
10391039
bb.log.Trace().Str("identifier", identifier).Msg("ResolveIdentifier")
1040-
return "", ErrNotImplemented
1040+
1041+
// if the identifier is a phone number, remove dashes and prepend the +
1042+
if !strings.Contains(identifier, "@") {
1043+
identifier = "+" + numericOnly(identifier)
1044+
}
1045+
1046+
return "iMessage;-;" + identifier, nil
10411047
}
10421048

10431049
func (bb *blueBubbles) PrepareDM(guid string) error {
10441050
bb.log.Trace().Str("guid", guid).Msg("PrepareDM")
1045-
return ErrNotImplemented
1051+
return nil
10461052
}
10471053

10481054
func (bb *blueBubbles) CreateGroup(users []string) (*imessage.CreateGroupResponse, error) {

main.go

+3
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,9 @@ func (br *IMBridge) Init() {
229229
br.IMHandler = NewiMessageHandler(br)
230230
br.WebsocketHandler = NewWebsocketCommandHandler(br)
231231
br.wsOnConnectWait.Add(1)
232+
233+
br.CommandProcessor = commands.NewProcessor(&br.Bridge)
234+
br.RegisterCommands()
232235
}
233236

234237
type PingResponse struct {

0 commit comments

Comments
 (0)