"Fossies" - the Fresh Open Source Software Archive

Member "imapfilter-2.8.2/src/core.c" (26 Dec 2023, 19083 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) C and C++ 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. For more information about "core.c" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 2.8.0_vs_2.8.1.

    1 #include <stdio.h>
    2 
    3 #include <lua.h>
    4 #include <lauxlib.h>
    5 #include <lualib.h>
    6 
    7 #include "imapfilter.h"
    8 #include "session.h"
    9 
   10 
   11 static int ifcore_noop(lua_State *lua);
   12 static int ifcore_login(lua_State *lua);
   13 static int ifcore_logout(lua_State *lua);
   14 static int ifcore_status(lua_State *lua);
   15 static int ifcore_select(lua_State *lua);
   16 static int ifcore_close(lua_State *lua);
   17 static int ifcore_expunge(lua_State *lua);
   18 static int ifcore_search(lua_State *lua);
   19 static int ifcore_list(lua_State *lua);
   20 static int ifcore_lsub(lua_State *lua);
   21 static int ifcore_fetchfast(lua_State *lua);
   22 static int ifcore_fetchflags(lua_State *lua);
   23 static int ifcore_fetchdate(lua_State *lua);
   24 static int ifcore_fetchsize(lua_State *lua);
   25 static int ifcore_fetchheader(lua_State *lua);
   26 static int ifcore_fetchtext(lua_State *lua);
   27 static int ifcore_fetchfields(lua_State *lua);
   28 static int ifcore_fetchstructure(lua_State *lua);
   29 static int ifcore_fetchpart(lua_State *lua);
   30 static int ifcore_store(lua_State *lua);
   31 static int ifcore_copy(lua_State *lua);
   32 static int ifcore_append(lua_State *lua);
   33 static int ifcore_create(lua_State *lua);
   34 static int ifcore_delete(lua_State *lua);
   35 static int ifcore_rename(lua_State *lua);
   36 static int ifcore_subscribe(lua_State *lua);
   37 static int ifcore_unsubscribe(lua_State *lua);
   38 static int ifcore_idle(lua_State *lua);
   39 
   40 
   41 /* Lua imapfilter core library functions. */
   42 static const luaL_Reg ifcorelib[] = {
   43     { "noop", ifcore_noop },
   44     { "logout", ifcore_logout },
   45     { "login", ifcore_login },
   46     { "select", ifcore_select },
   47     { "create", ifcore_create },
   48     { "delete", ifcore_delete },
   49     { "rename", ifcore_rename },
   50     { "subscribe", ifcore_subscribe },
   51     { "unsubscribe", ifcore_unsubscribe },
   52     { "list", ifcore_list },
   53     { "lsub", ifcore_lsub },
   54     { "status", ifcore_status },
   55     { "append", ifcore_append },
   56     { "close", ifcore_close },
   57     { "expunge", ifcore_expunge },
   58     { "search", ifcore_search },
   59     { "fetchfast", ifcore_fetchfast },
   60     { "fetchflags", ifcore_fetchflags },
   61     { "fetchdate", ifcore_fetchdate },
   62     { "fetchsize", ifcore_fetchsize },
   63 
   64     /*
   65      * RFC 822: message == header + body
   66      * RFC 3501: body == header + text
   67      *
   68      * RFC 3501 notation is used internally, and RFC 822 notation is used
   69      * for the interface available to the user.
   70      */
   71     { "fetchheader", ifcore_fetchheader },
   72     { "fetchbody", ifcore_fetchtext },
   73 
   74     { "fetchfields", ifcore_fetchfields },
   75     { "fetchstructure", ifcore_fetchstructure },
   76     { "fetchpart", ifcore_fetchpart },
   77     { "store", ifcore_store },
   78     { "copy", ifcore_copy },
   79     { "idle", ifcore_idle },
   80     { NULL, NULL }
   81 };
   82 
   83 
   84 /*
   85  * Core function to reset any inactivity autologout timer on the server.
   86  */
   87 static int
   88 ifcore_noop(lua_State *lua)
   89 {
   90     int r;
   91 
   92     if (lua_gettop(lua) != 1)
   93         luaL_error(lua, "wrong number of arguments");
   94     luaL_checktype(lua, 1, LUA_TLIGHTUSERDATA);
   95 
   96     r = request_noop((session *)(lua_topointer(lua, 1)));
   97 
   98     lua_pop(lua, 1);
   99     
  100     if (r < 0)
  101         return 0;
  102 
  103     lua_pushboolean(lua, (r == STATUS_OK));
  104 
  105     return 1;
  106 }
  107 
  108 
  109 /*
  110  * Core function to login to the server.
  111  */
  112 static int
  113 ifcore_login(lua_State *lua)
  114 {
  115     session *s = NULL;
  116     int t, r;
  117 
  118     if (lua_gettop(lua) != 6)
  119         luaL_error(lua, "wrong number of arguments");
  120     luaL_checktype(lua, 1, LUA_TSTRING);
  121     luaL_checktype(lua, 2, LUA_TSTRING);
  122     t = lua_type(lua, 3);
  123     luaL_argcheck(lua, t == LUA_TSTRING || t == LUA_TNIL, 3,
  124         "string or nil expected");
  125     t = lua_type(lua, 4);
  126     luaL_argcheck(lua, t == LUA_TSTRING || t == LUA_TNIL, 4,
  127         "string or nil expected");
  128     t = lua_type(lua, 5);
  129     luaL_argcheck(lua, t == LUA_TSTRING || t == LUA_TNIL, 5,
  130         "string or nil expected");
  131     t = lua_type(lua, 6);
  132     luaL_argcheck(lua, t == LUA_TSTRING || t == LUA_TNIL, 6,
  133         "string or nil expected");
  134 
  135     r = request_login(&s, lua_tostring(lua, 1), lua_tostring(lua, 2),
  136         lua_tostring(lua, 3), lua_tostring(lua, 4), lua_tostring(lua, 5),
  137         lua_tostring(lua, 6));
  138 
  139     lua_pop(lua, 5);
  140 
  141     if (r < 0)
  142         return 0;
  143     if (r != STATUS_OK && r != STATUS_PREAUTH) {
  144         lua_pushboolean(lua, 0);
  145         return 1;
  146     }
  147 
  148     lua_pushboolean(lua, 1);
  149     lua_pushlightuserdata(lua, (void *)(s));
  150 
  151     return 2;
  152 }
  153 
  154 
  155 /*
  156  * Core function to logout from the server.
  157  */
  158 static int
  159 ifcore_logout(lua_State *lua)
  160 {
  161     int r;
  162 
  163     if (lua_gettop(lua) != 1)
  164         luaL_error(lua, "wrong number of arguments");
  165     luaL_checktype(lua, 1, LUA_TLIGHTUSERDATA);
  166 
  167     r = request_logout((session *)(lua_topointer(lua, 1)));
  168 
  169     lua_pop(lua, 1);
  170 
  171     if (r < 0)
  172         return 0;
  173 
  174     lua_pushboolean(lua, (r == STATUS_OK));
  175 
  176     return 1;
  177 }
  178 
  179 
  180 /*
  181  * Core function to get the status of a mailbox.
  182  */
  183 static int
  184 ifcore_status(lua_State *lua)
  185 {
  186     int r;
  187     unsigned int exists, recent, unseen, uidnext;
  188 
  189     exists = recent = unseen = uidnext = -1;
  190 
  191     if (lua_gettop(lua) != 2)
  192         luaL_error(lua, "wrong number of arguments");
  193     luaL_checktype(lua, 1, LUA_TLIGHTUSERDATA);
  194     luaL_checktype(lua, 2, LUA_TSTRING);
  195 
  196     r = request_status((session *)(lua_topointer(lua, 1)),
  197         lua_tostring(lua, 2), &exists, &recent, &unseen, &uidnext);
  198 
  199     lua_pop(lua, 2);
  200 
  201     if (r < 0)
  202         return 0;
  203 
  204     lua_pushboolean(lua, (r == STATUS_OK));
  205     lua_pushinteger(lua, (lua_Integer) (exists));
  206     lua_pushinteger(lua, (lua_Integer) (recent));
  207     lua_pushinteger(lua, (lua_Integer) (unseen));
  208     lua_pushinteger(lua, (lua_Integer) (uidnext));
  209 
  210     return 5;
  211 }
  212 
  213 
  214 /*
  215  * Core function to select a mailbox.
  216  */
  217 static int
  218 ifcore_select(lua_State *lua)
  219 {
  220     int r;
  221 
  222     if (lua_gettop(lua) != 2)
  223         luaL_error(lua, "wrong number of arguments");
  224     luaL_checktype(lua, 1, LUA_TLIGHTUSERDATA);
  225     luaL_checktype(lua, 2, LUA_TSTRING);
  226 
  227     r = request_select((session *)(lua_topointer(lua, 1)),
  228         lua_tostring(lua, 2));
  229 
  230     lua_pop(lua, 2);
  231 
  232     if (r < 0)
  233         return 0;
  234 
  235     lua_pushboolean(lua, (r == STATUS_OK || r == STATUS_READONLY));
  236     lua_pushboolean(lua, (r == STATUS_READONLY));
  237 
  238     return 2;
  239 }
  240 
  241 
  242 /*
  243  * Core function to close a mailbox.
  244  */
  245 static int
  246 ifcore_close(lua_State *lua)
  247 {
  248     int r;
  249 
  250     if (lua_gettop(lua) != 1)
  251         luaL_error(lua, "wrong number of arguments");
  252     luaL_checktype(lua, 1, LUA_TLIGHTUSERDATA);
  253 
  254     r = request_close((session *)(lua_topointer(lua, 1)));
  255 
  256     lua_pop(lua, 1);
  257 
  258     if (r < 0)
  259         return 0;
  260 
  261     lua_pushboolean(lua, (r == STATUS_OK));
  262 
  263     return 1;
  264 }
  265 
  266 
  267 /*
  268  * Core function to expunge a mailbox.
  269  */
  270 static int
  271 ifcore_expunge(lua_State *lua)
  272 {
  273     int r;
  274 
  275     if (lua_gettop(lua) != 1)
  276         luaL_error(lua, "wrong number of arguments");
  277     luaL_checktype(lua, 1, LUA_TLIGHTUSERDATA);
  278 
  279     r = request_expunge((session *)(lua_topointer(lua, 1)));
  280 
  281     lua_pop(lua, 1);
  282 
  283     if (r < 0)
  284         return 0;
  285 
  286     lua_pushboolean(lua, (r == STATUS_OK));
  287 
  288     return 1;
  289 }
  290 
  291 
  292 /*
  293  * Core function to list available mailboxes.
  294  */
  295 static int
  296 ifcore_list(lua_State *lua)
  297 {
  298     int r;
  299     char *mboxs, *folders;
  300 
  301     mboxs = folders = NULL;
  302 
  303     if (lua_gettop(lua) != 3)
  304         luaL_error(lua, "wrong number of arguments");
  305     luaL_checktype(lua, 1, LUA_TLIGHTUSERDATA);
  306     luaL_checktype(lua, 2, LUA_TSTRING);
  307     luaL_checktype(lua, 3, LUA_TSTRING);
  308 
  309     r = request_list((session *)(lua_topointer(lua, 1)),
  310         lua_tostring(lua, 2), lua_tostring(lua, 3), &mboxs, &folders);
  311 
  312     lua_pop(lua, 3);
  313 
  314     if (r < 0)
  315         return 0;
  316 
  317     lua_pushboolean(lua, (r == STATUS_OK));
  318 
  319     if (!mboxs && !folders)
  320         return 1;
  321 
  322     lua_pushstring(lua, mboxs);
  323     lua_pushstring(lua, folders);
  324 
  325     xfree(mboxs);
  326     xfree(folders);
  327 
  328     return 3;
  329 }
  330 
  331 
  332 /*
  333  * Core function to list subscribed mailboxes.
  334  */
  335 static int
  336 ifcore_lsub(lua_State *lua)
  337 {
  338     int r;
  339     char *mboxs, *folders;
  340 
  341     mboxs = folders = NULL;
  342 
  343     if (lua_gettop(lua) != 3)
  344         luaL_error(lua, "wrong number of arguments");
  345     luaL_checktype(lua, 1, LUA_TLIGHTUSERDATA);
  346     luaL_checktype(lua, 2, LUA_TSTRING);
  347     luaL_checktype(lua, 3, LUA_TSTRING);
  348 
  349     r = request_lsub((session *)(lua_topointer(lua, 1)),
  350         lua_tostring(lua, 2), lua_tostring(lua, 3), &mboxs, &folders);
  351 
  352     lua_pop(lua, 3);
  353 
  354     if (r < 0)
  355         return 0;
  356 
  357     lua_pushboolean(lua, (r == STATUS_OK));
  358 
  359     if (!mboxs)
  360         return 1;
  361 
  362     lua_pushstring(lua, mboxs);
  363     lua_pushstring(lua, folders);
  364 
  365     xfree(mboxs);
  366 
  367     return 3;
  368 }
  369 
  370 
  371 /*
  372  * Core function to search the messages of a mailbox.
  373  */
  374 static int
  375 ifcore_search(lua_State *lua)
  376 {
  377     int r;
  378     char *mesgs;
  379 
  380     mesgs = NULL;
  381 
  382     if (lua_gettop(lua) != 3)
  383         luaL_error(lua, "wrong number of arguments");
  384     luaL_checktype(lua, 1, LUA_TLIGHTUSERDATA);
  385     luaL_checktype(lua, 2, LUA_TSTRING);
  386     luaL_checktype(lua, 3, LUA_TSTRING);
  387 
  388     r = request_search((session *)(lua_topointer(lua, 1)),
  389         lua_tostring(lua, 2), lua_tostring(lua, 3), &mesgs);
  390 
  391     lua_pop(lua, 3);
  392 
  393     if (r < 0)
  394         return 0;
  395 
  396     lua_pushboolean(lua, (r == STATUS_OK));
  397 
  398     if (!mesgs)
  399         return 1;
  400 
  401     lua_pushstring(lua, mesgs);
  402 
  403     xfree(mesgs);
  404 
  405     return 2;
  406 }
  407 
  408 
  409 /*
  410  * Core function to fetch message information (flags, date, size).
  411  */
  412 static int
  413 ifcore_fetchfast(lua_State *lua)
  414 {
  415     int r;
  416     char *flags, *date, *size;
  417 
  418     flags = date = size = NULL;
  419 
  420     if (lua_gettop(lua) != 2)
  421         luaL_error(lua, "wrong number of arguments");
  422     luaL_checktype(lua, 1, LUA_TLIGHTUSERDATA);
  423     luaL_checktype(lua, 2, LUA_TSTRING);
  424 
  425     r = request_fetchfast((session *)(lua_topointer(lua, 1)),
  426         lua_tostring(lua, 2), &flags, &date, &size);
  427 
  428     lua_pop(lua, 2);
  429 
  430     if (r < 0)
  431         return 0;
  432 
  433     lua_pushboolean(lua, (r == STATUS_OK));
  434 
  435     if (!flags || !date || !size)
  436         return 1;
  437 
  438     lua_pushstring(lua, flags);
  439     lua_pushstring(lua, date);
  440     lua_pushstring(lua, size);
  441 
  442     xfree(flags);
  443     xfree(date);
  444     xfree(size);
  445 
  446     return 4;
  447 }
  448 
  449 
  450 /*
  451  * Core function to fetch message flags.
  452  */
  453 static int
  454 ifcore_fetchflags(lua_State *lua)
  455 {
  456     int r;
  457     char *flags;
  458 
  459     flags = NULL;
  460 
  461     if (lua_gettop(lua) != 2)
  462         luaL_error(lua, "wrong number of arguments");
  463     luaL_checktype(lua, 1, LUA_TLIGHTUSERDATA);
  464     luaL_checktype(lua, 2, LUA_TSTRING);
  465 
  466     r = request_fetchflags((session *)(lua_topointer(lua, 1)),
  467         lua_tostring(lua, 2), &flags);
  468 
  469     lua_pop(lua, 2);
  470 
  471     if (r < 0)
  472         return 0;
  473 
  474     lua_pushboolean(lua, (r == STATUS_OK));
  475 
  476     if (!flags)
  477         return 1;
  478 
  479     lua_pushstring(lua, flags);
  480 
  481     xfree(flags);
  482 
  483     return 2;
  484 }
  485 
  486 
  487 /*
  488  * Core function to fetch message date.
  489  */
  490 static int
  491 ifcore_fetchdate(lua_State *lua)
  492 {
  493     int r;
  494     char *date;
  495 
  496     date = NULL;
  497 
  498     if (lua_gettop(lua) != 2)
  499         luaL_error(lua, "wrong number of arguments");
  500     luaL_checktype(lua, 1, LUA_TLIGHTUSERDATA);
  501     luaL_checktype(lua, 2, LUA_TSTRING);
  502 
  503     r = request_fetchdate((session *)(lua_topointer(lua, 1)),
  504         lua_tostring(lua, 2), &date);
  505 
  506     lua_pop(lua, 2);
  507 
  508     if (r < 0)
  509         return 0;
  510 
  511     lua_pushboolean(lua, (r == STATUS_OK));
  512 
  513     if (!date)
  514         return 1;
  515 
  516     lua_pushstring(lua, date);
  517 
  518     xfree(date);
  519 
  520     return 2;
  521 }
  522 
  523 
  524 /*
  525  * Core function to fetch message size.
  526  */
  527 static int
  528 ifcore_fetchsize(lua_State *lua)
  529 {
  530     int r;
  531     char *size;
  532 
  533     size = NULL;
  534 
  535     if (lua_gettop(lua) != 2)
  536         luaL_error(lua, "wrong number of arguments");
  537     luaL_checktype(lua, 1, LUA_TLIGHTUSERDATA);
  538     luaL_checktype(lua, 2, LUA_TSTRING);
  539 
  540     r = request_fetchsize((session *)(lua_topointer(lua, 1)),
  541         lua_tostring(lua, 2), &size);
  542 
  543     lua_pop(lua, 2);
  544 
  545     if (r < 0)
  546         return 0;
  547 
  548     lua_pushboolean(lua, (r == STATUS_OK));
  549 
  550     if (!size)
  551         return 1;
  552 
  553     lua_pushstring(lua, size);
  554 
  555     xfree(size);
  556 
  557     return 2;
  558 }
  559 
  560 
  561 /*
  562  * Core function to fetch message body structure.
  563  */
  564 static int
  565 ifcore_fetchstructure(lua_State *lua)
  566 {
  567     int r;
  568     char *structure;
  569 
  570     structure = NULL;
  571 
  572     if (lua_gettop(lua) != 2)
  573         luaL_error(lua, "wrong number of arguments");
  574     luaL_checktype(lua, 1, LUA_TLIGHTUSERDATA);
  575     luaL_checktype(lua, 2, LUA_TSTRING);
  576 
  577     r = request_fetchstructure((session *)(lua_topointer(lua, 1)),
  578         lua_tostring(lua, 2), &structure);
  579 
  580     lua_pop(lua, 3);
  581 
  582     if (r < 0)
  583         return 0;
  584 
  585     lua_pushboolean(lua, (r == STATUS_OK));
  586 
  587     if (!structure)
  588         return 1;
  589 
  590     lua_pushstring(lua, structure);
  591 
  592     return 2;
  593 }
  594 
  595 
  596 /*
  597  * Core function to fetch message header.
  598  */
  599 static int
  600 ifcore_fetchheader(lua_State *lua)
  601 {
  602     int r;
  603     char *header;
  604     size_t len;
  605 
  606     header = NULL;
  607     len = 0;
  608 
  609     if (lua_gettop(lua) != 2)
  610         luaL_error(lua, "wrong number of arguments");
  611     luaL_checktype(lua, 1, LUA_TLIGHTUSERDATA);
  612     luaL_checktype(lua, 2, LUA_TSTRING);
  613 
  614     r = request_fetchheader((session *)(lua_topointer(lua, 1)),
  615         lua_tostring(lua, 2), &header, &len);
  616 
  617     lua_pop(lua, 2);
  618 
  619     if (r < 0)
  620         return 0;
  621 
  622     lua_pushboolean(lua, (r == STATUS_OK));
  623 
  624     if (!header)
  625         return 1;
  626 
  627     lua_pushlstring(lua, header, len);
  628 
  629     return 2;
  630 }
  631 
  632 
  633 /*
  634  * Core function to fetch message text.
  635  */
  636 static int
  637 ifcore_fetchtext(lua_State *lua)
  638 {
  639     int r;
  640     char *text;
  641     size_t len;
  642 
  643     text = NULL;
  644     len = 0;
  645 
  646     if (lua_gettop(lua) != 2)
  647         luaL_error(lua, "wrong number of arguments");
  648     luaL_checktype(lua, 1, LUA_TLIGHTUSERDATA);
  649     luaL_checktype(lua, 2, LUA_TSTRING);
  650 
  651     r = request_fetchtext((session *)(lua_topointer(lua, 1)),
  652         lua_tostring(lua, 2), &text, &len);
  653 
  654     lua_pop(lua, 2);
  655 
  656     if (r < 0)
  657         return 0;
  658 
  659     lua_pushboolean(lua, (r == STATUS_OK));
  660 
  661     if (!text)
  662         return 1;
  663 
  664     lua_pushlstring(lua, text, len);
  665 
  666     return 2;
  667 }
  668 
  669 
  670 /*
  671  * Core function to fetch message specific header fields.
  672  */
  673 static int
  674 ifcore_fetchfields(lua_State *lua)
  675 {
  676     int r;
  677     char *fields;
  678     size_t len;
  679 
  680     fields = NULL;
  681     len = 0;
  682 
  683     if (lua_gettop(lua) != 3)
  684         luaL_error(lua, "wrong number of arguments");
  685     luaL_checktype(lua, 1, LUA_TLIGHTUSERDATA);
  686     luaL_checktype(lua, 2, LUA_TSTRING);
  687     luaL_checktype(lua, 3, LUA_TSTRING);
  688 
  689     r = request_fetchfields((session *)(lua_topointer(lua, 1)),
  690         lua_tostring(lua, 2), lua_tostring(lua, 3), &fields, &len);
  691 
  692     lua_pop(lua, 3);
  693 
  694     if (r < 0)
  695         return 0;
  696 
  697     lua_pushboolean(lua, (r == STATUS_OK));
  698 
  699     if (!fields)
  700         return 1;
  701 
  702     lua_pushlstring(lua, fields, len);
  703 
  704     return 2;
  705 }
  706 
  707 
  708 /*
  709  * Core function to fetch message specific part.
  710  */
  711 static int
  712 ifcore_fetchpart(lua_State *lua)
  713 {
  714     int r;
  715     char *part;
  716     size_t len;
  717 
  718     part = NULL;
  719     len = 0;
  720 
  721     if (lua_gettop(lua) != 3)
  722         luaL_error(lua, "wrong number of arguments");
  723     luaL_checktype(lua, 1, LUA_TLIGHTUSERDATA);
  724     luaL_checktype(lua, 2, LUA_TSTRING);
  725     luaL_checktype(lua, 3, LUA_TSTRING);
  726 
  727     r = request_fetchpart((session *)(lua_topointer(lua, 1)),
  728         lua_tostring(lua, 2), lua_tostring(lua, 3), &part, &len);
  729 
  730     lua_pop(lua, 3);
  731 
  732     if (r < 0)
  733         return 0;
  734 
  735     lua_pushboolean(lua, (r == STATUS_OK));
  736 
  737     if (!part)
  738         return 1;
  739 
  740     lua_pushlstring(lua, part, len);
  741 
  742     return 2;
  743 }
  744 
  745 
  746 /*
  747  * Core function to change message flags.
  748  */
  749 static int
  750 ifcore_store(lua_State *lua)
  751 {
  752     int r;
  753 
  754     if (lua_gettop(lua) != 4)
  755         luaL_error(lua, "wrong number of arguments");
  756     luaL_checktype(lua, 1, LUA_TLIGHTUSERDATA);
  757     luaL_checktype(lua, 2, LUA_TSTRING);
  758     luaL_checktype(lua, 3, LUA_TSTRING);
  759     luaL_checktype(lua, 4, LUA_TSTRING);
  760 
  761     r = request_store((session *)(lua_topointer(lua, 1)),
  762         lua_tostring(lua, 2), lua_tostring(lua, 3),
  763         lua_tostring(lua, 4));
  764 
  765     lua_pop(lua, 4);
  766 
  767     if (r < 0)
  768         return 0;
  769 
  770     lua_pushboolean(lua, (r == STATUS_OK));
  771 
  772     return 1;
  773 }
  774 
  775 
  776 /*
  777  * Core function to copy messages between mailboxes.
  778  */
  779 static int
  780 ifcore_copy(lua_State *lua)
  781 {
  782     int r;
  783 
  784     if (lua_gettop(lua) != 3)
  785         luaL_error(lua, "wrong number of arguments");
  786     luaL_checktype(lua, 1, LUA_TLIGHTUSERDATA);
  787     luaL_checktype(lua, 2, LUA_TSTRING);
  788     luaL_checktype(lua, 3, LUA_TSTRING);
  789 
  790     r = request_copy((session *)(lua_topointer(lua, 1)),
  791         lua_tostring(lua, 2), lua_tostring(lua, 3));
  792 
  793     lua_pop(lua, 3);
  794 
  795     if (r < 0)
  796         return 0;
  797 
  798     lua_pushboolean(lua, (r == STATUS_OK));
  799 
  800     return 1;
  801 }
  802 
  803 
  804 /*
  805  * Core function to append messages to a mailbox.
  806  */
  807 static int
  808 ifcore_append(lua_State *lua)
  809 {
  810     int r;
  811 
  812     if (lua_gettop(lua) != 5)
  813         luaL_error(lua, "wrong number of arguments");
  814     luaL_checktype(lua, 1, LUA_TLIGHTUSERDATA);
  815     luaL_checktype(lua, 2, LUA_TSTRING);
  816     luaL_checktype(lua, 3, LUA_TSTRING);
  817     if (lua_type(lua, 4) != LUA_TNIL)
  818         luaL_checktype(lua, 4, LUA_TSTRING);
  819     if (lua_type(lua, 5) != LUA_TNIL)
  820         luaL_checktype(lua, 5, LUA_TSTRING);
  821 
  822     r = request_append((session *)(lua_topointer(lua, 1)),
  823         lua_tostring(lua, 2), lua_tostring(lua, 3),
  824 #if LUA_VERSION_NUM < 502
  825         lua_objlen(lua, 3),
  826 #else
  827         lua_rawlen(lua, 3),
  828 #endif
  829         lua_type(lua, 4) == LUA_TSTRING ?
  830         lua_tostring(lua, 4) : NULL, lua_type(lua, 5) == LUA_TSTRING ?
  831         lua_tostring(lua, 5) : NULL);
  832 
  833     lua_pop(lua, lua_gettop(lua));
  834 
  835     if (r < 0)
  836         return 0;
  837 
  838     lua_pushboolean(lua, (r == STATUS_OK));
  839 
  840     return 1;
  841 }
  842 
  843 
  844 /*
  845  * Core function to create a mailbox.
  846  */
  847 static int
  848 ifcore_create(lua_State *lua)
  849 {
  850     int r;
  851 
  852     if (lua_gettop(lua) != 2)
  853         luaL_error(lua, "wrong number of arguments");
  854     luaL_checktype(lua, 1, LUA_TLIGHTUSERDATA);
  855     luaL_checktype(lua, 2, LUA_TSTRING);
  856 
  857     r = request_create((session *)(lua_topointer(lua, 1)),
  858         lua_tostring(lua, 2));
  859 
  860     lua_pop(lua, 2);
  861 
  862     if (r < 0)
  863         return 0;
  864 
  865     lua_pushboolean(lua, (r == STATUS_OK));
  866 
  867     return 1;
  868 }
  869 
  870 
  871 /*
  872  * Core function to delete a mailbox.
  873  */
  874 static int
  875 ifcore_delete(lua_State *lua)
  876 {
  877     int r;
  878 
  879     if (lua_gettop(lua) != 2)
  880         luaL_error(lua, "wrong number of arguments");
  881     luaL_checktype(lua, 1, LUA_TLIGHTUSERDATA);
  882     luaL_checktype(lua, 2, LUA_TSTRING);
  883 
  884     r = request_delete((session *)(lua_topointer(lua, 1)),
  885         lua_tostring(lua, 2));
  886 
  887     lua_pop(lua, 2);
  888 
  889     if (r < 0)
  890         return 0;
  891 
  892     lua_pushboolean(lua, (r == STATUS_OK));
  893 
  894     return 1;
  895 }
  896 
  897 
  898 /*
  899  * Core function to rename a mailbox.
  900  */
  901 static int
  902 ifcore_rename(lua_State *lua)
  903 {
  904     int r;
  905 
  906     if (lua_gettop(lua) != 3)
  907         luaL_error(lua, "wrong number of arguments");
  908     luaL_checktype(lua, 1, LUA_TLIGHTUSERDATA);
  909     luaL_checktype(lua, 2, LUA_TSTRING);
  910     luaL_checktype(lua, 3, LUA_TSTRING);
  911 
  912     r = request_rename((session *)(lua_topointer(lua, 1)),
  913         lua_tostring(lua, 2), lua_tostring(lua, 3));
  914 
  915     lua_pop(lua, 3);
  916 
  917     if (r < 0)
  918         return 0;
  919 
  920     lua_pushboolean(lua, (r == STATUS_OK));
  921 
  922     return 1;
  923 }
  924 
  925 
  926 /*
  927  * Core function to subscribe a mailbox.
  928  */
  929 static int
  930 ifcore_subscribe(lua_State *lua)
  931 {
  932     int r;
  933 
  934     if (lua_gettop(lua) != 2)
  935         luaL_error(lua, "wrong number of arguments");
  936     luaL_checktype(lua, 1, LUA_TLIGHTUSERDATA);
  937     luaL_checktype(lua, 2, LUA_TSTRING);
  938 
  939     r = request_subscribe((session *)(lua_topointer(lua, 1)),
  940         lua_tostring(lua, 2));
  941 
  942     lua_pop(lua, 2);
  943 
  944     if (r < 0)
  945         return 0;
  946 
  947     lua_pushboolean(lua, (r == STATUS_OK));
  948 
  949     return 1;
  950 }
  951 
  952 
  953 /*
  954  * Core function to unsubscribe a mailbox.
  955  */
  956 static int
  957 ifcore_unsubscribe(lua_State *lua)
  958 {
  959     int r;
  960 
  961     if (lua_gettop(lua) != 2)
  962         luaL_error(lua, "wrong number of arguments");
  963     luaL_checktype(lua, 1, LUA_TLIGHTUSERDATA);
  964     luaL_checktype(lua, 2, LUA_TSTRING);
  965 
  966     r = request_unsubscribe((session *)(lua_topointer(lua, 1)),
  967         lua_tostring(lua, 2));
  968 
  969     lua_pop(lua, 2);
  970 
  971     if (r < 0)
  972         return 0;
  973 
  974     lua_pushboolean(lua, (r == STATUS_OK));
  975 
  976     return 1;
  977 }
  978 
  979 
  980 /*
  981  * Core function to go to idle state.
  982  */
  983 static int
  984 ifcore_idle(lua_State *lua)
  985 {
  986     int r;
  987     char *event;
  988 
  989     event = NULL;
  990 
  991     if (lua_gettop(lua) != 1)
  992         luaL_error(lua, "wrong number of arguments");
  993     luaL_checktype(lua, 1, LUA_TLIGHTUSERDATA);
  994 
  995     if (get_option_boolean("reenter"))
  996         r = request_idle((session *)(lua_topointer(lua, 1)),
  997             &event);
  998     else
  999         r = request_idle((session *)(lua_topointer(lua, 1)), &event);
 1000 
 1001     lua_pop(lua, 1);
 1002 
 1003     if (r < 0)
 1004         return 0;
 1005 
 1006     lua_pushboolean(lua, (r == STATUS_OK));
 1007 
 1008     if (!event)
 1009         return 1;
 1010 
 1011     lua_pushstring(lua, event);
 1012 
 1013     xfree(event);
 1014 
 1015     return 2;
 1016 }
 1017 
 1018 
 1019 /*
 1020  * Open imapfilter core library.
 1021  */
 1022 LUALIB_API int
 1023 luaopen_ifcore(lua_State *lua)
 1024 {
 1025 
 1026 #if LUA_VERSION_NUM < 502
 1027     luaL_register(lua, "ifcore", ifcorelib);
 1028 #else
 1029     luaL_newlib(lua, ifcorelib);
 1030     lua_setglobal(lua, "ifcore");
 1031 #endif
 1032 
 1033     return 1;
 1034 }