Hanabi v0.1.1 Hanabi.User View Source

Entry point to interact with users.

Users are represented using the following structure :

%Hanabi.User{
  channels: [],
  hostname: nil,
  is_pass_validated?: false,
  key: nil,
  nick: nil,
  pid: nil,
  port: nil,
  realname: nil,
  type: :irc,
  username: nil,
  data: nil # allows you to store custom values
}

Hanabi maintains a registry storing every connected user. There are three different ‘type’ of users :

  • :irc : the user is directly connected via IRC and identified by its TCP session
  • :virtual : ‘virtual’ user defined by the system, its identifier is mannualy set with add/1 ot add/7
  • :void : same as :virtual, exept that no message will be transmitted to those users

The registry can be accessed using the get/1, get_all/0, update/2, set/2 and destroy/1 methods.

Link to this section Summary

Functions

Register an user given its struct

Convenience function to register an user

Sends a message to the user and to any channel containeg the user

Changes the nick of the given user (identifier or struct)

Remove an user from the registry given its struct or identifier

Returns the user structure registered under the identifier key

Returns a list containing all the pairs {key, user_struct}

Find the first user matching the pair field/value (e.g. get_by(:nick, "fnux")). Be careful with this method since it can be highly inefficient for a large set of users

Generate an user’s identity given its struct

Check if there is an user which has the value value in the field field

Remove an user from the server

Sends one or multiple messages to the given user

Convenience function to send a PRIVMSG to an user

Save the user struct in the registry under the given identifier. Returns false if the key is already in use

Update the values of an existing user struct stored in the registry

Link to this section Functions

Register an user given its struct.

Return values :

  • {:err, @err_needmoreparams}
  • {:err, @err_alreadyregistered} : there already is an user with the same username
  • {:err, @err_erroneusnickname}
  • {:err, @err_nicknameinuse}
  • {:err, :invalid_port} : :irc user but user.port is not a port
  • {:err, :invalid_pid} : :virtual user but user.pid is not a pid
  • {:err, :key_in_use}
  • {:ok, identifier}

@err_needmoreparams, @err_alreadyregistered, @err_erroneusnickname and @err_nicknameinuse are defined in Hanabi.IRC.Numeric.

Link to this function add(type, key, pid, nick, username, realname, hostname) View Source

Convenience function to register an user.

Sends a message to the user and to any channel containeg the user.

  • user is either the user’s identifier or its struct
  • msg is a single message struct
Link to this function change_nick(user, new_nickname) View Source

Changes the nick of the given user (identifier or struct).

Return values :

  • {:err, @err_erroneusnickname}
  • {:err, @err_nicknameinuse}
  • {:err, :no_such_user"}
  • {:ok, new_nickname}

@err_erroneusnickname and @err_nicknameinuse are defined in Hanabi.IRC.Numeric.

Remove an user from the registry given its struct or identifier.

Returns the user structure registered under the identifier key.

If no such identifier is found in the registry, returns nil.

Returns a list containing all the pairs {key, user_struct}.

Find the first user matching the pair field/value (e.g. get_by(:nick, "fnux")). Be careful with this method since it can be highly inefficient for a large set of users.

Generate an user’s identity given its struct.

Example

iex> user = %Hanabi.User{channels: [], hostname: 'localhost', key: #Port<0.8947>,
  nick: "fnux", pid: nil, port: #Port<0.8947>, realname: "realname", type: :irc,
  username: "fnux"}
iex> Hanabi.User.ident_for user
"fnux!~fnux@localhost"
Link to this function is_in_use?(field, value) View Source

Check if there is an user which has the value value in the field field.

Be careful, this method may be highly innificient with large sets.

Example

iex> Hanabi.User.is_in_use(:nick, "nonexisting")
false
iex> Hanabi.User.is_in_use(:nick, "existing")
true
Link to this function remove(user, part_msg \\ nil) View Source

Remove an user from the server.

  • user is either the user’s struct or identifier
  • part_msg is a string if specified

Sends one or multiple messages to the given user.

  • user is either the user’s identifier or its struct
  • msg is either a single message struct or a list of them
Link to this function send_privmsg(sender, receiver, content) View Source

Convenience function to send a PRIVMSG to an user.

Save the user struct in the registry under the given identifier. Returns false if the key is already in use.

Update the values of an existing user struct stored in the registry.

  • user is either the user’s identifier or struct.
  • value is a struct changeset, (nick: "fnux", %{nick: "lambda", realname: "Lamb Da", ...})