winmain.cpp 5.69 KB
Newer Older
Edric Milaret's avatar
Edric Milaret committed
1
/*
2
 *  Copyright (C) 2004-2018 Savoir-faire Linux Inc.
3
 *
Edric Milaret's avatar
Edric Milaret committed
4 5 6 7
 *  Author: Edric Milaret <edric.ladent-milaret@savoirfairelinux.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
8
 *  the Free Software Foundation; either version 3 of the License, or
Edric Milaret's avatar
Edric Milaret committed
9 10 11 12 13 14 15 16 17 18 19
 *  (at your option) any later version.
 *
 *  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 PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.
 */
20

Edric Milaret's avatar
Edric Milaret committed
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
#include <iostream>
#include <thread>
#include <cstring>
#include <signal.h>
#include <getopt.h>
#include <string>

#include "dring.h"
#include "callmanager_interface.h"
#include "configurationmanager_interface.h"
#include "presencemanager_interface.h"
#ifdef RING_VIDEO
#include "videomanager_interface.h"
#endif
#include "fileutils.h"

#include <windows.h>

using namespace std::placeholders;

bool isActive = false;
static int ringFlags = 0;
bool loop = true;

static void
print_title()
{
    std::cout
        << "Ring Daemon " << DRing::version()
50
        << ", by Savoir-faire Linux 2004-2018" << std::endl
51
        << "https://jami.net/" << std::endl
52 53 54 55
#ifdef RING_VIDEO
        << "[Video support enabled]" << std::endl
#endif
        << std::endl;
Edric Milaret's avatar
Edric Milaret committed
56 57 58 59 60 61 62 63 64
}

static void
print_usage()
{
    std::cout << std::endl <<
    "-c, --console \t- Log in console (instead of syslog)" << std::endl <<
    "-d, --debug \t- Debug mode (more verbose)" << std::endl <<
    "-p, --persistent \t- Stay alive after client quits" << std::endl <<
65
    "--auto-answer \t- Force automatic answer to incoming calls" << std::endl <<
Edric Milaret's avatar
Edric Milaret committed
66 67 68 69 70 71 72 73 74
    "-h, --help \t- Print help" << std::endl;
}

// Parse command line arguments, setting debug options or printing a help
// message accordingly.
// returns true if we should quit (i.e. help was printed), false otherwise
static bool
parse_args(int argc, char *argv[], bool& persistent)
{
75 76 77 78 79 80 81
    int consoleFlag = false;
    int debugFlag = false;
    int helpFlag = false;
    int versionFlag = false;
    int autoAnswer = false;

    const struct option long_options[] = {
Edric Milaret's avatar
Edric Milaret committed
82 83 84 85 86 87
        /* These options set a flag. */
        {"debug", no_argument, NULL, 'd'},
        {"console", no_argument, NULL, 'c'},
        {"persistent", no_argument, NULL, 'p'},
        {"help", no_argument, NULL, 'h'},
        {"version", no_argument, NULL, 'v'},
88
        {"auto-answer", no_argument, &autoAnswer, true},
Edric Milaret's avatar
Edric Milaret committed
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
        {0, 0, 0, 0} /* Sentinel */
    };

    while (true) {
        /* getopt_long stores the option index here. */
        int option_index = 0;

        auto c = getopt_long(argc, argv, "dcphv", long_options, &option_index);

        // end of the options
        if (c == -1)
            break;

        switch (c) {
            case 'd':
                debugFlag = true;
                break;

            case 'c':
                consoleFlag = true;
                break;

            case 'p':
                persistent = true;
                break;

            case 'h':
            case '?':
                helpFlag = true;
                break;

            case 'v':
                versionFlag = true;
                break;

            default:
                break;
        }
    }

    if (helpFlag) {
        print_usage();
        return true;
    }

    if (versionFlag) {
        // We've always print the title/version, so we can just exit
        return true;
    }

    if (consoleFlag)
        ringFlags |= DRing::DRING_FLAG_CONSOLE_LOG;

    if (debugFlag)
        ringFlags |= DRing::DRING_FLAG_DEBUG;

145 146 147
    if (autoAnswer)
        ringFlags |= DRing::DRING_FLAG_AUTOANSWER;

Edric Milaret's avatar
Edric Milaret committed
148 149 150 151 152 153 154
    return false;
}

void
IncomingCall(const std::string& accountId,
    const std::string& callId, const std::string& message)
{
155 156
    (void) accountId;
    (void) message;
Edric Milaret's avatar
Edric Milaret committed
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
    if (not isActive) {
        DRing::accept(callId);
        isActive = true;
    } else
        DRing::refuse(callId);
}

static int
run()
{
    using SharedCallback = std::shared_ptr<DRing::CallbackWrapperBase>;

    DRing::init(static_cast<DRing::InitFlag>(ringFlags));

    std::map<std::string, SharedCallback> callHandlers;
    callHandlers.insert(DRing::exportable_callback<DRing::CallSignal::IncomingCall>
        (std::bind(&IncomingCall, _1, _2, _3)));

175
    registerSignalHandlers(callHandlers);
Edric Milaret's avatar
Edric Milaret committed
176 177 178 179 180 181 182 183 184

    if (!DRing::start())
        return -1;

    while (loop) {
        Sleep(1000); // milliseconds
    }

    DRing::fini();
185 186

    return 0;
Edric Milaret's avatar
Edric Milaret committed
187 188 189 190
}

static void
interrupt()
191
{
Edric Milaret's avatar
Edric Milaret committed
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
    loop = false;
}

static void
signal_handler(int code)
{
    std::cerr << "Caught signal " << code
              << ", terminating..." << std::endl;
    // Unset signal handlers
    signal(SIGINT, SIG_DFL);
    signal(SIGTERM, SIG_DFL);

    interrupt();
}

int
main(int argc, char *argv [])
{
    // make a copy as we don't want to modify argv[0], copy it to a vector to
    // guarantee that memory is correctly managed/exception safe
    std::string programName {argv[0]};
    std::vector<char> writable(programName.size() + 1);
    std::copy(std::begin(programName), std::end(programName),std::begin(writable));

    ring::fileutils::set_program_dir(writable.data());

    print_title();

    bool persistent = false;
    if (parse_args(argc, argv, persistent))
        return 0;

    // TODO: Block signals for all threads but the main thread, decide how/if we should
    // handle other signals
    signal(SIGINT, signal_handler);
    signal(SIGTERM, signal_handler);

    return run();
230
}