annotate mod_twitter/mod_twitter.lua @ 5702:616c0459aca7

mod_aws_profile: Fix use of timer API
author Kim Alvefur <zash@zash.se>
date Sun, 12 Nov 2023 02:37:53 +0100
parents 65082d91950e
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
249
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
1 -- for Prosody
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
2 -- via dersd
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
3
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
4 if module:get_host_type() ~= "component" then
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
5 error(module.name.." should be loaded as a component, check out http://prosody.im/doc/components", 0);
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
6 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
7
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
8 local jid_split = require "util.jid".split;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
9 local st = require "util.stanza";
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
10 local componentmanager = require "core.componentmanager";
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
11 local datamanager = require "util.datamanager";
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
12 local timer = require "util.timer";
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
13 local http = require "net.http";
790
4f9cd19c4658 mod_twitter: fixed to depend on Prosody's internal util.json. TODO: Discuss (MattJ, Zash, Waqas, Maranda) about migration all json content to use cjson ( http://www.kyne.com.au/~mark/software/lua-cjson-manual.html ) library instead
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 249
diff changeset
14 local json = require "util.json";
249
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
15 local base64 = require "util.encodings".base64;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
16
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
17 local component_host = module:get_host();
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
18 local component_name = module.name;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
19 local data_cache = {};
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
20
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
21 function print_r(obj)
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
22 return require("util.serialization").serialize(obj);
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
23 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
24
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
25 function dmsg(jid, msg)
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
26 module:log("debug", msg or "nil");
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
27 if jid ~= nil then
2887
65082d91950e Many modules: Simplify st.message(…):tag("body"):text(…):up() into st.message(…, …)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 983
diff changeset
28 module:send(st.message({to=jid, from=component_host, type='chat'}, msg or "nil"));
249
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
29 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
30 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
31
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
32 function substring(string, start_string, ending_string)
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
33 local s_value_start, s_value_finish = nil, nil;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
34 if start_string ~= nil then
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
35 _, s_value_start = string:find(start_string);
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
36 if s_value_start == nil then
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
37 -- error
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
38 return nil;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
39 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
40 else
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
41 return nil;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
42 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
43 if ending_string ~= nil then
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
44 _, s_value_finish = string:find(ending_string, s_value_start+1);
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
45 if s_value_finish == nil then
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
46 -- error
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
47 return nil;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
48 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
49 else
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
50 s_value_finish = string:len()+1;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
51 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
52 return string:sub(s_value_start+1, s_value_finish-1);
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
53 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
54
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
55 local http_timeout = 30;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
56 local http_queue = setmetatable({}, { __mode = "k" }); -- auto-cleaning nil elements
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
57 data_cache['prosody_os'] = prosody.platform;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
58 data_cache['prosody_version'] = prosody.version;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
59 local http_headers = {
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
60 ["User-Agent"] = "Prosody ("..data_cache['prosody_version'].."; "..data_cache['prosody_os']..")" --"ELinks (0.4pre5; Linux 2.4.27 i686; 80x25)",
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
61 };
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
62
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
63 function http_action_callback(response, code, request, xcallback)
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
64 if http_queue == nil or http_queue[request] == nil then return; end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
65 local id = http_queue[request];
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
66 http_queue[request] = nil;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
67 if xcallback == nil then
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
68 dmsg(nil, "http_action_callback reports that xcallback is nil");
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
69 else
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
70 xcallback(id, response, request);
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
71 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
72 return true;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
73 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
74
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
75 function http_add_action(tid, url, method, post, fcallback)
983
ac9bf3fcbcfe mod_pubsub_feeds, mod_sms_clickatell, mod_twitter: Update for net.http API change in prosody:e3b9dc9dd940
Matthew Wild <mwild1@gmail.com>
parents: 803
diff changeset
76 local request = http.request(url, { headers = http_headers or {}, body = http.formencode(post or {}), method = method or "GET" }, function(response_body, code, response, request) http_action_callback(response_body, code, request, fcallback) end);
249
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
77 http_queue[request] = tid;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
78 timer.add_task(http_timeout, function() http.destroy_request(request); end);
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
79 return true;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
80 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
81
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
82 local users = setmetatable({}, {__mode="k"});
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
83 local user = {};
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
84 user.__index = user;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
85 user.dosync = false;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
86 user.valid = false;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
87 user.data = {};
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
88
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
89 function user:login()
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
90 userdata = datamanager.load(self.jid, component_host, "data");
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
91 if userdata ~= nil then
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
92 self.data = userdata;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
93 if self.data['_twitter_sess'] ~= nil then
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
94 http_headers['Cookie'] = "_twitter_sess="..self.data['_twitter_sess']..";";
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
95 end
803
be509416d64c mod_twitter: Use module:send()
Kim Alvefur <zash@zash.se>
parents: 790
diff changeset
96 module:send(st.presence({to=self.jid, from=component_host}));
249
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
97 self:twitterAction("VerifyCredentials");
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
98 if self.data.dosync == 1 then
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
99 self.dosync = true;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
100 timer.add_task(self.data.refreshrate, function() return users[self.jid]:sync(); end)
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
101 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
102 else
2887
65082d91950e Many modules: Simplify st.message(…):tag("body"):text(…):up() into st.message(…, …)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 983
diff changeset
103 module:send(st.message({to=self.jid, from=component_host, type='chat'}, "You are not signed in."));
249
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
104 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
105 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
106
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
107 function user:logout()
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
108 datamanager.store(self.jid, component_host, "data", self.data);
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
109 self.dosync = false;
803
be509416d64c mod_twitter: Use module:send()
Kim Alvefur <zash@zash.se>
parents: 790
diff changeset
110 module:send(st.presence({to=self.jid, from=component_host, type='unavailable'}));
249
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
111 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
112
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
113 function user:sync()
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
114 if self.dosync then
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
115 table.foreach(self.data.synclines, function(ind, line) self:twitterAction(line.name, {sinceid=line.sinceid}) end);
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
116 return self.data.refreshrate;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
117 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
118 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
119
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
120 function user:signin()
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
121 if datamanager.load(self.jid, component_host, "data") == nil then
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
122 datamanager.store(self.jid, component_host, "data", {login=self.data.login, password=self.data.password, refreshrate=60, dosync=1, synclines={{name='HomeTimeline', sinceid=0}}, syncstatus=0})
803
be509416d64c mod_twitter: Use module:send()
Kim Alvefur <zash@zash.se>
parents: 790
diff changeset
123 module:send(st.presence{to=self.jid, from=component_host, type='subscribe'});
be509416d64c mod_twitter: Use module:send()
Kim Alvefur <zash@zash.se>
parents: 790
diff changeset
124 module:send(st.presence{to=self.jid, from=component_host, type='subscribed'});
249
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
125 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
126 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
127
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
128 function user:signout()
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
129 if datamanager.load(self.jid, component_host, "data") ~= nil then
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
130 datamanager.store(self.jid, component_host, "data", nil);
803
be509416d64c mod_twitter: Use module:send()
Kim Alvefur <zash@zash.se>
parents: 790
diff changeset
131 module:send(st.presence({to=self.jid, from=component_host, type='unavailable'}));
be509416d64c mod_twitter: Use module:send()
Kim Alvefur <zash@zash.se>
parents: 790
diff changeset
132 module:send(st.presence({to=self.jid, from=component_host, type='unsubscribe'}));
be509416d64c mod_twitter: Use module:send()
Kim Alvefur <zash@zash.se>
parents: 790
diff changeset
133 module:send(st.presence({to=self.jid, from=component_host, type='unsubscribed'}));
249
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
134 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
135 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
136
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
137 local twitterApiUrl = "http://api.twitter.com";
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
138 local twitterApiVersion = "1";
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
139 local twitterApiDataType = "json";
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
140 local twitterActionUrl = function(action) return twitterApiUrl.."/"..twitterApiVersion.."/"..action.."."..twitterApiDataType end;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
141 local twitterActionMap = {
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
142 PublicTimeline = {
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
143 url = twitterActionUrl("statuses/public_timeline"),
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
144 method = "GET",
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
145 needauth = false,
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
146 },
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
147 HomeTimeline = {
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
148 url = twitterActionUrl("statuses/home_timeline"),
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
149 method = "GET",
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
150 needauth = true,
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
151 },
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
152 FriendsTimeline = {
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
153 url = twitterActionUrl("statuses/friends_timeline"),
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
154 method = "GET",
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
155 needauth = true,
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
156 },
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
157 UserTimeline = {
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
158 url = twitterActionUrl("statuses/friends_timeline"),
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
159 method = "GET",
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
160 needauth = true,
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
161 },
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
162 VerifyCredentials = {
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
163 url = twitterActionUrl("account/verify_credentials"),
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
164 method = "GET",
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
165 needauth = true,
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
166 },
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
167 UpdateStatus = {
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
168 url = twitterActionUrl("statuses/update"),
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
169 method = "POST",
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
170 needauth = true,
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
171 },
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
172 Retweet = {
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
173 url = twitterActionUrl("statuses/retweet/%tweetid"),
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
174 method = "POST",
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
175 needauth = true,
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
176 }
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
177 }
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
178
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
179 function user:twitterAction(line, params)
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
180 local action = twitterActionMap[line];
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
181 if action then
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
182 local url = action.url;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
183 local post = {};
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
184 --if action.needauth and not self.valid and line ~= "VerifyCredentials" then
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
185 -- return
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
186 --end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
187 if action.needauth then
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
188 http_headers['Authorization'] = "Basic "..base64.encode(self.data.login..":"..self.data.password);
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
189 --url = string.gsub(url, "http\:\/\/", string.format("http://%s:%s@", self.data.login, self.data.password));
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
190 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
191 if params and type(params) == "table" then
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
192 post = params;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
193 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
194 if action.method == "GET" and post ~= {} then
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
195 url = url.."?"..http.formencode(post);
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
196 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
197 http_add_action(line, url, action.method, post, function(...) self:twitterActionResult(...) end);
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
198 else
2887
65082d91950e Many modules: Simplify st.message(…):tag("body"):text(…):up() into st.message(…, …)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 983
diff changeset
199 module:send(st.message({to=self.jid, from=component_host, type='chat'}, "Wrong twitter action!"));
249
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
200 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
201 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
202
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
203 local twitterActionResultMap = {
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
204 PublicTimeline = {exec=function(jid, response)
2887
65082d91950e Many modules: Simplify st.message(…):tag("body"):text(…):up() into st.message(…, …)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 983
diff changeset
205 --module:send(st.message({to=jid, from=component_host, type='chat'}, print_r(response)));
249
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
206 return
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
207 end},
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
208 HomeTimeline = {exec=function(jid, response)
2887
65082d91950e Many modules: Simplify st.message(…):tag("body"):text(…):up() into st.message(…, …)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 983
diff changeset
209 --module:send(st.message({to=jid, from=component_host, type='chat'}, print_r(response)));
249
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
210 return
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
211 end},
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
212 FriendsTimeline = {function(jid, response)
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
213 return
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
214 end},
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
215 UserTimeline = {exec=function(jid, response)
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
216 return
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
217 end},
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
218 VerifyCredentials = {exec=function(jid, response)
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
219 if response ~= nil and response.id ~= nil then
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
220 users[jid].valid = true;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
221 users[jid].id = response.id;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
222 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
223 return
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
224 end},
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
225 UpdateStatus = {exec=function(jid, response)
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
226 return
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
227 end},
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
228 Retweet = {exec=function(jid, response)
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
229 return
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
230 end}
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
231 }
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
232
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
233 function user:twitterActionResult(id, response, request)
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
234 if request ~= nil and request.responseheaders['set-cookie'] ~= nil and request.responseheaders['location'] ~= nil then
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
235 --self.data['_twitter_sess'] = substring(request.responseheaders['set-cookie'], "_twitter_sess=", ";");
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
236 --http_add_action(id, request.responseheaders['location'], "GET", {}, function(...) self:twitterActionResult(...) end);
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
237 return true;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
238 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
239 local result, tmp_json = pcall(function() json.decode(response or "{}") end);
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
240 if result and id ~= nil then
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
241 twitterActionResultMap[id]:exec(self.jid, tmp_json);
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
242 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
243 return true;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
244 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
245
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
246 function iq_success(event)
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
247 local origin, stanza = event.origin, event.stanza;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
248 local reply = data_cache.success;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
249 if reply == nil then
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
250 reply = st.iq({type='result', from=stanza.attr.to or component_host});
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
251 data_cache.success = reply;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
252 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
253 reply.attr.id = stanza.attr.id;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
254 reply.attr.to = stanza.attr.from;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
255 origin.send(reply);
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
256 return true;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
257 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
258
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
259 function iq_disco_info(event)
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
260 local origin, stanza = event.origin, event.stanza;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
261 local from = {};
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
262 from.node, from.host, from.resource = jid_split(stanza.attr.from);
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
263 local bjid = from.node.."@"..from.host;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
264 local reply = data_cache.disco_info;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
265 if reply == nil then
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
266 reply = st.iq({type='result', from=stanza.attr.to or component_host}):query("http://jabber.org/protocol/disco#info")
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
267 :tag("identity", {category='gateway', type='chat', name=component_name}):up();
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
268 reply = reply:tag("feature", {var="urn:xmpp:receipts"}):up();
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
269 reply = reply:tag("feature", {var="http://jabber.org/protocol/commands"}):up();
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
270 reply = reply:tag("feature", {var="jabber:iq:register"}):up();
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
271 --reply = reply:tag("feature", {var="jabber:iq:time"}):up();
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
272 --reply = reply:tag("feature", {var="jabber:iq:version"}):up();
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
273 --reply = reply:tag("feature", {var="http://jabber.org/protocol/stats"}):up();
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
274 data_cache.disco_info = reply;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
275 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
276 reply.attr.id = stanza.attr.id;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
277 reply.attr.to = stanza.attr.from;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
278 origin.send(reply);
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
279 return true;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
280 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
281
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
282 function iq_disco_items(event)
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
283 local origin, stanza = event.origin, event.stanza;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
284 local reply = data_cache.disco_items;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
285 if reply == nil then
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
286 reply = st.iq({type='result', from=stanza.attr.to or component_host}):query("http://jabber.org/protocol/disco#items");
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
287 data_cache.disco_items = reply;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
288 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
289 reply.attr.id = stanza.attr.id;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
290 reply.attr.to = stanza.attr.from;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
291 origin.send(reply);
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
292 return true;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
293 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
294
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
295 function iq_register(event)
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
296 local origin, stanza = event.origin, event.stanza;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
297 if stanza.attr.type == "get" then
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
298 local reply = data_cache.registration_form;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
299 if reply == nil then
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
300 reply = st.iq({type='result', from=stanza.attr.to or component_host})
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
301 :tag("query", { xmlns="jabber:iq:register" })
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
302 :tag("instructions"):text("Enter your twitter data"):up()
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
303 :tag("username"):up()
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
304 :tag("password"):up();
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
305 data_cache.registration_form = reply
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
306 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
307 reply.attr.id = stanza.attr.id;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
308 reply.attr.to = stanza.attr.from;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
309 origin.send(reply);
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
310 elseif stanza.attr.type == "set" then
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
311 local from = {};
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
312 from.node, from.host, from.resource = jid_split(stanza.attr.from);
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
313 local bjid = from.node.."@"..from.host;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
314 local username, password = "", "";
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
315 local reply;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
316 for _, tag in ipairs(stanza.tags[1].tags) do
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
317 if tag.name == "remove" then
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
318 users[bjid]:signout();
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
319 iq_success(event);
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
320 return true;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
321 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
322 if tag.name == "username" then
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
323 username = tag[1];
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
324 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
325 if tag.name == "password" then
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
326 password = tag[1];
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
327 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
328 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
329 if username ~= nil and password ~= nil then
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
330 users[bjid] = setmetatable({}, user);
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
331 users[bjid].jid = bjid;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
332 users[bjid].data.login = username;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
333 users[bjid].data.password = password;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
334 users[bjid]:signin();
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
335 users[bjid]:login();
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
336 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
337 iq_success(event);
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
338 return true;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
339 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
340 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
341
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
342 function presence_stanza_handler(event)
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
343 local origin, stanza = event.origin, event.stanza;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
344 local to = {};
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
345 local from = {};
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
346 local pres = {};
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
347 to.node, to.host, to.resource = jid_split(stanza.attr.to);
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
348 from.node, from.host, from.resource = jid_split(stanza.attr.from);
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
349 pres.type = stanza.attr.type;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
350 for _, tag in ipairs(stanza.tags) do pres[tag.name] = tag[1]; end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
351 local from_bjid = nil;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
352 if from.node ~= nil and from.host ~= nil then
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
353 from_bjid = from.node.."@"..from.host;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
354 elseif from.host ~= nil then
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
355 from_bjid = from.host;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
356 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
357 if pres.type == nil then
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
358 if users[from_bjid] ~= nil then
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
359 -- Status change
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
360 if pres['status'] ~= nil and users[from_bjid]['data']['sync_status'] then
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
361 users[from_bjid]:twitterAction("UpdateStatus", {status=pres['status']});
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
362 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
363 else
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
364 -- User login request
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
365 users[from_bjid] = setmetatable({}, user);
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
366 users[from_bjid].jid = from_bjid;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
367 users[from_bjid]:login();
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
368 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
369 origin.send(st.presence({to=from_bjid, from=component_host}));
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
370 elseif pres.type == 'subscribe' and users[from_bjid] ~= nil then
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
371 origin.send(st.presence{to=from_bjid, from=component_host, type='subscribed'});
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
372 elseif pres.type == 'unsubscribed' and users[from_bjid] ~= nil then
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
373 users[from_bjid]:logout();
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
374 users[from_bjid]:signout();
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
375 users[from_bjid] = nil;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
376 elseif pres.type == 'unavailable' and users[from_bjid] ~= nil then
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
377 users[from_bjid]:logout();
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
378 users[from_bjid] = nil;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
379 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
380 return true;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
381 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
382
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
383 function confirm_message_delivery(event)
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
384 local reply = st.message({id=event.stanza.attr.id, to=event.stanza.attr.from, from=event.stanza.attr.to or component_host}):tag("received", {xmlns = "urn:xmpp:receipts"});
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
385 origin.send(reply);
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
386 return true;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
387 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
388
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
389 function message_stanza_handler(event)
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
390 local origin, stanza = event.origin, event.stanza;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
391 local to = {};
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
392 local from = {};
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
393 local msg = {};
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
394 to.node, to.host, to.resource = jid_split(stanza.attr.to);
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
395 from.node, from.host, from.resource = jid_split(stanza.attr.from);
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
396 local bjid = nil;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
397 if from.node ~= nil and from.host ~= nil then
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
398 from_bjid = from.node.."@"..from.host;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
399 elseif from.host ~= nil then
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
400 from_bjid = from.host;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
401 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
402 local to_bjid = nil;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
403 if to.node ~= nil and to.host ~= nil then
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
404 to_bjid = to.node.."@"..to.host;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
405 elseif to.host ~= nil then
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
406 to_bjid = to.host;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
407 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
408 for _, tag in ipairs(stanza.tags) do
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
409 msg[tag.name] = tag[1];
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
410 if tag.attr.xmlns == "urn:xmpp:receipts" then
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
411 confirm_message_delivery({origin=origin, stanza=stanza});
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
412 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
413 -- can handle more xmlns
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
414 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
415 -- Now parse the message
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
416 if stanza.attr.to == component_host then
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
417 if msg.body == "!myinfo" then
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
418 if users[from_bjid] ~= nil then
2887
65082d91950e Many modules: Simplify st.message(…):tag("body"):text(…):up() into st.message(…, …)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 983
diff changeset
419 origin.send(st.message({to=stanza.attr.from, from=component_host, type='chat'}, print_r(users[from_bjid])));
249
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
420 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
421 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
422 -- Other messages go to twitter
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
423 user:twitterAction("UpdateStatus", {status=msg.body});
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
424 else
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
425 -- Message to uid@host/resource
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
426 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
427 return true;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
428 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
429
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
430 module:hook("presence/host", presence_stanza_handler);
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
431 module:hook("message/host", message_stanza_handler);
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
432
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
433 module:hook("iq/host/jabber:iq:register:query", iq_register);
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
434 module:hook("iq/host/http://jabber.org/protocol/disco#info:query", iq_disco_info);
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
435 module:hook("iq/host/http://jabber.org/protocol/disco#items:query", iq_disco_items);
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
436 module:hook("iq/host", function(data)
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
437 -- IQ to a local host recieved
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
438 local origin, stanza = data.origin, data.stanza;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
439 if stanza.attr.type == "get" or stanza.attr.type == "set" then
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
440 return module:fire_event("iq/host/"..stanza.tags[1].attr.xmlns..":"..stanza.tags[1].name, data);
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
441 else
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
442 module:fire_event("iq/host/"..stanza.attr.id, data);
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
443 return true;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
444 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
445 end);
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
446
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
447 module.unload = function()
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
448 componentmanager.deregister_component(component_host);
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
449 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
450 component = componentmanager.register_component(component_host, function() return; end);