Skip to content

Commit

Permalink
Merge pull request #6 from friedPotat0/mailscanner-headers
Browse files Browse the repository at this point in the history
MailScanner headers
  • Loading branch information
friedPotat0 authored Oct 21, 2020
2 parents ec39e75 + 4ec56d4 commit cad9158
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 19 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ Spam Scores is an add-on for Thunderbird (Version 78.0a1 - *). It can display sp

To display the spam score column, right-click on the title bar of the columns in the list view and select "Spam score". If the column is empty, you must first restart Thunderbird and then right-click on any folder and select "Properties" and "Repair Folder". This will scan the mail headers of all mails in this folder so that the spam score column can be displayed correctly. Repair all folders like this in which you want to display this column.

If you have mails with the header "X-MYCOMPANY-MailScanner-SpamCheck", you have to open one of these mails first and then restart Thunderbird and repair the folder. Otherwise the spam score of the mails containing these headers will not be displayed.

The total score of each mail with an existing spam header will be displayed along with a red, yellow or green icon depending on the score. The colours are by default calculated as follows:

- ![Negative Score](https://raw.githubusercontent.com/friedPotat0/Spam-Scores/master/images/score_negative.png) Score greater than 2
Expand Down
28 changes: 27 additions & 1 deletion background.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,20 @@ var init = async () => {
browser.messageDisplayAction.setTitle({ tabId: tab.id, title: 'Spam Score: ' + score })
browser.messageDisplayAction.setIcon({ path: await getImageSrc(score) })
}
if (rawMessage.toLowerCase().indexOf('mailscanner-spamscore') !== -1) {
let header = rawMessage.replace(/.*(x-.*?mailscanner-spamcheck):.*/gis, '$1').toLowerCase()
let storage = await browser.storage.local.get(['customMailscannerHeaders'])
if (
storage &&
(!storage.customMailscannerHeaders ||
(storage.customMailscannerHeaders && storage.customMailscannerHeaders.indexOf(header) === -1))
) {
await browser.SpamScores.addDynamicCustomHeaders([header])
browser.storage.local.set({
customMailscannerHeaders: [...(storage.customMailscannerHeaders || []), header]
})
}
}
})

if (!(await browser.SpamScores.getHelloFlag())) {
Expand All @@ -27,14 +41,22 @@ var init = async () => {
browser.SpamScores.setHelloFlag()
}

let storage = await browser.storage.local.get(['scoreIconLowerBounds', 'scoreIconUpperBounds'])
let storage = await browser.storage.local.get([
'scoreIconLowerBounds',
'scoreIconUpperBounds',
'customMailscannerHeaders'
])
let lowerBounds = parseFloat(
storage && storage.scoreIconLowerBounds !== undefined ? storage.scoreIconLowerBounds : DEFAULT_SCORE_LOWER_BOUNDS
)
let upperBounds = parseFloat(
storage && storage.scoreIconLowerBounds !== undefined ? storage.scoreIconUpperBounds : DEFAULT_SCORE_UPPER_BOUNDS
)
browser.SpamScores.setScoreBounds(parseFloat(lowerBounds), parseFloat(upperBounds))

if (storage && storage.customMailscannerHeaders) {
browser.SpamScores.setCustomMailscannerHeaders(storage.customMailscannerHeaders)
}
}
init()

Expand All @@ -51,6 +73,10 @@ function getScore(raw) {
if (match && match.length > 0) {
return match[0].replace(/^x-spam-status: .*score=(.*?) .*$/gi, '$1')
}
match = raw.match(/x-.*?mailscanner-spamcheck: .*/gi)
if (match && match.length > 0) {
return match[0].replace(/^x-.*?mailscanner-spamcheck: .*score=(.*),$/gi, '$1')
}
return null
}

