annotate mod_twitter/mod_twitter.lua @ 626:f19f723571d9

mod_auth_phpbb3: Match the username_clean column instead of the username column when updating password.
author Waqas Hussain <waqas20@gmail.com>
date Sun, 25 Mar 2012 02:30:36 +0500
parents 50c4348c2494
children 4f9cd19c4658
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";
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
14 local json = require "json";
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 send_stanza(stanza)
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
26 if stanza ~= nil then
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
27 core_route_stanza(prosody.hosts[component_host], stanza)
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
28 end
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
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
31 function dmsg(jid, msg)
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
32 module:log("debug", msg or "nil");
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
33 if jid ~= nil then
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
34 send_stanza(st.message({to=jid, from=component_host, type='chat'}):tag("body"):text(msg or "nil"):up());
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
35 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
36 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
37
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
38 function substring(string, start_string, ending_string)
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
39 local s_value_start, s_value_finish = nil, nil;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
40 if start_string ~= nil then
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
41 _, s_value_start = string:find(start_string);
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
42 if s_value_start == nil then
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
43 -- error
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
44 return nil;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
45 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
46 else
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 if ending_string ~= nil then
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
50 _, s_value_finish = string:find(ending_string, s_value_start+1);
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
51 if s_value_finish == nil then
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
52 -- error
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
53 return nil;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
54 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
55 else
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
56 s_value_finish = string:len()+1;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
57 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
58 return string:sub(s_value_start+1, s_value_finish-1);
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
59 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
60
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
61 local http_timeout = 30;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
62 local http_queue = setmetatable({}, { __mode = "k" }); -- auto-cleaning nil elements
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
63 data_cache['prosody_os'] = prosody.platform;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
64 data_cache['prosody_version'] = prosody.version;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
65 local http_headers = {
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
66 ["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
67 };
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
68
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
69 function http_action_callback(response, code, request, xcallback)
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
70 if http_queue == nil or http_queue[request] == nil then return; end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
71 local id = http_queue[request];
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
72 http_queue[request] = nil;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
73 if xcallback == nil then
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
74 dmsg(nil, "http_action_callback reports that xcallback is nil");
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
75 else
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
76 xcallback(id, response, request);
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
77 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
78 return true;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
79 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
80
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
81 function http_add_action(tid, url, method, post, fcallback)
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
82 local request = http.request(url, { headers = http_headers or {}, body = http.formencode(post or {}), method = method or "GET" }, function(response, code, request) http_action_callback(response, code, request, fcallback) end);
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
83 http_queue[request] = tid;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
84 timer.add_task(http_timeout, function() http.destroy_request(request); end);
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
85 return true;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
86 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
87
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
88 local users = setmetatable({}, {__mode="k"});
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
89 local user = {};
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
90 user.__index = user;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
91 user.dosync = false;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
92 user.valid = false;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
93 user.data = {};
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
94
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
95 function user:login()
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
96 userdata = datamanager.load(self.jid, component_host, "data");
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
97 if userdata ~= nil then
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
98 self.data = userdata;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
99 if self.data['_twitter_sess'] ~= nil then
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
100 http_headers['Cookie'] = "_twitter_sess="..self.data['_twitter_sess']..";";
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 send_stanza(st.presence({to=self.jid, from=component_host}));
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
103 self:twitterAction("VerifyCredentials");
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
104 if self.data.dosync == 1 then
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
105 self.dosync = true;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
106 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
107 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
108 else
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
109 send_stanza(st.message({to=self.jid, from=component_host, type='chat'}):tag("body"):text("You are not signed in."));
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
110 end
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:logout()
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
114 datamanager.store(self.jid, component_host, "data", self.data);
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
115 self.dosync = false;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
116 send_stanza(st.presence({to=self.jid, from=component_host, type='unavailable'}));
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
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
119 function user:sync()
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
120 if self.dosync then
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
121 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
122 return self.data.refreshrate;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
123 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
124 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
125
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
126 function user:signin()
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
127 if datamanager.load(self.jid, component_host, "data") == nil then
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
128 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})
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
129 send_stanza(st.presence{to=self.jid, from=component_host, type='subscribe'});
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
130 send_stanza(st.presence{to=self.jid, from=component_host, type='subscribed'});
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
131 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
132 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
133
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
134 function user:signout()
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
135 if datamanager.load(self.jid, component_host, "data") ~= nil then
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
136 datamanager.store(self.jid, component_host, "data", nil);
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
137 send_stanza(st.presence({to=self.jid, from=component_host, type='unavailable'}));
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
138 send_stanza(st.presence({to=self.jid, from=component_host, type='unsubscribe'}));
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
139 send_stanza(st.presence({to=self.jid, from=component_host, type='unsubscribed'}));
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
140 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
141 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
142
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
143 local twitterApiUrl = "http://api.twitter.com";
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
144 local twitterApiVersion = "1";
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
145 local twitterApiDataType = "json";
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
146 local twitterActionUrl = function(action) return twitterApiUrl.."/"..twitterApiVersion.."/"..action.."."..twitterApiDataType end;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
147 local twitterActionMap = {
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
148 PublicTimeline = {
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
149 url = twitterActionUrl("statuses/public_timeline"),
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
150 method = "GET",
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
151 needauth = false,
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
152 },
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
153 HomeTimeline = {
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
154 url = twitterActionUrl("statuses/home_timeline"),
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
155 method = "GET",
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
156 needauth = true,
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
157 },
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
158 FriendsTimeline = {
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
159 url = twitterActionUrl("statuses/friends_timeline"),
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
160 method = "GET",
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
161 needauth = true,
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
162 },
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
163 UserTimeline = {
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
164 url = twitterActionUrl("statuses/friends_timeline"),
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
165 method = "GET",
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
166 needauth = true,
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
167 },
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
168 VerifyCredentials = {
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
169 url = twitterActionUrl("account/verify_credentials"),
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
170 method = "GET",
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
171 needauth = true,
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
172 },
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
173 UpdateStatus = {
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
174 url = twitterActionUrl("statuses/update"),
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
175 method = "POST",
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
176 needauth = true,
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 Retweet = {
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
179 url = twitterActionUrl("statuses/retweet/%tweetid"),
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
180 method = "POST",
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
181 needauth = true,
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
182 }
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
183 }
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
184
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
185 function user:twitterAction(line, params)
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
186 local action = twitterActionMap[line];
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
187 if action then
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
188 local url = action.url;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
189 local post = {};
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
190 --if action.needauth and not self.valid and line ~= "VerifyCredentials" then
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
191 -- return
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
192 --end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
193 if action.needauth then
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
194 http_headers['Authorization'] = "Basic "..base64.encode(self.data.login..":"..self.data.password);
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
195 --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
196 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
197 if params and type(params) == "table" then
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
198 post = params;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
199 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
200 if action.method == "GET" and post ~= {} then
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
201 url = url.."?"..http.formencode(post);
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
202 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
203 http_add_action(line, url, action.method, post, function(...) self:twitterActionResult(...) end);
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
204 else
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
205 send_stanza(st.message({to=self.jid, from=component_host, type='chat'}):tag("body"):text("Wrong twitter action!"):up());
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
206 end
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
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
209 local twitterActionResultMap = {
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
210 PublicTimeline = {exec=function(jid, response)
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
211 --send_stanza(st.message({to=jid, from=component_host, type='chat'}):tag("body"):text(print_r(response)):up());
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
212 return
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
213 end},
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
214 HomeTimeline = {exec=function(jid, response)
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
215 --send_stanza(st.message({to=jid, from=component_host, type='chat'}):tag("body"):text(print_r(response)):up());
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 FriendsTimeline = {function(jid, response)
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
219 return
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
220 end},
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
221 UserTimeline = {exec=function(jid, response)
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
222 return
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
223 end},
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
224 VerifyCredentials = {exec=function(jid, response)
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
225 if response ~= nil and response.id ~= nil then
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
226 users[jid].valid = true;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
227 users[jid].id = response.id;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
228 end
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 UpdateStatus = {exec=function(jid, response)
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
232 return
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
233 end},
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
234 Retweet = {exec=function(jid, response)
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
235 return
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
236 end}
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
237 }
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
238
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
239 function user:twitterActionResult(id, response, request)
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
240 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
241 --self.data['_twitter_sess'] = substring(request.responseheaders['set-cookie'], "_twitter_sess=", ";");
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
242 --http_add_action(id, request.responseheaders['location'], "GET", {}, function(...) self:twitterActionResult(...) 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 local result, tmp_json = pcall(function() json.decode(response or "{}") end);
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
246 if result and id ~= nil then
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
247 twitterActionResultMap[id]:exec(self.jid, tmp_json);
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
248 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
249 return true;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
250 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
251
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
252 function iq_success(event)
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
253 local origin, stanza = event.origin, event.stanza;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
254 local reply = data_cache.success;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
255 if reply == nil then
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
256 reply = st.iq({type='result', from=stanza.attr.to or component_host});
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
257 data_cache.success = reply;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
258 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
259 reply.attr.id = stanza.attr.id;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
260 reply.attr.to = stanza.attr.from;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
261 origin.send(reply);
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
262 return true;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
263 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
264
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
265 function iq_disco_info(event)
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
266 local origin, stanza = event.origin, event.stanza;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
267 local from = {};
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
268 from.node, from.host, from.resource = jid_split(stanza.attr.from);
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
269 local bjid = from.node.."@"..from.host;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
270 local reply = data_cache.disco_info;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
271 if reply == nil then
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
272 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
273 :tag("identity", {category='gateway', type='chat', name=component_name}):up();
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
274 reply = reply:tag("feature", {var="urn:xmpp:receipts"}):up();
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
275 reply = reply:tag("feature", {var="http://jabber.org/protocol/commands"}):up();
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
276 reply = reply:tag("feature", {var="jabber:iq:register"}):up();
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
277 --reply = reply:tag("feature", {var="jabber:iq:time"}):up();
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
278 --reply = reply:tag("feature", {var="jabber:iq:version"}):up();
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
279 --reply = reply:tag("feature", {var="http://jabber.org/protocol/stats"}):up();
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
280 data_cache.disco_info = reply;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
281 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
282 reply.attr.id = stanza.attr.id;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
283 reply.attr.to = stanza.attr.from;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
284 origin.send(reply);
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
285 return true;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
286 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
287
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
288 function iq_disco_items(event)
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
289 local origin, stanza = event.origin, event.stanza;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
290 local reply = data_cache.disco_items;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
291 if reply == nil then
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
292 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
293 data_cache.disco_items = reply;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
294 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
295 reply.attr.id = stanza.attr.id;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
296 reply.attr.to = stanza.attr.from;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
297 origin.send(reply);
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
298 return true;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
299 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
300
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
301 function iq_register(event)
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
302 local origin, stanza = event.origin, event.stanza;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
303 if stanza.attr.type == "get" then
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
304 local reply = data_cache.registration_form;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
305 if reply == nil then
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
306 reply = st.iq({type='result', from=stanza.attr.to or component_host})
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
307 :tag("query", { xmlns="jabber:iq:register" })
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
308 :tag("instructions"):text("Enter your twitter data"):up()
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
309 :tag("username"):up()
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
310 :tag("password"):up();
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
311 data_cache.registration_form = reply
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
312 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
313 reply.attr.id = stanza.attr.id;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
314 reply.attr.to = stanza.attr.from;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
315 origin.send(reply);
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
316 elseif stanza.attr.type == "set" then
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
317 local from = {};
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
318 from.node, from.host, from.resource = jid_split(stanza.attr.from);
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
319 local bjid = from.node.."@"..from.host;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
320 local username, password = "", "";
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
321 local reply;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
322 for _, tag in ipairs(stanza.tags[1].tags) do
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
323 if tag.name == "remove" then
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
324 users[bjid]:signout();
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
325 iq_success(event);
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
326 return true;
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 if tag.name == "username" then
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
329 username = tag[1];
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
330 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
331 if tag.name == "password" then
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
332 password = tag[1];
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
333 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
334 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
335 if username ~= nil and password ~= nil then
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
336 users[bjid] = setmetatable({}, user);
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
337 users[bjid].jid = bjid;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
338 users[bjid].data.login = username;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
339 users[bjid].data.password = password;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
340 users[bjid]:signin();
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
341 users[bjid]:login();
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
342 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
343 iq_success(event);
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
344 return true;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
345 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
346 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
347
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
348 function presence_stanza_handler(event)
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
349 local origin, stanza = event.origin, event.stanza;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
350 local to = {};
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
351 local from = {};
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
352 local pres = {};
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
353 to.node, to.host, to.resource = jid_split(stanza.attr.to);
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
354 from.node, from.host, from.resource = jid_split(stanza.attr.from);
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
355 pres.type = stanza.attr.type;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
356 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
357 local from_bjid = nil;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
358 if from.node ~= nil and from.host ~= nil then
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
359 from_bjid = from.node.."@"..from.host;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
360 elseif from.host ~= nil then
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
361 from_bjid = from.host;
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 if pres.type == nil then
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
364 if users[from_bjid] ~= nil then
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
365 -- Status change
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
366 if pres['status'] ~= nil and users[from_bjid]['data']['sync_status'] then
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
367 users[from_bjid]:twitterAction("UpdateStatus", {status=pres['status']});
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 else
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
370 -- User login request
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
371 users[from_bjid] = setmetatable({}, user);
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
372 users[from_bjid].jid = from_bjid;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
373 users[from_bjid]:login();
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
374 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
375 origin.send(st.presence({to=from_bjid, from=component_host}));
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
376 elseif pres.type == 'subscribe' and users[from_bjid] ~= nil then
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
377 origin.send(st.presence{to=from_bjid, from=component_host, type='subscribed'});
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
378 elseif pres.type == 'unsubscribed' and users[from_bjid] ~= nil then
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
379 users[from_bjid]:logout();
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
380 users[from_bjid]:signout();
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
381 users[from_bjid] = nil;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
382 elseif pres.type == 'unavailable' and users[from_bjid] ~= nil then
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
383 users[from_bjid]:logout();
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
384 users[from_bjid] = nil;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
385 end
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 confirm_message_delivery(event)
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
390 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
391 origin.send(reply);
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
392 return true;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
393 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
394
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
395 function message_stanza_handler(event)
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
396 local origin, stanza = event.origin, event.stanza;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
397 local to = {};
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
398 local from = {};
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
399 local msg = {};
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
400 to.node, to.host, to.resource = jid_split(stanza.attr.to);
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
401 from.node, from.host, from.resource = jid_split(stanza.attr.from);
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
402 local bjid = nil;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
403 if from.node ~= nil and from.host ~= nil then
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
404 from_bjid = from.node.."@"..from.host;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
405 elseif from.host ~= nil then
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
406 from_bjid = from.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 local to_bjid = nil;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
409 if to.node ~= nil and to.host ~= nil then
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
410 to_bjid = to.node.."@"..to.host;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
411 elseif to.host ~= nil then
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
412 to_bjid = to.host;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
413 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
414 for _, tag in ipairs(stanza.tags) do
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
415 msg[tag.name] = tag[1];
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
416 if tag.attr.xmlns == "urn:xmpp:receipts" then
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
417 confirm_message_delivery({origin=origin, stanza=stanza});
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
418 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
419 -- can handle more xmlns
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 -- Now parse the message
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
422 if stanza.attr.to == component_host then
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
423 if msg.body == "!myinfo" then
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
424 if users[from_bjid] ~= nil then
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
425 origin.send(st.message({to=stanza.attr.from, from=component_host, type='chat'}):tag("body"):text(print_r(users[from_bjid])):up());
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 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
428 -- Other messages go to twitter
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
429 user:twitterAction("UpdateStatus", {status=msg.body});
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
430 else
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
431 -- Message to uid@host/resource
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
432 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
433 return true;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
434 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
435
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
436 module:hook("presence/host", presence_stanza_handler);
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
437 module:hook("message/host", message_stanza_handler);
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
438
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
439 module:hook("iq/host/jabber:iq:register:query", iq_register);
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
440 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
441 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
442 module:hook("iq/host", function(data)
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
443 -- IQ to a local host recieved
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
444 local origin, stanza = data.origin, data.stanza;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
445 if stanza.attr.type == "get" or stanza.attr.type == "set" then
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
446 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
447 else
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
448 module:fire_event("iq/host/"..stanza.attr.id, data);
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
449 return true;
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
450 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
451 end);
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
452
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
453 module.unload = function()
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
454 componentmanager.deregister_component(component_host);
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
455 end
50c4348c2494 mod_twitter: Initial commit.
dersd <xdersd@gmail.com>
parents:
diff changeset
456 component = componentmanager.register_component(component_host, function() return; end);