module-datehandler.cc 2.77 KB
Newer Older
1 2
/*
	Flexisip, a flexible SIP proxy server with media capabilities.
3
	Copyright (C) 2010  Belledonne Communications SARL.
4

5 6 7 8
	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU Affero General Public License as
	published by the Free Software Foundation, either version 3 of the
	License, or (at your option) any later version.
9

10 11 12 13
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTIC<ULAR PURPOSE.  See the
	GNU Affero General Public License for more details.
14

15 16
	You should have received a copy of the GNU Affero General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 18
*/

19 20
#include <flexisip/module.hh>
#include <flexisip/agent.hh>
21 22 23

#include <sofia-sip/msg_addr.h>

24
using namespace std;
25
using namespace flexisip;
26

27
class DateHandler : public Module, protected ModuleToolbox {
28 29 30 31
public:
	DateHandler(Agent *ag) : Module(ag) {}
	~DateHandler() {}

32
	virtual void onRequest(shared_ptr<RequestSipEvent> &ev) {
33 34
		if (mCommand.empty())
			return;
35 36
		const shared_ptr<MsgSip> &ms = ev->getMsgSip();
		sip_t *sip = ms->getSip();
37
		if (sip->sip_date) {
38
			char command[256];
39 40 41 42
			snprintf(command, sizeof(command) - 1, "%s %lu", mCommand.c_str(), sip->sip_date->d_time);
			int err = system(command);
			if (err == -1) {
				LOGE("Command invocation '%s' failed: %s", command, strerror(errno));
43 44 45
			}
		}
	}
46

47 48 49
	virtual void onResponse(shared_ptr<ResponseSipEvent> &ev) {}

protected:
50 51 52 53 54 55
	virtual void onDeclare(GenericStruct *module_config) {
		ConfigItemDescriptor items[] = {{String, "assign-date-command",
										 "Path to script to assign Date to system. The date is passed as first "
										 "argument of the command, as number of seconds since January 1st, 1900.",
										 ""},
										config_item_end};
56
		module_config->get<ConfigBoolean>("enabled")->setDefault("false");
57 58
		module_config->get<ConfigBooleanExpression>("filter")
			->setDefault("is_request && request.method-name == 'REGISTER'");
59 60
		module_config->addChildrenValues(items);
	}
61 62
	virtual void onLoad(const GenericStruct *root) {
		mCommand = root->get<ConfigString>("assign-date-command")->read();
63
	}
64

65
private:
66 67 68 69
	string mCommand;
	static ModuleInfo<DateHandler> sInfo;
};

70 71 72 73 74 75 76 77 78 79
ModuleInfo<DateHandler> DateHandler::sInfo(
	"DateHandler",
	"The purpose of the DateHandler module is to catch 'Date' "
	"headers from sip requests, and call config-defined script "
	"passing it the date value. The typical use case "
	"is for deploying a Flexisip proxy in an embedded system "
	"that doesn't have time information when booting up. The "
	"command can be used to assign the date to the system.",
	{ "Authentication" },
	ModuleInfoBase::ModuleOid::DateHandler,
80
	ModuleClass::Experimental
81
);