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

#include "sia.h"
#include "email.h"

static void msg (char *account, char *event, char *ascii, int log, const char *email)
{
  char body[256];
  char subject[256];


  snprintf (body, sizeof (body) - 1, "%s %64s %s", account, event, ascii);
  body[sizeof (body) - 1] = 0;

  if (log)
    syslog (LOG_WARNING, "SIA: %s", body);

  printf ("%s\n", body);


  if (!email) return;

  if (strstr (body, "HEARTBT")) return;

  snprintf (subject, sizeof (subject) - 1, "Galaxy SIA: %s", ascii);

  send_email (email, subject, body);
}


int new_block (int fd, SIA_Block *b, int log, const char *email)
{
  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, email);

    break;

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

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

  return 0;
}

void periodic_task (void)
{
}