2015-11-07 07:53:21 +08:00
|
|
|
NylasStore = require 'nylas-store'
|
|
|
|
fs = require 'fs'
|
|
|
|
path = require 'path'
|
|
|
|
{Utils, MessageBodyProcessor} = require 'nylas-exports'
|
|
|
|
AutoloadImagesActions = require './autoload-images-actions'
|
|
|
|
|
2015-12-29 06:59:07 +08:00
|
|
|
# Match:
|
|
|
|
# - any of the DOM attributes supporting images starting with a protocol
|
|
|
|
# (src, background, placeholder, icon, poster, or srcset)
|
|
|
|
# - any url() value
|
|
|
|
#
|
|
|
|
ImagesRegexp = /((?:src|background|placeholder|icon|background|poster|srcset)\s*=\s*['"]?(?=\w*:\/\/)|:\s*url\()+([^"'\)]*)/gi
|
2015-11-07 07:53:21 +08:00
|
|
|
|
|
|
|
class AutoloadImagesStore extends NylasStore
|
|
|
|
constructor: ->
|
|
|
|
@_whitelistEmails = {}
|
|
|
|
@_whitelistMessageIds = {}
|
|
|
|
|
2015-11-12 02:25:11 +08:00
|
|
|
@_whitelistEmailsPath = path.join(NylasEnv.getConfigDirPath(), 'autoload-images-whitelist.txt')
|
2015-11-07 07:53:21 +08:00
|
|
|
|
|
|
|
@_loadWhitelist()
|
|
|
|
|
|
|
|
@listenTo AutoloadImagesActions.temporarilyEnableImages, @_onTemporarilyEnableImages
|
|
|
|
@listenTo AutoloadImagesActions.permanentlyEnableImages, @_onPermanentlyEnableImages
|
|
|
|
|
2016-01-29 16:31:10 +08:00
|
|
|
NylasEnv.config.onDidChange 'core.reading.autoloadImages', =>
|
2015-11-07 07:53:21 +08:00
|
|
|
MessageBodyProcessor.resetCache()
|
|
|
|
|
|
|
|
shouldBlockImagesIn: (message) =>
|
2015-11-12 02:25:11 +08:00
|
|
|
return false if NylasEnv.config.get('core.reading.autoloadImages') is true
|
2015-11-07 07:53:21 +08:00
|
|
|
return false if @_whitelistEmails[Utils.toEquivalentEmailForm(message.fromContact().email)]
|
|
|
|
return false if @_whitelistMessageIds[message.id]
|
|
|
|
return false unless ImagesRegexp.test(message.body)
|
|
|
|
true
|
|
|
|
|
|
|
|
_loadWhitelist: =>
|
2015-11-10 04:13:46 +08:00
|
|
|
fs.exists @_whitelistEmailsPath, (exists) =>
|
|
|
|
return unless exists
|
|
|
|
fs.readFile @_whitelistEmailsPath, (err, body) =>
|
|
|
|
return console.log(err) if err or not body
|
|
|
|
@_whitelistEmails = {}
|
|
|
|
for email in body.toString().split(/[\n\r]+/)
|
|
|
|
@_whitelistEmails[Utils.toEquivalentEmailForm(email)] = true
|
2015-11-07 07:53:21 +08:00
|
|
|
|
|
|
|
_saveWhitelist: =>
|
|
|
|
data = Object.keys(@_whitelistEmails).join('\n')
|
|
|
|
fs.writeFile @_whitelistEmailsPath, data, (err) =>
|
|
|
|
console.error("AutoloadImagesStore could not save whitelist: #{err.toString()}") if err
|
|
|
|
|
|
|
|
_onTemporarilyEnableImages: (message) ->
|
|
|
|
@_whitelistMessageIds[message.id] = true
|
|
|
|
MessageBodyProcessor.resetCache()
|
|
|
|
|
|
|
|
_onPermanentlyEnableImages: (message) ->
|
|
|
|
@_whitelistEmails[Utils.toEquivalentEmailForm(message.fromContact().email)] = true
|
|
|
|
MessageBodyProcessor.resetCache()
|
|
|
|
setTimeout(@_saveWhitelist, 1)
|
|
|
|
|
|
|
|
module.exports = new AutoloadImagesStore
|
|
|
|
module.exports.ImagesRegexp = ImagesRegexp
|