summaryrefslogtreecommitdiff
path: root/src/core/proxychat.c
blob: d26a17832b73e52175d530f08349cea566de2828 (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
#include "hybbx/proxychat.h"
#include "hybbx/mains_proxy.h"
#include "hybbx/service.h"
#include "hybbx/session.h"

#include <stdio.h>
#include <string.h>

typedef struct proxychat_fanout_ctx {
    hybbx_session_t *from;
    const char *line;
    const char *from_address;
} proxychat_fanout_ctx_t;

static void proxychat_local_visitor(hybbx_session_t *session, void *userdata)
{
    proxychat_fanout_ctx_t *ctx = (proxychat_fanout_ctx_t *)userdata;
    char line[HYBBX_PROXYMAIL_ADDRESS_MAX + HYBBX_LINE_MAX + 8];

    if (session == NULL || ctx == NULL || ctx->line == NULL) {
        return;
    }

    if (session == ctx->from) {
        return;
    }

    if (hybbx_session_area(session) != HYBBX_AREA_PROXYCHAT) {
        return;
    }

    if (hybbx_session_is_guest(session)) {
        return;
    }

    snprintf(line, sizeof(line), "<%s> %s",
             ctx->from_address != NULL ? ctx->from_address : "?",
             ctx->line);
    hybbx_session_write_line(session, line);
}

static void proxychat_show_local(hybbx_session_t *session,
                                 const char *from_address,
                                 const char *line)
{
    char out[HYBBX_PROXYMAIL_ADDRESS_MAX + HYBBX_LINE_MAX + 8];

    snprintf(out, sizeof(out), "<%s> %s",
             from_address != NULL ? from_address : "?", line);
    hybbx_session_write_line(session, out);
}

hybbx_result_t hybbx_proxychat_post(hybbx_service_t *service,
                                    hybbx_session_t *session,
                                    const char *line)
{
    char from_address[HYBBX_PROXYMAIL_ADDRESS_MAX];
    proxychat_fanout_ctx_t ctx;
    hybbx_result_t rc;

    if (session == NULL || line == NULL || line[0] == '\0') {
        return HYBBX_ERR_INVALID;
    }

    if (service == NULL) {
        return HYBBX_ERR_INVALID;
    }

    snprintf(from_address, sizeof(from_address), "%s@%s",
             hybbx_session_display_name(session),
             hybbx_service_get_name(service));

    proxychat_show_local(session, from_address, line);

    if (!hybbx_mains_proxy_mesh_active()) {
        hybbx_session_write_line(session,
            "Mains proxy is not running — message shown locally only.");
        return HYBBX_OK;
    }

    ctx.from = session;
    ctx.line = line;
    ctx.from_address = from_address;
    hybbx_service_visit_sessions(service, proxychat_local_visitor, &ctx);

    rc = hybbx_mains_proxy_send_chat(service, from_address, line);
    if (rc == HYBBX_ERR_UNSUPPORTED) {
        hybbx_session_write_line(session,
            "No outbound peer links — message shown locally only.");
        return HYBBX_OK;
    }
    if (rc == HYBBX_ERR_NOT_FOUND) {
        hybbx_session_write_line(session,
            "No peer links available to relay your message.");
        return HYBBX_OK;
    }

    return rc;
}

void hybbx_proxychat_receive(hybbx_service_t *service,
                             const char *from_address,
                             const char *line)
{
    proxychat_fanout_ctx_t ctx;

    if (service == NULL || line == NULL || line[0] == '\0') {
        return;
    }

    ctx.from = NULL;
    ctx.line = line;
    ctx.from_address = from_address;
    hybbx_service_visit_sessions(service, proxychat_local_visitor, &ctx);
}

void hybbx_proxychat_show_banner(hybbx_session_t *session)
{
    if (session == NULL) {
        return;
    }

    hybbx_session_write_line(session,
        "Proxychat — talk with users on linked mains.");
    if (hybbx_mains_proxy_mesh_active()) {
        hybbx_session_write_line(session,
            "Type a line to send; /leave or /main to exit.");
    } else {
        hybbx_session_write_line(session,
            "No peer links active — check mains_proxy configuration.");
        hybbx_session_write_line(session,
            "Type a line to try; /leave or /main to exit.");
    }
}
git clone -b <branch> https://cgit.mode42.com/<repo>.git
git clone -b <branch> git://cgit.mode42.com/<repo>.git

info@mode42.com