File size: 4,399 Bytes
402daee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
--[[

  ReaSpeechProductActivation.lua - Product key entry and activation checks

]]--

ReaSpeechProductActivation = Polo {
  ACTIVATION_URL = "https://techaud.io/ProductActivationDeveloper.php",

  PRODUCT_ID = 79004057,
  PRODUCT_CHECK_COUNT = 4,
  PRODUCT_NAME = "ReaSpeech",

  config = nil,

  -- nil = not activated
  -- 'pending' = activation in process
  -- 'activated' = activation complete
  state = nil,
  activation_message =""
}

function ReaSpeechProductActivation:init()
  self:init_config()
  self:activation_state_check()
end

function ReaSpeechProductActivation:init_config()
  self.config = OptionsConfig:new {
    section = 'ReaSpeech',
    options = {
      product_run_check_count = {'number', 0},
      product_license = {'string', ''},
      product_license_value = {'string', ''},
      eula_signed = {'boolean', false},
    }
  }
end

function ReaSpeechProductActivation:is_activated()
  return self.state == 'activated' and self.config:get('eula_signed')
end

function ReaSpeechProductActivation:activation_state_check()
  local has_l = self.config:exists('product_license')
  local has_lv = self.config:exists('product_license_value')

  if has_l and has_lv then
    local count = self.config:get('product_run_check_count')

    if count > self.PRODUCT_CHECK_COUNT then
      self.state = 'pending'
      self:handle_product_activation_recheck()
    else
      self.state = 'activated'
      self.config:set('product_run_check_count', count + 1)
    end
  else
    self.state = nil
  end
end

function ReaSpeechProductActivation:handle_product_activation(product_key)
  product_key = string.gsub(product_key, "%s+", "")
  if #product_key == 0 then
    self.state = nil
    return
  end

  local process_result = self:send_activation_request(product_key, false)

  if process_result then
    self:process_activation_reply(product_key, process_result)
  end

end

function ReaSpeechProductActivation:handle_product_activation_recheck()
  local process_result = self:send_activation_request(self.config:get('product_license'), true)

  if process_result then
    if string.find(process_result, "SUCCESS") then
      self.state = 'activated'
      self.config:set('product_run_check_count', 0)
    elseif string.find(process_result, "FAILURE") then
      self.state = nil
      self.config:delete('product_license')
      self.config:delete('product_license_value')
    else
      -- Connection failed, silently ignore
      self.state = 'activated'
    end
  else
    -- Command failed, silently ignore
    self.state = 'activated'
  end
end

function ReaSpeechProductActivation:send_activation_request(product_key, is_recheck)
  local curl = "curl"
  if not reaper.GetOS():find("Win") then
    curl = "/usr/bin/curl"
  end

  local cmd_data_id = "user_product_id=" .. self.PRODUCT_ID
  local cmd_data_license = "user_license=" .. product_key
  local cmd_data_p_n = "user_product_name=" .. self.PRODUCT_NAME
  local cmd_data_p_v = "user_product_version=" .. ReaSpeechUI.VERSION
  local cmd_data_recheck = "recheck=" .. tostring(is_recheck)

  local cmd_args = (
    curl.." -X POST"
    .. " -d " .. cmd_data_id
    .. " -d " .. cmd_data_license
    .. " -d " .. cmd_data_p_n
    .. " -d " .. cmd_data_p_v
    .. " -d " .. cmd_data_recheck
    .. " \"" .. self.ACTIVATION_URL .. "\""
  )

  local process_result = reaper.ExecProcess(cmd_args, 8000)
  if process_result then
    return process_result
  else
    self.activation_message = "Activation failed: Connection Error"
    reaper.ShowConsoleMsg("Failed CURL at activation request" .. '\n')
    return nil
  end

end

function ReaSpeechProductActivation:process_activation_reply(product_key, process_result)
  process_result = string.gsub(process_result, "%s+", "")

  if string.find(process_result, "SUCCESS") then
    self.config:set('product_run_check_count', 1)
    self.config:set('product_license', product_key)
    self.config:set('product_license_value', process_result)

    self.state = 'activated'
    self.activation_message = "Thanks for your support! Enjoy :)"
  elseif string.find(process_result, "FAILURE") then
    self.state = nil
    if string.find(process_result, "Invalid_License") then
      self.activation_message = "Activation failed: Sorry, we didn't find a valid license :("
    else
      self.activation_message = "Activation failed: Sorry, you are out of activations :("
    end

  end
end