bard / purplebridge

This URL has Read+Write access

bard (author)
Sun Aug 10 07:05:06 -0700 2008
purplebridge / server.lua
100644 335 lines (265 sloc) 8.349 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
require 'lxp'
require 'base64'
 
-- Utilities
----------------------------------------------------------------------
 
local function dbg(msg)
   print(msg)
   print()
end
 
local function warn(msg)
   print('*** WARNING ***: ' .. msg)
   print()
end
 
local function get_child(el, tag)
   for i, child in ipairs(el) do
      if child.tag == tag then
         return child
      end
   end
end
 
local function has_child(el, tag)
   return get_child(el, tag)
end
 
local function ser(el)
   if not el.tag then
      return el
   end
 
   local ins = table.insert
   local s = {}
   ins(s, '<' .. el.tag)
 
   if el.attr then
      for k,v in pairs(el.attr) do
         if type(k) ~= 'number' then
            ins(s, ' ' .. k .. '="' .. v .. '"')
         end
      end
   end
 
   if table.getn(el) > 0 then
      ins(s, '>')
 
      for i,child in ipairs(el) do
         ins(s, ser(child))
      end
 
      ins(s, '</' .. el.tag .. '>')
   else
      ins(s, '/>')
   end
 
   return table.concat(s)
end
 
 
----------------------------------------------------------------------
-- Raw I/O
 
local function to_client(thing)
   local data
   if type(thing) == 'table' then
      data = ser(thing)
   else
      data = thing
   end
 
   dbg('(to_client) BRIDGE -> CLIENT: ' .. data)
end
 
----------------------------------------------------------------------
-- Us -> Purple
 
 
 
----------------------------------------------------------------------
-- Purple -> XMPP
 
local function roster_retrieved(el, contacts)
   dbg('(roster_retrieved) PURPLE -> BRIDGE: [contacts]')
 
   local response = {
      tag = 'iq',
      attr = { type = 'result', id = el.attr.id }
   }
 
   for i,contact in ipairs(contacts) do
      table.insert(
         response, {
            tag = 'item',
            attr = {
               jid = contact,
               subscription = 'both'
            }
         })
   end
 
   to_client(response)
end
 
local function got_auth_result(el, error)
   if not error then
      to_client([[<success xmlns="urn:ietf:params:xml:ns:xmpp-sasl"/>]])
   end
end
 
 
----------------------------------------------------------------------
-- Client -> Us
 
local function stream(attr)
   local attrvals = {}
   for k,v in pairs(attr) do
      if type(k)~= 'number' then table.insert(attrvals, k .. ': ' .. v) end
   end
 
   dbg('(stream) CLIENT -> BRIDGE: <stream> (' .. table.concat(attrvals, ', ') ..')')
 
   if attr.to == 'msn' then
      to_client('<stream:features xmlns:stream="http://etherx.jabber.org/streams"><mechanisms xmlns="urn:ietf:params:xml:ns:xmpp-sasl">' ..
                '<mechanism>PLAIN</mechanism></mechanisms>' ..
                '</stream:features>')
   else
      warn('foreign network not recognized (' .. attr.to .. ')')
   end
end
 
local function message(el)
   -- does not handle groupchat...
   if el.attr.type == 'chat' or el.attr.type == 'normal' then
      purple.send_message(el)
   end
end
 
local function iq(el)
   if el.attr.type == 'get' and has_child(el, 'jabber:iq:roster|query') then
      purple.retrieve_roster(el, roster_retrieved)
   end
end
 
local function presence(el)
   if not el.attr.type then
      purple.set_status(el)
   elseif has_child('http://jabber.org/protocol/muc|user') then
      purple.join_room(el)
   elseif el.attr.type == 'subscribe' then
      purple.subscribe_to_presence(el)
   elseif el.attr.type == 'unsubscribe' then
      purple.unsubscribe_from_presence(el)
   end
end
 
local function auth(el)
   local authstr, address, user, pwd, protocol
 
   assert(el.attr.mechanism == 'PLAIN')
 
   authstr = base64.decode(el[1])
   address, user, pwd = authstr:match("([^%z]+)%z([^%z]+)%z([^%z]+)")
   protocol = address:match('@(.+)')
   purple.signon('prpl-' .. protocol, user:gsub('%%', '@'), pwd)
end
 
function on_network_signon(username, protocol)
   dbg('(on_network_signon) ' .. username .. ' logged on on ' .. protocol)
end
 
local function element(el)
   local data = ser(el)
   dbg('(element) CLIENT -> BRIDGE: ' .. data)
 
   if el.tag == 'jabber:client|message' then
      message(el)
   elseif el.tag == 'jabber:client|iq' then
      iq(el)
   elseif el.tag == 'jabber:client|presence' then
      presence(el)
   elseif el.tag == 'urn:ietf:params:xml:ns:xmpp-sasl|auth' then
      auth(el)
   end
