Mercurial > prosody-modules
comparison mod_twitter/mod_twitter.lua @ 803:be509416d64c
mod_twitter: Use module:send()
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sat, 25 Aug 2012 19:25:16 +0200 |
parents | 4f9cd19c4658 |
children | ac9bf3fcbcfe |
comparison
equal
deleted
inserted
replaced
802:4e43becc3bbe | 803:be509416d64c |
---|---|
20 | 20 |
21 function print_r(obj) | 21 function print_r(obj) |
22 return require("util.serialization").serialize(obj); | 22 return require("util.serialization").serialize(obj); |
23 end | 23 end |
24 | 24 |
25 function send_stanza(stanza) | |
26 if stanza ~= nil then | |
27 core_route_stanza(prosody.hosts[component_host], stanza) | |
28 end | |
29 end | |
30 | |
31 function dmsg(jid, msg) | 25 function dmsg(jid, msg) |
32 module:log("debug", msg or "nil"); | 26 module:log("debug", msg or "nil"); |
33 if jid ~= nil then | 27 if jid ~= nil then |
34 send_stanza(st.message({to=jid, from=component_host, type='chat'}):tag("body"):text(msg or "nil"):up()); | 28 module:send(st.message({to=jid, from=component_host, type='chat'}):tag("body"):text(msg or "nil"):up()); |
35 end | 29 end |
36 end | 30 end |
37 | 31 |
38 function substring(string, start_string, ending_string) | 32 function substring(string, start_string, ending_string) |
39 local s_value_start, s_value_finish = nil, nil; | 33 local s_value_start, s_value_finish = nil, nil; |
97 if userdata ~= nil then | 91 if userdata ~= nil then |
98 self.data = userdata; | 92 self.data = userdata; |
99 if self.data['_twitter_sess'] ~= nil then | 93 if self.data['_twitter_sess'] ~= nil then |
100 http_headers['Cookie'] = "_twitter_sess="..self.data['_twitter_sess']..";"; | 94 http_headers['Cookie'] = "_twitter_sess="..self.data['_twitter_sess']..";"; |
101 end | 95 end |
102 send_stanza(st.presence({to=self.jid, from=component_host})); | 96 module:send(st.presence({to=self.jid, from=component_host})); |
103 self:twitterAction("VerifyCredentials"); | 97 self:twitterAction("VerifyCredentials"); |
104 if self.data.dosync == 1 then | 98 if self.data.dosync == 1 then |
105 self.dosync = true; | 99 self.dosync = true; |
106 timer.add_task(self.data.refreshrate, function() return users[self.jid]:sync(); end) | 100 timer.add_task(self.data.refreshrate, function() return users[self.jid]:sync(); end) |
107 end | 101 end |
108 else | 102 else |
109 send_stanza(st.message({to=self.jid, from=component_host, type='chat'}):tag("body"):text("You are not signed in.")); | 103 module:send(st.message({to=self.jid, from=component_host, type='chat'}):tag("body"):text("You are not signed in.")); |
110 end | 104 end |
111 end | 105 end |
112 | 106 |
113 function user:logout() | 107 function user:logout() |
114 datamanager.store(self.jid, component_host, "data", self.data); | 108 datamanager.store(self.jid, component_host, "data", self.data); |
115 self.dosync = false; | 109 self.dosync = false; |
116 send_stanza(st.presence({to=self.jid, from=component_host, type='unavailable'})); | 110 module:send(st.presence({to=self.jid, from=component_host, type='unavailable'})); |
117 end | 111 end |
118 | 112 |
119 function user:sync() | 113 function user:sync() |
120 if self.dosync then | 114 if self.dosync then |
121 table.foreach(self.data.synclines, function(ind, line) self:twitterAction(line.name, {sinceid=line.sinceid}) end); | 115 table.foreach(self.data.synclines, function(ind, line) self:twitterAction(line.name, {sinceid=line.sinceid}) end); |
124 end | 118 end |
125 | 119 |
126 function user:signin() | 120 function user:signin() |
127 if datamanager.load(self.jid, component_host, "data") == nil then | 121 if datamanager.load(self.jid, component_host, "data") == nil then |
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}) | 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}) |
129 send_stanza(st.presence{to=self.jid, from=component_host, type='subscribe'}); | 123 module:send(st.presence{to=self.jid, from=component_host, type='subscribe'}); |
130 send_stanza(st.presence{to=self.jid, from=component_host, type='subscribed'}); | 124 module:send(st.presence{to=self.jid, from=component_host, type='subscribed'}); |
131 end | 125 end |
132 end | 126 end |
133 | 127 |
134 function user:signout() | 128 function user:signout() |
135 if datamanager.load(self.jid, component_host, "data") ~= nil then | 129 if datamanager.load(self.jid, component_host, "data") ~= nil then |
136 datamanager.store(self.jid, component_host, "data", nil); | 130 datamanager.store(self.jid, component_host, "data", nil); |
137 send_stanza(st.presence({to=self.jid, from=component_host, type='unavailable'})); | 131 module:send(st.presence({to=self.jid, from=component_host, type='unavailable'})); |
138 send_stanza(st.presence({to=self.jid, from=component_host, type='unsubscribe'})); | 132 module:send(st.presence({to=self.jid, from=component_host, type='unsubscribe'})); |
139 send_stanza(st.presence({to=self.jid, from=component_host, type='unsubscribed'})); | 133 module:send(st.presence({to=self.jid, from=component_host, type='unsubscribed'})); |
140 end | 134 end |
141 end | 135 end |
142 | 136 |
143 local twitterApiUrl = "http://api.twitter.com"; | 137 local twitterApiUrl = "http://api.twitter.com"; |
144 local twitterApiVersion = "1"; | 138 local twitterApiVersion = "1"; |
200 if action.method == "GET" and post ~= {} then | 194 if action.method == "GET" and post ~= {} then |
201 url = url.."?"..http.formencode(post); | 195 url = url.."?"..http.formencode(post); |
202 end | 196 end |
203 http_add_action(line, url, action.method, post, function(...) self:twitterActionResult(...) end); | 197 http_add_action(line, url, action.method, post, function(...) self:twitterActionResult(...) end); |
204 else | 198 else |
205 send_stanza(st.message({to=self.jid, from=component_host, type='chat'}):tag("body"):text("Wrong twitter action!"):up()); | 199 module:send(st.message({to=self.jid, from=component_host, type='chat'}):tag("body"):text("Wrong twitter action!"):up()); |
206 end | 200 end |
207 end | 201 end |
208 | 202 |
209 local twitterActionResultMap = { | 203 local twitterActionResultMap = { |
210 PublicTimeline = {exec=function(jid, response) | 204 PublicTimeline = {exec=function(jid, response) |
211 --send_stanza(st.message({to=jid, from=component_host, type='chat'}):tag("body"):text(print_r(response)):up()); | 205 --module:send(st.message({to=jid, from=component_host, type='chat'}):tag("body"):text(print_r(response)):up()); |
212 return | 206 return |
213 end}, | 207 end}, |
214 HomeTimeline = {exec=function(jid, response) | 208 HomeTimeline = {exec=function(jid, response) |
215 --send_stanza(st.message({to=jid, from=component_host, type='chat'}):tag("body"):text(print_r(response)):up()); | 209 --module:send(st.message({to=jid, from=component_host, type='chat'}):tag("body"):text(print_r(response)):up()); |
216 return | 210 return |
217 end}, | 211 end}, |
218 FriendsTimeline = {function(jid, response) | 212 FriendsTimeline = {function(jid, response) |
219 return | 213 return |
220 end}, | 214 end}, |