Skip to content

Instantly share code, notes, and snippets.

@adrianparvino
Created June 5, 2019 15:14
Show Gist options
  • Save adrianparvino/9d0cfbde02135f99b5e44126202833c2 to your computer and use it in GitHub Desktop.
Save adrianparvino/9d0cfbde02135f99b5e44126202833c2 to your computer and use it in GitHub Desktop.
defmodule TermuxSmsCli.Fetcher do
use GenServer
defp fetch_int(filter_fn, minimum) do
fetch_int(filter_fn, minimum, minimum)
end
defp fetch_int(filter_fn, minimum, count) do
{reply, 0} = System.cmd("ssh", ["192.168.2.8", "-p", "8022", "termux-sms-list", "-l", "#{count}"])
reply = Jason.decode!(reply)
reply = Enum.filter(reply, filter_fn)
if (Enum.count(reply) < minimum) do
fetch_int(filter_fn, minimum, 2*count)
else
reply
end
end
def start_link(opts) do
GenServer.start_link(__MODULE__, :ok, opts)
end
def init(:ok) do
{:ok, nil}
end
def handle_call({:fetch, count}, _From, nil) do
{:reply, fetch_int(fn _ -> true end, count), nil}
end
def handle_call({:fetch_thread, count, tid}, _From, nil) do
{:reply, fetch_int(fn %{"threadid" => tid_} -> tid == tid_ end, count), nil}
end
def fetch(pid, count) do
GenServer.call(pid, {:fetch, count})
end
def fetch_thread(pid, count, tid) do
GenServer.call(pid, {:fetch_thread, count, tid})
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment