]> source.dussan.org Git - rspamd.git/commitdiff
[Minor] Rspamadm: Allow `-` as filename to read from stdin
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Thu, 24 Jan 2019 14:54:13 +0000 (14:54 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Thu, 24 Jan 2019 14:54:13 +0000 (14:54 +0000)
lualib/rspamadm/mime.lua
src/lua/lua_task.c

index 96bdfd72c0f03631caa533a85b7796dc10c8a24f..7b633b75dacb2df9bcf1b47615a7f019bf41c66c 100644 (file)
@@ -170,7 +170,7 @@ end
 
 local function load_task(opts, fname)
   if not fname then
-    parser:error('no file specified')
+    fname = '-'
   end
 
   local res,task = rspamd_task.load_from_file(fname, rspamd_config)
index 2606478d15bc430b52da8ae8a23c46a2e78a8921..f8c624e600bc45ec3e7e8b18a85cd7a3fdd5ddb4 100644 (file)
@@ -1369,18 +1369,49 @@ lua_task_load_from_file (lua_State * L)
                        }
                }
 
-               map = rspamd_file_xmap (fname, PROT_READ, &sz, TRUE);
+               if (strcmp (fname, "-") == 0) {
+                       /* Read from stdin */
+                       gint fd = STDIN_FILENO;
+                       GString *data = g_string_sized_new (BUFSIZ);
+                       gchar buf[BUFSIZ];
+                       gssize r;
+
+                       for (;;) {
+                               r = read (fd, buf, sizeof (buf));
+
+                               if (r == -1) {
+                                       err = strerror (errno);
+                                       break;
+                               }
+                               else if (r == 0) {
+                                       break;
+                               }
+                               else {
+                                       g_string_append_len (data, buf, r);
+                               }
 
-               if (!map) {
-                       err = strerror (errno);
+                               task = rspamd_task_new (NULL, cfg, NULL, NULL, NULL);
+                               task->msg.begin = data->str;
+                               task->msg.len = data->len;
+                               rspamd_mempool_add_destructor (task->task_pool,
+                                               lua_task_free_dtor, task);
+                               res = TRUE;
+                               g_string_free (data, FALSE); /* Buffer is still valid */
+                       }
                }
                else {
-                       task = rspamd_task_new (NULL, cfg, NULL, NULL, NULL);
-                       task->msg.begin = map;
-                       task->msg.len = sz;
-                       rspamd_mempool_add_destructor (task->task_pool,
-                                       lua_task_unmap_dtor, task);
-                       res = TRUE;
+                       map = rspamd_file_xmap (fname, PROT_READ, &sz, TRUE);
+
+                       if (!map) {
+                               err = strerror (errno);
+                       } else {
+                               task = rspamd_task_new (NULL, cfg, NULL, NULL, NULL);
+                               task->msg.begin = map;
+                               task->msg.len = sz;
+                               rspamd_mempool_add_destructor (task->task_pool,
+                                               lua_task_unmap_dtor, task);
+                               res = TRUE;
+                       }
                }
        }
        else {