summaryrefslogtreecommitdiffstats
path: root/rx.c
blob: 09d969f8cf7b2ed4042ab72d4743f805c63aa7cc (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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <syslog.h>

#include "sia.h"

static void msg (char *account, char *event, char *ascii, int log)
{
  if (log)
    syslog (LOG_WARNING, "SIA: %s %64s %s", account, event, ascii);

  printf ("%s %64s %s\n", account, event, ascii);
}


int new_block (int fd, SIA_Block *b, int log)
{
  static int have_ascii_messages = 0; /*SIA level 3 doesn't have ascii, SIA level 4 does */

  static char account[SIA_MAX_DATA_LENGTH + 1];
  static char event[SIA_MAX_DATA_LENGTH + 1];
  static char ascii[SIA_MAX_DATA_LENGTH + 1];

  unsigned len = sia_data_length (b);

  if (sia_ack_if_needed (fd, b))
    return -1;

  switch (b->function) {
  case SIA_FN_ACCOUNT_ID:
    memcpy (account, b->data, len);
    account[len] = 0;
    break;

  case SIA_FN_NEW_EVENT:
  case SIA_FN_OLD_EVENT:
    memcpy (event, b->data, len);
    event[len] = 0;

    if (!have_ascii_messages) msg (account, event, "", log);

    break;

  case SIA_FN_ASCII:
    have_ascii_messages = 1;
    memcpy (ascii, b->data, len);
    ascii[len] = 0;

    msg (account, event, ascii, log);
    break;
  }

  return 0;
}

void periodic_task (void)
{
}