summaryrefslogtreecommitdiff
path: root/lib/lsg_irc/outline_plugin.ex
blob: 47fa6fa03c868d826751fe70cb5c30c652d173b1 (plain) (blame)
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
defmodule LSG.IRC.OutlinePlugin do
  @moduledoc """
  # outline auto-link

  Envoie un lien vers Outline quand un lien est envoyé.

  * **!outline `<url>`** crée un lien outline pour `<url>`.
  * **+outline `<host>`** active outline pour `<host>`.
  * **-outline `<host>`** désactive outline pour `<host>`.
  """
  def short_irc_doc, do: false
  def irc_doc, do: @moduledoc
  require Logger

  def start_link() do
    GenServer.start_link(__MODULE__, [], name: __MODULE__)
  end

  defstruct [:file, :hosts]

  def init([]) do
    regopts = [plugin: __MODULE__]
    {:ok, _} = Registry.register(IRC.PubSub, "trigger:outline", regopts)
    {:ok, _} = Registry.register(IRC.PubSub, "messages", regopts)
    file = Path.join(LSG.data_path, "/outline.txt")
    hosts = case File.read(file) do
      {:error, :enoent} ->
        []
      {:ok, lines} ->
        String.split(lines, "\n", trim: true)
    end
    {:ok, %__MODULE__{file: file, hosts: hosts}}
  end

  def handle_info({:irc, :trigger, "outline", message = %IRC.Message{trigger: %IRC.Trigger{type: :plus, args: [host]}}}, state) do
    state = %{state | hosts: [host | state.hosts]}
    save(state)
    message.replyfun.("ok")
    {:noreply, state}
  end

  def handle_info({:irc, :trigger, "outline", message = %IRC.Message{trigger: %IRC.Trigger{type: :minus, args: [host]}}}, state) do
    state = %{state | hosts: List.delete(state.hosts, host)}
    save(state)
    message.replyfun.("ok")
    {:noreply, state}
  end

  def handle_info({:irc, :trigger, "outline", message = %IRC.Message{trigger: %IRC.Trigger{type: :bang, args: [url]}}}, state) do
    line = "-> #{outline(url)}"
    message.replyfun.(line)
  end

  def handle_info({:irc, :text, message = %IRC.Message{text: text}}, state) do
    String.split(text)
    |> Enum.map(fn(word) ->
      if String.starts_with?(word, "http://") || String.starts_with?(word, "https://") do
        uri = URI.parse(word)
        if uri.scheme && uri.host do
          if Enum.any?(state.hosts, fn(host) -> String.ends_with?(uri.host, host) end) do
            outline_url = outline(word)
            line = "-> #{outline_url}"
            message.replyfun.(line)
          end
       end
      end
    end)
    {:noreply, state}
  end

  def handle_info(msg, state) do
    {:noreply, state}
  end

  def save(state = %{file: file, hosts: hosts}) do
    string = Enum.join(hosts, "\n")
    File.write(file, string)
  end

  def outline(url) do
    unexpanded = "https://outline.com/#{url}"
    headers = [
      {"User-Agent", "Mozilla/5.0 (X11; Linux x86_64; rv:77.0) Gecko/20100101 Firefox/77.0"},
      {"Accept", "*/*"},
      {"Accept-Language", "en-US,en;q=0.5"},
      {"Origin", "https://outline.com"},
      {"DNT", "1"},
      {"Referer", unexpanded},
      {"Pragma", "no-cache"},
      {"Cache-Control", "no-cache"}
    ]
    params = %{"source_url" => url}
    case HTTPoison.get("https://api.outline.com/v3/parse_article", headers, params: params) do
      {:ok, %HTTPoison.Response{status_code: 200, body: json}} ->
        body = Poison.decode!(json)
        if Map.get(body, "success") do
          code = get_in(body, ["data", "short_code"])
          "https://outline.com/#{code}"
        else
          unexpanded
        end
      error ->
        Logger.info("outline.com error: #{inspect error}")
        unexpanded
    end
  end

end