author | Myhailo Danylenko <isbear@ukrpost.net> |
Sat, 14 Mar 2009 16:51:11 +0200 | |
changeset 19 | 365e77518376 |
parent 8 | fc9060b9b7cc |
child 21 | 2384ce322282 |
permissions | -rw-r--r-- |
5
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
1 |
|
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
2 |
-- Requires libsocket |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
3 |
|
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
4 |
-- TODO: |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
5 |
-- do pubsub tunes+notify instead of status hacking |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
6 |
|
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
7 |
require 'socket' |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
8 |
|
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
9 |
local settings = { |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
10 |
hostname = "localhost", |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
11 |
password = nil, |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
12 |
port = 6600, |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
13 |
} |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
14 |
|
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
15 |
mpd = {} |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
16 |
|
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
17 |
-- separator allows split output into records, that are started by any of present in separator keys |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
18 |
-- returns table of field (lowercase) - value records |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
19 |
-- command status is returned in STATUS field |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
20 |
-- on unexpected errors returns just false, dropping any available data |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
21 |
function mpd.receive_message ( tcp, separator ) |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
22 |
local ret = {} |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
23 |
local line = tcp:receive ( '*l' ) |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
24 |
while line and not ( line:find ( '^OK' ) or line:find ( '^ACK' ) ) do |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
25 |
local key, val = line:match ( '^(.-):%s*(.*)$' ) |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
26 |
if key then |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
27 |
if separator then |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
28 |
key = key:lower () |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
29 |
if separator[key] then |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
30 |
table.insert ( ret, {} ) |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
31 |
end |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
32 |
ret[#ret][key] = val |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
33 |
else |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
34 |
ret[key:lower()] = val |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
35 |
end |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
36 |
end |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
37 |
line = tcp:receive ( '*l' ) |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
38 |
end |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
39 |
if not line then |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
40 |
return false -- an error occured, ret, most likely, does not contains exact and complete info... |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
41 |
else |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
42 |
ret.STATUS = line:match ( '^%S+' ) |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
43 |
return ret |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
44 |
end |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
45 |
end |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
46 |
|
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
47 |
-- use: mpd.call_command { 'status' } |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
48 |
-- mpd.call_command { 'lsinfo misc', list = { file = true, directory = true } } |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
49 |
-- mpd.call_command { 'next', noret = true } |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
50 |
-- mpd.call_command { 'status', 'currentsong' } |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
51 |
-- on one command returns just results of that command, on multi - array of results |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
52 |
-- on errors returns nil |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
53 |
-- noret can terminate socket too early, thus, do not use it with lists of commands |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
54 |
function mpd.call_command ( opts ) |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
55 |
local tcp = socket.tcp () |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
56 |
if not tcp then |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
57 |
print ( 'mpd: cannot get master tcp object' ) |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
58 |
return nil |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
59 |
elseif not tcp:connect ( settings.hostname, settings.port ) then |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
60 |
tcp:close () |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
61 |
print ( 'mpd: cannot connect to server' ) |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
62 |
return nil |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
63 |
end |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
64 |
|
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
65 |
local ret = {} |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
66 |
if not opts.noret then |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
67 |
ret = mpd.receive_message ( tcp ) |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
68 |
if not ret then |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
69 |
tcp:close () |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
70 |
print ( 'mpd: error getting greeting from server' ) |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
71 |
return nil |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
72 |
elseif ret.STATUS ~= 'OK' then |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
73 |
print ( 'mpd: server ack\'s in greeting' ) |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
74 |
end |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
75 |
end |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
76 |
|
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
77 |
for num, command in ipairs ( opts ) do |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
78 |
if not tcp:send ( command .. "\n" ) then |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
79 |
tcp:close () |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
80 |
print ( 'mpd: error sending command ' .. command ) |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
81 |
return nil |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
82 |
end |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
83 |
if not opts.noret then |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
84 |
ret[num] = mpd.receive_message ( tcp, opts.list ) |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
85 |
if not ret[num] then |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
86 |
print ( 'mpd: error getting result' ) |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
87 |
end |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
88 |
if ret[num]['STATUS'] ~= 'OK' then |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
89 |
print ( 'mpd: server acks our command ' .. command ) |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
90 |
end |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
91 |
end |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
92 |
end |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
93 |
|
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
94 |
tcp:close () |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
95 |
if #ret > 1 then |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
96 |
return ret |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
97 |
else |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
98 |
return ret[1] |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
99 |
end |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
100 |
end |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
101 |
|
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
102 |
-- MCABBER PART -- |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
103 |
|
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
104 |
mpd_enabled = false |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
105 |
|
8 | 106 |
-- XXX: to C? |
107 |
char2status = { |
|
108 |
f = 'free', |
|
109 |
o = 'online', |
|
110 |
a = 'away', |
|
111 |
d = 'dnd', |
|
112 |
n = 'notavail', |
|
113 |
i = 'invisible', |
|
114 |
['_'] = 'offline', |
|
115 |
['?'] = 'message', |
|
116 |
} |
|
117 |
||
19 | 118 |
mpd_pub_song = { xmlns = 'http://jabber.org/protocol/tune' } |
119 |
||
5
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
120 |
function mpd_getstatus () |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
121 |
if not mpd_enabled then |
19 | 122 |
mpd_pub_song.artist = nil |
123 |
mpd_pub_song.length = nil |
|
124 |
mpd_pub_song.source = nil |
|
125 |
mpd_pub_song.title = nil |
|
126 |
mpd_pub_song.track = nil |
|
127 |
return mpd_pub_song |
|
5
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
128 |
end |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
129 |
|
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
130 |
local stats = mpd.call_command { 'status', 'currentsong' } |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
131 |
if stats[1].state ~= 'play' and stats[1].state ~= 'pause' then |
19 | 132 |
mpd_pub_song.artist = nil |
133 |
mpd_pub_song.length = nil |
|
134 |
mpd_pub_song.source = nil |
|
135 |
mpd_pub_song.title = nil |
|
136 |
mpd_pub_song.track = nil |
|
137 |
return mpd_pub_song |
|
138 |
end |
|
139 |
||
140 |
local dir, file = stats[2].file:match ( '(.+)/(.-)' ) |
|
141 |
local modified = false |
|
142 |
-- populate according to currentsong fields: artist - artist, length - time, source - album, title - title, track - id, rating - ?, uri - ? |
|
143 |
local artist, length, source, title, track = stats[2].artist, stats[2].time, stats[2].album, stats[2].title, stats[2].id |
|
144 |
||
145 |
if not artist or artist == '' then |
|
146 |
artist = 'Unknown' |
|
147 |
end |
|
148 |
if not mpd_pub_song.artist or artist ~= mpd_pub_song.artist[1] then |
|
149 |
mpd_pub_song.artist = { artist } |
|
150 |
modified = true |
|
5
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
151 |
end |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
152 |
|
19 | 153 |
if length and length ~= 0 then |
154 |
if not mpd_pub_song.length or length ~= mpd_pub_song.length[1] then |
|
155 |
mpd_pub_song.length = { length } |
|
156 |
modified = true |
|
5
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
157 |
end |
19 | 158 |
elseif mpd_pub_song.length then -- no length |
159 |
mpd_pub_song.length = nil |
|
160 |
modified = true |
|
161 |
end |
|
162 |
||
163 |
if not source or source == '' then |
|
164 |
source = dir |
|
165 |
end |
|
166 |
if not mpd_pub_song.source or source ~= mpd_pub_song.source[1] then |
|
167 |
mpd_pub_song.source = { source } |
|
168 |
modified = true |
|
5
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
169 |
end |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
170 |
|
19 | 171 |
if not title or title == '' then |
172 |
title = file |
|
173 |
end |
|
174 |
if not mpd_pub_song.title or title ~= mpd_pub_song.title[1] then |
|
175 |
mpd_pub_song.title = { title } |
|
176 |
modified = true |
|
177 |
end |
|
178 |
||
179 |
if not mpd_pub_song.track or track ~= mpd_pub_song.track[1] then |
|
180 |
mpd_pub_song.track = { track } |
|
181 |
modified = true |
|
182 |
end |
|
183 |
||
184 |
if modified then |
|
185 |
return mpd_pub_song |
|
5
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
186 |
else |
19 | 187 |
return nil |
5
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
188 |
end |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
189 |
end |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
190 |
|
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
191 |
function parse_status () |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
192 |
local stletter, stmessage = main.status () |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
193 |
local cmd = char2status[stletter] |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
194 |
local message, mpd_string = stmessage:match ( "^(.-)%s+(%[mpd:%s+.+%s*%])" ) |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
195 |
if message then |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
196 |
return cmd, message, mpd_string |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
197 |
else |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
198 |
return cmd, stmessage, '' |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
199 |
end |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
200 |
end |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
201 |
|
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
202 |
function mpd_callback () |
19 | 203 |
local sdata = mpd_getstatus () |
204 |
if sdata then |
|
205 |
local conn = lm.connection.bless ( main.connection () ) |
|
206 |
if conn:status () == 'authenticated' then |
|
207 |
conn:send ( |
|
208 |
lm.message.create { mtype = 'iq-set', from = conn:jid (), |
|
209 |
pubsub = { xmlns = 'http://jabber.org/protocol/pubsub', |
|
210 |
publish = { node = 'http://jabber.org/protocol/tune', |
|
211 |
item = { |
|
212 |
tune = sdata, |
|
213 |
}, |
|
214 |
}, |
|
215 |
}, |
|
216 |
}) |
|
217 |
end |
|
5
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
218 |
end |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
219 |
if mpd_enabled then |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
220 |
return true |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
221 |
else |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
222 |
return false |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
223 |
end |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
224 |
end |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
225 |
|
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
226 |
-- do not call it too fast, or you end up with many daemons at once |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
227 |
function enable_mpd ( yn ) |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
228 |
if yn == nil then |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
229 |
yn = true |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
230 |
end |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
231 |
if yn then |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
232 |
if not mpd_enabled then |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
233 |
main.timer ( 15, mpd_callback ) |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
234 |
mpd_enabled = true |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
235 |
-- update status |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
236 |
end |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
237 |
else |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
238 |
if mpd_enabled then |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
239 |
mpd_enabled = false |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
240 |
-- update status |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
241 |
end |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
242 |
end |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
243 |
end |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
244 |
|
7
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
5
diff
changeset
|
245 |
main.command ( 'mpd', |
5
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
246 |
function ( args ) |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
247 |
local enable = yesno ( args ) |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
248 |
if enable == nil then |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
249 |
if mpd_enabled then |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
250 |
print ( "MPD status string is enabled" ) |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
251 |
else |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
252 |
print ( "MPD status string is disabled" ) |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
253 |
end |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
254 |
else |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
255 |
enable_mpd ( enable ) |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
256 |
end |
8 | 257 |
end, boolean_cid ) |
5
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
258 |
|
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
259 |
commands_help['mpd'] = "[enable|disable|on|off|yes|no|true|false]\n\nSets state of mpd string in your status.\nIf state is omitted, prints current state." |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
260 |
|
19 | 261 |
mpd_incoming_message_handler = lm.message_handler.new ( |
262 |
function ( conn, mess ) |
|
263 |
-- we can add that validation stuff later, if it will be necessary |
|
264 |
local tune = mess:path ( 'event', 'items', 'item', 'tune' ) |
|
265 |
if tune then |
|
266 |
local from = mess:attribute ( 'from' ) |
|
267 |
local item = tune:children () |
|
268 |
main.print_info ( from, "Tunes notification:" ) |
|
269 |
while item do |
|
270 |
main.print_info ( from, ("- %s: %s"):format ( item:name (), item:value () ) ) |
|
271 |
item = item:next () |
|
272 |
end |
|
273 |
return true |
|
274 |
end |
|
275 |
return false |
|
276 |
end ) |
|
277 |
||
278 |
mpd_handler_registered = false |
|
279 |
||
8 | 280 |
hooks_d['hook-post-connect'].mpd = |
281 |
function ( args ) |
|
19 | 282 |
lm.connection.bless( main.connection () ):handler ( mpd_incoming_message_handler, 'message', 'normal' ) |
283 |
mpd_handler_registered = true |
|
284 |
hooks_d['hook-post-connect'].mpd = |
|
285 |
function ( args ) |
|
286 |
if mpd_enabled then |
|
287 |
mpd_callback () |
|
288 |
end |
|
289 |
end |
|
290 |
hooks_d['hook-quit'].mpd = |
|
291 |
function ( args ) |
|
292 |
if mpd_handler_registered then |
|
293 |
lm.connection.bless( main.connection () ):handler ( mpd_incoming_message_handler, 'message' ) |
|
294 |
mpd_handler_registered = false |
|
295 |
end |
|
296 |
end |
|
8 | 297 |
if mpd_enabled then |
298 |
mpd_callback () |
|
299 |
end |
|
300 |
end |
|
301 |
||
19 | 302 |
-- XXX: this really should be initialized after connection establishment? |
303 |
-- but as this thing is implemented by now, it will be cached by server, |
|
304 |
-- and, thus, we will be unable to get notifications. |
|
305 |
main.add_feature ( 'http://jabber.org/protocol/tune' ) |
|
306 |
main.add_feature ( 'http://jabber.org/protocol/tune+notify' ) |
|
307 |
||
5
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
308 |
-- vim: se ts=4: -- |