From db9be1edc8d6ffb1068bc8fa9701b8d43b089858 Mon Sep 17 00:00:00 2001 From: Rachel Fae Fox Date: Mon, 7 Nov 2022 07:08:38 -0500 Subject: [PATCH] bring this up to date finally, this should be considered a merge. --- .gitignore | 0 README.md | 0 lib/channel_map.ex | 0 lib/discordhandler.ex | 1 - lib/discordirc.ex | 2 +- lib/formatter.ex | 14 +++--- lib/irc_bot.ex | 99 ++++++++++++++++++++++++++++++++++--------- lib/webhookservice.ex | 10 ++--- mix.exs | 3 +- mix.lock | 41 +++++++++++------- 10 files changed, 120 insertions(+), 50 deletions(-) mode change 100644 => 100755 .gitignore mode change 100644 => 100755 README.md mode change 100644 => 100755 lib/channel_map.ex mode change 100644 => 100755 lib/discordhandler.ex mode change 100644 => 100755 lib/discordirc.ex mode change 100644 => 100755 lib/formatter.ex mode change 100644 => 100755 lib/irc_bot.ex mode change 100644 => 100755 lib/webhookservice.ex mode change 100644 => 100755 mix.exs mode change 100644 => 100755 mix.lock diff --git a/.gitignore b/.gitignore old mode 100644 new mode 100755 diff --git a/README.md b/README.md old mode 100644 new mode 100755 diff --git a/lib/channel_map.ex b/lib/channel_map.ex old mode 100644 new mode 100755 diff --git a/lib/discordhandler.ex b/lib/discordhandler.ex old mode 100644 new mode 100755 index 81981c2..4fd1757 --- a/lib/discordhandler.ex +++ b/lib/discordhandler.ex @@ -7,7 +7,6 @@ defmodule Discordirc.DiscordHandler do Consumer.start_link(__MODULE__) end - def is_me_or_my_webhook(msg) do {:ok, me} = Api.get_current_user() diff --git a/lib/discordirc.ex b/lib/discordirc.ex old mode 100644 new mode 100755 index b16bad3..d0cfa5e --- a/lib/discordirc.ex +++ b/lib/discordirc.ex @@ -13,7 +13,7 @@ defmodule Discordirc do ircnets ++ [ Discordirc.DiscordHandler, - Discordirc.WebhookService + Discordirc.WebhookService ] options = [strategy: :one_for_one, name: Discordirc.Supervisor] diff --git a/lib/formatter.ex b/lib/formatter.ex old mode 100644 new mode 100755 index c5cc3ee..f48b7ba --- a/lib/formatter.ex +++ b/lib/formatter.ex @@ -75,7 +75,7 @@ defmodule Discordirc.Formatter do s %{str: r, dui: dui} -> - if String.match?(s, ~r/\<\@\!(\d+)\>/) do + if String.match?(s, ~r/\<\@(\d+)\>/) do if s == r do if is_binary(dui.nickname) do String.replace(s, r, "@" <> dui.nickname) @@ -120,12 +120,15 @@ defmodule Discordirc.Formatter do end def fixdiscordidstrings(%{:content => content, :guild_id => guild}) do - pattern = ~r/\<(\@\!|#)(\d+)\>/um + pattern = ~r/\<(\@!?|#)(\d+)\>/um matches = Regex.scan(pattern, content) |> Enum.uniq() |> Enum.map(fn + [fst, "@", lst] -> + %{str: fst, id: lst, dui: DiscordUserInfo.from_id(String.to_integer(lst), guild)} + [fst, "@!", lst] -> %{str: fst, id: lst, dui: DiscordUserInfo.from_id(String.to_integer(lst), guild)} @@ -169,11 +172,12 @@ defmodule Discordirc.Formatter do # discord may give... many lines. split and format. case Enum.count(messages) do 0 -> - "<#{usr}> #{messages[0]}" + {usr, "#{messages[0]}"} _ -> - messages - |> Enum.map(fn m -> "<#{usr}> #{m}" end) + {usr, + messages + |> Enum.map(fn m -> "#{m}" end)} end end end diff --git a/lib/irc_bot.ex b/lib/irc_bot.ex old mode 100644 new mode 100755 index d0e7d12..a0553a8 --- a/lib/irc_bot.ex +++ b/lib/irc_bot.ex @@ -40,15 +40,11 @@ defmodule Discordirc.IRC do Process.flag(:trap_exit, true) Client.add_handler(client, self()) - Logger.debug( - "connecting #{ - if state.ssl? do - "ssl" - else - "unsecured" - end - } on #{state.network} (#{state.server} #{state.port})" - ) + Logger.debug("connecting #{if state.ssl? do + "ssl" + else + "unsecured" + end} on #{state.network} (#{state.server} #{state.port})") if state.ssl? do Client.connect_ssl!(client, state.server, state.port) @@ -65,18 +61,73 @@ defmodule Discordirc.IRC do {:noreply, state} end + def ircsplit(str, pfxlen) do + str + |> String.split(" ") + |> Enum.chunk_while( + [], + fn ele, acc -> + if Enum.join(Enum.reverse([ele | acc]), " ") |> String.length() > 512 - pfxlen do + {:cont, Enum.reverse(acc), [ele]} + else + {:cont, [ele | acc]} + end + end, + fn + [] -> {:cont, []} + acc -> {:cont, Enum.reverse(acc), []} + end + ) + |> Enum.map(fn x -> Enum.join(x, " ") end) + |> Enum.map(fn x -> + case String.length(x) do + n when is_integer(n) and n > 512 -> + x + |> String.to_charlist() + |> Enum.chunk_every(512 - pfxlen) + |> Enum.map(&List.to_string(&1)) + + _ -> + x + end + end) + |> List.flatten() + |> Enum.filter(&(&1 !== "")) + end + + def discord_ircsplit(msg, nick, target) do + pfx = "PRIVMSG #{target} :" |> String.length() + nkl = "<#{nick}> " |> String.length() + msg + |> String.split("\n") + |> Enum.map(&ircsplit(&1, pfx+nkl)) + |> List.flatten() + end + def handle_info({:discordmsg, msg}, state) do channel = ChannelMap.irc(msg.channel_id) - response = Formatter.from_discord(msg) + {usr, response} = Formatter.from_discord(msg) case channel do {:ok, _, chan} -> - case response do + # irc messages can only be 512b in length + split_response = + case response do + x when is_binary(x) -> + discord_ircsplit(x, usr, chan) + + x when is_list(x) -> + x + |> Enum.map(&discord_ircsplit(&1, usr, chan)) + |> List.flatten() + end + + case split_response do x when is_binary(x) -> - ExIRC.Client.msg(state.client, :privmsg, chan, x) + ExIRC.Client.msg(state.client, :privmsg, chan, "<#{usr}> #{x}") x when is_list(x) -> - for m <- x, do: ExIRC.Client.msg(state.client, :privmsg, chan, m) + for m <- x, do: ExIRC.Client.msg(state.client, :privmsg, chan, "<#{usr}> #{m}") end end @@ -120,22 +171,28 @@ defmodule Discordirc.IRC do {:noreply, state} end + # lets try using the supervisor instead... def handle_info(:disconnected, state) do - if state.ssl? do - Client.connect_ssl!(state.client, state.server, state.port) - else - Client.connect!(state.client, state.server, state.port) - end - - {:noreply, state} + Logger.debug("Disconnected, throwing self to hell.") + {:stop, "disconnection", state} end + # def handle_info(:disconnected, state) do + # if state.ssl? do + # Client.connect_ssl!(state.client, state.server, state.port) + # else + # Client.connect!(state.client, state.server, state.port) + # end + # + # {:noreply, state} + # end + def handle_info(_event, state) do {:noreply, state} end def terminate(_, state) do - Logger.debug("Qutting..") + Logger.debug("Qutting...") Client.quit(state.client, "discordirc.ex") Client.stop!(state.client) :ok diff --git a/lib/webhookservice.ex b/lib/webhookservice.ex old mode 100644 new mode 100755 index 5269e01..2bff605 --- a/lib/webhookservice.ex +++ b/lib/webhookservice.ex @@ -25,8 +25,8 @@ defmodule Discordirc.WebhookService do case DiscordAPI.create_webhook( channel_id, - %{name: "otherbot hook", avatar: avatar}, - "otherbot proxy hook" + %{name: "discordirc hook", avatar: avatar}, + "discordirc proxy hook" ) do {:ok, hook} -> case state.hooks do @@ -86,12 +86,12 @@ defmodule Discordirc.WebhookService do DiscordAPI.execute_webhook(wh.id, wh.token, args) rescue e in MatchError -> - Logger.warn("MatchError from nostrum workaround in place.") + Logger.warn("MatchError from nostrum workaround in place.") + e in FunctionClauseError -> - Logger.warn("FunctionClauseError from nostrum workaround in place.") + Logger.warn("FunctionClauseError from nostrum workaround in place.") end {:noreply, state} end - end diff --git a/mix.exs b/mix.exs old mode 100644 new mode 100755 index c531e9a..8013be3 --- a/mix.exs +++ b/mix.exs @@ -23,8 +23,7 @@ defmodule Discordirc.MixProject do defp deps do [ {:nostrum, "~> 0.4"}, - {:exirc, "~> 1.1.0"}, - {:distillery, "~> 2.0"} + {:exirc, "~> 1.1.0"} # {:dep_from_hexpm, "~> 0.3.0"}, # {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"} ] diff --git a/mix.lock b/mix.lock old mode 100644 new mode 100755 index 3eb9a58..a667945 --- a/mix.lock +++ b/mix.lock @@ -1,17 +1,28 @@ %{ - "certifi": {:hex, :certifi, "2.5.2", "b7cfeae9d2ed395695dd8201c57a2d019c0c43ecaf8b8bcb9320b40d6662f340", [:rebar3], [{:parse_trans, "~>3.3", [hex: :parse_trans, repo: "hexpm", optional: false]}], "hexpm"}, - "cowlib": {:hex, :cowlib, "2.6.0", "8aa629f81a0fc189f261dc98a42243fa842625feea3c7ec56c48f4ccdb55490f", [:rebar3], [], "hexpm"}, - "exirc": {:hex, :exirc, "1.1.0", "212a86124a113d87752ce528d52f8e2e2dab3accada66e5330591b9de2c628f9", [:mix], [], "hexpm"}, - "gen_stage": {:hex, :gen_stage, "0.14.3", "d0c66f1c87faa301c1a85a809a3ee9097a4264b2edf7644bf5c123237ef732bf", [:mix], [], "hexpm"}, - "gun": {:hex, :gun, "1.3.2", "542064cbb9f613650b8a8100b3a927505f364fbe198b7a5a112868ff43f3e477", [:rebar3], [{:cowlib, "~> 2.6.0", [hex: :cowlib, repo: "hexpm", optional: false]}], "hexpm"}, - "hackney": {:hex, :hackney, "1.16.0", "5096ac8e823e3a441477b2d187e30dd3fff1a82991a806b2003845ce72ce2d84", [:rebar3], [{:certifi, "2.5.2", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "6.0.1", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "1.0.1", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "~>1.1", [hex: :mimerl, repo: "hexpm", optional: false]}, {:parse_trans, "3.3.0", [hex: :parse_trans, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "1.1.6", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}], "hexpm"}, - "httpoison": {:hex, :httpoison, "1.7.0", "abba7d086233c2d8574726227b6c2c4f6e53c4deae7fe5f6de531162ce9929a0", [:mix], [{:hackney, "~> 1.16", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm"}, - "idna": {:hex, :idna, "6.0.1", "1d038fb2e7668ce41fbf681d2c45902e52b3cb9e9c77b55334353b222c2ee50c", [:rebar3], [{:unicode_util_compat, "0.5.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm"}, - "metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], [], "hexpm"}, - "mimerl": {:hex, :mimerl, "1.2.0", "67e2d3f571088d5cfd3e550c383094b47159f3eee8ffa08e64106cdf5e981be3", [:rebar3], [], "hexpm"}, - "nostrum": {:hex, :nostrum, "0.4.3", "393f22d9ede8f24aded2eaef9d9637c67512a6c6579c23a5c45a37e1466f7f1b", [:mix], [{:gen_stage, "~> 0.11", [hex: :gen_stage, repo: "hexpm", optional: false]}, {:gun, "~> 1.3", [hex: :gun, repo: "hexpm", optional: false]}, {:httpoison, "~> 1.7", [hex: :httpoison, repo: "hexpm", optional: false]}, {:poison, "~> 3.0", [hex: :poison, repo: "hexpm", optional: false]}], "hexpm"}, - "parse_trans": {:hex, :parse_trans, "3.3.0", "09765507a3c7590a784615cfd421d101aec25098d50b89d7aa1d66646bc571c1", [:rebar3], [], "hexpm"}, - "poison": {:hex, :poison, "3.1.0", "d9eb636610e096f86f25d9a46f35a9facac35609a7591b3be3326e99a0484665", [:mix], [], "hexpm"}, - "ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.6", "cf344f5692c82d2cd7554f5ec8fd961548d4fd09e7d22f5b62482e5aeaebd4b0", [:make, :mix, :rebar3], [], "hexpm"}, - "unicode_util_compat": {:hex, :unicode_util_compat, "0.5.0", "8516502659002cec19e244ebd90d312183064be95025a319a6c7e89f4bccd65b", [:rebar3], [], "hexpm"}, + "artificery": {:hex, :artificery, "0.4.3", "0bc4260f988dcb9dda4b23f9fc3c6c8b99a6220a331534fdf5bf2fd0d4333b02", [:mix], [], "hexpm", "12e95333a30e20884e937abdbefa3e7f5e05609c2ba8cf37b33f000b9ffc0504"}, + "certifi": {:hex, :certifi, "2.9.0", "6f2a475689dd47f19fb74334859d460a2dc4e3252a3324bd2111b8f0429e7e21", [:rebar3], [], "hexpm", "266da46bdb06d6c6d35fde799bcb28d36d985d424ad7c08b5bb48f5b5cdd4641"}, + "chacha20": {:hex, :chacha20, "1.0.3", "26372176993172260968b36b4e7bc2e007e6b2b397ae08083e4836df67cdf03c", [:mix], [], "hexpm", "27f23b680e63f04b5bced77a9d8867033b381c091f92d544de0ad93accfb7cec"}, + "cowlib": {:hex, :remedy_cowlib, "2.11.1", "7abb4d0779a7d1c655f7642dc0bd0af754951e95005dfa01b500c68fe35a5961", [:rebar3], [], "hexpm", "0b613dc308e080cb6134285f1b1b55c3873e101652e70c70010fc6651c91b130"}, + "curve25519": {:hex, :curve25519, "1.0.4", "e570561b832c29b3ce4fd8b9fcd9c9546916188860568f1c1fc9428d7cb00894", [:mix], [], "hexpm", "1a068bf9646e7067bf6aa5bf802b404002734e09bb5300f098109df03e31f9f5"}, + "distillery": {:hex, :distillery, "2.1.1", "f9332afc2eec8a1a2b86f22429e068ef35f84a93ea1718265e740d90dd367814", [:mix], [{:artificery, "~> 0.2", [hex: :artificery, repo: "hexpm", optional: false]}], "hexpm", "bbc7008b0161a6f130d8d903b5b3232351fccc9c31a991f8fcbf2a12ace22995"}, + "ed25519": {:hex, :ed25519, "1.4.1", "479fb83c3e31987c9cad780e6aeb8f2015fb5a482618cdf2a825c9aff809afc4", [:mix], [], "hexpm", "0dacb84f3faa3d8148e81019ca35f9d8dcee13232c32c9db5c2fb8ff48c80ec7"}, + "equivalex": {:hex, :equivalex, "1.0.3", "170d9a82ae066e0020dfe1cf7811381669565922eb3359f6c91d7e9a1124ff74", [:mix], [], "hexpm", "46fa311adb855117d36e461b9c0ad2598f72110ad17ad73d7533c78020e045fc"}, + "exirc": {:hex, :exirc, "1.1.0", "212a86124a113d87752ce528d52f8e2e2dab3accada66e5330591b9de2c628f9", [:mix], [], "hexpm", "71d01fd9c3378e3f13acd855188ec2b4f225884d2d33fabdb77b77e34b7f5dc7"}, + "gen_stage": {:hex, :gen_stage, "1.1.2", "b1656cd4ba431ed02c5656fe10cb5423820847113a07218da68eae5d6a260c23", [:mix], [], "hexpm", "9e39af23140f704e2b07a3e29d8f05fd21c2aaf4088ff43cb82be4b9e3148d02"}, + "gun": {:hex, :remedy_gun, "2.0.1", "0f0caed812ed9e4da4f144df2d5bf73b0a99481d395ecde990a3791decf321c6", [:rebar3], [{:cowlib, "~> 2.11.1", [hex: :remedy_cowlib, repo: "hexpm", optional: false]}], "hexpm", "b6685a85fbd12b757f86809be1b3d88fcef365b77605cd5aa34db003294c446e"}, + "hackney": {:hex, :hackney, "1.16.0", "5096ac8e823e3a441477b2d187e30dd3fff1a82991a806b2003845ce72ce2d84", [:rebar3], [{:certifi, "2.5.2", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "6.0.1", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "1.0.1", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "~>1.1", [hex: :mimerl, repo: "hexpm", optional: false]}, {:parse_trans, "3.3.0", [hex: :parse_trans, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "1.1.6", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}], "hexpm", "3bf0bebbd5d3092a3543b783bf065165fa5d3ad4b899b836810e513064134e18"}, + "httpoison": {:hex, :httpoison, "1.7.0", "abba7d086233c2d8574726227b6c2c4f6e53c4deae7fe5f6de531162ce9929a0", [:mix], [{:hackney, "~> 1.16", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm", "975cc87c845a103d3d1ea1ccfd68a2700c211a434d8428b10c323dc95dc5b980"}, + "idna": {:hex, :idna, "6.0.1", "1d038fb2e7668ce41fbf681d2c45902e52b3cb9e9c77b55334353b222c2ee50c", [:rebar3], [{:unicode_util_compat, "0.5.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "a02c8a1c4fd601215bb0b0324c8a6986749f807ce35f25449ec9e69758708122"}, + "jason": {:hex, :jason, "1.3.0", "fa6b82a934feb176263ad2df0dbd91bf633d4a46ebfdffea0c8ae82953714946", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "53fc1f51255390e0ec7e50f9cb41e751c260d065dcba2bf0d08dc51a4002c2ac"}, + "kcl": {:hex, :kcl, "1.4.2", "8b73a55a14899dc172fcb05a13a754ac171c8165c14f65043382d567922f44ab", [:mix], [{:curve25519, ">= 1.0.4", [hex: :curve25519, repo: "hexpm", optional: false]}, {:ed25519, "~> 1.3", [hex: :ed25519, repo: "hexpm", optional: false]}, {:poly1305, "~> 1.0", [hex: :poly1305, repo: "hexpm", optional: false]}, {:salsa20, "~> 1.0", [hex: :salsa20, repo: "hexpm", optional: false]}], "hexpm", "9f083dd3844d902df6834b258564a82b21a15eb9f6acdc98e8df0c10feeabf05"}, + "metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], [], "hexpm", "69b09adddc4f74a40716ae54d140f93beb0fb8978d8636eaded0c31b6f099f16"}, + "mime": {:hex, :mime, "2.0.2", "0b9e1a4c840eafb68d820b0e2158ef5c49385d17fb36855ac6e7e087d4b1dcc5", [:mix], [], "hexpm", "e6a3f76b4c277739e36c2e21a2c640778ba4c3846189d5ab19f97f126df5f9b7"}, + "mimerl": {:hex, :mimerl, "1.2.0", "67e2d3f571088d5cfd3e550c383094b47159f3eee8ffa08e64106cdf5e981be3", [:rebar3], [], "hexpm", "f278585650aa581986264638ebf698f8bb19df297f66ad91b18910dfc6e19323"}, + "nostrum": {:hex, :nostrum, "0.6.0", "773b0a8953f74abb3d7633be94d5dc2d780f8ecaabba161e57f656afd6620c92", [:mix], [{:certifi, "~> 2.8", [hex: :certifi, repo: "hexpm", optional: false]}, {:gen_stage, "~> 0.11 or ~> 1.0", [hex: :gen_stage, repo: "hexpm", optional: false]}, {:gun, "== 2.0.1", [hex: :remedy_gun, repo: "hexpm", optional: false]}, {:jason, "~> 1.2", [hex: :jason, repo: "hexpm", optional: false]}, {:kcl, "~> 1.4", [hex: :kcl, repo: "hexpm", optional: false]}, {:mime, "~> 1.6 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}], "hexpm", "f44846457e5e40137948164633ed2ea4cc21c434c224c69073f4df1cc90a3f90"}, + "parse_trans": {:hex, :parse_trans, "3.3.0", "09765507a3c7590a784615cfd421d101aec25098d50b89d7aa1d66646bc571c1", [:rebar3], [], "hexpm", "17ef63abde837ad30680ea7f857dd9e7ced9476cdd7b0394432af4bfc241b960"}, + "poison": {:hex, :poison, "3.1.0", "d9eb636610e096f86f25d9a46f35a9facac35609a7591b3be3326e99a0484665", [:mix], [], "hexpm", "fec8660eb7733ee4117b85f55799fd3833eb769a6df71ccf8903e8dc5447cfce"}, + "poly1305": {:hex, :poly1305, "1.0.4", "7cdc8961a0a6e00a764835918cdb8ade868044026df8ef5d718708ea6cc06611", [:mix], [{:chacha20, "~> 1.0", [hex: :chacha20, repo: "hexpm", optional: false]}, {:equivalex, "~> 1.0", [hex: :equivalex, repo: "hexpm", optional: false]}], "hexpm", "e14e684661a5195e149b3139db4a1693579d4659d65bba115a307529c47dbc3b"}, + "salsa20": {:hex, :salsa20, "1.0.3", "fb900fc9b26b713a98618f3c6d6b6c35a5514477c6047caca8d03f3a70175ab0", [:mix], [], "hexpm", "91cbfa537f369d074a79f926f36a6c2ac24fba12cbadcb23aa04a759282887fe"}, + "ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.6", "cf344f5692c82d2cd7554f5ec8fd961548d4fd09e7d22f5b62482e5aeaebd4b0", [:make, :mix, :rebar3], [], "hexpm", "bdb0d2471f453c88ff3908e7686f86f9be327d065cc1ec16fa4540197ea04680"}, + "unicode_util_compat": {:hex, :unicode_util_compat, "0.5.0", "8516502659002cec19e244ebd90d312183064be95025a319a6c7e89f4bccd65b", [:rebar3], [], "hexpm", "d48d002e15f5cc105a696cf2f1bbb3fc72b4b770a184d8420c8db20da2674b38"}, }