Initial work on a URL analyzer plugin

This commit is contained in:
Mikko Ahlroth 2013-06-27 23:56:27 +03:00
parent f280f07395
commit 38d3ae44b4

View file

@ -0,0 +1,51 @@
defmodule Nulform.Plugins.URLAnalyzer do
@moduledoc """
This is an example plugin which analyzes URLs on IRC. It scans incoming
IRC messages for URLs and analyzes them, returning data about them.
The algorithm for analyzing URLs is as follows:
* Send HEAD request to URL. Store content-length, content-type, and
HTTP code.
* If an error happened, return the collected data.
* If request succeeded and content-type is text/html, issue a GET
request to the URL.
* Try to find <title> contents from body and return it with other data
if found.
"""
def run() do
run nil
end
def run(parent) do
receive do
{:nulform, :set_parent, parent} ->
:ok = :inets.start()
{:nulform, :urlanalyze, url, id} ->
Kernel.spawn __MODULE__, :run_analyzer, [parent, Kernel.binary_to_list url]
end
run parent
end
def run_analyzer(parent, url) do
{:ok, {{_, status, _}, headers, body}} = :httpc.request :head, {url, []}, [], []
[content_type | _] = String.split to_binary(headers['content-type']), ";"
if status != 200 do
result = {status, content_type, headers['content-length'], body}
else
if content_type == "text/html" do
{:ok, {{_, status, _}, headers, body}} = :httpc.request url
end
result = {status, content_type, headers['content_length'], body}
end
parent <- result
end
def find_urls(message) do
[]
end
end