summaryrefslogtreecommitdiff
path: root/src/common/utils.cpp
blob: d8c0c1216dd9ae31d6a126fe9039851944b6726e (plain) (blame)
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
#include "utils.hpp"

#include <netinet/in.h>
#include <sys/time.h>

#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <algorithm>

//-----------------------------------------------------
// Function to suppress control characters in a string.
//-----------------------------------------------------
int remove_control_chars(char *str)
{
    int i;
    int change = 0;

    for (i = 0; str[i]; i++)
    {
        if (0 <= str[i] && str[i] < 32)
        {
            str[i] = '_';
            change = 1;
        }
    }

    return change;
}

//---------------------------------------------------
// E-mail check: return 0 (not correct) or 1 (valid).
//---------------------------------------------------
int e_mail_check(const char *email)
{
    char ch;
    const char *last_arobas;

    // athena limits
    if (strlen(email) < 3 || strlen(email) > 39)
        return 0;

    // part of RFC limits (official reference of e-mail description)
    if (strchr(email, '@') == NULL || email[strlen(email) - 1] == '@')
        return 0;

    if (email[strlen(email) - 1] == '.')
        return 0;

    last_arobas = strrchr(email, '@');

    if (strstr(last_arobas, "@.") != NULL ||
        strstr(last_arobas, "..") != NULL)
        return 0;

    for (ch = 1; ch < 32; ch++)
    {
        if (strchr(last_arobas, ch) != NULL)
        {
            return 0;
            break;
        }
    }

    if (strchr(last_arobas, ' ') != NULL ||
        strchr(last_arobas, ';') != NULL)
        return 0;

    // all correct
    return 1;
}

//-------------------------------------------------
// Return numerical value of a switch configuration
// on/off, english, français, deutsch, español
//-------------------------------------------------
int config_switch (const char *str)
{
    if (strcasecmp(str, "on") == 0 || strcasecmp(str, "yes") == 0
        || strcasecmp(str, "oui") == 0 || strcasecmp(str, "ja") == 0
        || strcasecmp(str, "si") == 0)
        return 1;
    if (strcasecmp(str, "off") == 0 || strcasecmp(str, "no") == 0
        || strcasecmp(str, "non") == 0 || strcasecmp(str, "nein") == 0)
        return 0;

    return atoi(str);
}

const char *ip2str(struct in_addr ip, bool extra_dot)
{
    const uint8_t *p = (const uint8_t *)(&ip);
    static char buf[17];
    if (extra_dot)
        sprintf(buf, "%d.%d.%d.%d.", p[0], p[1], p[2], p[3]);
    else
        sprintf(buf, "%d.%d.%d.%d", p[0], p[1], p[2], p[3]);
    return buf;
}

bool split_key_value(const std::string& line, std::string *w1, std::string *w2)
{
    std::string::const_iterator begin = line.begin(), end = line.end();

    if (line[0] == '/' && line[1] == '/')
        return false;
    if (line.back() == '\r')
        --end;
    if (line.empty())
        return false;

    if (std::find_if(begin, end,
                [](unsigned char c) { return c < ' '; }
                ) != line.end())
        return false;
    std::string::const_iterator colon = std::find(begin, end, ':');
    if (colon == end)
        return false;
    w1->assign(begin, colon);
    ++colon;
    while (std::isspace(*colon))
        ++colon;
    w2->assign(colon, end);
    return true;
}

void stamp_time(timestamp_seconds_buffer& out, time_t *t)
{
    time_t when = t ? *t : time(NULL);
    strftime(out, sizeof(out), "%Y-%m-%d %H:%M:%S", gmtime(&when));
}
void stamp_time(timestamp_milliseconds_buffer& out)
{
    struct timeval tv;
    gettimeofday(&tv, NULL);
    strftime(out, sizeof(out), "%Y-%m-%d %H:%M:%S", gmtime(&tv.tv_sec));
    sprintf(out + 20, ".%03d", int(tv.tv_usec / 1000));
}

void log_with_timestamp(FILE *out, const_string line)
{
    if (!line)
    {
        fputc('\n', out);
        return;
    }
    timestamp_milliseconds_buffer tmpstr;
    stamp_time(tmpstr);
    fwrite(tmpstr, 1, sizeof(tmpstr), out);
    fputs(": ", out);
    fwrite(line.data(), 1, line.size(), out);
    if (line.back() != '\n')
        fputc('\n', out);
}