From dd5fa7f900bde3837b93463dcabe4a58227c8711 Mon Sep 17 00:00:00 2001 From: mappu Date: Sun, 3 Jun 2018 19:15:16 +1200 Subject: [PATCH] add userlist/whois commands --- NTFServer.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ README.txt | 7 +++++++ TODO.txt | 8 ++++---- 3 files changed, 59 insertions(+), 4 deletions(-) diff --git a/NTFServer.go b/NTFServer.go index 95eaafa..85eb5db 100644 --- a/NTFServer.go +++ b/NTFServer.go @@ -5,6 +5,7 @@ import ( "fmt" "html" "log" + "sort" "strings" "sync" "time" @@ -542,6 +543,51 @@ func (this *NTFServer) HandleDirectMessage(update telegram.Update) error { return respond("You are either already joined (try /quit first), or not yet registered (try /join first).") } + } else if strings.HasPrefix(msg, "/userlist") { + conn, ok := this.conns[hubNick] + if !ok { + return respond("Can't get userlist for you (No upstream hub connection)") + } + + usernames := make([]string, 0) + err := conn.Users(func(umap *map[string]libnmdc.UserInfo) error { + for k, _ := range *umap { + usernames = append(usernames, k) + } + return nil + }) + if err != nil { + log.Printf("Error retrieving userlist: %s", err.Error()) + return respond("Can't get userlist for you (internal error)") + } + + sort.Strings(usernames) + return respond("Online users: " + strings.Join(usernames, " ")) + + } else if strings.HasPrefix(msg, "/whois ") { + target := msg[7:] + conn, ok := this.conns[hubNick] + if !ok { + return respond("Can't get user details for you (No upstream hub connection)") + } + + var userinfo libnmdc.UserInfo + var exists bool + err := conn.Users(func(umap *map[string]libnmdc.UserInfo) error { + userinfo, exists = (*umap)[target] + return nil + }) + if err != nil { + log.Printf("Error retrieving userlist: %s", err.Error()) + return respond("Can't get user details for you (internal error)") + } + + if !exists { + return respond(fmt.Sprintf("There is no online user '%s'.", target)) + } + + return respond(fmt.Sprintf("Description: %s\nShare size: %d\nClient: %s %s", userinfo.Description, userinfo.ShareSize, userinfo.ClientTag, userinfo.ClientVersion)) + } else if strings.HasPrefix(msg, "/quit") { this.kickAndDrop(userID) @@ -568,6 +614,8 @@ Available commands: /join [hubnick] - Register as 'hubnick' and join ` + this.config.HubDescription + ` /rejoin - Re-invite if you are already registered /pm [recipient] [message] - Send a native PM (if connected) +/userlist - List native online users +/whois [hubnick] - Check if native user is online, and read their description /quit - Unregister your nick and leave the group chat ` diff --git a/README.txt b/README.txt index f38547c..a975084 100644 --- a/README.txt +++ b/README.txt @@ -14,6 +14,13 @@ Written in Go Create a new telegram bot - Use BotFather to create a new bot - Use BotFather to disable its privacy mode for group chats + - Use BotFather to add commands. Recommendation: + +` +setup - List available commands +userlist - List native online users +quit - Leave the group chat +` Create a telegram group - Manually create a group chat and add the bot to it diff --git a/TODO.txt b/TODO.txt index 7bafbb8..ab30527 100644 --- a/TODO.txt +++ b/TODO.txt @@ -32,9 +32,9 @@ [ ] Editing Messages support -[ ] Extra DM commands - [ ] Get native userlist - [ ] Check if native user is online - [ ] Get native user description / sharesize / ... +[X] Extra DM commands + [X] Get native userlist + [X] Check if native user is online + [X] Get native user description / sharesize / ... [ ] Publish