"Fossies" - the Fresh Open Source Software Archive

Member "imapfilter-2.8.2/src/set.lua" (26 Dec 2023, 15511 Bytes) of package /linux/privat/imapfilter-2.8.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Lua source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 -- A simple implementation of sets.
    2 
    3 Set = {}
    4 
    5 Set._mt = {}
    6 setmetatable(Set, Set._mt)
    7 
    8 
    9 function Set._new(self, values)
   10     local object
   11 
   12     object = values or {}
   13 
   14     object._type = 'set'
   15 
   16     for key, value in pairs(Set) do
   17         if type(value) == 'function' then object[key] = value end
   18     end
   19 
   20     object._mt = {}
   21     object._mt.__add = object._union
   22     object._mt.__mul = object._intersection
   23     object._mt.__sub = object._difference
   24     setmetatable(object, object._mt)
   25 
   26     return object
   27 end
   28 
   29 function Set._union(seta, setb)
   30     local set = Set()
   31     local t = {}
   32 
   33     for _, v in ipairs(seta) do
   34         b, m = table.unpack(v)
   35         if not t[b] then t[b] = {} end
   36         t[b][m] = true
   37     end
   38     for _, v in ipairs(setb) do
   39         b, m = table.unpack(v)
   40         if not t[b] then t[b] = {} end
   41         t[b][m] = true
   42     end
   43     for b in pairs(t) do
   44         for m in pairs(t[b]) do table.insert(set, { b, m }) end
   45     end
   46 
   47     return set
   48 end
   49 
   50 function Set._intersection(seta, setb)
   51     local set = Set()
   52     local ta = {}
   53     local tb = {}
   54 
   55     for _, v in ipairs(seta) do
   56         b, m = table.unpack(v)
   57         if not ta[b] then ta[b] = {} end
   58         ta[b][m] = true
   59     end
   60     for _, v in ipairs(setb) do
   61         b, m = table.unpack(v)
   62         if not tb[b] then tb[b] = {} end
   63         tb[b][m] = true
   64     end
   65     for b in pairs(ta) do
   66         if tb[b] then
   67             for m in pairs(ta[b]) do
   68                 if tb[b][m] then table.insert(set, { b, m }) end
   69             end
   70         end
   71     end
   72 
   73     return set
   74 end
   75 
   76 function Set._difference(seta, setb)
   77     local set = Set()
   78     local t = {}
   79 
   80     for _, v in ipairs(seta) do
   81         b, m = table.unpack(v)
   82         if not t[b] then t[b] = {} end
   83         t[b][m] = true
   84     end
   85     for _, v in ipairs(setb) do
   86         b, m = table.unpack(v)
   87         if t[b] then t[b][m] = nil end
   88     end
   89     for b in pairs(t) do
   90         for m in pairs(t[b]) do table.insert(set, { b, m }) end
   91     end
   92 
   93     return set
   94 end
   95 
   96 
   97 function Set.add_flags(self, flags)
   98     _check_required(flags, 'table')
   99 
  100     local r = true
  101     for mbox in pairs(_extract_mailboxes(self)) do
  102         if not mbox.add_flags(mbox, flags, self) then r = false end
  103     end
  104     return r
  105 end
  106 
  107 function Set.remove_flags(self, flags)
  108     _check_required(flags, 'table')
  109 
  110     local r = true
  111     for mbox in pairs(_extract_mailboxes(self)) do
  112         if not mbox.remove_flags(mbox, flags, self) then r = false end
  113     end
  114     return r
  115 end
  116 
  117 function Set.replace_flags(self, flags)
  118     _check_required(flags, 'table')
  119 
  120     local r = true
  121     for mbox in pairs(_extract_mailboxes(self)) do
  122         if not mbox.replace_flags(mbox, flags, self) then r = false end
  123     end
  124     return r
  125 end
  126 
  127 function Set.mark_answered(self)
  128     local r = true
  129     for mbox in pairs(_extract_mailboxes(self)) do
  130         if not mbox.mark_answered(mbox, self) then r = false end
  131     end
  132     return r
  133 end
  134 
  135 function Set.mark_deleted(self)
  136     local r = true
  137     for mbox in pairs(_extract_mailboxes(self)) do
  138         if not mbox.mark_deleted(mbox, self) then r = false end
  139     end
  140     return r
  141 end
  142 
  143 function Set.mark_draft(self)
  144     local r = true
  145     for mbox in pairs(_extract_mailboxes(self)) do
  146         if not mbox.mark_draft(mbox, self) then r = false end
  147     end
  148     return r
  149 end
  150 
  151 function Set.mark_flagged(self)
  152     local r = true
  153     for mbox in pairs(_extract_mailboxes(self)) do
  154         if not mbox.mark_flagged(mbox, self) then r = false end
  155     end
  156     return r
  157 end
  158 
  159 function Set.mark_seen(self)
  160     local r = true
  161     for mbox in pairs(_extract_mailboxes(self)) do
  162         if not mbox.mark_seen(mbox, self) then r = false end
  163     end
  164     return r
  165 end
  166 
  167 function Set.unmark_answered(self)
  168     local r = true
  169     for mbox in pairs(_extract_mailboxes(self)) do
  170         if not mbox.unmark_answered(mbox, self) then r = false end
  171     end
  172     return r
  173 end
  174 
  175 function Set.unmark_deleted(self)
  176     local r = true
  177     for mbox in pairs(_extract_mailboxes(self)) do
  178         if not mbox.unmark_deleted(mbox, self) then r = false end
  179     end
  180     return r
  181 end
  182 
  183 function Set.unmark_draft(self)
  184     local r = true
  185     for mbox in pairs(_extract_mailboxes(self)) do
  186         if not mbox.unmark_draft(mbox, self) then r = false end
  187     end
  188     return r
  189 end
  190 
  191 function Set.unmark_flagged(self)
  192     local r = true
  193     for mbox in pairs(_extract_mailboxes(self)) do
  194         if not mbox.unmark_flagged(mbox, self) then r = false end
  195     end
  196     return r
  197 end
  198 
  199 function Set.unmark_seen(self)
  200     local r = true
  201     for mbox in pairs(_extract_mailboxes(self)) do
  202         if not mbox.unmark_seen(mbox, self) then r = false end
  203     end
  204     return r
  205 end
  206 
  207 function Set.delete_messages(self)
  208     local r = true
  209     for mbox in pairs(_extract_mailboxes(self)) do
  210         if not mbox.delete_messages(mbox, self) then r = false end
  211     end
  212     return r
  213 end
  214 
  215 function Set.copy_messages(self, dest)
  216     _check_required(dest, 'table')
  217 
  218     local r = true
  219     for mbox in pairs(_extract_mailboxes(self)) do
  220         if not mbox.copy_messages(mbox, dest, self) then r = false end
  221     end
  222     return r
  223 end
  224 
  225 function Set.move_messages(self, dest)
  226     _check_required(dest, 'table')
  227 
  228     local r = true
  229     for mbox in pairs(_extract_mailboxes(self)) do
  230         if not mbox.move_messages(mbox, dest, self) then r = false end
  231     end
  232     return r
  233 end
  234 
  235 
  236 function Set.select_all(self)
  237     local set = Set()
  238     for mbox in pairs(_extract_mailboxes(self)) do
  239         set = set + mbox.select_all(mbox)
  240     end
  241     return self * set
  242 end
  243 
  244 function Set.send_query(self, criteria)
  245     local set = Set()
  246     for mbox in pairs(_extract_mailboxes(self)) do
  247         set = set + mbox.send_query(mbox, criteria, self)
  248     end
  249     return self * set
  250 end
  251 
  252 function Set.is_answered(self)
  253     local set = Set()
  254     for mbox in pairs(_extract_mailboxes(self)) do
  255         set = set + mbox.is_answered(mbox, self)
  256     end
  257     return self * set
  258 end
  259 
  260 function Set.is_deleted(self)
  261     local set = Set()
  262     for mbox in pairs(_extract_mailboxes(self)) do
  263         set = set + mbox.is_deleted(mbox, self)
  264     end
  265     return self * set
  266 end
  267 
  268 function Set.is_draft(self)
  269     local set = Set()
  270     for mbox in pairs(_extract_mailboxes(self)) do
  271         set = set + mbox.is_draft(mbox, self)
  272     end
  273     return self * set
  274 end
  275 
  276 function Set.is_flagged(self)
  277     local set = Set()
  278     for mbox in pairs(_extract_mailboxes(self)) do
  279         set = set + mbox.is_flagged(mbox, self)
  280     end
  281     return self * set
  282 end
  283 
  284 function Set.is_new(self)
  285     local set = Set()
  286     for mbox in pairs(_extract_mailboxes(self)) do
  287         set = set + mbox.is_new(mbox, self)
  288     end
  289     return self * set
  290 end
  291 
  292 function Set.is_old(self)
  293     local set = Set()
  294     for mbox in pairs(_extract_mailboxes(self)) do
  295         set = set + mbox.is_old(mbox, self)
  296     end
  297     return self * set
  298 end
  299 
  300 function Set.is_recent(self)
  301     local set = Set()
  302     for mbox in pairs(_extract_mailboxes(self)) do
  303         set = set + mbox.is_recent(mbox, self)
  304     end
  305     return self * set
  306 end
  307 
  308 function Set.is_seen(self)
  309     local set = Set()
  310     for mbox in pairs(_extract_mailboxes(self)) do
  311         set = set + mbox.is_seen(mbox, self)
  312     end
  313     return self * set
  314 end
  315 
  316 function Set.is_unanswered(self)
  317     local set = Set()
  318     for mbox in pairs(_extract_mailboxes(self)) do
  319         set = set + mbox.is_unanswered(mbox, self)
  320     end
  321     return self * set
  322 end
  323 
  324 function Set.is_undeleted(self)
  325     local set = Set()
  326     for mbox in pairs(_extract_mailboxes(self)) do
  327         set = set + mbox.is_undeleted(mbox, self)
  328     end
  329     return self * set
  330 end
  331 
  332 function Set.is_undraft(self)
  333     local set = Set()
  334     for mbox in pairs(_extract_mailboxes(self)) do
  335         set = set + mbox.is_undraft(mbox, self)
  336     end
  337     return self * set
  338 end
  339 
  340 function Set.is_unflagged(self)
  341     local set = Set()
  342     for mbox in pairs(_extract_mailboxes(self)) do
  343         set = set + mbox.is_unflagged(mbox, self)
  344     end
  345     return self * set
  346 end
  347 
  348 function Set.is_unseen(self)
  349     local set = Set()
  350     for mbox in pairs(_extract_mailboxes(self)) do
  351         set = set + mbox.is_unseen(mbox, self)
  352     end
  353     return self * set
  354 end
  355 
  356 
  357 function Set.has_keyword(self, flag)
  358     _check_required(flag, 'string')
  359 
  360     local set = Set()
  361     for mbox in pairs(_extract_mailboxes(self)) do
  362         set = set + mbox.has_keyword(mbox, flag, self)
  363     end
  364     return self * set
  365 end
  366 
  367 Set.has_flag = Set.has_keyword
  368 
  369 function Set.has_unkeyword(self, flag)
  370     _check_required(flag, 'string')
  371 
  372     local set = Set()
  373     for mbox in pairs(_extract_mailboxes(self)) do
  374         set = set + mbox.has_unkeyword(mbox, flag, self)
  375     end
  376     return self * set
  377 end
  378 
  379 
  380 function Set.is_larger(self, size)
  381     _check_required(size, 'number')
  382 
  383     local set = Set()
  384     for mbox in pairs(_extract_mailboxes(self)) do
  385         set = set + mbox.is_larger(mbox, size, self)
  386     end
  387     return self * set
  388 end
  389 
  390 function Set.is_smaller(self, size)
  391     _check_required(size, 'number')
  392 
  393     local set = Set()
  394     for mbox in pairs(_extract_mailboxes(self)) do
  395         set = set + mbox.is_smaller(mbox, size, self)
  396     end
  397     return self * set
  398 end
  399 
  400 
  401 function Set.arrived_on(self, date)
  402     _check_required(date, 'string')
  403 
  404     local set = Set()
  405     for mbox in pairs(_extract_mailboxes(self)) do
  406         set = set + mbox.arrived_on(mbox, date, self)
  407     end
  408     return self * set
  409 end
  410 
  411 function Set.arrived_before(self, date)
  412     _check_required(date, 'string')
  413 
  414     local set = Set()
  415     for mbox in pairs(_extract_mailboxes(self)) do
  416         set = set + mbox.arrived_before(mbox, date, self)
  417     end
  418     return self * set
  419 end
  420 
  421 function Set.arrived_since(self, date)
  422     _check_required(date, 'string')
  423 
  424     local set = Set()
  425     for mbox in pairs(_extract_mailboxes(self)) do
  426         set = set + mbox.arrived_since(mbox, date, self)
  427     end
  428     return self * set
  429 end
  430 
  431 function Set.sent_on(self, date)
  432     _check_required(date, 'string')
  433 
  434     local set = Set()
  435     for mbox in pairs(_extract_mailboxes(self)) do
  436         set = set + mbox.sent_on(mbox, date, self)
  437     end
  438     return self * set
  439 end
  440 
  441 function Set.sent_before(self, date)
  442     _check_required(date, 'string')
  443 
  444     local set = Set()
  445     for mbox in pairs(_extract_mailboxes(self)) do
  446         set = set + mbox.sent_before(mbox, date, self)
  447     end
  448     return self * set
  449 end
  450 
  451 function Set.sent_since(self, date)
  452     _check_required(date, 'string')
  453 
  454     local set = Set()
  455     for mbox in pairs(_extract_mailboxes(self)) do
  456         set = set + mbox.sent_since(mbox, date, self)
  457     end
  458     return self * set
  459 end
  460 
  461 function Set.is_newer(self, days)
  462     _check_required(days, 'number')
  463 
  464     local set = Set()
  465     for mbox in pairs(_extract_mailboxes(self)) do
  466         set = set + mbox.is_newer(mbox, days, self)
  467     end
  468     return self * set
  469 end
  470 
  471 function Set.is_older(self, days)
  472     _check_required(days, 'number')
  473 
  474     local set = Set()
  475     for mbox in pairs(_extract_mailboxes(self)) do
  476         set = set + mbox.is_older(mbox, days, self)
  477     end
  478     return self * set
  479 end
  480 
  481 
  482 function Set.contain_field(self, field, string)
  483     _check_required(field, 'string')
  484     _check_required(string, 'string')
  485 
  486     local set = Set()
  487     for mbox in pairs(_extract_mailboxes(self)) do
  488         set = set + mbox.contain_field(mbox, field, string, self)
  489     end
  490     return self * set
  491 end
  492 
  493 function Set.contain_bcc(self, string)
  494     _check_required(string, 'string')
  495 
  496     local set = Set()
  497     for mbox in pairs(_extract_mailboxes(self)) do
  498         set = set + mbox.contain_bcc(mbox, string, self)
  499     end
  500     return self * set
  501 end
  502 
  503 function Set.contain_cc(self, string)
  504     _check_required(string, 'string')
  505 
  506     local set = Set()
  507     for mbox in pairs(_extract_mailboxes(self)) do
  508         set = set + mbox.contain_cc(mbox, string, self)
  509     end
  510     return self * set
  511 end
  512 
  513 function Set.contain_from(self, string)
  514     _check_required(string, 'string')
  515 
  516     local set = Set()
  517     for mbox in pairs(_extract_mailboxes(self)) do
  518         set = set + mbox.contain_from(mbox, string, self)
  519     end
  520     return self * set
  521 end
  522 
  523 function Set.contain_subject(self, string)
  524     _check_required(string, 'string')
  525 
  526     local set = Set()
  527     for mbox in pairs(_extract_mailboxes(self)) do
  528         set = set + mbox.contain_subject(mbox, string, self)
  529     end
  530     return self * set
  531 end
  532 
  533 function Set.contain_to(self, string)
  534     _check_required(string, 'string')
  535 
  536     local set = Set()
  537     for mbox in pairs(_extract_mailboxes(self)) do
  538         set = set + mbox.contain_to(mbox, string, self)
  539     end
  540     return self * set
  541 end
  542 
  543 function Set.contain_header(self, string)
  544     _check_required(string, 'string')
  545 
  546     local set = Set()
  547     for mbox in pairs(_extract_mailboxes(self)) do
  548         set = set + mbox.contain_header(mbox, string, self)
  549     end
  550     return self * set
  551 end
  552 
  553 function Set.contain_body(self, string)
  554     _check_required(string, 'string')
  555 
  556     local set = Set()
  557     for mbox in pairs(_extract_mailboxes(self)) do
  558         set = set + mbox.contain_body(mbox, string, self)
  559     end
  560     return self * set
  561 end
  562 
  563 function Set.contain_message(self, string)
  564     _check_required(string, 'string')
  565 
  566     local set = Set()
  567     for mbox in pairs(_extract_mailboxes(self)) do
  568         set = set + mbox.contain_message(mbox, string, self)
  569     end
  570     return self * set
  571 end
  572 
  573 function Set.match_bcc(self, pattern)
  574     _check_required(pattern, 'string')
  575 
  576     local set = Set()
  577     for mbox in pairs(_extract_mailboxes(self)) do
  578         set = set + mbox.match_bcc(mbox, pattern, self)
  579     end
  580     return self * set
  581 end
  582 
  583 function Set.match_cc(self, pattern)
  584     _check_required(pattern, 'string')
  585 
  586     local set = Set()
  587     for mbox in pairs(_extract_mailboxes(self)) do
  588         set = set + mbox.match_cc(mbox, pattern, self)
  589     end
  590     return self * set
  591 end
  592 
  593 function Set.match_from(self, pattern)
  594     _check_required(pattern, 'string')
  595 
  596     local set = Set()
  597     for mbox in pairs(_extract_mailboxes(self)) do
  598         set = set + mbox.match_from(mbox, pattern, self)
  599     end
  600     return self * set
  601 end
  602 
  603 function Set.match_subject(self, pattern)
  604     _check_required(pattern, 'string')
  605 
  606     local set = Set()
  607     for mbox in pairs(_extract_mailboxes(self)) do
  608         set = set + mbox.match_subject(mbox, pattern, self)
  609     end
  610     return self * set
  611 end
  612 
  613 function Set.match_to(self, pattern)
  614     _check_required(pattern, 'string')
  615 
  616     local set = Set()
  617     for mbox in pairs(_extract_mailboxes(self)) do
  618         set = set + mbox.match_to(mbox, pattern, self)
  619     end
  620     return self * set
  621 end
  622 
  623 function Set.match_field(self, field, pattern)
  624     _check_required(field, 'string')
  625     _check_required(pattern, 'string')
  626 
  627     local set = Set()
  628     for mbox in pairs(_extract_mailboxes(self)) do
  629         set = set + mbox.match_field(mbox, field, pattern, self)
  630     end
  631     return self * set
  632 end
  633 
  634 function Set.match_header(self, pattern)
  635     _check_required(pattern, 'string')
  636 
  637     local set = Set()
  638     for mbox in pairs(_extract_mailboxes(self)) do
  639         set = set + mbox.match_header(mbox, pattern, self)
  640     end
  641     return self * set
  642 end
  643 
  644 function Set.match_body(self, pattern)
  645     _check_required(pattern, 'string')
  646 
  647     local set = Set()
  648     for mbox in pairs(_extract_mailboxes(self)) do
  649         set = set + mbox.match_body(mbox, pattern, self)
  650     end
  651     return self * set
  652 end
  653 
  654 function Set.match_message(self, pattern)
  655     _check_required(pattern, 'string')
  656 
  657     local set = Set()
  658     for mbox in pairs(_extract_mailboxes(self)) do
  659         set = set + mbox.match_message(mbox, pattern, self)
  660     end
  661     return self * set
  662 end
  663 
  664 
  665 Set._mt.__call = Set._new