Class: BitlbeeConfig::User

Inherits:
Object
  • Object
show all
Includes:
XmlBuildable
Defined in:
lib/bitlbee_config/user.rb

Overview

A BitlBee user account - has channels for control / chat, and accounts with various IM services

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from XmlBuildable

#to_xml_with_options

Constructor Details

#initialize(options = {}) ⇒ User

Returns a new instance of User.

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :nick (String)

    Nickname for the user, as it appears in the IRC channel

  • :password (String)

    The hashed password of the user, as it appears in the XML document

  • :cleartext_password (String)

    Cleartext password for the user, will be hashed before saving to XML

  • :version (String)

    I have no idea what this does

  • :channels (Array<BitlbeeConfig::Channel>)

    Channels for this user

  • :accounts (Array<BitlbeeConfig::Account|BitlbeeConfig::Accounts::Icq>)

    IM accounts for this user

  • All (String)

    other entries will be converted to settings



60
61
62
63
64
65
66
67
68
69
# File 'lib/bitlbee_config/user.rb', line 60

def initialize(options = {})
  @nick = options.delete(:nick)
  @password = options.delete(:password)
  @cleartext_password = options.delete(:cleartext_password)
  @version = options.delete(:version) || "1"
  @channels = options.delete(:channels) || []
  @accounts = options.delete(:accounts) || []

  @settings = options || {}
end

Instance Attribute Details

#accountsObject

Returns the value of attribute accounts.



6
7
8
# File 'lib/bitlbee_config/user.rb', line 6

def accounts
  @accounts
end

#channelsObject

Returns the value of attribute channels.



6
7
8
# File 'lib/bitlbee_config/user.rb', line 6

def channels
  @channels
end

#cleartext_passwordObject

Returns the value of attribute cleartext_password.



6
7
8
# File 'lib/bitlbee_config/user.rb', line 6

def cleartext_password
  @cleartext_password
end

#nickObject

Returns the value of attribute nick.



6
7
8
# File 'lib/bitlbee_config/user.rb', line 6

def nick
  @nick
end

#passwordObject

Returns the value of attribute password.



6
7
8
# File 'lib/bitlbee_config/user.rb', line 6

def password
  @password
end

#settingsObject

Returns the value of attribute settings.



6
7
8
# File 'lib/bitlbee_config/user.rb', line 6

def settings
  @settings
end

#versionObject

Returns the value of attribute version.



6
7
8
# File 'lib/bitlbee_config/user.rb', line 6

def version
  @version
end

Class Method Details

.from_xml(xml) ⇒ BitlbeeConfig::User

Rubocop thinks this method is too long, but I think it’s more readable than split up rubocop:disable MethodLength

Parameters:

  • xml (Nokogiri::XML::Element)

    XML element to create user from

Returns:



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
# File 'lib/bitlbee_config/user.rb', line 14

def from_xml(xml)
  new_user = {}

  %w(nick password version).each do |att|
    new_user[att.to_sym] = xml.attributes[att].value
  end

  xml.xpath("setting").each do |setting|
    new_user[setting.attributes["name"].value] = setting.text
  end

  new_user[:channels] = xml.xpath("channel").collect do |item|
    BitlbeeConfig::Channel.from_xml(item)
  end

  new_user[:accounts] = xml.xpath("account").collect do |item|
    BitlbeeConfig::Account.from_xml(item)
  end

  user = BitlbeeConfig::User.new(new_user)

  user.accounts.each do ||
    .user = user
  end

  user
end

.username_to_filename(username) ⇒ String

XML configuration files are currently saved with the downcased username and an xml extension

Parameters:

  • username (String)

    Username to turn into file name

Returns:

  • (String)

    The file name of the this user’s configuration file



47
48
49
# File 'lib/bitlbee_config/user.rb', line 47

def username_to_filename(username)
  "#{ username.downcase }.xml"
end

Instance Method Details

#add_or_replace_account(new_account) ⇒ Object

Add an account. If an account with the same id already exists, it will be replaced

Parameters:



74
75
76
77
78
79
# File 'lib/bitlbee_config/user.rb', line 74

def ()
  @accounts.reject! { || .id == .id }

  .user = self
  @accounts << 
end

#build_xml(xml_builder) ⇒ Object

Parameters:

  • xml_builder (Nokogiri::XML::Builder)

    All XML will be added to this builder



101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/bitlbee_config/user.rb', line 101

def build_xml(xml_builder)
  regenerate_password_if_needed

  to_xml_with_options(xml_builder, nick: @nick, password: @password, version: @version) do |user_xml|
    @accounts.each do ||
      .build_xml(user_xml)
    end

    @channels.each do |channel|
      channel.build_xml(user_xml)
    end
  end
end

#regenerate_password_if_neededObject

If a cleartext password is given, hash it and set password attribute



96
97
98
# File 'lib/bitlbee_config/user.rb', line 96

def regenerate_password_if_needed
  @password = @cleartext_password.to_bitlbee_password_hash if @cleartext_password
end

#remove_account(account_to_remove) ⇒ Object

Remove an account

Parameters:



91
92
93
# File 'lib/bitlbee_config/user.rb', line 91

def ()
  (.id)
end

#remove_account_by_id(id_to_remove) ⇒ Object

Remove an account by its id

Parameters:

  • id_to_remove (String)

    The account with this ID will be removed



84
85
86
# File 'lib/bitlbee_config/user.rb', line 84

def (id_to_remove)
  @accounts.reject! { || .id == id_to_remove }
end