summaryrefslogtreecommitdiff
path: root/src/event_main.cc
blob: 99b7d1a772901e49ae7a3e490d34cc0a223c8ce3 (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include "common.hh"

#include <algorithm>
#include <functional>
#include <memory>
#include <sstream>
#include <string>
#include <vector>

#include "args.hh"
#include "cgi.hh"
#include "event.hh"
#include "db.hh"
#include "http.hh"
#include "sqlite3_db.hh"

#define DB_PATH "/var/stuff/"

    /*
token=gIkuvaNzQIHg97ATvDxqgjtO
team_id=T0001
team_domain=example
channel_id=C2147483705
channel_name=test
user_id=U2147483697
user_name=Steve
command=/weather
text=94070
        */

using namespace stuff;

namespace {

std::shared_ptr<DB> open(const std::string& channel) {
    std::string tmp = channel;
    for (auto it = tmp.begin(); it != tmp.end(); ++it) {
        if (!((*it >= 'a' && *it <= 'z') ||
              (*it >= 'A' && *it <= 'Z') ||
              (*it >= '0' && *it <= '9') ||
              *it == '-' || *it == '_' || *it == '.')) {
            *it = '.';
        }
    }
    auto db = SQLite3::open(DB_PATH + tmp + ".db");
    if (!db) {
        Http::response(200, "Unable to open database");
    }
    return std::move(db);
}

bool parse(const std::string& text, std::vector<std::string>* args) {
    if (Args::parse(text, args)) return true;
    Http::response(200, "Invalid parameter format");
    return false;
}

bool create(const std::string& channel,
            std::map<std::string, std::string>& data) {
    std::vector<std::string> args;
    if (!parse(data["text"], &args) || args.empty()) return true;
    
    return true;
}

bool cancel(const std::string& channel,
            std::map<std::string, std::string>& data) {
    std::vector<std::string> args;
    if (!parse(data["text"], &args)) return true;
    std::vector<unsigned long> indexes;
    if (args.empty()) {
        indexes.push_back(0);
    } else {
        for (const auto& arg : args) {
            try {
                size_t end;
                auto tmp = std::stoul(arg, &end);
                if (end != arg.size()) {
                    Http::response(200, "Bad index: " + arg);
                    return true;
                }
                indexes.push_back(tmp);
            } catch (std::invalid_argument& e) {
                Http::response(200, "Bad index: " + arg);
                return true;
            }
        }
        std::sort(indexes.begin(), indexes.end(),
                  std::greater<unsigned long>());
        std::unique(indexes.begin(), indexes.end());
    }
    auto db = open(channel);
    if (!db) return true;
    auto events = Event::all(db);
    if (indexes.front() >= events.size()) {
        if (events.empty()) {
            Http::response(200, "There are no events");
        } else {
            std::ostringstream ss;
            ss << "There are only " << events.size() << " events";
            Http::response(200, ss.str());
        }
        return true;
    }
    bool signal_channel = false;
    for (const auto& index : indexes) {
        if (index == 0) signal_channel = true;
        events[index]->remove();
    }
    if (indexes.size() > 1) {
        Http::response(200, "Events removed");
    } else {
        Http::response(200, "Event removed");
    }
    if (signal_channel) {
        
    }
    return true;
}

bool update(const std::string& channel,
            std::map<std::string, std::string>& data) {
    std::vector<std::string> args;
    if (!parse(data["text"], &args)) return true;
    
    return true;
}

bool show(const std::string& channel,
          std::map<std::string, std::string>& data) {
    std::vector<std::string> args;
    if (!parse(data["text"], &args)) return true;
    
    return true;
}

bool going(const std::string& channel,
           std::map<std::string, std::string>& data,
           bool going) {
    std::vector<std::string> args;
    if (!parse(data["text"], &args)) return true;
    
    return true;
}

bool handle_request(CGI* cgi) {
    switch (cgi->request_type()) {
    case CGI::GET:
    case CGI::POST:
        break;
    default:
        Http::response(500, "Unsupported request");
        return true;
    }

    std::map<std::string, std::string> data;
    cgi->get_data(&data);
    const auto& channel = data["channel"];
    if (channel.empty()) {
        Http::response(500, "No channel");
        return true;
    }
    auto command = data["command"];
    if (command.front() == '/') command = command.substr(1);
    if (command == "create") {
        return create(channel, data);
    }
    if (command == "cancel") {
        return cancel(channel, data);
    }
    if (command == "update") {
        return update(channel, data);
    }
    if (command == "show") {
        return show(channel, data);
    }
    if (command == "going") {
        return going(channel, data, true);
    }
    if (command == "!going") {
        return going(channel, data, false);
    }
    Http::response(500, "Unknown command: " + command);
    return true;
}

}  // namespace

int main() {
    return CGI::run(handle_request);
}