aboutsummaryrefslogtreecommitdiff
path: root/src/main.c
blob: fe0ff96218658740e56042b057e0c1aa8fb92c80 (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
/* main.c

    Probotic is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License version 3 only as
    published by the Free Software Foundation.

    Probotic 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 version 3 for more details.

    You should have received a copy of the GNU General Public License
    version 3 along with Probotic.

*/

#define VERSION_STRING "0.999"

void
help(void)
{
  ERRMSG(PROGN ": usage\n"
         "-server   SERVER   - Sets server\n"
         "-port     PORT     - Sets port\n"
         "-username USERNAME - Sets username\n"
         "-password PASSW0RD - Sets password\n"
         "-auth     FILE     - Use auth file");
}

void
version(void)
{
  ERRMSG(PROGN ": " VERSION_STRING);
}

int
main (int argc,
      char ** argv)
{
  char const * authfile = NULL;
  if (argc > 1)
  {
    char * arg;
    char * buf;
    while (++argv, --argc)
    {
      arg = *argv;
      if (*arg == '-')
      {
        ++arg;
        if (strcmp(arg, "version") == 0)
        { return 0; }
        else if (strcmp(arg, "help") == 0)
        { goto help; }
        if (argc < 2)
        { goto nop; }
        if (strcmp(arg, "db") == 0)
        { db = argv[1]; }
        else if (strcmp(arg, "server") == 0)
        { free(creds.server); creds.server = strdup(argv[1]); }
        else if (strcmp(arg, "port") == 0)
        { creds.port = atoi(argv[1]); }
        else if (strcmp(arg, "channel") == 0)
        { free(creds.channel); creds.channel = strdup(argv[1]); }
        else if (strcmp(arg, "username") == 0)
        { free(creds.username); creds.username = strdup(argv[1]); }
        else if (strcmp(arg, "password") == 0)
        { free(creds.password); creds.password = strdup(argv[1]); }
        else if (strcmp(arg, "auth") == 0)
        {
          authfile = argv[1];
          buf = slurp(authfile);
          if (!buf)
          {
            fprintf(stderr, "file: %s\n", authfile);
            PERROR(1);
          }
          if (parse_pair(buf, strlen(buf)))
          {
            free(buf);
            creds_free_rest();
            ERR(CREDS_ERROR, "Cannot parse creds");
          }
          free(buf);
          atexit(creds_free_rest);
        }
        ++argv; --argc;
      }
      else
      { nop: ERRFMT(1, "Oprand without option '%s'", arg); }
    }
  }
#ifndef NDEBUG
  fprintf(stderr, "-- server:'%s:%d' channel:'%s' username:'%s' pass:%s --\n",
          creds.server, creds.port, creds.channel, creds.username,
          creds.password);
#endif /* NDEBUG */

  if (init())
  { return 1; }

  return loop();
help:
  help();
  return 1;
}