mirror of
https://github.com/Unkn0wnCat/matrix-veles.git
synced 2025-05-21 04:46:38 +02:00
Create auth API & comment code
This commit is contained in:
parent
1b15b12859
commit
c8d1c33cb4
17 changed files with 475 additions and 101 deletions
|
@ -17,6 +17,7 @@ import (
|
|||
"maunium.net/go/mautrix/event"
|
||||
)
|
||||
|
||||
// handleHashing hashes and checks a message, taking configured actions on match
|
||||
func handleHashing(content *event.MessageEventContent, evt *event.Event, matrixClient *mautrix.Client) {
|
||||
url, err := content.URL.Parse()
|
||||
if err != nil {
|
||||
|
@ -30,9 +31,7 @@ func handleHashing(content *event.MessageEventContent, evt *event.Event, matrixC
|
|||
return
|
||||
}
|
||||
|
||||
defer func(reader io.ReadCloser) {
|
||||
_ = reader.Close()
|
||||
}(reader)
|
||||
defer func(reader io.ReadCloser) { _ = reader.Close() }(reader)
|
||||
|
||||
hashWriter := sha512.New()
|
||||
if _, err = io.Copy(hashWriter, reader); err != nil {
|
||||
|
@ -42,52 +41,51 @@ func handleHashing(content *event.MessageEventContent, evt *event.Event, matrixC
|
|||
|
||||
sum := hex.EncodeToString(hashWriter.Sum(nil))
|
||||
|
||||
// Fetch room configuration for adjusting behaviour
|
||||
roomConfig := config.GetRoomConfig(evt.RoomID.String())
|
||||
|
||||
hashObj, err := db.GetEntryByHash(sum)
|
||||
if err != nil {
|
||||
if errors.Is(err, mongo.ErrNoDocuments) {
|
||||
/*entry := model.DBEntry{
|
||||
ID: primitive.NewObjectID(),
|
||||
HashValue: sum,
|
||||
FileURL: "placeholder",
|
||||
Timestamp: time.Now(),
|
||||
AddedBy: nil,
|
||||
Comments: nil,
|
||||
}
|
||||
|
||||
db.SaveEntry(&entry)*/
|
||||
|
||||
if roomConfig.Debug {
|
||||
matrixClient.SendNotice(evt.RoomID, fmt.Sprintf("DEBUG - This file is not on the hashlist: %s", sum))
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if roomConfig.Debug {
|
||||
matrixClient.SendNotice(evt.RoomID, "DEBUG - Failed to check file. See log.")
|
||||
}
|
||||
|
||||
fmt.Printf("Error trying to check database: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
if roomConfig.Debug {
|
||||
matrixClient.SendNotice(evt.RoomID, fmt.Sprintf("DEBUG !!! This file is on the hashlist: %s", sum))
|
||||
|
||||
jsonVal, _ := json.Marshal(hashObj)
|
||||
|
||||
var buf bytes.Buffer
|
||||
|
||||
json.Indent(&buf, jsonVal, "", " ")
|
||||
|
||||
matrixClient.SendNotice(evt.RoomID, fmt.Sprintf("DEBUG:\n%s", buf.String()))
|
||||
matrixClient.SendNotice(evt.RoomID, fmt.Sprintf("DEBUG:\n%s", makeFancyJSON(jsonVal)))
|
||||
}
|
||||
|
||||
if !checkSubscription(&roomConfig, hashObj) {
|
||||
return
|
||||
}
|
||||
|
||||
log.Printf("Illegal content detected in room %s!", roomConfig.RoomID)
|
||||
|
||||
handleIllegalContent(evt, matrixClient, hashObj, roomConfig)
|
||||
}
|
||||
|
||||
// makeFancyJSON formats / indents a JSON string
|
||||
func makeFancyJSON(input []byte) string {
|
||||
var buf bytes.Buffer
|
||||
json.Indent(&buf, input, "", " ")
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
// checkSubscription checks if the room is subscribed to one of hashObjs lists
|
||||
func checkSubscription(roomConfig *config.RoomConfig, hashObj *model.DBEntry) bool {
|
||||
if roomConfig.HashChecker.SubscribedLists == nil {
|
||||
log.Printf("Room %s is not subscribed to any lists!", roomConfig.RoomID)
|
||||
return // Not subscribed to any lists
|
||||
return false // Not subscribed to any lists
|
||||
}
|
||||
|
||||
subMap := make(map[string]bool)
|
||||
|
@ -110,14 +108,42 @@ func handleHashing(content *event.MessageEventContent, evt *event.Event, matrixC
|
|||
|
||||
if !found {
|
||||
log.Printf("Room %s is not subscribed to any lists of hashobj %s!", roomConfig.RoomID, hashObj.ID.Hex())
|
||||
return // Not subscribed
|
||||
return false // Not subscribed
|
||||
}
|
||||
|
||||
log.Printf("Illegal content detected in room %s!", roomConfig.RoomID)
|
||||
|
||||
handleIllegalContent(evt, matrixClient, hashObj, roomConfig)
|
||||
return true
|
||||
}
|
||||
|
||||
// handleIllegalContent is called when a hash-match is found to take configured actions
|
||||
func handleIllegalContent(evt *event.Event, matrixClient *mautrix.Client, hashObj *model.DBEntry, roomConfig config.RoomConfig) {
|
||||
switch roomConfig.HashChecker.HashCheckMode {
|
||||
case 0:
|
||||
postNotice(evt, matrixClient, hashObj, roomConfig)
|
||||
break
|
||||
case 1:
|
||||
redactMessage(evt, matrixClient, hashObj)
|
||||
if roomConfig.HashChecker.NoticeToChat {
|
||||
postNotice(evt, matrixClient, hashObj, roomConfig)
|
||||
}
|
||||
break
|
||||
case 2:
|
||||
muteUser(evt, matrixClient)
|
||||
redactMessage(evt, matrixClient, hashObj)
|
||||
if roomConfig.HashChecker.NoticeToChat {
|
||||
postNotice(evt, matrixClient, hashObj, roomConfig)
|
||||
}
|
||||
break
|
||||
case 3:
|
||||
banUser(evt, matrixClient, hashObj)
|
||||
redactMessage(evt, matrixClient, hashObj)
|
||||
if roomConfig.HashChecker.NoticeToChat {
|
||||
postNotice(evt, matrixClient, hashObj, roomConfig)
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// redactMessage deletes the message sent in the given event
|
||||
func redactMessage(evt *event.Event, matrixClient *mautrix.Client, hashObj *model.DBEntry) {
|
||||
opts := mautrix.ReqRedact{Reason: fmt.Sprintf("Veles has detected an hash-map-match! Tags: %s, ID: %s", hashObj.Tags, hashObj.ID.Hex())}
|
||||
_, err := matrixClient.RedactEvent(evt.RoomID, evt.ID, opts)
|
||||
|
@ -126,8 +152,9 @@ func redactMessage(evt *event.Event, matrixClient *mautrix.Client, hashObj *mode
|
|||
}
|
||||
}
|
||||
|
||||
func muteUser(evt *event.Event, matrixClient *mautrix.Client, hashObj *model.DBEntry) {
|
||||
plEventContent, err := GetRoomState(matrixClient, evt.RoomID)
|
||||
// muteUser sets a users power-level to -1 to prevent them from sending messages
|
||||
func muteUser(evt *event.Event, matrixClient *mautrix.Client) {
|
||||
plEventContent, err := GetRoomPowerLevelState(matrixClient, evt.RoomID)
|
||||
if err != nil {
|
||||
log.Printf("ERROR: Could mute user - %v", err)
|
||||
return
|
||||
|
@ -140,9 +167,9 @@ func muteUser(evt *event.Event, matrixClient *mautrix.Client, hashObj *model.DBE
|
|||
log.Printf("ERROR: Could mute user - %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// banUser bans the sender of an event from the room
|
||||
func banUser(evt *event.Event, matrixClient *mautrix.Client, hashObj *model.DBEntry) {
|
||||
req := mautrix.ReqBanUser{
|
||||
Reason: fmt.Sprintf("Veles has detected an hash-map-match! Tags: %s, ID: %s", hashObj.Tags, hashObj.ID.Hex()),
|
||||
|
@ -152,6 +179,7 @@ func banUser(evt *event.Event, matrixClient *mautrix.Client, hashObj *model.DBEn
|
|||
matrixClient.BanUser(evt.RoomID, &req)
|
||||
}
|
||||
|
||||
// postNotice posts a notice about the given event into its room
|
||||
func postNotice(evt *event.Event, matrixClient *mautrix.Client, hashObj *model.DBEntry, roomConfig config.RoomConfig) {
|
||||
local, server, err := evt.Sender.Parse()
|
||||
if err != nil {
|
||||
|
@ -172,32 +200,3 @@ If you believe this action was an accident, please contact an room administrator
|
|||
SendAlert(matrixClient, evt.RoomID.String(), fmt.Sprintf(
|
||||
`Veles Triggered: The message by %s (on %s) was flagged for containing material used by spammers or trolls! (Reference: %s)`, local, server, hashObj.ID.Hex()))
|
||||
}*/
|
||||
|
||||
func handleIllegalContent(evt *event.Event, matrixClient *mautrix.Client, hashObj *model.DBEntry, roomConfig config.RoomConfig) {
|
||||
switch roomConfig.HashChecker.HashCheckMode {
|
||||
case 0:
|
||||
postNotice(evt, matrixClient, hashObj, roomConfig)
|
||||
break
|
||||
case 1:
|
||||
redactMessage(evt, matrixClient, hashObj)
|
||||
if roomConfig.HashChecker.NoticeToChat {
|
||||
postNotice(evt, matrixClient, hashObj, roomConfig)
|
||||
}
|
||||
break
|
||||
case 2:
|
||||
muteUser(evt, matrixClient, hashObj)
|
||||
redactMessage(evt, matrixClient, hashObj)
|
||||
if roomConfig.HashChecker.NoticeToChat {
|
||||
postNotice(evt, matrixClient, hashObj, roomConfig)
|
||||
}
|
||||
break
|
||||
case 3:
|
||||
banUser(evt, matrixClient, hashObj)
|
||||
redactMessage(evt, matrixClient, hashObj)
|
||||
if roomConfig.HashChecker.NoticeToChat {
|
||||
postNotice(evt, matrixClient, hashObj, roomConfig)
|
||||
}
|
||||
break
|
||||
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue