Initial work on a URL analyzer plugin
This commit is contained in:
parent
f280f07395
commit
38d3ae44b4
1 changed files with 51 additions and 0 deletions
51
lib/nulform/plugins/urlanalyzer.ex
Normal file
51
lib/nulform/plugins/urlanalyzer.ex
Normal 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
|
Loading…
Reference in a new issue