summaryrefslogtreecommitdiff
path: root/src/plugins/relay/relay-protocol-weechat.c
blob: 1e007c0b1aea9c9349452fead95f83f77b2d3ac8 (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
/*
 * Copyright (c) 2003-2010 by FlashCode <flashcode@flashtux.org>
 * See README for License detail, AUTHORS for developers list.
 *
 * This program 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.
 *
 * This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/* relay-protocol-weechat.c: WeeChat protocol for 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-protocol-weechat.h"
#include "relay-client.h"


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

int
relay_protocol_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_protocol_weechat_send_infolist: send infolist to client
 */

void
relay_protocol_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_protocol_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_protocol_weechat_sendf (client, "%s %c %d",
                                                          argv[i] + 2, argv[i][0],
                                                          weechat_infolist_integer (infolist,
                                                                                    argv[i] + 2));
                            break;
                        case 's':
                            relay_protocol_weechat_sendf (client, "%s %c %s",
                                                          argv[i] + 2, argv[i][0],
                                                          weechat_infolist_string (infolist,
                                                                                   argv[i] + 2));
                            break;
                        case 'p':
                            relay_protocol_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_protocol_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_protocol_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_protocol_weechat_recv_one_msg: read one message from client
 */

void
relay_protocol_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_protocol_weechat_send_infolist (client, data, infolist);
            weechat_infolist_free (infolist);
        }
    }
}

/*
 * relay_protocol_weechat_recv: read data from client
 */

void
relay_protocol_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_protocol_weechat_recv_one_msg (client, items[i]);
    }
    if (items)
        weechat_string_free_split (items);
}

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

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

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

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

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

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