<commit_before>-- mod_push_appserver_fcm
--
-- Copyright (C) 2017 <NAME>
--
-- This file is MIT/X11 licensed.
--
-- Submodule implementing FCM communication
--

-- imports
local socket = require "socket";
local https = require "ssl.https";
local ltn12 = require "ltn12";
local string = require "string";
local t_remove = table.remove;
local datetime = require "util.datetime";
local json = require "util.json";

local pretty = require "pl.pretty";

-- this is the master module
module:depends("push_appserver");

-- configuration
local fcm_key = module:get_option_string("push_appserver_fcm_key", nil);						--push api key (no default)
local capath = module:get_option_string("push_appserver_fcm_capath", "/etc/ssl/certs");			--ca path on debian systems
local ciphers = module:get_option_string("push_appserver_fcm_ciphers", 
	"ECDHE-RSA-AES256-GCM-SHA384:"..
	"ECDHE-ECDSA-AES256-GCM-SHA384:"..
	"ECDHE-RSA-AES128-GCM-SHA256:"..
	"ECDHE-ECDSA-AES128-GCM-SHA256"
);	--supported ciphers
local push_ttl = module:get_option_number("push_appserver_fcm_push_ttl", nil);					--no ttl (equals 4 weeks)
local push_priority = module:get_option_string("push_appserver_fcm_push_priority", "high");		--high priority pushes (can be "high" or "normal")
local push_endpoint = "https://fcm.googleapis.com/fcm/send";

-- low level network functions
local function socket_tcp()
	local socket = socket.tcp();
	
end

-- high level network (https) functions
local function send_request(data)
	local respbody = {} -- for the response body
	prosody.unlock_globals();	-- unlock globals (https.request() tries to access global PROXY)
	local result, status_code, headers, status_line = https.request({
		method = "POST",
		url = push_endpoint,
		source = ltn12.source.string(data),
		headers = {
			["authorization"] = "key="..tostring(fcm_key),
			["content-type"] = "application/json",
			["content-length"] = tostring(#data)
		},
		sink = ltn12.sink.table(respbody),
		-- luasec tls options
		mode = "client",
		protocol = "tlsv1_2",
		verify = {"peer", "fail_if_no_peer_cert"},
		capath = capath,
		ciphers = ciphers,
		options = {
			"no_sslv2",
			"no_sslv3",
			"no_ticket",
			"no_compression",
			"cipher_server_preference",
			"single_dh_use",
			"single_ecdh_use",
		},
	});<commit_msg>Fix #8 for prosody 0.11<commit_after>-- mod_push_appserver_fcm
--
-- Copyright (C) 2017 <NAME>
--
-- This file is MIT/X11 licensed.
--
-- Submodule implementing FCM communication
--

-- imports
-- unlock prosody globals and allow ltn12 to pollute the global space
-- this fixes issue #8 in prosody 0.11, see also https://issues.prosody.im/1033
prosody.unlock_globals()
require "ltn12";
prosody.lock_globals()
local https = require "ssl.https";
local string = require "string";
local t_remove = table.remove;
local datetime = require "util.datetime";
local json = require "util.json";

local pretty = require "pl.pretty";

-- this is the master module
module:depends("push_appserver");

-- configuration
local fcm_key = module:get_option_string("push_appserver_fcm_key", nil);						--push api key (no default)
local capath = module:get_option_string("push_appserver_fcm_capath", "/etc/ssl/certs");			--ca path on debian systems
local ciphers = module:get_option_string("push_appserver_fcm_ciphers", 
	"ECDHE-RSA-AES256-GCM-SHA384:"..
	"ECDHE-ECDSA-AES256-GCM-SHA384:"..
	"ECDHE-RSA-AES128-GCM-SHA256:"..
	"ECDHE-ECDSA-AES128-GCM-SHA256"
);	--supported ciphers
local push_ttl = module:get_option_number("push_appserver_fcm_push_ttl", nil);					--no ttl (equals 4 weeks)
local push_priority = module:get_option_string("push_appserver_fcm_push_priority", "high");		--high priority pushes (can be "high" or "normal")
local push_endpoint = "https://fcm.googleapis.com/fcm/send";

-- high level network (https) functions
local function send_request(data)
	local respbody = {} -- for the response body
	prosody.unlock_globals();	-- unlock globals (https.request() tries to access global PROXY)
	local result, status_code, headers, status_line = https.request({
		method = "POST",
		url = push_endpoint,
		source = ltn12.source.string(data),
		headers = {
			["authorization"] = "key="..tostring(fcm_key),
			["content-type"] = "application/json",
			["content-length"] = tostring(#data)
		},
		sink = ltn12.sink.table(respbody),
		-- luasec tls options
		mode = "client",
		protocol = "tlsv1_2",
		verify = {"peer", "fail_if_no_peer_cert"},
		capath = capath,
		ciphers = ciphers,
		options = {
			"no_sslv2",
			"no_sslv3",
			"no_ticket",
			"no_compression",
			"cipher_server_preference",
			"single_dh_use",
			"single_ecdh_use",
		},
	});
{'fraction_non_alphanumeric': 0.14304319513783373, 'fraction_numerical': 0.021271977425656608, 'mean_word_length': 4.4532544378698224, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '408571', 'n_tokens_mistral': 1804, 'n_tokens_neox': 1617, 'n_words': 374}