summaryrefslogtreecommitdiff
path: root/vdr-vdrmanager/handler.cpp
blob: 4a787c70fab7a1b01b97f766bfb15c8d238a93a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
 * event und message handler
 */

#include <unistd.h>
#include <vdr/plugin.h>
#include <vdr/timers.h>
#include "sock.h"
#include "select.h"
#include "vdrmanagerthread.h"
#include "helpers.h"

bool cHandler::HandleNewClient(cVdrmanagerClientSocket * sock)
{
  return true;
}

bool cHandler::HandleClientRequest(cVdrmanagerClientSocket * sock)
{
  while (!sock->IsLineComplete() && sock->Read())
    ;

  if (sock->IsLineComplete()) {

    string line;
    sock->GetLine(line);

    isyslog("[vdrmanager] got %s", line.c_str());

    // parse request
    size_t space = line.find(' ');
    string cmd;
    string args;
    if (space != string::npos) {
      cmd = cHelpers::ToUpper(line.substr(0, space));
      args = cHelpers::Trim(line.substr(space+1));
    } else {
      cmd = cHelpers::ToUpper(line);
      args = "";
    }

    if (!sock->IsLoggedIn() && cmd != "PASSWD") {
      sock->Write("!ERROR\r\n");
    }
    else if (cmd == "PASSWD")
    {
      if (args != sock->GetPassword()) {
        sock->Write("!ERROR\r\n");
      } else {
        sock->SetLoggedIn();
        sock->Write("!OK\r\n");
      }
    }
    else if (cmd == "COMPRESS")
    {
      sock->ActivateCompression();
    }
    else if (cmd == "TIMERS")
    {
      string text = cHelpers::GetTimers(args);
      sock->Write(text);
    }
    else if (cmd == "CHANNELS")
    {
      string text = cHelpers::GetChannels(args);
      sock->Write(text);
    }
    else if (cmd == "TEVENTS")
    {
      string text = cHelpers::GetTimeEvents(args);
      sock->Write(text);
    }
    else if (cmd == "CEVENTS")
    {
      string text = cHelpers::GetChannelEvents(args);
      sock->Write(text);
    }
    else if (cmd == "TIMER")
    {
      string text = cHelpers::SetTimer(args);
      sock->Write(text);
    }
    else if (cmd == "SEARCH")
    {
      string text = cHelpers::SearchEvents(args);
      sock->Write(text);
    }
    else if(cmd == "RECORDINGS")
    {
      string text = cHelpers::GetRecordings(args);
      sock->Write(text);
    }
    else if(cmd == "DRECORDING")
    {
      string text = cHelpers::DelRecording(args);
      sock->Write(text);
    }
    else if (cmd == "SETCHANNEL")
    {
      string text = cHelpers::SetChannel(args);
      sock->Write(text);
    }
    else if (cmd == "QUIT")
    {
      // close socket
      sock->Write(string("Good bye! :-)\r\n"));
      sock->Disconnect();
    }

    sock->Flush();
  }

  return true;
}