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
| defmodule FilterPacketDemo do import Ejabberd.Logger @behaviour :gen_mod
def start(_host, _opts) do info('Starting ejabberd module Filter Packet Demo') Ejabberd.Hooks.add(:filter_packet, :global, __ENV__.module, :on_filter_packet, 50) :ok end
def stop(_host) do info('Stopping ejabberd module Filter Packet Demo') Ejabberd.Hooks.delete(:filter_packet, :global, __ENV__.module, :on_filter_packet, 50) :ok end
def on_filter_packet({from, to, xml={:xmlel, "message", attributes, children}} = packet) do info("Filtering packet: #{inspect {from, to, xml}}") convert = fn {k,v} -> {String.to_atom(k), v} end attribute_keywords = attributes |> Enum.map convert type = attribute_keywords[:type] cond do type == "chat" -> dosomething() type == "headline" -> dosomething() type == "groupchat" -> dosomething() true -> true end packet end
def dosomething do IO.puts "Do something." end end
|