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

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

#include <netinet/in.h>

//-----------------------------------------------------
// 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;
}