【问题标题】:strange behaviour of procmail when piping content to c++ executable将内容传送到 C++ 可执行文件时 procmail 的奇怪行为
【发布时间】:2013-08-24 00:52:46
【问题描述】:

我有一个有效的 procmail 配置。
这是 rc.filters :

:0 w :a.lock
* ^From:(.*\<)?(try@gmail\.com)\>
| $HOME/executable/a.out

此文件编译并运行,procmail 传递邮件, 并且可执行文件将内容写入输出文件。

#include <stdlib.h>
#include <iostream>
#include <fstream>

using namespace std;

int main(void)
{
ofstream myfile;
myfile.open ("output.txt");

    string line;
    while (getline(cin, line)) 
    {
    myfile << line << endl;     
    }    
myfile.close();    
return EXIT_SUCCESS;
} 

问题是我需要一个带有内容的 cin 对象来传递 到Mimetic 库的构造函数。 我需要这个可执行文件才能工作:

#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <mimetic/mimetic.h>

using namespace std;
using namespace mimetic;

int main(void)
{
ofstream myfile;
myfile.open ("output.txt");

MimeEntity me(cin);                         
const Header& h = me.header();  
string subjectString = h.subject();
myfile << subjectString;
myfile << "Check";      
myfile.close();
return EXIT_SUCCESS;
}

如果我收到一条名为 message.txt 的 Mime 消息并使用第二个代码执行以下操作:

cat message.txt | ./a.out

./a.out < message.txt

在这两种情况下,可执行文件都有效,我在 output.txt 中获得了主题
但是对于某些当它被调用并且通过 procmail 管道传输的内容时它不起作用,
我在 output.txt 中得到的只是“检查”,这意味着该文件 至少被调用了。

procmail.log 表明一切正常。

【问题讨论】:

  • @MichaelHampton 谢谢。是的,我确实在模仿网站上尝试过这个例子。同样的事情,当我从控制台尝试时效果很好,并且不适用于 procmail 管道。另一件事是,当我使用 shell 脚本尝试 procmail 时,它也可以工作。 procmail管道的方式......
  • 您的SHELL 的价值是多少?如果将SHELL=/bin/sh 添加到.procmailrc 的顶部,情况会有所改善吗?

标签: c++ piping procmail


【解决方案1】:

我不知道到底发生了什么,但我会将来自std::cin 的输入捕获到一个字符串中,然后将由该值构造的std::istringstream 传递给MimeEntity。通过这种方式,您可以检查来自 std::cin 的输入是什么,同时仍由库处理:

std::istreambuf_iterator<char> begin(std::cin), end;
std::string message(begin, end);
out << "received >>>" << message << "<<<\n";
std::istringstream in(message);
MimeEntity me(in);
// ...

【讨论】:

  • 我已经尝试了你的建议。当使用 procmail 调用时,将 MimeEntity 打印到输出文件之前的部分。但 MimeEntity 的解析内容不是。当我从控制台尝试时,所有内容都会打印出来。还有其他方法可以“重建”类似 cin 的对象吗?
  • @Stasv:这个描述暗示管道不是问题。相反,程序环境中的某些内容由 procmail 设置以关闭您的MimeEntity。我会检查MimeEntity 是否使用了一些设置不同的环境变量(例如,语言环境或编码),是否对用户有依赖性(例如,从控制台以用户 procmail 的身份运行程序)等。
  • 谢谢!您是否知道我将如何从代码中检查用户和环境变量?
  • @Stasv:了解用户取决于操作系统。在 UNIX 上,您将使用 getuid()。它几乎可以肯定只是作为 procmail 的用户运行。您可以通过设置 setuid 位 (chmod u+s a.out) 以您的用户身份运行它。标准库仅具有检查环境的功能(例如,getenv()),而 shell 提供printenv。您可以从 procmail 运行一个 shell 脚本,它将printenv 的结果转储到合适的文件中。
  • 我已经将内容通过管道传输到一个 shell 脚本,发现运行它的用户就是邮件被发送到的用户。只是一个普通的用户帐户。当我以该用户身份运行可执行文件时,它可以正常工作。该用户的控制台与 procmail 的 printenv 输出完全不同。 LANG=en_US.UTF-8 出现在工作环境中,在非工作环境中有一个 SHELLFLAGS=-c ,PATH变量有点不同,其他的似乎无关
猜你喜欢
  • 1970-01-01
  • 2015-09-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-06
相关资源
最近更新 更多