1
0
mirror of https://github.com/VDR4Arch/vdr.git synced 2023-10-10 13:36:52 +02:00
vdr/svdrp.c

967 lines
28 KiB
C
Raw Permalink Normal View History

2000-07-23 15:01:31 +02:00
/*
* svdrp.c: Simple Video Disk Recorder Protocol
*
* See the main source file 'vdr.c' for copyright information and
* how to reach the author.
*
* The "Simple Video Disk Recorder Protocol" (SVDRP) was inspired
* by the "Simple Mail Transfer Protocol" (SMTP) and is fully ASCII
* text based. Therefore you can simply 'telnet' to your VDR port
* and interact with the Video Disk Recorder - or write a full featured
* graphical interface that sits on top of an SVDRP connection.
*
2002-02-02 17:20:54 +01:00
* $Id: svdrp.c 1.30 2002/02/02 15:59:18 kls Exp $
2000-07-23 15:01:31 +02:00
*/
#include "svdrp.h"
#include <arpa/inet.h>
#include <ctype.h>
2000-12-08 16:23:32 +01:00
#include <errno.h>
2000-07-23 15:01:31 +02:00
#include <fcntl.h>
#include <netinet/in.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <unistd.h>
#include "config.h"
#include "dvbapi.h"
2000-07-23 15:01:31 +02:00
#include "interface.h"
#include "tools.h"
// --- cSocket ---------------------------------------------------------------
cSocket::cSocket(int Port, int Queue)
{
port = Port;
sock = -1;
}
cSocket::~cSocket()
{
Close();
}
void cSocket::Close(void)
{
if (sock >= 0) {
close(sock);
sock = -1;
}
}
bool cSocket::Open(void)
{
if (sock < 0) {
// create socket:
sock = socket(PF_INET, SOCK_STREAM, 0);
if (sock < 0) {
LOG_ERROR;
port = 0;
return false;
}
// allow it to always reuse the same port:
int ReUseAddr = 1;
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &ReUseAddr, sizeof(ReUseAddr));
//
2000-07-23 15:01:31 +02:00
struct sockaddr_in name;
name.sin_family = AF_INET;
name.sin_port = htons(port);
name.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(sock, (struct sockaddr *)&name, sizeof(name)) < 0) {
LOG_ERROR;
Close();
return false;
}
// make it non-blocking:
int oldflags = fcntl(sock, F_GETFL, 0);
if (oldflags < 0) {
LOG_ERROR;
return false;
}
oldflags |= O_NONBLOCK;
if (fcntl(sock, F_SETFL, oldflags) < 0) {
LOG_ERROR;
return false;
}
// listen to the socket:
if (listen(sock, queue) < 0) {
LOG_ERROR;
return false;
}
}
return true;
}
int cSocket::Accept(void)
{
if (Open()) {
struct sockaddr_in clientname;
uint size = sizeof(clientname);
int newsock = accept(sock, (struct sockaddr *)&clientname, &size);
2002-02-02 17:20:54 +01:00
if (newsock > 0) {
bool accepted = SVDRPhosts.Acceptable(clientname.sin_addr.s_addr);
if (!accepted) {
const char *s = "Access denied!\n";
write(newsock, s, strlen(s));
close(newsock);
newsock = -1;
}
isyslog(LOG_INFO, "connect from %s, port %hd - %s", inet_ntoa(clientname.sin_addr), ntohs(clientname.sin_port), accepted ? "accepted" : "DENIED");
}
else if (errno != EINTR && errno != EAGAIN)
2000-07-23 15:01:31 +02:00
LOG_ERROR;
return newsock;
}
return -1;
}
// --- cSVDRP ----------------------------------------------------------------
#define MAXHELPTOPIC 10
const char *HelpPages[] = {
"CHAN [ + | - | <number> | <name> ]\n"
" Switch channel up, down or to the given channel number or name.\n"
" Without option (or after successfully switching to the channel)\n"
" it returns the current channel number and name.",
"DELC <number>\n"
" Delete channel.",
"DELR <number>\n"
" Delete the recording with the given number. Before a recording can be\n"
" deleted, an LSTR command must have been executed in order to retrieve\n"
" the recording numbers. The numbers don't change during subsequent DELR\n"
" commands. CAUTION: THERE IS NO CONFIRMATION PROMPT WHEN DELETING A\n"
" RECORDING - BE SURE YOU KNOW WHAT YOU ARE DOING!",
2000-07-23 15:01:31 +02:00
"DELT <number>\n"
" Delete timer.",
2000-09-17 11:53:35 +02:00
"GRAB <filename> [ jpeg | pnm [ <quality> [ <sizex> <sizey> ] ] ]\n"
" Grab the current frame and save it to the given file. Images can\n"
" be stored as JPEG (default) or PNM, at the given quality (default\n"
" is 'maximum', only applies to JPEG) and size (default is full screen).",
2000-07-23 15:01:31 +02:00
"HELP [ <topic> ]\n"
" The HELP command gives help info.",
2000-09-17 09:36:50 +02:00
"HITK [ <key> ]\n"
" Hit the given remote control key. Without option a list of all\n"
" valid key names is given.",
2000-07-23 15:01:31 +02:00
"LSTC [ <number> | <name> ]\n"
" List channels. Without option, all channels are listed. Otherwise\n"
" only the given channel is listed. If a name is given, all channels\n"
" containing the given string as part of their name are listed.",
"LSTE\n"
" List EPG data.",
"LSTR [ <number> ]\n"
" List recordings. Without option, all recordings are listed. Otherwise\n"
" the summary for the given recording is listed.",
2000-07-23 15:01:31 +02:00
"LSTT [ <number> ]\n"
" List timers. Without option, all timers are listed. Otherwise\n"
" only the given timer is listed.",
"MESG [ <message> ]\n"
" Displays the given message on the OSD. If message is omitted, the\n"
" currently pending message (if any) will be returned. The message\n"
" will be displayed for a few seconds as soon as the OSD has become\n"
" idle. If a new MESG command is entered while the previous message\n"
" has not yet been displayed, the old message will be overwritten.",
2000-07-23 15:01:31 +02:00
"MODC <number> <settings>\n"
" Modify a channel. Settings must be in the same format as returned\n"
" by the LSTC command.",
"MODT <number> on | off | <settings>\n"
" Modify a timer. Settings must be in the same format as returned\n"
" by the LSTT command. The special keywords 'on' and 'off' can be\n"
" used to easily activate or deactivate a timer.",
"MOVC <number> <to>\n"
" Move a channel to a new position.",
"MOVT <number> <to>\n"
" Move a timer to a new position.",
"NEWC <settings>\n"
" Create a new channel. Settings must be in the same format as returned\n"
" by the LSTC command.",
"NEWT <settings>\n"
" Create a new timer. Settings must be in the same format as returned\n"
2000-08-06 12:56:49 +02:00
" by the LSTT command. It is an error if a timer with the same channel,\n"
" day, start and stop time already exists.",
"NEXT [ abs | rel ]\n"
" Show the next timer event. If no option is given, the output will be\n"
" in human readable form. With option 'abs' the absolute time of the next\n"
" event will be given as the number of seconds since the epoch (time_t\n"
" format), while with option 'rel' the relative time will be given as the\n"
" number of seconds from now until the event. If the absolute time given\n"
" is smaller than the current time, or if the relative time is less than\n"
" zero, this means that the timer is currently recording and has started\n"
" at the given time. The first value in the resulting line is the number\n"
" of the timer.",
2000-08-06 12:56:49 +02:00
"UPDT <settings>\n"
" Updates a timer. Settings must be in the same format as returned\n"
" by the LSTT command. If a timer with the same channel, day, start\n"
" and stop time does not yet exists, it will be created.",
2000-07-23 15:01:31 +02:00
"QUIT\n"
" Exit vdr (SVDRP).\n"
" You can also hit Ctrl-D to exit.",
NULL
};
/* SVDRP Reply Codes:
214 Help message
215 EPG data record
2000-07-23 15:01:31 +02:00
220 VDR service ready
221 VDR service closing transmission channel
250 Requested VDR action okay, completed
451 Requested action aborted: local error in processing
500 Syntax error, command unrecognized
501 Syntax error in parameters or arguments
502 Command not implemented
504 Command parameter not implemented
550 Requested action not taken
554 Transaction failed
*/
const char *GetHelpTopic(const char *HelpPage)
{
static char topic[MAXHELPTOPIC];
const char *q = HelpPage;
while (*q) {
if (isspace(*q)) {
uint n = q - HelpPage;
if (n >= sizeof(topic))
n = sizeof(topic) - 1;
strncpy(topic, HelpPage, n);
topic[n] = 0;
return topic;
}
q++;
}
return NULL;
}
const char *GetHelpPage(const char *Cmd)
{
const char **p = HelpPages;
while (*p) {
const char *t = GetHelpTopic(*p);
if (strcasecmp(Cmd, t) == 0)
return *p;
p++;
}
return NULL;
}
cSVDRP::cSVDRP(int Port)
:socket(Port)
{
numChars = 0;
message = NULL;
lastActivity = 0;
2000-07-23 15:01:31 +02:00
isyslog(LOG_INFO, "SVDRP listening on port %d", Port);
}
cSVDRP::~cSVDRP()
{
Close();
delete message;
2000-07-23 15:01:31 +02:00
}
void cSVDRP::Close(bool Timeout)
2000-07-23 15:01:31 +02:00
{
if (file.IsOpen()) {
2000-07-23 15:01:31 +02:00
//TODO how can we get the *full* hostname?
char buffer[BUFSIZ];
2000-07-23 15:01:31 +02:00
gethostname(buffer, sizeof(buffer));
Reply(221, "%s closing connection%s", buffer, Timeout ? " (timeout)" : "");
isyslog(LOG_INFO, "closing SVDRP connection"); //TODO store IP#???
file.Close();
2000-07-23 15:01:31 +02:00
}
}
bool cSVDRP::Send(const char *s, int length)
{
if (length < 0)
length = strlen(s);
2001-08-12 15:22:48 +02:00
int wbytes = safe_write(file, s, length);
2000-07-23 15:01:31 +02:00
if (wbytes == length)
return true;
2000-09-17 13:47:06 +02:00
if (wbytes < 0) {
2000-07-23 15:01:31 +02:00
LOG_ERROR;
2000-09-17 13:47:06 +02:00
file.Close();
}
2000-07-23 15:01:31 +02:00
else //XXX while...???
esyslog(LOG_ERR, "Wrote %d bytes to client while expecting %d\n", wbytes, length);
return false;
}
void cSVDRP::Reply(int Code, const char *fmt, ...)
{
if (file.IsOpen()) {
2000-07-23 15:01:31 +02:00
if (Code != 0) {
va_list ap;
va_start(ap, fmt);
char *buffer;
vasprintf(&buffer, fmt, ap);
char *nl = strchr(buffer, '\n');
if (Code > 0 && nl && *(nl + 1)) // trailing newlines don't count!
Code = -Code;
char number[16];
sprintf(number, "%03d%c", abs(Code), Code < 0 ? '-' : ' ');
const char *s = buffer;
while (s && *s) {
const char *n = strchr(s, '\n');
if (!(Send(number) && Send(s, n ? n - s : -1) && Send("\r\n"))) {
Close();
break;
}
s = n ? n + 1 : NULL;
}
delete buffer;
va_end(ap);
}
else {
Reply(451, "Zero return code - looks like a programming error!");
esyslog(LOG_ERR, "SVDRP: zero return code!");
}
}
}
2000-09-17 09:36:50 +02:00
void cSVDRP::CmdCHAN(const char *Option)
2000-07-23 15:01:31 +02:00
{
if (*Option) {
int n = -1;
if (isnumber(Option)) {
2000-09-09 14:57:43 +02:00
int o = strtol(Option, NULL, 10);
if (o >= 1 && o <= Channels.MaxNumber())
2000-07-23 15:01:31 +02:00
n = o;
}
else if (strcmp(Option, "-") == 0) {
2000-11-05 18:39:17 +01:00
n = cDvbApi::CurrentChannel();
if (n > 1)
2000-07-23 15:01:31 +02:00
n--;
}
else if (strcmp(Option, "+") == 0) {
2000-11-05 18:39:17 +01:00
n = cDvbApi::CurrentChannel();
if (n < Channels.MaxNumber())
2000-07-23 15:01:31 +02:00
n++;
}
else {
2000-09-09 14:57:43 +02:00
int i = 1;
2000-07-23 15:01:31 +02:00
cChannel *channel;
2000-09-09 14:57:43 +02:00
while ((channel = Channels.GetByNumber(i)) != NULL) {
2000-07-23 15:01:31 +02:00
if (strcasecmp(channel->name, Option) == 0) {
n = i;
break;
}
i++;
}
}
if (n < 0) {
Reply(501, "Undefined channel \"%s\"", Option);
return;
}
if (Interface->Recording()) {
2000-07-23 15:01:31 +02:00
Reply(550, "Can't switch channel, interface is recording");
return;
}
2000-09-09 14:57:43 +02:00
cChannel *channel = Channels.GetByNumber(n);
2000-07-23 15:01:31 +02:00
if (channel) {
if (!channel->Switch()) {
2000-09-09 14:57:43 +02:00
Reply(554, "Error switching to channel \"%d\"", channel->number);
2000-07-23 15:01:31 +02:00
return;
}
}
else {
Reply(550, "Unable to find channel \"%s\"", Option);
return;
}
}
2000-11-05 18:39:17 +01:00
cChannel *channel = Channels.GetByNumber(cDvbApi::CurrentChannel());
2000-07-23 15:01:31 +02:00
if (channel)
2000-11-05 18:39:17 +01:00
Reply(250, "%d %s", channel->number, channel->name);
2000-07-23 15:01:31 +02:00
else
2000-11-05 18:39:17 +01:00
Reply(550, "Unable to find channel \"%d\"", cDvbApi::CurrentChannel());
2000-07-23 15:01:31 +02:00
}
2000-09-17 09:36:50 +02:00
void cSVDRP::CmdDELC(const char *Option)
2000-07-23 15:01:31 +02:00
{
//TODO combine this with menu action (timers must be updated)
Reply(502, "DELC not yet implemented");
}
void cSVDRP::CmdDELR(const char *Option)
{
if (*Option) {
if (isnumber(Option)) {
cRecording *recording = Recordings.Get(strtol(Option, NULL, 10) - 1);
if (recording) {
if (recording->Delete())
Reply(250, "Recording \"%s\" deleted", Option);
else
Reply(554, "Error while deleting recording!");
}
else
Reply(550, "Recording \"%s\" not found%s", Option, Recordings.Count() ? "" : " (use LSTR before deleting)");
}
else
Reply(501, "Error in recording number \"%s\"", Option);
}
else
Reply(501, "Missing recording number");
}
2000-09-17 09:36:50 +02:00
void cSVDRP::CmdDELT(const char *Option)
2000-07-23 15:01:31 +02:00
{
if (*Option) {
if (isnumber(Option)) {
cTimer *timer = Timers.Get(strtol(Option, NULL, 10) - 1);
if (timer) {
if (!timer->recording) {
Timers.Del(timer);
Timers.Save();
isyslog(LOG_INFO, "timer %s deleted", Option);
Reply(250, "Timer \"%s\" deleted", Option);
}
else
Reply(550, "Timer \"%s\" is recording", Option);
}
else
Reply(501, "Timer \"%s\" not defined", Option);
}
else
Reply(501, "Error in timer number \"%s\"", Option);
}
else
Reply(501, "Missing timer number");
}
2000-09-17 11:53:35 +02:00
void cSVDRP::CmdGRAB(const char *Option)
{
char *FileName = NULL;
bool Jpeg = true;
int Quality = -1, SizeX = -1, SizeY = -1;
if (*Option) {
char buf[strlen(Option) + 1];
char *p = strcpy(buf, Option);
const char *delim = " \t";
FileName = strtok(p, delim);
if ((p = strtok(NULL, delim)) != NULL) {
if (strcasecmp(p, "JPEG") == 0)
Jpeg = true;
else if (strcasecmp(p, "PNM") == 0)
Jpeg = false;
else {
Reply(501, "Unknown image type \"%s\"", p);
return;
}
}
if ((p = strtok(NULL, delim)) != NULL) {
if (isnumber(p))
Quality = atoi(p);
else {
Reply(501, "Illegal quality \"%s\"", p);
return;
}
}
if ((p = strtok(NULL, delim)) != NULL) {
if (isnumber(p))
SizeX = atoi(p);
else {
Reply(501, "Illegal sizex \"%s\"", p);
return;
}
if ((p = strtok(NULL, delim)) != NULL) {
if (isnumber(p))
SizeY = atoi(p);
else {
Reply(501, "Illegal sizey \"%s\"", p);
return;
}
}
else {
Reply(501, "Missing sizey");
return;
}
}
if ((p = strtok(NULL, delim)) != NULL) {
Reply(501, "Unexpected parameter \"%s\"", p);
return;
}
if (cDvbApi::PrimaryDvbApi->GrabImage(FileName, Jpeg, Quality, SizeX, SizeY))
Reply(250, "Grabbed image %s", Option);
else
Reply(451, "Grab image failed");
}
else
Reply(501, "Missing filename");
}
2000-09-17 09:36:50 +02:00
void cSVDRP::CmdHELP(const char *Option)
2000-07-23 15:01:31 +02:00
{
if (*Option) {
const char *hp = GetHelpPage(Option);
if (hp)
Reply(214, hp);
else {
Reply(504, "HELP topic \"%s\" unknown", Option);
return;
}
}
else {
2000-07-29 18:19:49 +02:00
Reply(-214, "This is VDR version %s", VDRVERSION);
2000-07-23 15:01:31 +02:00
Reply(-214, "Topics:");
const char **hp = HelpPages;
int NumPages = 0;
2000-07-23 15:01:31 +02:00
while (*hp) {
NumPages++;
2000-07-23 15:01:31 +02:00
hp++;
}
const int TopicsPerLine = 5;
int x = 0;
for (int y = 0; (y * TopicsPerLine + x) < NumPages; y++) {
char buffer[TopicsPerLine * (MAXHELPTOPIC + 5)];
char *q = buffer;
for (x = 0; x < TopicsPerLine && (y * TopicsPerLine + x) < NumPages; x++) {
const char *topic = GetHelpTopic(HelpPages[(y * TopicsPerLine + x)]);
if (topic)
q += sprintf(q, " %s", topic);
}
x = 0;
Reply(-214, buffer);
}
2000-07-23 15:01:31 +02:00
Reply(-214, "To report bugs in the implementation send email to");
Reply(-214, " vdr-bugs@cadsoft.de");
}
Reply(214, "End of HELP info");
}
2000-09-17 09:36:50 +02:00
void cSVDRP::CmdHITK(const char *Option)
{
if (*Option) {
eKeys k = Keys.Translate(Option);
if (k != kNone) {
Interface->PutKey(k);
2000-09-17 09:36:50 +02:00
Reply(250, "Key \"%s\" accepted", Option);
}
else
Reply(504, "Unknown key: \"%s\"", Option);
}
else {
Reply(-214, "Valid <key> names for the HITK command:");
for (int i = 0; i < kNone; i++) {
Reply(-214, " %s", Keys.keys[i].name);
}
Reply(214, "End of key list");
}
}
void cSVDRP::CmdLSTC(const char *Option)
2000-07-23 15:01:31 +02:00
{
if (*Option) {
if (isnumber(Option)) {
2000-09-09 14:57:43 +02:00
cChannel *channel = Channels.GetByNumber(strtol(Option, NULL, 10));
2000-07-23 15:01:31 +02:00
if (channel)
2000-09-09 14:57:43 +02:00
Reply(250, "%d %s", channel->number, channel->ToText());
2000-07-23 15:01:31 +02:00
else
Reply(501, "Channel \"%s\" not defined", Option);
}
else {
2000-09-09 14:57:43 +02:00
int i = 1;
2000-07-23 15:01:31 +02:00
cChannel *next = NULL;
2000-09-09 14:57:43 +02:00
while (i <= Channels.MaxNumber()) {
cChannel *channel = Channels.GetByNumber(i);
2000-07-23 15:01:31 +02:00
if (channel) {
if (strcasestr(channel->name, Option)) {
if (next)
2000-09-09 14:57:43 +02:00
Reply(-250, "%d %s", next->number, next->ToText());
2000-07-23 15:01:31 +02:00
next = channel;
}
}
else {
2000-09-09 14:57:43 +02:00
Reply(501, "Channel \"%d\" not found", i);
2000-07-23 15:01:31 +02:00
return;
}
i++;
}
if (next)
2000-09-09 14:57:43 +02:00
Reply(250, "%d %s", next->number, next->ToText());
2000-07-23 15:01:31 +02:00
}
}
else if (Channels.MaxNumber() >= 1) {
2000-09-09 14:57:43 +02:00
for (int i = 1; i <= Channels.MaxNumber(); i++) {
cChannel *channel = Channels.GetByNumber(i);
2000-07-23 15:01:31 +02:00
if (channel)
2000-09-09 14:57:43 +02:00
Reply(i < Channels.MaxNumber() ? -250 : 250, "%d %s", channel->number, channel->ToText());
2000-07-23 15:01:31 +02:00
else
2000-09-09 14:57:43 +02:00
Reply(501, "Channel \"%d\" not found", i);
2000-07-23 15:01:31 +02:00
}
}
else
Reply(550, "No channels defined");
2000-07-23 15:01:31 +02:00
}
void cSVDRP::CmdLSTE(const char *Option)
{
cThreadLock ThreadLock;
const cSchedules *Schedules = cDvbApi::PrimaryDvbApi->Schedules(&ThreadLock);
if (Schedules) {
FILE *f = fdopen(file, "w");
if (f) {
Schedules->Dump(f, "215-");
fflush(f);
Reply(215, "End of EPG data");
// don't 'fclose(f)' here!
}
else
Reply(451, "Can't open file connection");
}
else
Reply(451, "Can't get EPG data");
}
void cSVDRP::CmdLSTR(const char *Option)
{
bool recordings = Recordings.Load();
if (*Option) {
if (isnumber(Option)) {
cRecording *recording = Recordings.Get(strtol(Option, NULL, 10) - 1);
if (recording) {
if (recording->Summary()) {
char *summary = strdup(recording->Summary());
Reply(250, "%s", strreplace(summary,'\n','|'));
delete summary;
}
else
Reply(550, "No summary availabe");
}
else
Reply(550, "Recording \"%s\" not found", Option);
}
else
Reply(501, "Error in recording number \"%s\"", Option);
}
else if (recordings) {
cRecording *recording = Recordings.First();
while (recording) {
Reply(recording == Recordings.Last() ? 250 : -250, "%d %s", recording->Index() + 1, recording->Title(' ', true));
recording = Recordings.Next(recording);
}
}
else
Reply(550, "No recordings available");
}
2000-09-17 09:36:50 +02:00
void cSVDRP::CmdLSTT(const char *Option)
2000-07-23 15:01:31 +02:00
{
if (*Option) {
if (isnumber(Option)) {
cTimer *timer = Timers.Get(strtol(Option, NULL, 10) - 1);
if (timer)
Reply(250, "%d %s", timer->Index() + 1, timer->ToText());
else
Reply(501, "Timer \"%s\" not defined", Option);
}
else
Reply(501, "Error in timer number \"%s\"", Option);
}
else if (Timers.Count()) {
2000-07-23 15:01:31 +02:00
for (int i = 0; i < Timers.Count(); i++) {
cTimer *timer = Timers.Get(i);
if (timer)
Reply(i < Timers.Count() - 1 ? -250 : 250, "%d %s", timer->Index() + 1, timer->ToText());
else
Reply(501, "Timer \"%d\" not found", i + 1);
}
}
else
Reply(550, "No timers defined");
2000-07-23 15:01:31 +02:00
}
void cSVDRP::CmdMESG(const char *Option)
{
if (*Option) {
delete message;
message = strdup(Option);
isyslog(LOG_INFO, "SVDRP message: '%s'", message);
Reply(250, "Message stored");
}
else if (message)
Reply(250, "%s", message);
else
Reply(550, "No pending message");
}
2000-09-17 09:36:50 +02:00
void cSVDRP::CmdMODC(const char *Option)
2000-07-23 15:01:31 +02:00
{
if (*Option) {
char *tail;
int n = strtol(Option, &tail, 10);
if (tail && tail != Option) {
tail = skipspace(tail);
2000-09-09 14:57:43 +02:00
cChannel *channel = Channels.GetByNumber(n);
2000-07-23 15:01:31 +02:00
if (channel) {
cChannel c = *channel;
if (!c.Parse(tail)) {
Reply(501, "Error in channel settings");
return;
}
*channel = c;
Channels.Save();
2000-09-09 14:57:43 +02:00
isyslog(LOG_INFO, "channel %d modified", channel->number);
Reply(250, "%d %s", channel->number, channel->ToText());
2000-07-23 15:01:31 +02:00
}
else
Reply(501, "Channel \"%d\" not defined", n);
}
else
Reply(501, "Error in channel number");
}
else
Reply(501, "Missing channel settings");
}
2000-09-17 09:36:50 +02:00
void cSVDRP::CmdMODT(const char *Option)
2000-07-23 15:01:31 +02:00
{
if (*Option) {
char *tail;
int n = strtol(Option, &tail, 10);
if (tail && tail != Option) {
tail = skipspace(tail);
cTimer *timer = Timers.Get(n - 1);
if (timer) {
cTimer t = *timer;
if (strcasecmp(tail, "ON") == 0)
t.active = 1;
else if (strcasecmp(tail, "OFF") == 0)
t.active = 0;
else if (!t.Parse(tail)) {
Reply(501, "Error in timer settings");
return;
}
*timer = t;
Timers.Save();
isyslog(LOG_INFO, "timer %d modified (%s)", timer->Index() + 1, timer->active ? "active" : "inactive");
Reply(250, "%d %s", timer->Index() + 1, timer->ToText());
}
else
Reply(501, "Timer \"%d\" not defined", n);
}
else
Reply(501, "Error in timer number");
}
else
Reply(501, "Missing timer settings");
}
2000-09-17 09:36:50 +02:00
void cSVDRP::CmdMOVC(const char *Option)
2000-07-23 15:01:31 +02:00
{
//TODO combine this with menu action (timers must be updated)
Reply(502, "MOVC not yet implemented");
}
2000-09-17 09:36:50 +02:00
void cSVDRP::CmdMOVT(const char *Option)
2000-07-23 15:01:31 +02:00
{
//TODO combine this with menu action
Reply(502, "MOVT not yet implemented");
}
2000-09-17 09:36:50 +02:00
void cSVDRP::CmdNEWC(const char *Option)
2000-07-23 15:01:31 +02:00
{
if (*Option) {
cChannel *channel = new cChannel;
if (channel->Parse(Option)) {
Channels.Add(channel);
2000-09-09 14:57:43 +02:00
Channels.ReNumber();
2000-07-23 15:01:31 +02:00
Channels.Save();
2000-09-09 14:57:43 +02:00
isyslog(LOG_INFO, "channel %d added", channel->number);
Reply(250, "%d %s", channel->number, channel->ToText());
2000-07-23 15:01:31 +02:00
}
else
Reply(501, "Error in channel settings");
}
else
Reply(501, "Missing channel settings");
}
2000-09-17 09:36:50 +02:00
void cSVDRP::CmdNEWT(const char *Option)
2000-07-23 15:01:31 +02:00
{
if (*Option) {
cTimer *timer = new cTimer;
if (timer->Parse(Option)) {
2000-08-06 12:56:49 +02:00
cTimer *t = Timers.GetTimer(timer);
if (!t) {
Timers.Add(timer);
Timers.Save();
isyslog(LOG_INFO, "timer %d added", timer->Index() + 1);
Reply(250, "%d %s", timer->Index() + 1, timer->ToText());
return;
}
else
Reply(550, "Timer already defined: %d %s", t->Index() + 1, t->ToText());
}
else
Reply(501, "Error in timer settings");
delete timer;
}
else
Reply(501, "Missing timer settings");
}
void cSVDRP::CmdNEXT(const char *Option)
{
cTimer *t = Timers.GetNextActiveTimer();
if (t) {
time_t Start = t->StartTime();
int Number = t->Index() + 1;
if (!*Option) {
char *s = ctime(&Start);
s[strlen(s) - 1] = 0; // strip trailing newline
Reply(250, "%d %s", Number, s);
}
else if (strcasecmp(Option, "ABS") == 0)
Reply(250, "%d %ld", Number, Start);
else if (strcasecmp(Option, "REL") == 0)
Reply(250, "%d %ld", Number, Start - time(NULL));
else
Reply(501, "Unknown option: \"%s\"", Option);
}
else
Reply(550, "No active timers");
}
2000-09-17 09:36:50 +02:00
void cSVDRP::CmdUPDT(const char *Option)
2000-08-06 12:56:49 +02:00
{
if (*Option) {
cTimer *timer = new cTimer;
if (timer->Parse(Option)) {
cTimer *t = Timers.GetTimer(timer);
if (t) {
t->Parse(Option);
delete timer;
timer = t;
isyslog(LOG_INFO, "timer %d updated", timer->Index() + 1);
}
else {
Timers.Add(timer);
isyslog(LOG_INFO, "timer %d added", timer->Index() + 1);
}
2000-07-23 15:01:31 +02:00
Timers.Save();
Reply(250, "%d %s", timer->Index() + 1, timer->ToText());
2000-08-06 12:56:49 +02:00
return;
2000-07-23 15:01:31 +02:00
}
else
Reply(501, "Error in timer settings");
2000-08-06 12:56:49 +02:00
delete timer;
2000-07-23 15:01:31 +02:00
}
else
Reply(501, "Missing timer settings");
}
#define CMD(c) (strcasecmp(Cmd, c) == 0)
void cSVDRP::Execute(char *Cmd)
{
// skip leading whitespace:
Cmd = skipspace(Cmd);
// find the end of the command word:
char *s = Cmd;
while (*s && !isspace(*s))
s++;
if (*s)
*s++ = 0;
s = skipspace(s);
2000-09-17 09:36:50 +02:00
if (CMD("CHAN")) CmdCHAN(s);
else if (CMD("DELC")) CmdDELC(s);
else if (CMD("DELR")) CmdDELR(s);
2000-09-17 09:36:50 +02:00
else if (CMD("DELT")) CmdDELT(s);
2000-09-17 11:53:35 +02:00
else if (CMD("GRAB")) CmdGRAB(s);
2000-09-17 09:36:50 +02:00
else if (CMD("HELP")) CmdHELP(s);
else if (CMD("HITK")) CmdHITK(s);
else if (CMD("LSTC")) CmdLSTC(s);
else if (CMD("LSTE")) CmdLSTE(s);
else if (CMD("LSTR")) CmdLSTR(s);
2000-09-17 09:36:50 +02:00
else if (CMD("LSTT")) CmdLSTT(s);
else if (CMD("MESG")) CmdMESG(s);
2000-09-17 09:36:50 +02:00
else if (CMD("MODC")) CmdMODC(s);
else if (CMD("MODT")) CmdMODT(s);
else if (CMD("MOVC")) CmdMOVC(s);
else if (CMD("MOVT")) CmdMOVT(s);
else if (CMD("NEWC")) CmdNEWC(s);
else if (CMD("NEWT")) CmdNEWT(s);
else if (CMD("NEXT")) CmdNEXT(s);
2000-09-17 09:36:50 +02:00
else if (CMD("UPDT")) CmdUPDT(s);
else if (CMD("QUIT")) Close();
2000-07-23 15:01:31 +02:00
else Reply(500, "Command unrecognized: \"%s\"", Cmd);
}
void cSVDRP::Process(void)
{
bool NewConnection = !file.IsOpen();
bool SendGreeting = NewConnection;
2000-07-23 15:01:31 +02:00
if (file.IsOpen() || file.Open(socket.Accept())) {
2000-07-23 15:01:31 +02:00
if (SendGreeting) {
//TODO how can we get the *full* hostname?
char buffer[BUFSIZ];
2000-07-23 15:01:31 +02:00
gethostname(buffer, sizeof(buffer));
time_t now = time(NULL);
2000-08-26 12:51:51 +02:00
Reply(220, "%s SVDRP VideoDiskRecorder %s; %s", buffer, VDRVERSION, ctime(&now));
2000-07-23 15:01:31 +02:00
}
if (NewConnection)
lastActivity = time(NULL);
while (file.Ready(false)) {
unsigned char c;
int r = safe_read(file, &c, 1);
if (r > 0) {
if (c == '\n' || c == 0x00) {
// strip trailing whitespace:
while (numChars > 0 && strchr(" \t\r\n", cmdLine[numChars - 1]))
cmdLine[--numChars] = 0;
// make sure the string is terminated:
cmdLine[numChars] = 0;
// showtime!
Execute(cmdLine);
numChars = 0;
}
else if (c == 0x04 && numChars == 0) {
// end of file (only at beginning of line)
Close();
}
else if (c == 0x08 || c == 0x7F) {
// backspace or delete (last character)
if (numChars > 0)
numChars--;
}
else if (c <= 0x03 || c == 0x0D) {
// ignore control characters
}
else if (numChars < sizeof(cmdLine) - 1) {
cmdLine[numChars++] = c;
cmdLine[numChars] = 0;
}
else {
Reply(501, "Command line too long");
esyslog(LOG_ERR, "SVDRP: command line too long: '%s'", cmdLine);
numChars = 0;
}
lastActivity = time(NULL);
}
else if (r <= 0) {
isyslog(LOG_INFO, "lost connection to SVDRP client");
Close();
}
}
if (Setup.SVDRPTimeout && time(NULL) - lastActivity > Setup.SVDRPTimeout) {
isyslog(LOG_INFO, "timeout on SVDRP connection");
Close(true);
}
2000-07-23 15:01:31 +02:00
}
}
char *cSVDRP::GetMessage(void)
{
char *s = message;
message = NULL;
return s;
}
2000-07-23 15:01:31 +02:00
//TODO more than one connection???