end
 
 
-- XMPP session
----------------------------------------------------------------------
 
Session = {}
 
function Session:stream(attr)
   -- local attrvals = {}
   -- for k,v in pairs(attr) do
   -- if type(k)~= 'number' then table.insert(attrvals, k .. ': ' .. v) end
   -- end
 
   -- dbg('CLIENT -> BRIDGE: <stream> (' .. table.concat(attrvals, ', ') ..')')
 
   if attr.to == 'msn' then
      self:to_client('<stream:features xmlns:stream="http://etherx.jabber.org/streams"><mechanisms xmlns="urn:ietf:params:xml:ns:xmpp-sasl">' ..
                '<mechanism>PLAIN</mechanism></mechanisms>' ..
                '</stream:features>')
   else
      warn('foreign network not recognized (' .. attr.to .. ')')
   end
end
 
function Session:to_client(thing)
   local data
   if type(thing) == 'table' then
      data = ser(thing)
   else
      data = thing
   end
 
   glib.io_channel_write(self.conn, data)
   dbg('(Session:to_client) BRIDGE -> CLIENT: ' .. data)
end
 
function Session:new(conn)
   o = {}
   setmetatable(o, self)
   self.__index = self
   self.conn = conn
 
   local stack = {{}}
 
   local function start_element(p, tag, attr)
      if tag == 'http://etherx.jabber.org/streams|stream' then
         self:stream(attr)
         return
      end
 
      table.insert(stack, {tag = tag, attr = attr})
   end
 
   local function end_element(p, tag)
      if tag == 'http://etherx.jabber.org/streams|stream' then
         return
      end
 
      local el = table.remove(stack)
      assert(el.tag == tag)
      local level = table.getn(stack)
      table.insert(stack[level], el)
 
      if level == 1 then
         element(stack[1][1])
         stack = {{}}
      end
   end
 
   local function character_data(p, txt)
      local el = stack[table.getn(stack)]
      local n = table.getn(el)
      local level = table.getn(stack)
      if level == 1 then
         return
      end
 
      if type(el[n]) == "string" then
         el[n] = el[n] .. txt
      else
         table.insert(el, txt)
      end
   end
 
   self.parser = lxp.new({ StartElement = start_element,
                           EndElement = end_element,
                           CharacterData = character_data }, '|')
   return self
end
 
function Session:receive(data)
   dbg('(Session:receive) CLIENT -> BRIDGE: ' .. data)
   self.parser:parse(data)
end
 
function Session:close()
   self.parser:close()
end
 
 
-- Test
----------------------------------------------------------------------
 
-- command line if not called as library
if (arg ~= nil) then
 
   purple = {}
   function purple.signon(user, pwd, callback)
      dbg('PURPLE <- BRIDGE: signon("' .. user .. '", "' .. pwd .. '")')
      callback(nil)
   end
 
   function purple.retrieve_roster(el, callback)
      dbg('PURPLE <- BRIDGE: retrieve_roster()')
      callback(el, {'someone', 'sometwo'})
   end
 
   function purple.set_status(el)
      dbg('PURPLE <- BRIDGE: set_status()')
   end
 
   function purple.subscribe_to_presence(el)
      dbg('PURPLE <- BRIDGE: subscribe_to_presence() of ' .. el.attr.to)
   end
 
   function purple.send_message(el)
      local body = get_child(el, 'jabber:client|body')
      local content = body[1]
 
      dbg('PURPLE <- BRIDGE: send_message() to ' .. el.attr.to ..
          ' with content "' .. content .. '"...')
   end
 
   local s = Session:new()
   print('------------------------------------------------------------')
   s:receive([[<?xml version='1.0'?>]])
   s:receive([[<stream:stream xmlns='jabber:client' ]])
   s:receive([[xmlns:stream='http://etherx.jabber.org/streams' to='msn' xml:lang='en' version='1.0'>]])
   s:receive([[<auth mechanism="PLAIN" xmlns="urn:ietf:params:xml:ns:xmpp-sasl">dXNlciVob3RtYWlsLmNvbQBzZWNyZXQ=</auth>]])
   s:receive([[<presence/>]])
   s:receive([[<iq type='get' id='rost1'><query xmlns='jabber:iq:roster'/></iq>]])
   s:receive([[<message to='me@here'><body>hello</body></message>]])
   s:receive([[<message to='someone@there' type='chat'><body>again</body></message>]])
   s:receive([[</stream:stream>]])
   s:close()
else
  module('server',package.seeall)
end