From 1bb39eba8717f62336cc98c5bb7cfbef194f3626 Mon Sep 17 00:00:00 2001 From: Wim Date: Sat, 23 Feb 2019 16:39:44 +0100 Subject: Add scripting (tengo) support for every incoming message (#731) TengoModifyMessage allows you to specify the location of a tengo (https://github.com/d5/tengo/) script. This script will receive every incoming message and can be used to modify the Username and the Text of that message. The script will have the following global variables: to modify: msgUsername and msgText to read: msgChannel and msgAccount The script is reloaded on every message, so you can modify the script on the fly. Example script can be found in https://github.com/42wim/matterbridge/tree/master/gateway/bench.tengo and https://github.com/42wim/matterbridge/tree/master/contrib/example.tengo The example below will check if the text contains blah and if so, it'll replace the text and the username of that message. text := import("text") if text.re_match("blah",msgText) { msgText="replaced by this" msgUsername="fakeuser" } More information about tengo on: https://github.com/d5/tengo/blob/master/docs/tutorial.md and https://github.com/d5/tengo/blob/master/docs/stdlib.md --- .../github.com/d5/tengo/compiler/token/keywords.go | 19 ++ .../github.com/d5/tengo/compiler/token/tokens.go | 208 +++++++++++++++++++++ 2 files changed, 227 insertions(+) create mode 100644 vendor/github.com/d5/tengo/compiler/token/keywords.go create mode 100644 vendor/github.com/d5/tengo/compiler/token/tokens.go (limited to 'vendor/github.com/d5/tengo/compiler/token') diff --git a/vendor/github.com/d5/tengo/compiler/token/keywords.go b/vendor/github.com/d5/tengo/compiler/token/keywords.go new file mode 100644 index 00000000..fd6e9d0b --- /dev/null +++ b/vendor/github.com/d5/tengo/compiler/token/keywords.go @@ -0,0 +1,19 @@ +package token + +var keywords map[string]Token + +func init() { + keywords = make(map[string]Token) + for i := _keywordBeg + 1; i < _keywordEnd; i++ { + keywords[tokens[i]] = i + } +} + +// Lookup returns corresponding keyword if ident is a keyword. +func Lookup(ident string) Token { + if tok, isKeyword := keywords[ident]; isKeyword { + return tok + } + + return Ident +} diff --git a/vendor/github.com/d5/tengo/compiler/token/tokens.go b/vendor/github.com/d5/tengo/compiler/token/tokens.go new file mode 100644 index 00000000..b32d36ee --- /dev/null +++ b/vendor/github.com/d5/tengo/compiler/token/tokens.go @@ -0,0 +1,208 @@ +package token + +import "strconv" + +// Token represents a token. +type Token int + +// List of tokens +const ( + Illegal Token = iota + EOF + Comment + _literalBeg + Ident + Int + Float + Char + String + _literalEnd + _operatorBeg + Add // + + Sub // - + Mul // * + Quo // / + Rem // % + And // & + Or // | + Xor // ^ + Shl // << + Shr // >> + AndNot // &^ + AddAssign // += + SubAssign // -= + MulAssign // *= + QuoAssign // /= + RemAssign // %= + AndAssign // &= + OrAssign // |= + XorAssign // ^= + ShlAssign // <<= + ShrAssign // >>= + AndNotAssign // &^= + LAnd // && + LOr // || + Inc // ++ + Dec // -- + Equal // == + Less // < + Greater // > + Assign // = + Not // ! + NotEqual // != + LessEq // <= + GreaterEq // >= + Define // := + Ellipsis // ... + LParen // ( + LBrack // [ + LBrace // { + Comma // , + Period // . + RParen // ) + RBrack // ] + RBrace // } + Semicolon // ; + Colon // : + Question // ? + _operatorEnd + _keywordBeg + Break + Continue + Else + For + Func + Error + Immutable + If + Return + Export + True + False + In + Undefined + Import + _keywordEnd +) + +var tokens = [...]string{ + Illegal: "ILLEGAL", + EOF: "EOF", + Comment: "COMMENT", + Ident: "IDENT", + Int: "INT", + Float: "FLOAT", + Char: "CHAR", + String: "STRING", + Add: "+", + Sub: "-", + Mul: "*", + Quo: "/", + Rem: "%", + And: "&", + Or: "|", + Xor: "^", + Shl: "<<", + Shr: ">>", + AndNot: "&^", + AddAssign: "+=", + SubAssign: "-=", + MulAssign: "*=", + QuoAssign: "/=", + RemAssign: "%=", + AndAssign: "&=", + OrAssign: "|=", + XorAssign: "^=", + ShlAssign: "<<=", + ShrAssign: ">>=", + AndNotAssign: "&^=", + LAnd: "&&", + LOr: "||", + Inc: "++", + Dec: "--", + Equal: "==", + Less: "<", + Greater: ">", + Assign: "=", + Not: "!", + NotEqual: "!=", + LessEq: "<=", + GreaterEq: ">=", + Define: ":=", + Ellipsis: "...", + LParen: "(", + LBrack: "[", + LBrace: "{", + Comma: ",", + Period: ".", + RParen: ")", + RBrack: "]", + RBrace: "}", + Semicolon: ";", + Colon: ":", + Question: "?", + Break: "break", + Continue: "continue", + Else: "else", + For: "for", + Func: "func", + Error: "error", + Immutable: "immutable", + If: "if", + Return: "return", + Export: "export", + True: "true", + False: "false", + In: "in", + Undefined: "undefined", + Import: "import", +} + +func (tok Token) String() string { + s := "" + + if 0 <= tok && tok < Token(len(tokens)) { + s = tokens[tok] + } + + if s == "" { + s = "token(" + strconv.Itoa(int(tok)) + ")" + } + + return s +} + +// LowestPrec represents lowest operator precedence. +const LowestPrec = 0 + +// Precedence returns the precedence for the operator token. +func (tok Token) Precedence() int { + switch tok { + case LOr: + return 1 + case LAnd: + return 2 + case Equal, NotEqual, Less, LessEq, Greater, GreaterEq: + return 3 + case Add, Sub, Or, Xor: + return 4 + case Mul, Quo, Rem, Shl, Shr, And, AndNot: + return 5 + } + return LowestPrec +} + +// IsLiteral returns true if the token is a literal. +func (tok Token) IsLiteral() bool { + return _literalBeg < tok && tok < _literalEnd +} + +// IsOperator returns true if the token is an operator. +func (tok Token) IsOperator() bool { + return _operatorBeg < tok && tok < _operatorEnd +} + +// IsKeyword returns true if the token is a keyword. +func (tok Token) IsKeyword() bool { + return _keywordBeg < tok && tok < _keywordEnd +} -- cgit v1.2.3