Expand Down
9 changes: 9 additions & 0 deletions custom_score_column.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ class ColumnHandler {
hdr.getStringProperty('x-spamd-result').replace(/^default.*\[(.*) \/ .*\];.*$/gi, '$1') ||
hdr.getStringProperty('x-spam-score') ||
hdr.getStringProperty('x-spam-status').replace(/.*score=(.*?) .*$/gi, '$1')
if (!score && this.params.customMailscannerHeaders) {
for (let header of this.params.customMailscannerHeaders) {
let headerScore = hdr.getStringProperty(header).replace(/.*?score=(.*?),.*$/gi, '$1')
if (!isNaN(parseFloat(headerScore))) {
score = headerScore
break
}
}
}
if (score) return parseFloat(score)
return null
}
Expand Down
29 changes: 20 additions & 9 deletions experiments.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ var SpamScores = class extends ExtensionCommon.ExtensionAPI {
},
setHelloFlag() {
Services.prefs.setBoolPref('spamscores.hello', true)
},
addDynamicCustomHeaders(dynamicHeaders) {
updatePrefs(dynamicHeaders)
},
setCustomMailscannerHeaders(customMailscannerHeaders) {
scoreHdrViewParams.customMailscannerHeaders = customMailscannerHeaders
}
}
}
Expand All @@ -74,19 +80,24 @@ function unpaint(win) {
delete win.SpamScores
}

function updatePrefs() {
function updatePrefs(dynamicHeaders = []) {
let staticHeaders = ['x-spam-status', 'x-spamd-result', 'x-spam-score', 'x-rspamd-score']
let customDBHeaders = Services.prefs.getCharPref('mailnews.customDBHeaders')
let newCustomDBHeaders = customDBHeaders
if (customDBHeaders.indexOf('x-spam-status') === -1) newCustomDBHeaders += ' x-spam-status'
if (customDBHeaders.indexOf('x-spamd-result') === -1) newCustomDBHeaders += ' x-spamd-result'
if (customDBHeaders.indexOf('x-spam-score') === -1) newCustomDBHeaders += ' x-spam-score'
if (customDBHeaders.indexOf('x-rspamd-score') === -1) newCustomDBHeaders += ' x-rspamd-score'
for (let header of staticHeaders) {
if (customDBHeaders.indexOf(header) === -1) newCustomDBHeaders += ` ${header}`
}
for (let header of dynamicHeaders) {
if (customDBHeaders.indexOf(header) === -1) newCustomDBHeaders += ` ${header}`
}
Services.prefs.getBranch('mailnews').setCharPref('.customDBHeaders', newCustomDBHeaders.trim())
let customHeaders = Services.prefs.getCharPref('mailnews.customHeaders')
let newCustomHeaders = customHeaders
if (customHeaders.indexOf('x-spam-status:') === -1) newCustomHeaders += ' x-spam-status:'
if (customHeaders.indexOf('x-spamd-result:') === -1) newCustomHeaders += ' x-spamd-result:'
if (customHeaders.indexOf('x-spam-score:') === -1) newCustomHeaders += ' x-spam-score:'
if (customHeaders.indexOf('x-rspamd-score:') === -1) newCustomHeaders += ' x-rspamd-score:'
for (let header of staticHeaders) {
if (customHeaders.indexOf(`${header}:`) === -1) newCustomHeaders += ` ${header}:`
}
for (let header of dynamicHeaders) {
if (customHeaders.indexOf(`${header}:`) === -1) newCustomHeaders += ` ${header}:`
}
Services.prefs.getBranch('mailnews').setCharPref('.customHeaders', newCustomHeaders.trim())
}
19 changes: 11 additions & 8 deletions popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,17 @@ function getParsedDetailScores(raw) {
info: ''
}))
}
let spamStatusAltRegex = /\* +(-?[\d.]+) ([A-Z0-9_]+)/gs
match = raw.match(spamStatusAltRegex)
if (match && match.length > 0) {
return match.map(el => ({
name: el.replace(spamStatusAltRegex, '$2'),
score: parseFloat(el.replace(spamStatusAltRegex, '$1')),
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: ''
}))
}
}
return null
}
2 changes: 1 addition & 1 deletion rspamd_symbols.js

Large diffs are not rendered by default.

30 changes: 30 additions & 0 deletions schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,36 @@
"description": "",
"async": false,
"parameters": []
},
{
"name": "addDynamicCustomHeaders",
"type": "function",
"description": "",
"async": true,
"parameters": [
{
"name": "dynamicHeaders",
"type": "array",
"items": {
"type": "string"
}
}
]
},
{
"name": "setCustomMailscannerHeaders",
"type": "function",
"description": "",
"async": false,
"parameters": [
{
"name": "customMailscannerHeaders",
"type": "array",
"items": {
"type": "string"
}
}
]
}
]
}
Expand Down

0 comments on commit cad9158

Please sign in to comment.