AmendHub

Download:

jcs

/

subtext

/

amendments

/

363

fidopkt: Add encoder


jcs made amendment 363 about 1 year ago
--- fidopkt.c Fri Mar 3 21:52:07 2023 +++ fidopkt.c Tue Mar 7 16:32:47 2023 @@ -447,6 +447,169 @@ parse_fail: return NULL; } +size_t +fidopkt_encode(char **pkt_buf, char *pkt_password, + struct fidopkt_address *orig_node, struct fidopkt_address *dest_node, + struct fidopkt_msgid *msgid, unsigned long time, short tzoff, char *from, + char *to, char *area, char *reply, char *subject, char *body) +{ + static char scratch[30]; + struct tm *tm; + char *buf; + size_t len, off, buf_size, subject_len, body_len; + + subject_len = strlen(subject); + body_len = strlen(body); + + buf_size = FIDOPKT_HEADER_SIZE + FIDOPKT_MSG_HEADER_SIZE + subject_len + + body_len + 512; + buf = xmalloczero(buf_size); + + tm = localtime(&time); + + PUT_U16(buf + FIDOPKT_ORIGZONE, orig_node->zone); + PUT_U16(buf + FIDOPKT_QORIGZONE, orig_node->zone); + PUT_U16(buf + FIDOPKT_ORIGNET, orig_node->net); + PUT_U16(buf + FIDOPKT_ORIGNODE, orig_node->node); + memcpy(buf + FIDOPKT_PASSWORD, pkt_password, strlen(pkt_password)); + + PUT_U16(buf + FIDOPKT_DESTZONE, dest_node->zone); + PUT_U16(buf + FIDOPKT_QDESTZONE, dest_node->zone); + PUT_U16(buf + FIDOPKT_DESTNET, dest_node->net); + PUT_U16(buf + FIDOPKT_DESTNODE, dest_node->node); + + PUT_U16(buf + FIDOPKT_YEAR, tm->tm_year + 1900); + PUT_U16(buf + FIDOPKT_MONTH, tm->tm_mon + 1); + PUT_U16(buf + FIDOPKT_DAY, tm->tm_mday); + PUT_U16(buf + FIDOPKT_HOUR, tm->tm_hour); + PUT_U16(buf + FIDOPKT_MINUTE, tm->tm_min); + PUT_U16(buf + FIDOPKT_SECOND, tm->tm_sec); + + PUT_U16(buf + FIDOPKT_PKTTYPE, 2); + + buf[FIDOPKT_CWVALIDCOPY + 1] = 1; + buf[FIDOPKT_CAPABILWORD] = 1; + + PUT_U16(buf + FIDOPKT_HEADER_SIZE + FIDOPKT_MSG_PKTTYPE, 2); + + PUT_U16(buf + FIDOPKT_HEADER_SIZE + FIDOPKT_MSG_DESTNET, + dest_node->net); + PUT_U16(buf + FIDOPKT_HEADER_SIZE + FIDOPKT_MSG_DESTNODE, + dest_node->node); + + PUT_U16(buf + FIDOPKT_HEADER_SIZE + FIDOPKT_MSG_ORIGNET, + orig_node->net); + PUT_U16(buf + FIDOPKT_HEADER_SIZE + FIDOPKT_MSG_ORIGNODE, + orig_node->node); + + off = FIDOPKT_HEADER_SIZE + FIDOPKT_MSG_HEADER_SIZE; + + /* date, null-terminated */ + if (!grow_to_fit(&buf, &buf_size, off, 21, 21)) + goto fail; + off += strftime(buf + off, 20, "%d %b %y %H:%M:%S", tm); + off++; + + /* to, null-terminated */ + len = strlen(to); + if (!grow_to_fit(&buf, &buf_size, off, len + 1, len + 1)) + goto fail; + memcpy(buf + off, to, len); + off += len + 1; + + /* from, null-terminated */ + len = strlen(from); + if (!grow_to_fit(&buf, &buf_size, off, len + 1, len + 1)) + goto fail; + memcpy(buf + off, from, len); + off += len + 1; + + /* subject, null-terminated */ + if (!grow_to_fit(&buf, &buf_size, off, subject_len + 1, subject_len + 1)) + goto fail; + memcpy(buf + off, subject, subject_len); + off += subject_len + 1; + + /* optional area (non-kludge) \r-terminated */ + if (area != NULL) { + len = snprintf(scratch, sizeof(scratch), "AREA: %s\r", area); + if (len > sizeof(scratch)) + goto fail; + if (!grow_to_fit(&buf, &buf_size, off, len, len)) + goto fail; + memcpy(buf + off, scratch, len); + off += len; + } + + /* kludge line: INTL to from, \r-terminated */ + len = snprintf(scratch, sizeof(scratch), "%cINTL %d:%d/%d %d:%d/%d\r", + 0x1, dest_node->zone, dest_node->net, dest_node->node, + orig_node->zone, orig_node->net, orig_node->node); + if (len > sizeof(scratch)) + goto fail; + if (!grow_to_fit(&buf, &buf_size, off, len, len)) + goto fail; + memcpy(buf + off, scratch, len); + off += len; + + /* kludge line: TZOFF tzoff, \r-terminated */ + len = snprintf(scratch, sizeof(scratch), "%cTZUTC: %+04d\r", + 0x1, tzoff); + if (len > sizeof(scratch)) + goto fail; + if (!grow_to_fit(&buf, &buf_size, off, len, len)) + goto fail; + memcpy(buf + off, scratch, len); + off += len; + + /* kludge line: MSGID zone:net/node id, \r-terminated */ + len = snprintf(scratch, sizeof(scratch), "%cMSGID: %d:%d/%d %lx\r", + 0x1, msgid->zone, msgid->net, msgid->node, msgid->id); + if (len > sizeof(scratch)) + goto fail; + if (!grow_to_fit(&buf, &buf_size, off, len, len)) + goto fail; + memcpy(buf + off, scratch, len); + off += len; + + /* kludge line: REPLY orig-msgid, \r-terminated */ + if (reply != NULL && reply[0]) { + len = snprintf(scratch, sizeof(scratch), "%cREPLY: %s\r", + 0x1, reply); + if (len > sizeof(scratch)) + goto fail; + if (!grow_to_fit(&buf, &buf_size, off, len, len)) + goto fail; + memcpy(buf + off, scratch, len); + off += len; + } + + /* kludge line: PID spam, \r-terminated */ + len = snprintf(scratch, sizeof(scratch), "%cPID: Subtext %s\r", + 0x1, get_version(false)); + if (len > sizeof(scratch)) + goto fail; + if (!grow_to_fit(&buf, &buf_size, off, len, len)) + goto fail; + memcpy(buf + off, scratch, len); + off += len; + + if (!grow_to_fit(&buf, &buf_size, off, body_len + 6, body_len + 6)) + goto fail; + memcpy(buf + off, body, body_len); + off += body_len; + sprintf(buf + off, "\r--- \r"); + + *pkt_buf = buf; + return off; + +fail: + if (buf != NULL) + xfree(&buf); + return 0; + return off; +} + void fidopkt_free(struct fidopkt **pktp) { --- fidopkt.h Fri Mar 3 09:10:13 2023 +++ fidopkt.h Tue Mar 7 16:29:01 2023 @@ -53,6 +53,10 @@ struct fidopkt { bool fidopkt_parse_address(char *str, struct fidopkt_address **address); struct fidopkt * fidopkt_parse(char *packet_filename, char *buf, size_t len); +size_t fidopkt_encode(char **pkt_buf, char *pkt_password, + struct fidopkt_address *orig_node, struct fidopkt_address *dest_node, + struct fidopkt_msgid *msgid, unsigned long time, short tzoff, char *from, + char *to, char *area, char *reply, char *subject, char *body); void fidopkt_free(struct fidopkt **pktp); #endif