2024-01-01 16:52:18 +00:00
// Package linkdetector contains an actor to detect links in a message
// and add them to a variable
2023-04-07 22:41:00 +00:00
package linkdetector
import (
2023-09-11 17:51:38 +00:00
"gopkg.in/irc.v4"
2023-04-07 22:41:00 +00:00
"github.com/Luzifer/twitch-bot/v3/internal/linkcheck"
"github.com/Luzifer/twitch-bot/v3/plugins"
)
const actorName = "linkdetector"
2023-07-24 21:27:25 +00:00
var ptrFalse = func ( v bool ) * bool { return & v } ( false )
2024-01-01 16:52:18 +00:00
// Register provides the plugins.RegisterFunc
2023-04-07 22:41:00 +00:00
func Register ( args plugins . RegistrationArguments ) error {
args . RegisterActor ( actorName , func ( ) plugins . Actor { return & Actor { } } )
args . RegisterActorDocumentation ( plugins . ActionDocumentation {
Description : ` Scans for links in the message and adds the "links" field to the event data ` ,
Name : "Scan for Links" ,
Type : actorName ,
2023-07-24 21:27:25 +00:00
Fields : [ ] plugins . ActionDocumentationField {
{
Default : "false" ,
2023-12-05 17:58:23 +00:00
Description : "Enable heuristic scans to find links with spaces or other means of obfuscation in them (quite slow and will detect MANY false-positive links, only use for blacklisting links!)" ,
2023-07-24 21:27:25 +00:00
Key : "heuristic" ,
Name : "Heuristic Scan" ,
Optional : true ,
SupportTemplate : false ,
Type : plugins . ActionDocumentationFieldTypeBool ,
} ,
} ,
2023-04-07 22:41:00 +00:00
} )
return nil
}
2024-01-01 16:52:18 +00:00
// Actor implements the actor interface
2023-04-07 22:41:00 +00:00
type Actor struct { }
2024-01-01 16:52:18 +00:00
// Execute implements the actor interface
2023-07-24 21:27:25 +00:00
func ( Actor ) Execute ( _ * irc . Client , m * irc . Message , _ * plugins . Rule , eventData * plugins . FieldCollection , attrs * plugins . FieldCollection ) ( preventCooldown bool , err error ) {
2023-04-07 22:41:00 +00:00
if eventData . HasAll ( "links" ) {
// We already detected links, lets not do it again
return false , nil
}
2023-07-24 21:27:25 +00:00
if attrs . MustBool ( "heuristic" , ptrFalse ) {
eventData . Set ( "links" , linkcheck . New ( ) . HeuristicScanForLinks ( m . Trailing ( ) ) )
} else {
eventData . Set ( "links" , linkcheck . New ( ) . ScanForLinks ( m . Trailing ( ) ) )
}
2023-04-07 22:41:00 +00:00
return false , nil
}
2024-01-01 16:52:18 +00:00
// IsAsync implements the actor interface
2023-04-07 22:41:00 +00:00
func ( Actor ) IsAsync ( ) bool { return false }
2024-01-01 16:52:18 +00:00
// Name implements the actor interface
2023-04-07 22:41:00 +00:00
func ( Actor ) Name ( ) string { return actorName }
2024-01-01 16:52:18 +00:00
// Validate implements the actor interface
2023-04-07 22:41:00 +00:00
func ( Actor ) Validate ( plugins . TemplateValidatorFunc , * plugins . FieldCollection ) error { return nil }