comparison mod_sms_clickatell/mod_sms_clickatell.lua @ 347:cd838419a85d

mod_sms_clickatell: moved clickatell API ID and source number settings to component registration
author Phil Stewart <phil.stewart@lichp.co.uk>
date Sun, 03 Apr 2011 22:47:06 +0100
parents 2e6a74842c00
children ac9bf3fcbcfe
comparison
equal deleted inserted replaced
346:2e6a74842c00 347:cd838419a85d
47 47
48 local component_host = module:get_host(); 48 local component_host = module:get_host();
49 local component_name = module.name; 49 local component_name = module.name;
50 local data_cache = {}; 50 local data_cache = {};
51 51
52 local clickatell_api_id = module:get_option_string("clickatell_api_id"); 52 --local clickatell_api_id = module:get_option_string("clickatell_api_id");
53 local sms_message_prefix = module:get_option_string("sms_message_prefix") or ""; 53 local sms_message_prefix = module:get_option_string("sms_message_prefix") or "";
54 local sms_source_number = module:get_option_string("sms_source_number") or ""; 54 --local sms_source_number = module:get_option_string("sms_source_number") or "";
55 55
56 --local users = setmetatable({}, {__mode="k"}); 56 --local users = setmetatable({}, {__mode="k"});
57 57
58 -- User data is held in smsuser objects 58 -- User data is held in smsuser objects
59 local smsuser = {}; 59 local smsuser = {};
214 return false; 214 return false;
215 end 215 end
216 216
217 local sms_message = sms_message_prefix..message; 217 local sms_message = sms_message_prefix..message;
218 local clickatell_base_url = "https://api.clickatell.com/http/sendmsg"; 218 local clickatell_base_url = "https://api.clickatell.com/http/sendmsg";
219 local params = {user=user.data.username, password=user.data.password, api_id=clickatell_api_id, from=sms_source_number, to=number, text=sms_message}; 219 local params = {user=user.data.username, password=user.data.password, api_id=user.data.api_id, from=user.data.source_number, to=number, text=sms_message};
220 local query_string = ""; 220 local query_string = "";
221 221
222 for param, data in pairs(params) do 222 for param, data in pairs(params) do
223 --module:log("info", "Inside query constructor: "..param..data); 223 --module:log("info", "Inside query constructor: "..param..data);
224 if query_string ~= "" then 224 if query_string ~= "" then
296 module:log("info", "Register event triggered"); 296 module:log("info", "Register event triggered");
297 if stanza.attr.type == "get" then 297 if stanza.attr.type == "get" then
298 local reply = data_cache.registration_form; 298 local reply = data_cache.registration_form;
299 if reply == nil then 299 if reply == nil then
300 reply = st.iq({type='result', from=stanza.attr.to or component_host}) 300 reply = st.iq({type='result', from=stanza.attr.to or component_host})
301 :tag("query", { xmlns="jabber:iq:register" }) 301 :tag("query", {xmlns="jabber:iq:register"})
302 :tag("instructions"):text("Enter the Clickatell username and password to use with API ID "..clickatell_api_id):up() 302 :tag("instructions"):text("Use the enclosed form to register."):up();
303 :tag("username"):up() 303 reply:tag("x", {xmlns="jabber:x:data", type='form'});
304 :tag("password"):up(); 304 reply:tag("title"):text('SMS Gateway Registration: Clickatell'):up();
305 reply:tag("instructions"):text("Enter your Clickatell username, password, API ID, and a source number for your text messages in international format (phone field)"):up();
306 reply:tag("field", {type='hidden', var='FORM_TYPE'})
307 :tag("value"):text('jabber:iq:register'):up():up();
308 reply:tag("field", {type='text-single', label='Username', var='username'})
309 :tag("required"):up():up();
310 reply:tag("field", {type='text-private', label='Password', var='password'})
311 :tag("required"):up():up();
312 reply:tag("field", {type='text-single', label='API ID', var='api_id'})
313 :tag("required"):up():up();
314 reply:tag("field", {type='text-single', label='Source Number', var='source_number'})
315 :tag("required"):up():up();
305 data_cache.registration_form = reply; 316 data_cache.registration_form = reply;
317 --module:log("info", "Register stanza to go: "..reply:pretty_print());
306 end 318 end
307 reply.attr.id = stanza.attr.id; 319 reply.attr.id = stanza.attr.id;
308 reply.attr.to = stanza.attr.from; 320 reply.attr.to = stanza.attr.from;
309 origin.send(reply); 321 origin.send(reply);
310 elseif stanza.attr.type == "set" then 322 elseif stanza.attr.type == "set" then
311 local from = {}; 323 local from = {};
312 from.node, from.host, from.resource = jid_split(stanza.attr.from); 324 from.node, from.host, from.resource = jid_split(stanza.attr.from);
313 local bjid = from.node.."@"..from.host; 325 local bjid = from.node.."@"..from.host;
314 local username, password = "", ""; 326 local username, password, api_id, source_number = "", "", "", "";
315 local reply; 327 local reply;
316 for _, tag in ipairs(stanza.tags[1].tags) do 328 for tag in stanza.tags[1].tags[1]:childtags() do
317 if tag.name == "remove" then 329 -- if tag.name == "remove" then
318 iq_success(origin, stanza); 330 -- iq_success(origin, stanza);
319 return true; 331 -- return true;
332 -- end
333 if tag.attr.var == "username" then
334 username = tag.tags[1][1];
320 end 335 end
321 if tag.name == "username" then 336 if tag.attr.var == "password" then
322 username = tag[1]; 337 password = tag.tags[1][1];
323 end 338 end
324 if tag.name == "password" then 339 if tag.attr.var == "api_id" then
325 password = tag[1]; 340 api_id = tag.tags[1][1];
326 end 341 end
327 end 342 if tag.attr.var == "source_number" then
328 if username ~= nil and password ~= nil then 343 source_number = tag.tags[1][1];
344 end
345 end
346 if username ~= nil and password ~= nil and api_id ~= nil then
329 users[bjid] = smsuser:register(bjid); 347 users[bjid] = smsuser:register(bjid);
330 users[bjid].data.username = username; 348 users[bjid].data.username = username;
331 users[bjid].data.password = password; 349 users[bjid].data.password = password;
350 users[bjid].data.api_id = api_id;
351 users[bjid].data.source_number = source_number;
332 users[bjid]:store(); 352 users[bjid]:store();
333 end 353 end
334 iq_success(origin, stanza); 354 iq_success(origin, stanza);
335 return true; 355 return true;
336 end 356 end