1
2 /*
3 * aprsc
4 *
5 * (c) Heikki Hannikainen, OH7LZB
6 *
7 * This program is licensed under the BSD license, which can be found
8 * in the file LICENSE.
9 *
10 *
11 * The messaging module implements utility functions for processing and
12 * generating APRS messages.
13 */
14
15
16 #include <stdlib.h>
17 #include <stdarg.h>
18 #include <stdio.h>
19
20 #include "messaging.h"
21 #include "version.h"
22 #include "config.h"
23
24 /*
25 * Generate a message ID
26 */
27
messaging_generate_msgid(char * buf,int buflen)28 void messaging_generate_msgid(char *buf, int buflen)
29 {
30 int i, c;
31
32 for (i = 0; i < buflen-1; i++) {
33 // coverity[dont_call] // squelch warning: not security sensitive use of random(): APRS message-id
34 c = random() % (2*26 + 10); /* letters and numbers */
35
36 if (c < 10)
37 buf[i] = c + 48; /* number */
38 else if (c < 26+10)
39 buf[i] = c - 10 + 97; /* lower-case letter */
40 else
41 buf[i] = c - 36 + 65; /* upper-case letter */
42 }
43
44 /* null-terminate */
45 buf[i] = 0;
46 }
47
48
49 /*
50 * Ack an incoming message
51 */
52
messaging_ack(struct worker_t * self,struct client_t * c,struct pbuf_t * pb,struct aprs_message_t * am)53 int messaging_ack(struct worker_t *self, struct client_t *c, struct pbuf_t *pb, struct aprs_message_t *am)
54 {
55 return client_printf(self, c, "SERVER>" APRSC_TOCALL ",TCPIP*,qAZ,%s::%-9.*s:ack%.*s\r\n",
56 serverid, pb->srcname_len, pb->srcname, am->msgid_len, am->msgid);
57 }
58
59 /*
60 * Send a message to a local logged-in client
61 */
62
messaging_message_client(struct worker_t * self,struct client_t * c,const char * fmt,...)63 extern int messaging_message_client(struct worker_t *self, struct client_t *c, const char *fmt, ...)
64 {
65 va_list args;
66 char s[PACKETLEN_MAX];
67 char msgid[5];
68
69 va_start(args, fmt);
70 vsnprintf(s, PACKETLEN_MAX, fmt, args);
71 va_end(args);
72
73 messaging_generate_msgid(msgid, sizeof(msgid));
74
75 return client_printf(self, c, "SERVER>" APRSC_TOCALL ",TCPIP*,qAZ,%s::%-9s:%s{%s\r\n",
76 serverid, c->username, s, msgid);
77 }
78
79