Class: Yast::MailAliasesClass

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

Instance Method Summary collapse

Instance Method Details

#FilterRootAliasObject

Separates aliases into aliases, root_alias and root_alias_comment


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

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

#GetRootAliasObject

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


182
183
184
185
186
# File 'library/general/src/modules/MailAliases.rb', line 182

def GetRootAlias
  return "" if !ReadAliases()

  @root_alias
end

#mainObject


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

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

#MergeRootAlias(aliases) ⇒ Object

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


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

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

#mergeTables(new, old) ⇒ Object

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


121
122
123
124
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
# File 'library/general/src/modules/MailAliases.rb', line 121

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

#ReadAliasesObject

Read the aliases table (and separate the root alias)

Returns:

  • success?


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

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

#SetRootAlias(destinations) ⇒ Object

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


195
196
197
198
199
200
201
202
203
204
205
206
# File 'library/general/src/modules/MailAliases.rb', line 195

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

#WriteAliasesObject

Part of Write.

Returns:

  • success

See Also:


165
166
167
168
169
170
171
172
173
174
175
# File 'library/general/src/modules/MailAliases.rb', line 165

def WriteAliases
  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
  MailTable.Write("aliases", a_raw)
  true
end