summaryrefslogtreecommitdiff
path: root/src/plugins/xfer/xfer-chat.c
blob: 579e47fd0ccf332eed640de15a16df94198c86bd (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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*
 * Copyright (c) 2003-2009 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/>.
 */

/* xfer-chat.c: chat with direct connection to remote host */


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

#include "../weechat-plugin.h"
#include "xfer.h"
#include "xfer-chat.h"
#include "xfer-buffer.h"


/*
 * xfer_chat_send: send data to remote host via xfer chat
 */

int
xfer_chat_send (struct t_xfer *xfer, const char *buffer, int size_buf)
{
    if (!xfer)
        return -1;
    
    return send (xfer->sock, buffer, size_buf, 0);
}

/*
 * xfer_chat_sendf: send formatted data to remote host via DCC CHAT
 */

void
xfer_chat_sendf (struct t_xfer *xfer, const char *format, ...)
{
    va_list args;
    static char buffer[4096];
    int size_buf;
    
    if (!xfer || (xfer->sock < 0))
        return;
    
    va_start (args, format);
    size_buf = vsnprintf (buffer, sizeof (buffer) - 1, format, args);
    va_end (args);
    
    if (size_buf == 0)
        return;
    
    buffer[sizeof (buffer) - 1] = '\0';
    if ((size_buf < 0) || (size_buf > (int) (sizeof (buffer) - 1)))
        size_buf = strlen (buffer);
    
    if (xfer_chat_send (xfer, buffer, strlen (buffer)) <= 0)
    {
        weechat_printf (NULL,
                        _("%s%s: error sending data to \"%s\" via xfer chat"),
                        weechat_prefix ("error"), XFER_PLUGIN_NAME,
                        xfer->remote_nick);
        xfer_close (xfer, XFER_STATUS_FAILED);
    }
}

/*
 * xfer_chat_recv_cb: receive data from xfer chat remote host
 */

int
xfer_chat_recv_cb (void *arg_xfer, int fd)
{
    struct t_xfer *xfer;
    static char buffer[4096 + 2];
    char *buf2, *pos, *ptr_buf, *next_ptr_buf;
    char *ptr_buf_without_weechat_colors, *ptr_buf_color;
    int num_read;
    
    /* make C compiler happy */
    (void) fd;
    
    xfer = (struct t_xfer *)arg_xfer;
    
    num_read = recv (xfer->sock, buffer, sizeof (buffer) - 2, 0);
    if (num_read > 0)
    {
        buffer[num_read] = '\0';
        
        buf2 = NULL;
        ptr_buf = buffer;
        if (xfer->unterminated_message)
        {
            buf2 = malloc (strlen (xfer->unterminated_message) +
                                   strlen (buffer) + 1);
            if (buf2)
            {
                strcpy (buf2, xfer->unterminated_message);
                strcat (buf2, buffer);
            }
            ptr_buf = buf2;
            free (xfer->unterminated_message);
            xfer->unterminated_message = NULL;
        }
        
        while (ptr_buf && ptr_buf[0])
        {
            next_ptr_buf = NULL;
            pos = strstr (ptr_buf, "\n");
            if (pos)
            {
                pos[0] = '\0';
                next_ptr_buf = pos + 1;
            }
            else
            {
                xfer->unterminated_message = strdup (ptr_buf);
                ptr_buf = NULL;
                next_ptr_buf = NULL;
            }
            
            if (ptr_buf)
            {
                ptr_buf_without_weechat_colors = weechat_string_remove_color (ptr_buf, "?");
                ptr_buf_color = weechat_hook_modifier_exec ("irc_color_decode",
                                                            "1",
                                                            (ptr_buf_without_weechat_colors) ?
                                                            ptr_buf_without_weechat_colors : ptr_buf);
                weechat_printf_tags (xfer->buffer, "notify_message", "%s\t%s",
                                     xfer->remote_nick,
                                     (ptr_buf_color) ?
                                     ptr_buf_color : ((ptr_buf_without_weechat_colors) ? ptr_buf_without_weechat_colors : ptr_buf));
                if (ptr_buf_without_weechat_colors)
                    free (ptr_buf_without_weechat_colors);
                if (ptr_buf_color)
                    free (ptr_buf_color);
            }
            
            ptr_buf = next_ptr_buf;
        }
        
        if (buf2)
            free (buf2);
    }
    else
    {
        xfer_close (xfer, XFER_STATUS_ABORTED);
        xfer_buffer_refresh (WEECHAT_HOTLIST_MESSAGE);
    }
    
    return WEECHAT_RC_OK;
}

/*
 * xfer_chat_buffer_input_cb: callback called when user send data to xfer chat
 *                            buffer
 */

int
xfer_chat_buffer_input_cb (void *data, struct t_gui_buffer *buffer,
                           const char *input_data)
{
    struct t_xfer *ptr_xfer;
    char *input_data_color;
    
    /* make C compiler happy */
    (void) data;
    
    ptr_xfer = xfer_search_by_buffer (buffer);
    
    if (!ptr_xfer)
    {
        weechat_printf (NULL,
                        _("%s%s: can't find xfer for buffer \"%s\""),
                        weechat_prefix ("error"), XFER_PLUGIN_NAME,
                        weechat_buffer_get_string (buffer, "name"));
        return WEECHAT_RC_OK;
    }
    
    if (!XFER_HAS_ENDED(ptr_xfer->status))
    {
        xfer_chat_sendf (ptr_xfer, "%s\n", input_data);
        if (!XFER_HAS_ENDED(ptr_xfer->status))
        {
            input_data_color = weechat_hook_modifier_exec ("irc_color_decode",
                                                           "1",
                                                           input_data);
            weechat_printf (buffer,
                            "%s\t%s",
                            ptr_xfer->local_nick,
                            (input_data_color) ? input_data_color : input_data);
            if (input_data_color)
                free (input_data_color);
        }
    }
    
    return WEECHAT_RC_OK;
}

/*
 * xfer_chat_close_buffer_cb: callback called when a buffer with direct chat
 *                            is closed
 */

int
xfer_chat_buffer_close_cb (void *data, struct t_gui_buffer *buffer)
{
    struct t_xfer *ptr_xfer;
    
    /* make C compiler happy */
    (void) data;
    (void) buffer;
    
    ptr_xfer = xfer_search_by_buffer (buffer);
    
    if (ptr_xfer)
    {    
        if (!XFER_HAS_ENDED(ptr_xfer->status))
        {
            xfer_close (ptr_xfer, XFER_STATUS_ABORTED);
            xfer_buffer_refresh (WEECHAT_HOTLIST_MESSAGE);
        }
        
        ptr_xfer->buffer = NULL;
    }
    
    return WEECHAT_RC_OK;
}

/*
 * xfer_chat_open_buffer: create buffer for DCC chat
 */

void
xfer_chat_open_buffer (struct t_xfer *xfer)
{
    char *name;
    int length;
    
    length = strlen (xfer->plugin_name) + 8 + strlen (xfer->remote_nick) + 1;
    name = malloc (length);
    if (name)
    {
        snprintf (name, length, "%s_dcc_%s",
                  xfer->plugin_name, xfer->remote_nick);
        xfer->buffer = weechat_buffer_search (XFER_PLUGIN_NAME, name);
        if (!xfer->buffer)
        {
            xfer->buffer = weechat_buffer_new (name,
                                               &xfer_chat_buffer_input_cb, NULL,
                                               &xfer_chat_buffer_close_cb, NULL);
            
            /* failed to create buffer ? then return */
            if (!xfer->buffer)
                return;
        }
        
        weechat_buffer_set (xfer->buffer, "title", _("xfer chat"));
        weechat_buffer_set (xfer->buffer, "short_name", xfer->remote_nick);
        weechat_buffer_set (xfer->buffer, "localvar_set_type", "private");
        weechat_buffer_set (xfer->buffer, "localvar_set_nick", xfer->local_nick);
        weechat_buffer_set (xfer->buffer, "localvar_set_channel", xfer->remote_nick);
        weechat_buffer_set (xfer->buffer, "highlight_words", xfer->local_nick);
        weechat_printf (xfer->buffer,
                        _("Connected to %s (%d.%d.%d.%d) via "
                          "xfer chat"),
                        xfer->remote_nick,
                        xfer->address >> 24,
                        (xfer->address >> 16) & 0xff,
                        (xfer->address >> 8) & 0xff,
                        xfer->address & 0xff);
        
        free (name);
    }
}