summaryrefslogtreecommitdiff
blob: f2231f75b92921a4c6d549bfa90de0870da5b9ef (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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include <sys/socket.h>
#include <sys/un.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>

#include "error/error.h"

#include "pervasive.h"

static void print_help (const char runnable [const restrict static 1])
{
   printf
   (
      "JabberHive - CLI Gateway\n"
      "Software Version %d\n"
      "Protocol Version %d\n"
      "\nUsages:\n"
      "   JH GATEWAY:\t%s SOCKET_NAME\n"
      "   SHOW HELP:\tAnything else\n"
      "\nParameters:\n"
      "   SOCKET_NAME: valid UNIX socket.\n",
      JH_PROGRAM_VERSION,
      JH_PROTOCOL_VERSION,
      runnable
   );
}

static int open_socket
(
   FILE * s [const restrict static 1],
   const char socket_name [const restrict static 1]
)
{
   int fd;
   struct sockaddr_un addr;

   const int old_errno = errno;

   errno = 0;

   if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
   {
      JH_FATAL
      (
         stderr,
         "Unable to create socket: %s.",
         strerror(errno)
      );

      errno = old_errno;

      return -1;
   }

   errno = old_errno;

   memset(&addr, 0, sizeof(addr));
   addr.sun_family = AF_UNIX;
   strncpy(addr.sun_path, socket_name, sizeof(addr.sun_path)-1);

   errno = 0;

   if (connect(fd, (struct sockaddr*)&addr, sizeof(addr)) == -1)
   {
      JH_FATAL
      (
         stderr,
         "Unable to connect to address: %s.",
         strerror(errno)
      );

      errno = old_errno;

      close(fd);

      return -1;
   }

   errno = 0;

   *s = fdopen(fd, "w+");

   if (*s == (FILE *) NULL)
   {
      JH_FATAL
      (
         stderr,
         "Unable to open socket as a file: %s.",
         strerror(errno)
      );

      errno = old_errno;

      close(fd);

      return -1;
   }

   errno = old_errno;

   return 0;
}

static void gateway
(
   FILE socket [const restrict static 1]
)
{
   int is_sending;
   int data, data_prev, is_last_message;

   is_sending = 1;

   for (;;)
   {
      if (is_sending)
      {
         data = fgetc(stdin);

         if (data == EOF)
         {
            return;
         }
         else
         {
            if (fputc(data, socket) == EOF)
            {
               JH_S_FATAL(stderr, "Unable to write to socket.");

               return;
            }
         }

         if (data == '\n')
         {
            is_sending = 0;
            data = 0;
            is_last_message = 0;
         }
      }
      else
      {
         data_prev = data;
         data = fgetc(socket);

         if (data == EOF)
         {
            JH_S_FATAL(stderr, "Unable to read from socket.");

            return;
         }
         else
         {
            if (fputc(data, stdout) == EOF)
            {
               /* ... seems like it wouldn't work. */
               JH_S_FATAL(stderr, "Unable to write to stdout.");

               return;
            }
         }

         is_last_message =
            (
               is_last_message
               || (
                  (data_prev == '!')
                  && (
                     (data == 'P')
                     || (data == 'N')
                  )
                  // TODO: check for the ' '.
               )
            );

         if (is_last_message && (data == '\n'))
         {
            is_sending = 1;
         }
      }
   }
}

int main (int const argc, const char * argv [const static argc])
{
   FILE * socket;

   if (argc != 2)
   {
      print_help(argv[0]);

      return -1;
   }

   if (open_socket(&socket, argv[1]) < 0)
   {
      return -1;
   }

   gateway(socket);

   fclose(socket);

   return 0;
}