Class: Yast::MailAliasesClass

Inherits:
Module
  • Object
show all
Defined in:
../../library/general/src/modules/MailAliases.rb

Instance Method Summary (collapse)

Instance Method Details

- (Object) FilterRootAlias

Separates aliases into aliases, root_alias and root_alias_comment



69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File '../../library/general/src/modules/MailAliases.rb', line 69

def FilterRootAlias
  @root_alias = ""
  @root_alias_comment = ""
  @aliases = Builtins.filter(@aliases) do |e|
    if Ops.get_string(e, "alias", "") == "root"
      @root_alias = Ops.get_string(e, "destinations", "")
      @root_alias_comment = Ops.get_string(e, "comment", "")
      next false
    end
    true
  end

  nil
end

- (Object) GetRootAlias

For use by the Users package. Does not rely on the internal state, first calls the agent.



193
194
195
196
# File '../../library/general/src/modules/MailAliases.rb', line 193

def GetRootAlias
  return "" if !ReadAliases()
  @root_alias
end

- (Object) main



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File '../../library/general/src/modules/MailAliases.rb', line 47

def main
  # no translatable strings, no textdomain.
  Yast.import "MailTable"

  # ----------------------------------------------------------------

  # List of maps: $[comment:, alias:, destinations:] (all are strings)
  # Except root.
  @aliases = []
  # Separated/joined with aliases by read/write/set/export
  @root_alias = ""
  # Separated/joined with aliases by read/write/set/export
  @root_alias_comment = ""
end

- (Object) MergeRootAlias(aliases)

Returns prepend root alias data to aliases, if set

Parameters:

  • aliases (Array<Hash>)

    an alias table

Returns:

  • prepend root alias data to aliases, if set



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File '../../library/general/src/modules/MailAliases.rb', line 101

def MergeRootAlias(aliases)
  aliases = deep_copy(aliases)
  ret = deep_copy(aliases)
  if @root_alias != ""
    ret = Builtins.prepend(
      ret,
      {
        "alias"        => "root",
        "destinations" => @root_alias,
        "comment"      => @root_alias_comment
      }
    )
  end
  deep_copy(ret)
end

- (Object) mergeTables(new, old)

Merges mail tables, which are order-preserving maps. First are the entries of the old map, with values updated from the new one, then the rest of the new map.

Parameters:

  • new (Array<Hash>)

    new table

  • old (Array<Hash>)

    old table

Returns:

  • merged table



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File '../../library/general/src/modules/MailAliases.rb', line 125

def mergeTables(new, old)
  new = deep_copy(new)
  old = deep_copy(old)
  # make a true map
  new_m = Builtins.listmap(new) do |e|
    {
      Ops.get_string(e, "key", "") => [
        Ops.get_string(e, "comment", ""),
        Ops.get_string(e, "value", "")
      ]
    }
  end
  # update old values
  replaced = Builtins.maplist(old) do |e|
    k = Ops.get_string(e, "key", "")
    cv = Ops.get_list(new_m, k, [])
    if Ops.greater_than(Builtins.size(cv), 0)
      Ops.set(new_m, k, []) # ok, newly constructed
      next {
        "key"     => k,
        "comment" => Ops.get_string(cv, 0, ""),
        "value"   => Ops.get_string(cv, 1, "")
      }
    else
      next {
        "key"     => k,
        "comment" => Ops.get_string(e, "comment", ""),
        "value"   => Ops.get_string(e, "value", "")
      }
    end
  end
  # remove already updated values
  filtered = Builtins.filter(new) do |e|
    Ops.greater_than(
      Builtins.size(Ops.get_list(new_m, Ops.get_string(e, "key", ""), [])),
      0
    )
  end
  Builtins.flatten([replaced, filtered])
end

- (Object) ReadAliases

Read the aliases table (and separate the root alias)

Returns:

  • success?



86
87
88
89
90
91
92
93
94
95
96
97
# File '../../library/general/src/modules/MailAliases.rb', line 86

def ReadAliases
  a_raw = MailTable.Read("aliases")
  @aliases = Builtins.maplist(a_raw) do |e|
    {
      "comment"      => Ops.get_string(e, "comment", ""),
      "alias"        => Ops.get_string(e, "key", ""),
      "destinations" => Ops.get_string(e, "value", "")
    }
  end
  FilterRootAlias()
  true
end

- (Object) SetRootAlias(destinations)

For use by the Users package. Does not use the internal state, just calls the agent. SuSEconfig or newaliases is NOT called! (TODO: what if it is called while the main module is running?) Errors are reported via Report::Error.

Parameters:

  • destinations (String)

    The new alias. If “”, it is removed.

Returns:

  • true on success



206
207
208
209
210
211
212
213
214
215
216
# File '../../library/general/src/modules/MailAliases.rb', line 206

def SetRootAlias(destinations)
  return false if !ReadAliases()

  @root_alias = destinations
  @root_alias_comment = "" # TODO: "created by the ... yast2 module"?

  return false if !WriteAliases()

  return false if !MailTable.Flush("aliases")
  true
end

- (Object) WriteAliases

Part of Write.

Returns:

  • success

See Also:



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File '../../library/general/src/modules/MailAliases.rb', line 169

def WriteAliases
  # aliases
  alias_path = "aliases"
  a_raw = Builtins.maplist(MergeRootAlias(@aliases)) do |e|
    {
      "comment" => Ops.get_string(e, "comment", ""),
      "key"     => Ops.get_string(e, "alias", ""),
      "value"   => Ops.get_string(e, "destinations", "")
    }
  end
  #	if (merge_aliases)
  #	{
  #	    a_raw = mergeTables (a_raw, MailTable::Read (alias_path));
  #	}
  MailTable.Write("aliases", a_raw)
  true
end