Skip to content

Commit

Permalink
Merge pull request #13 from friedPotat0/change-symbol-parsing
Browse files Browse the repository at this point in the history
Change symbol parsing
  • Loading branch information
friedPotat0 authored Jan 7, 2021
2 parents 316349e + a84e366 commit 16c10d6
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 33 deletions.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"manifest_version": 2,
"name": "__MSG_extensionName__",
"description": "__MSG_extensionDescription__",
"version": "1.1.4",
"version": "1.1.5",
"author": "Christian Zaenker",
"default_locale": "en",
"applications": {
Expand Down
58 changes: 27 additions & 31 deletions popup.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
const SYMBOL_HEADER_REGEX = /(X-.*?(?:Spamd-Result|Spam-Report|SpamCheck|Spam-Status):.*(?:\r?\n(?:\t+ *| +).*)*)/g
const SYMBOL_PREFIX_REGEX = /\* +(-?[\d.]+)[ \)=]+([A-Z][A-Z0-9_]+)(?:(?:.|\r?\n)+?\[(.*?)\])?/g
const SYMBOL_SUFFIX_REGEX = /([A-Z][A-Z0-9_]+)(?:(?:[ \(=](-?[\d.]+)\)?(?:\[(.*?)\])?)|, *| )/g

browser.tabs
.query({
active: true,
Expand All @@ -6,8 +10,9 @@ browser.tabs
.then(async tabs => {
let tabId = tabs[0].id
browser.messageDisplay.getDisplayedMessage(tabId).then(async message => {
let rawMessage = await browser.messages.getRaw(message.id)
let parsedDetailScores = getParsedDetailScores(rawMessage)
const rawMessage = await browser.messages.getRaw(message.id)
const rawHeader = rawMessage.split('\r\n\r\n')[0]
let parsedDetailScores = getParsedDetailScores(rawHeader)
if (parsedDetailScores) {
let groupedDetailScores = {
positive: parsedDetailScores.filter(el => el.score > 0).sort((a, b) => b.score - a.score),
Expand Down Expand Up @@ -43,35 +48,26 @@ browser.tabs
})
})

function getParsedDetailScores(raw) {
let spamdResultRegex = /([A-Z0-9_]+)\((-?\d+\.\d+)\)\[(.*?)\];?/g
let match = raw.match(spamdResultRegex)
if (match && match.length > 0) {
return match.map(el => ({
name: el.replace(spamdResultRegex, '$1'),
score: parseFloat(el.replace(spamdResultRegex, '$2')),
info: el.replace(spamdResultRegex, '$3') || ''
}))
}
let spamStatusRegex = /([A-Z0-9_]+)=(-?[\d.]+)[,\]]/g
match = raw.match(/x-spam-status: .*tests=\[.*?\]/gis)
if (match && match.length > 0) {
return match[0].match(spamStatusRegex).map(el => ({
name: el.replace(spamStatusRegex, '$1'),
score: parseFloat(el.replace(spamStatusRegex, '$2')),
info: ''
}))
}
if (raw.toLowerCase().indexOf('mailscanner-spamcheck') !== -1) {
raw = raw.replace(/.*?x-.*?mailscanner-spamcheck: (.*?)\).*/gis, '$1')
let spamMailscannerRegex = /,.*?([A-Z0-9_]+) (-?[\d.]+)/gs
match = raw.match(spamMailscannerRegex)
if (match && match.length > 0) {
return match.map(el => ({
name: el.replace(spamMailscannerRegex, '$1'),
score: parseFloat(el.replace(spamMailscannerRegex, '$2')),
info: ''
}))
function getParsedDetailScores(rawHeader) {
let spamHeaderMatch = rawHeader.match(SYMBOL_HEADER_REGEX)
if (spamHeaderMatch && spamHeaderMatch.length > 0) {
for (let spamHeader of spamHeaderMatch) {
let symbolMatch = spamHeader.match(SYMBOL_PREFIX_REGEX)
if (symbolMatch && symbolMatch.length > 0) {
return symbolMatch.map(el => ({
name: el.replace(SYMBOL_PREFIX_REGEX, '$2'),
score: parseFloat(el.replace(SYMBOL_PREFIX_REGEX, '$1') || 0),
info: el.replace(SYMBOL_PREFIX_REGEX, '$3') || ''
}))
}
symbolMatch = spamHeader.match(SYMBOL_SUFFIX_REGEX)
if (symbolMatch && symbolMatch.length > 0) {
return symbolMatch.map(el => ({
name: el.replace(SYMBOL_SUFFIX_REGEX, '$1'),
score: parseFloat(el.replace(SYMBOL_SUFFIX_REGEX, '$2') || 0),
info: el.replace(SYMBOL_SUFFIX_REGEX, '$3') || ''
}))
}
}
}
return null
Expand Down
2 changes: 1 addition & 1 deletion rspamd_symbols.js

Large diffs are not rendered by default.

0 comments on commit 16c10d6

Please sign in to comment.