Last active
August 22, 2018 19:30
-
-
Save babolivier/3e67630e458f8404074f09707912ec60 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"strings" | |
"github.com/matrix-org/gomatrix" | |
) | |
func main() { | |
var roomsMap = make(map[string]bool) | |
client, _ := gomatrix.NewClient("https://matrix.example.tld", "@alice:example.tld", "ACCESS_TOKEN") | |
var movedMsg = "Moved to @new_alice:example2.tld" | |
if display, err := client.GetDisplayName("@alice:example.tld"); err != nil { | |
panic(err) | |
} else if display.DisplayName != movedMsg { | |
if err = client.SetDisplayName(movedMsg); err != nil { | |
panic(err) | |
} | |
} | |
rooms, _ := client.JoinedRooms() | |
for _, room := range rooms.JoinedRooms { | |
roomsMap[room] = true | |
} | |
resp, err := client.SyncRequest(1000, "", "0", false, "online") | |
if err != nil { | |
panic(err) | |
} | |
for _, e := range resp.AccountData.Events { | |
if e.Type == "m.direct" { | |
for _, rooms := range e.Content { | |
for _, room := range rooms.([]interface{}) { | |
delete(roomsMap, room.(string)) | |
} | |
} | |
} | |
} | |
for room := range roomsMap { | |
// Most rooms on my HS were tests or monitoring stuff I don't need anymore | |
if strings.HasSuffix(room, "example.tld") { | |
continue | |
} | |
var id string | |
nameContent := struct { | |
Name string `json:"name"` | |
}{} | |
_ = client.StateEvent(room, "m.room.name", "", &nameContent) | |
if len(nameContent.Name) > 0 { | |
id = nameContent.Name | |
} else { | |
aliasContent := struct { | |
Alias string `json:"alias"` | |
}{} | |
_ = client.StateEvent(room, "m.room.canonical_alias", "", &aliasContent) | |
if len(aliasContent.Alias) > 0 { | |
id = aliasContent.Alias | |
} else { | |
aliasesContent := struct { | |
Aliases []string `json:"aliases"` | |
}{} | |
_ = client.StateEvent(room, "m.room.aliases", "", &aliasesContent) | |
id = fmt.Sprintf("%s", aliasesContent.Aliases) | |
} | |
} | |
fmt.Printf("----- %s (%s) -----\n", id, room) | |
memberContent := struct { | |
Membership string `json:"membership"` | |
}{} | |
// Make sure I don't invite myself twice | |
// Matrix (through Synapse) might handle this well but I'm too lazy to check | |
_ = client.StateEvent(room, "m.room.member", "@new_alice:example2.tld", &memberContent) | |
var status string | |
if len(memberContent.Membership) == 0 { | |
req := gomatrix.ReqInviteUser{ | |
UserID: "@new_alice:example2.tld", | |
} | |
_, err := client.InviteUser(room, &req) | |
if err != nil { | |
status = fmt.Sprintf("failed: %s", err.Error()) | |
} else { | |
status = "invited" | |
} | |
} else { | |
status = fmt.Sprintf("ignored (membership exists and is '%s')", memberContent.Membership) | |
} | |
fmt.Printf("%s\n", status) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment