Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connect to multiple IRC servers with ExIrc (Elixir)?

Tags:

elixir

irc

I would like to connect to two servers in ExIrc with elixir, and I cannot find an easy solution to do so. I am fairly new to elixir, and all that I can see that I can do is use 'umbrellas' to run two apps and have them interface with each other? (I would like to use one app to connect to one IRC server, and if it has some certain words, parse the data and send to another IRC server)

like image 863
desu Avatar asked Nov 01 '22 01:11

desu


1 Answers

So to connect a single client you can do something like:

ExIrc.start!
{:ok, client} = ExIrc.Client.start_link
{:ok, handler} = ExampleHandler.start_link(nil)
ExIrc.Client.add_handler(client, handler)
ExIrc.Client.connect!(client, "chat.freenode.net", 6667)

I'm using the ExampleHandler just as the README suggests. Now if you do something like:

pass = ""
nick = "my_nick"
ExIrc.Client.logon(client, pass, nick, nick, nick)
ExIrc.Client.join(client, "#elixir-lang")

You will start seeing messages from #elixir-lang being output to the console - that's how the ExampleHandler is implemented, you will probably implement something else in its place.

Now nothing is stopping you from doing this a second time:

{:ok, client2} = ExIrc.Client.start_link
{:ok, handler2} = ExampleHandler.start_link(nil)
# and so on

To create a client client2 that is connected to the same or another server. To achieve what you want you'll just have to write a handler that reacts to messages from client by calling ExIrc.Client.msg(client2, ...) to post to the other client.

like image 147
Paweł Obrok Avatar answered Nov 15 '22 10:11

Paweł Obrok