summaryrefslogtreecommitdiff
path: root/src/plugins/relay/relay-client-weechat.c
blob: 9ae63eeb87d0b624b73c342e43f8a3d2ff74028e (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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*
 * Copyright (C) 2003-2010 Sebastien Helleu <flashcode@flashtux.org>
 *
 * This file is part of WeeChat, the extensible chat client.
 *
 * WeeChat is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * WeeChat is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with WeeChat.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
 * relay-client-weechat.c: WeeChat protocol for relay to client
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <errno.h>

#include "../weechat-plugin.h"
#include "relay.h"
#include "relay-client-weechat.h"
#include "relay-client.h"


/*
 * relay_client_weechat_sendf: send formatted data to client
 */

int
relay_client_weechat_sendf (struct t_relay_client *client,
                            const char *format, ...)
{
    va_list args;
    static char buffer[4096];
    char str_length[8];
    int length, num_sent;
    
    if (!client)
        return 0;
    
    va_start (args, format);
    vsnprintf (buffer + 7, sizeof (buffer) - 7 - 1, format, args);
    va_end (args);
    
    length = strlen (buffer + 7);
    snprintf (str_length, sizeof (str_length), "%07d", length);
    memcpy (buffer, str_length, 7);
    
    num_sent = send (client->sock, buffer, length + 7, 0);
    
    client->bytes_sent += length + 7;
    
    if (num_sent < 0)
    {
        weechat_printf (NULL,
                        _("%s%s: error sending data to client %s"),
                        weechat_prefix ("error"), RELAY_PLUGIN_NAME,
                        strerror (errno));
    }
    
    return num_sent;
}

/*
 * relay_client_weechat_send_infolist: send infolist to client
 */

void
relay_client_weechat_send_infolist (struct t_relay_client *client,
                                    const char *name,
                                    struct t_infolist *infolist)
{
    const char *fields;
    char **argv;
    int i, argc, size;
    
    relay_client_weechat_sendf (client, "name %s", name);
    
    while (weechat_infolist_next (infolist))
    {
        fields = weechat_infolist_fields (infolist);
        if (fields)
        {
            argv = weechat_string_split (fields, ",", 0, 0, &argc);
            if (argv && (argc > 0))
            {
                for (i = 0; i < argc; i++)
                {
                    switch (argv[i][0])
                    {
                        case 'i':
                            relay_client_weechat_sendf (client, "%s %c %d",
                                                        argv[i] + 2, argv[i][0],
                                                        weechat_infolist_integer (infolist,
                                                                                  argv[i] + 2));
                            break;
                        case 's':
                            relay_client_weechat_sendf (client, "%s %c %s",
                                                        argv[i] + 2, argv[i][0],
                                                        weechat_infolist_string (infolist,
                                                                                 argv[i] + 2));
                            break;
                        case 'p':
                            relay_client_weechat_sendf (client, "%s %c %lx",
                                                        argv[i] + 2, argv[i][0],
                                                        (long unsigned int)weechat_infolist_pointer (infolist,
                                                                                                     argv[i] + 2));
                            break;
                        case 'b':
                            relay_client_weechat_sendf (client, "%s %c %lx",
                                                        argv[i] + 2, argv[i][0],
                                                        (long unsigned int)weechat_infolist_buffer (infolist,
                                                                                                    argv[i] + 2,
                                                                                                      &size));
                            break;
                        case 't':
                            relay_client_weechat_sendf (client, "%s %c %ld",
                                                        argv[i] + 2, argv[i][0],
                                                        weechat_infolist_time (infolist, argv[i] + 2));
                            break;
                    }
                }
            }
            if (argv)
                weechat_string_free_split (argv);
        }
    }
}

/*
 * relay_client_weechat_recv_one_msg: read one message from client
 */

void
relay_client_weechat_recv_one_msg (struct t_relay_client *client, char *data)
{
    char *pos;
    struct t_infolist *infolist;
    
    pos = strchr (data, '\r');
    if (pos)
        pos[0] = '\0';
    
    if (weechat_relay_plugin->debug)
    {
        weechat_printf (NULL, "relay: weechat: \"%s\"", data);
    }
    
    if (weechat_strcasecmp (data, "quit") == 0)
        relay_client_set_status (client, RELAY_STATUS_DISCONNECTED);
    else
    {
        infolist = weechat_infolist_get (data, NULL, NULL);
        if (infolist)
        {
            relay_client_weechat_send_infolist (client, data, infolist);
            weechat_infolist_free (infolist);
        }
    }
}

/*
 * relay_client_weechat_recv: read data from client
 */

void
relay_client_weechat_recv (struct t_relay_client *client, const char *data)
{
    char **items;
    int items_count, i;
    
    items = weechat_string_split (data, "\n", 0, 0, &items_count);
    for (i = 0; i < items_count; i++)
    {
        relay_client_weechat_recv_one_msg (client, items[i]);
    }
    if (items)
        weechat_string_free_split (items);
}

/*
 * relay_client_weechat_alloc: init relay data specific to weechat protocol
 */

void
relay_client_weechat_alloc (struct t_relay_client *client)
{
    struct t_relay_client_weechat_data *weechat_data;
    
    client->protocol_data = malloc (sizeof (*weechat_data));
    if (client->protocol_data)
    {
        /* ... */
    }
}

/*
 * relay_client_weechat_alloc_with_infolist: init relay data specific to
 *                                           weechat protocol with an infolist
 */

void
relay_client_weechat_alloc_with_infolist (struct t_relay_client *client,
                                          struct t_infolist *infolist)
{
    struct t_relay_client_weechat_data *weechat_data;
    
    /* make C compiler happy */
    (void) infolist;
    
    client->protocol_data = malloc (sizeof (*weechat_data));
    if (client->protocol_data)
    {
        /* ... */
    }
}

/*
 * relay_client_weechat_free: free relay data specific to weechat protocol
 */

void
relay_client_weechat_free (struct t_relay_client *client)
{
    if (client->protocol_data)
        free (client->protocol_data);
}

/*
 * relay_client_weechat_add_to_infolist: add client weechat data in an
 *                                       infolist item
 *                                       return 1 if ok, 0 if error
 */

int
relay_client_weechat_add_to_infolist (struct t_infolist_item *item,
                                      struct t_relay_client *client)
{
    if (!item || !client)
        return 0;
    
    return 1;
}

/*
 * relay_client_weechat_print_log: print weechat client infos in log (usually
 *                                 for crash dump)
 */

void
relay_client_weechat_print_log (struct t_relay_client *client)
{
    if (client->protocol_data)
    {
    }
}