author | Myhailo Danylenko <isbear@ukrpost.net> |
Mon, 23 Mar 2009 09:22:04 +0200 | |
changeset 63 | 423555c07763 |
parent 62 | fb81aeb45e00 |
child 64 | bf7521ed96eb |
permissions | -rw-r--r-- |
62 | 1 |
|
2 |
-- MALICIOUS STANZAS (XEP-0076) |
|
3 |
||
4 |
-- FIXME for now we only can send evil messages |
|
5 |
-- also we cannot detect stream-level evil :( |
|
6 |
||
7 |
-- library |
|
8 |
||
9 |
require 'lm' |
|
10 |
require 'iq' |
|
11 |
||
12 |
-- public |
|
13 |
||
14 |
evil = { |
|
15 |
handler = |
|
16 |
function ( mess ) |
|
17 |
return false |
|
18 |
end, |
|
19 |
} |
|
20 |
||
21 |
function evil.message ( conn, to, mtype, message ) |
|
22 |
conn:send ( lm.message.create { mtype = 'message-' .. mtype, to = to, |
|
23 |
body = { message }, |
|
24 |
evil = { xmlns = 'http://jabber.org/protocol/evil' }, |
|
25 |
} ) |
|
26 |
end |
|
27 |
||
28 |
function evil.presence ( conn, to, status, message ) |
|
29 |
local mtype = 'presence-available' |
|
30 |
if status == 'unavailable' then |
|
31 |
mtype = 'presence-unavailable' |
|
32 |
status = '' |
|
33 |
end |
|
34 |
conn:send ( lm.message.create { mtype = mtype, from = conn:jid (), to = to, |
|
35 |
show = { status }, |
|
36 |
status = { message }, |
|
37 |
evil = { xmlns = 'http://jabber.org/protocol/evil' }, |
|
38 |
} ) |
|
39 |
end |
|
40 |
||
41 |
-- private |
|
42 |
||
43 |
local evil_incoming_stanza_handler = lm.message_handler.new ( |
|
44 |
function ( conn, mess ) |
|
45 |
local e = mess:child ( 'evil' ) |
|
46 |
if e and e:attribute ( 'xmlns' ) == 'http://jabber.org/protocol/evil' then |
|
47 |
return evil.handler ( mess ) |
|
48 |
end |
|
49 |
end ) |
|
50 |
||
51 |
-- mcabber |
|
52 |
||
53 |
evil.handler = |
|
54 |
function ( mess ) |
|
63
423555c07763
Evil configurability improvements and fixes
Myhailo Danylenko <isbear@ukrpost.net>
parents:
62
diff
changeset
|
55 |
local evillevel = tonumber(main.option ( 'lua_evil_sensibility' )) |
62 | 56 |
local mtype, smtype = mess:type () |
63
423555c07763
Evil configurability improvements and fixes
Myhailo Danylenko <isbear@ukrpost.net>
parents:
62
diff
changeset
|
57 |
if evillevel > 1 then |
423555c07763
Evil configurability improvements and fixes
Myhailo Danylenko <isbear@ukrpost.net>
parents:
62
diff
changeset
|
58 |
main.print_info ( mess:attribute ( 'from' ), 'Evil stanza of type "' .. mtype .. ' ' .. smtype .. '" detected!' ) |
423555c07763
Evil configurability improvements and fixes
Myhailo Danylenko <isbear@ukrpost.net>
parents:
62
diff
changeset
|
59 |
elseif evillevel > 0 then |
423555c07763
Evil configurability improvements and fixes
Myhailo Danylenko <isbear@ukrpost.net>
parents:
62
diff
changeset
|
60 |
print ( 'Tainted by evil stanza of type "' .. mtype .. ' ' .. smtype .. '" from ' .. ( mess:attribute ( 'from' ) or '... unknown in black' ) ) |
423555c07763
Evil configurability improvements and fixes
Myhailo Danylenko <isbear@ukrpost.net>
parents:
62
diff
changeset
|
61 |
end |
62 | 62 |
return main.yesno ( main.option ( 'lua_filter_evil' ) ) |
63 |
end |
|
64 |
||
65 |
local stat2xmpp = { |
|
66 |
free = 'chat', |
|
67 |
online = '', |
|
68 |
away = 'away', |
|
69 |
dnd = 'dnd', |
|
70 |
notavail = 'xa', |
|
71 |
offline = 'unavailable', |
|
72 |
} |
|
73 |
||
74 |
-- TODO improve interface, check if we sending right thing for offline |
|
75 |
main.command ( 'evil', |
|
76 |
function ( args ) |
|
77 |
local conn = lm.connection.bless ( main.connection () ) |
|
78 |
if args[1] == 'status' then |
|
79 |
local text = '' |
|
80 |
for i, mesg in ipairs ( args ) do |
|
81 |
if i > 2 then |
|
82 |
text = text .. ' ' .. mesg |
|
83 |
end |
|
84 |
end |
|
85 |
local st = stat2xmpp[args[2]] |
|
86 |
if not st then |
|
87 |
st = '' |
|
88 |
end |
|
89 |
evil.presence ( conn, args.t, st, text:sub ( 2 ) ) |
|
90 |
else |
|
91 |
local text = '' |
|
92 |
if args[1] == 'message' then |
|
93 |
for i, mesg in ipairs ( args ) do |
|
94 |
if i > 1 then |
|
95 |
text = text .. ' ' .. mesg |
|
96 |
end |
|
97 |
end |
|
98 |
else |
|
99 |
for i, mesg in ipairs ( args ) do |
|
100 |
text = text .. ' ' .. mesg |
|
101 |
end |
|
102 |
end |
|
103 |
local mtype = 'chat' |
|
104 |
if args.k then |
|
105 |
mtype = args.k |
|
106 |
end |
|
107 |
local who |
|
108 |
if args.t then |
|
109 |
who = args.t |
|
110 |
else |
|
111 |
who = main.current_buddy () |
|
112 |
end |
|
113 |
evil.message ( conn, who, mtype, text:sub ( 2 ) ) |
|
114 |
end |
|
115 |
end, true ) |
|
116 |
||
117 |
commands_help['evil'] = "[-t jid] [status stat [message] | [-k message_type] [message] message]\n\nSends evil message or presence.\nmessage_type may be chat, normal, headline.\nNote, that for now it will not change mcabber's status." |
|
118 |
||
119 |
local evil_handler_registered = false |
|
120 |
||
121 |
hooks_d['hook-post-connect'].evil = |
|
122 |
function ( args ) |
|
63
423555c07763
Evil configurability improvements and fixes
Myhailo Danylenko <isbear@ukrpost.net>
parents:
62
diff
changeset
|
123 |
lm.connection.bless( main.connection () ):handler ( evil_incoming_stanza_handler, 'iq', 'first' ) |
423555c07763
Evil configurability improvements and fixes
Myhailo Danylenko <isbear@ukrpost.net>
parents:
62
diff
changeset
|
124 |
lm.connection.bless( main.connection () ):handler ( evil_incoming_stanza_handler, 'message', 'first' ) |
423555c07763
Evil configurability improvements and fixes
Myhailo Danylenko <isbear@ukrpost.net>
parents:
62
diff
changeset
|
125 |
lm.connection.bless( main.connection () ):handler ( evil_incoming_stanza_handler, 'presence', 'first' ) |
62 | 126 |
evil_handler_registered = true |
127 |
hooks_d['hook-post-connect'].evil = nil |
|
128 |
hooks_d['hook-quit'].evil = |
|
129 |
function ( args ) |
|
130 |
if evil_handler_registered then |
|
63
423555c07763
Evil configurability improvements and fixes
Myhailo Danylenko <isbear@ukrpost.net>
parents:
62
diff
changeset
|
131 |
lm.connection.bless( main.connection () ):handler ( evil_incoming_stanza_handler, 'iq' ) |
423555c07763
Evil configurability improvements and fixes
Myhailo Danylenko <isbear@ukrpost.net>
parents:
62
diff
changeset
|
132 |
lm.connection.bless( main.connection () ):handler ( evil_incoming_stanza_handler, 'message' ) |
62 | 133 |
lm.connection.bless( main.connection () ):handler ( evil_incoming_stanza_handler, 'presence' ) |
134 |
end |
|
135 |
end |
|
136 |
end |
|
137 |
||
63
423555c07763
Evil configurability improvements and fixes
Myhailo Danylenko <isbear@ukrpost.net>
parents:
62
diff
changeset
|
138 |
local char2xmpp = { |
423555c07763
Evil configurability improvements and fixes
Myhailo Danylenko <isbear@ukrpost.net>
parents:
62
diff
changeset
|
139 |
f = 'chat', |
423555c07763
Evil configurability improvements and fixes
Myhailo Danylenko <isbear@ukrpost.net>
parents:
62
diff
changeset
|
140 |
o = '', |
423555c07763
Evil configurability improvements and fixes
Myhailo Danylenko <isbear@ukrpost.net>
parents:
62
diff
changeset
|
141 |
a = 'away', |
423555c07763
Evil configurability improvements and fixes
Myhailo Danylenko <isbear@ukrpost.net>
parents:
62
diff
changeset
|
142 |
d = 'dnd', |
423555c07763
Evil configurability improvements and fixes
Myhailo Danylenko <isbear@ukrpost.net>
parents:
62
diff
changeset
|
143 |
n = 'xa', |
423555c07763
Evil configurability improvements and fixes
Myhailo Danylenko <isbear@ukrpost.net>
parents:
62
diff
changeset
|
144 |
_ = 'unavailable', |
423555c07763
Evil configurability improvements and fixes
Myhailo Danylenko <isbear@ukrpost.net>
parents:
62
diff
changeset
|
145 |
} |
423555c07763
Evil configurability improvements and fixes
Myhailo Danylenko <isbear@ukrpost.net>
parents:
62
diff
changeset
|
146 |
|
423555c07763
Evil configurability improvements and fixes
Myhailo Danylenko <isbear@ukrpost.net>
parents:
62
diff
changeset
|
147 |
-- hack, but working ;) |
423555c07763
Evil configurability improvements and fixes
Myhailo Danylenko <isbear@ukrpost.net>
parents:
62
diff
changeset
|
148 |
hooks_d['hook-my-status-change'].evil = |
423555c07763
Evil configurability improvements and fixes
Myhailo Danylenko <isbear@ukrpost.net>
parents:
62
diff
changeset
|
149 |
function ( args ) |
423555c07763
Evil configurability improvements and fixes
Myhailo Danylenko <isbear@ukrpost.net>
parents:
62
diff
changeset
|
150 |
if main.yesno ( main.option ( 'lua_evil_mode' ) ) then |
423555c07763
Evil configurability improvements and fixes
Myhailo Danylenko <isbear@ukrpost.net>
parents:
62
diff
changeset
|
151 |
evil.presence ( lm.connection.bless ( main.connection () ), nil, char2xmpp[args.new_status], args.message ) |
423555c07763
Evil configurability improvements and fixes
Myhailo Danylenko <isbear@ukrpost.net>
parents:
62
diff
changeset
|
152 |
end |
423555c07763
Evil configurability improvements and fixes
Myhailo Danylenko <isbear@ukrpost.net>
parents:
62
diff
changeset
|
153 |
end |
423555c07763
Evil configurability improvements and fixes
Myhailo Danylenko <isbear@ukrpost.net>
parents:
62
diff
changeset
|
154 |
|
62 | 155 |
main.add_feature ( 'http://jabber.org/protocol/evil' ) |
156 |
||
157 |
-- vim: se ts=4: -- |