Debian Codegraph @

example/main.go

raw
1 // Copyright (C) 2017 Tulir Asokan
2 // Copyright (C) 2018-2020 Luca Weiss
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU 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 General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17 package main
18
19 import (
20 "flag"
21 "fmt"
22 "os"
23
24 "maunium.net/go/mautrix"
25 "maunium.net/go/mautrix/event"
26 )
27
28 var homeserver = flag.String("homeserver", "", "Matrix homeserver")
29 var username = flag.String("username", "", "Matrix username localpart")
30 var password = flag.String("password", "", "Matrix password")
31
32 func main() {
33 flag.Parse()
34 if *username == "" || *password == "" || *homeserver == "" {
35 _, _ = fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
36 flag.PrintDefaults()
37 os.Exit(1)
38 }
39
40 fmt.Println("Logging into", *homeserver, "as", *username)
41 client, err := mautrix.NewClient(*homeserver, "", "")
42 if err != nil {
43 panic(err)
44 }
45 _, err = client.Login(&mautrix.ReqLogin{
46 Type: "m.login.password",
47 Identifier: mautrix.UserIdentifier{Type: mautrix.IdentifierTypeUser, User: *username},
48 Password: *password,
49 StoreCredentials: true,
50 })
51 if err != nil {
52 panic(err)
53 }
54 fmt.Println("Login successful")
55
56 syncer := client.Syncer.(*mautrix.DefaultSyncer)
57 syncer.OnEventType(event.EventMessage, func(source mautrix.EventSource, evt *event.Event) {
58 fmt.Printf("<%[1]s> %[4]s (%[2]s/%[3]s)\n", evt.Sender, evt.Type.String(), evt.ID, evt.Content.AsMessage().Body)
59 })
60
61 err = client.Sync()
62 if err != nil {
63 panic(err)
64 }
65 }