|
| 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 |
0 commit comments