From a84b492a9a3c2d758278c069d1a2859feaa8c16a Mon Sep 17 00:00:00 2001 From: mappu Date: Tue, 5 Jun 2018 18:32:53 +1200 Subject: [PATCH] heuristic detect DC /me messages --- NTFServer.go | 39 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/NTFServer.go b/NTFServer.go index 6c538ce..6a4613b 100644 --- a/NTFServer.go +++ b/NTFServer.go @@ -289,8 +289,43 @@ func (this *NTFServer) HandleHubMessage(msg upstreamMessage) { case libnmdc.EVENT_SYSTEM_MESSAGE_FROM_CONN, libnmdc.EVENT_SYSTEM_MESSAGE_FROM_HUB: // This includes "want to register your nick?" and chat-catchup messages - // Don't mirror them into telegram - log.Printf("Hub(%s): * %s", msg.hubNick, msg.evt.Message) + // But it also includes /me messages + // Hard to tell them apart - heuristic detection by matching the first space-separated word against known online users + + isActionMessage := false + + if conn, ok := this.conns[msg.hubNick]; ok { + firstWord := strings.SplitN(msg.evt.Message, ` `, 2)[0] + _ = conn.Users(func(umap *map[string]libnmdc.UserInfo) error { + for knownHubNick, _ := range *umap { + if knownHubNick == firstWord { + isActionMessage = true + return nil + } + } + return nil + }) + } + + if isActionMessage { + // Treat it as a full post + + // Coalesce from multiple connections + if this.Coalesce("*", msg.evt.Message) { + return // ignore - we heard this message already recently + } + + // Display the message + htmlMsg := "* " + html.EscapeString(msg.evt.Message) + "" + err := this.GroupChatSayHTML(htmlMsg) + if err != nil { + log.Printf("Delivering action message to group chat: %s", err.Error()) + } + + } else { + // Don't mirror them into telegram + log.Printf("Hub(%s): * %s", msg.hubNick, msg.evt.Message) + } case libnmdc.EVENT_CONNECTION_STATE_CHANGED: log.Printf("Hub(%s): * Connection %s", msg.evt.StateChange.String())