summaryrefslogtreecommitdiff
path: root/src/plugins/relay/relay-buffer.c
blob: 1a95fc3244387702ba73b9ea6e27e2ca37b233a4 (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
/*
 * 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-buffer.c: display clients list on relay buffer
 */

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

#include "../weechat-plugin.h"
#include "relay.h"
#include "relay-buffer.h"
#include "relay-client.h"
#include "relay-config.h"
#include "relay-raw.h"


struct t_gui_buffer *relay_buffer = NULL;
int relay_buffer_selected_line = 0;


/*
 * relay_buffer_refresh: update a client in buffer and update hotlist for
 *                       relay buffer
 */

void
relay_buffer_refresh (const char *hotlist)
{
    struct t_relay_client *ptr_client, *client_selected;
    char str_color[256], status[64], date_start[128], date_end[128];
    char *str_recv, *str_sent;
    int i, length, line;
    struct tm *date_tmp;
    
    if (relay_buffer)
    {
        weechat_buffer_clear (relay_buffer);
        line = 0;
        client_selected = relay_client_search_by_number (relay_buffer_selected_line);
        if (client_selected)
        {
            weechat_printf_y (relay_buffer, 0,
                              "%s%s%s%s%s%s%s",
                              weechat_color("green"),
                              _("Actions (letter+enter):"),
                              weechat_color("lightgreen"),
                              /* disconnect */
                              (RELAY_CLIENT_HAS_ENDED(client_selected->status)) ?
                              "" : _("  [D] Disconnect"),
                              /* remove */
                              (RELAY_CLIENT_HAS_ENDED(client_selected->status)) ?
                              _("  [R] Remove") : "",
                              /* purge old */
                              _("  [P] Purge finished"),
                              /* quit */
                              _("  [Q] Close this buffer"));
        }
        for (ptr_client = relay_clients; ptr_client;
             ptr_client = ptr_client->next_client)
        {
            snprintf (str_color, sizeof (str_color),
                      "%s,%s",
                      (line == relay_buffer_selected_line) ?
                      weechat_config_string (relay_config_color_text_selected) :
                      weechat_config_string (relay_config_color_text),
                      weechat_config_string (relay_config_color_text_bg));
            
            snprintf (status, sizeof (status),
                      "%s", _(relay_client_status_string[ptr_client->status]));
            length = weechat_utf8_strlen_screen (status);
            if (length < 20)
            {
                for (i = 0; i < 20 - length; i++)
                {
                    strcat (status, " ");
                }
            }
            
            date_tmp = localtime (&(ptr_client->start_time));
            strftime (date_start, sizeof (date_start),
                      "%a, %d %b %Y %H:%M:%S", date_tmp);
            date_end[0] = '\0';
            if (ptr_client->end_time > 0)
            {
                date_tmp = localtime (&(ptr_client->end_time));
                strftime (date_end, sizeof (date_end),
                          "%a, %d %b %Y %H:%M:%S", date_tmp);
            }
            
            /* first line with status and start time */
            weechat_printf_y (relay_buffer, (line * 2) + 2,
                              _("%s%s[%s%s%s%s] %s (started on: %s%s%s%s)"),
                              weechat_color(str_color),
                              (line == relay_buffer_selected_line) ?
                              "*** " : "    ",
                              weechat_color(weechat_config_string (relay_config_color_status[ptr_client->status])),
                              status,
                              weechat_color ("reset"),
                              weechat_color (str_color),
                              ptr_client->address,
                              date_start,
                              (ptr_client->end_time > 0) ? ", " : "",
                              (ptr_client->end_time > 0) ? _("ended on: ") : "",
                              (ptr_client->end_time > 0) ? date_end : "");
            
            /* second line with protocol and bytes recv/sent */
            str_recv = weechat_string_format_size (ptr_client->bytes_recv);
            str_sent = weechat_string_format_size (ptr_client->bytes_sent);
            weechat_printf_y (relay_buffer, (line * 2) + 3,
                              _("%s%-26s id: %d, protocol: %s, received: %s, sent: %s"),
                              weechat_color(str_color),
                              " ",
                              ptr_client->id,
                              relay_protocol_string[ptr_client->protocol],
                              (str_recv) ? str_recv : "?",
                              (str_sent) ? str_sent : "?");
            if (str_recv)
                free (str_recv);
            if (str_sent)
                free (str_sent);
            
            line++;
        }
        if (hotlist)
            weechat_buffer_set (relay_buffer, "hotlist", hotlist);
    }
}

/*
 * relay_buffer_input_cb: callback called when user send data to client list
 *                        buffer
 */

int
relay_buffer_input_cb (void *data, struct t_gui_buffer *buffer,
                       const char *input_data)
{
    struct t_relay_client *client, *ptr_client, *next_client;
    
    /* make C compiler happy */
    (void) data;
    
    if (buffer == relay_raw_buffer)
    {
        if (weechat_strcasecmp (input_data, "q") == 0)
            weechat_buffer_close (buffer);
    }
    else if (buffer == relay_buffer)
    {
        client = relay_client_search_by_number (relay_buffer_selected_line);
        
        /* disconnect client */
        if (weechat_strcasecmp (input_data, "d") == 0)
        {
            if (client && !RELAY_CLIENT_HAS_ENDED(client->status))
            {
                relay_client_disconnect (client);
                relay_buffer_refresh (WEECHAT_HOTLIST_MESSAGE);
            }
        }
        /* purge old clients */
        else if (weechat_strcasecmp (input_data, "p") == 0)
        {
            ptr_client = relay_clients;
            while (ptr_client)
            {
                next_client = ptr_client->next_client;
                if (RELAY_CLIENT_HAS_ENDED(ptr_client->status))
                    relay_client_free (ptr_client);
                ptr_client = next_client;
            }
            relay_buffer_refresh (WEECHAT_HOTLIST_MESSAGE);
        }
        /* quit relay buffer (close it) */
        else if (weechat_strcasecmp (input_data, "q") == 0)
        {
            weechat_buffer_close (buffer);
        }
        /* remove client */
        else if (weechat_strcasecmp (input_data, "r") == 0)
        {
            if (client && RELAY_CLIENT_HAS_ENDED(client->status))
            {
                relay_client_free (client);
                relay_buffer_refresh (WEECHAT_HOTLIST_MESSAGE);
            }
        }
    }
    
    return WEECHAT_RC_OK;
}

/*
 * relay_buffer_close_cb: callback called when relay buffer is closed
 */

int
relay_buffer_close_cb (void *data, struct t_gui_buffer *buffer)
{
    /* make C compiler happy */
    (void) data;
    
    if (buffer == relay_raw_buffer)
    {
        relay_raw_buffer = NULL;
    }
    else if (buffer == relay_buffer)
    {
        relay_buffer = NULL;
    }
    
    return WEECHAT_RC_OK;
}

/*
 * relay_buffer_open: open relay buffer (to display list of clients)
 */

void
relay_buffer_open ()
{
    if (!relay_buffer)
    {
        relay_buffer = weechat_buffer_new (RELAY_BUFFER_NAME,
                                           &relay_buffer_input_cb, NULL,
                                           &relay_buffer_close_cb, NULL);
        
        /* failed to create buffer ? then exit */
        if (!relay_buffer)
            return;
        
        weechat_buffer_set (relay_buffer, "type", "free");
        weechat_buffer_set (relay_buffer, "title", _("List of clients for relay"));
        weechat_buffer_set (relay_buffer, "key_bind_meta2-A", "/relay up");
        weechat_buffer_set (relay_buffer, "key_bind_meta2-B", "/relay down");
        weechat_buffer_set (relay_buffer, "localvar_set_type", "relay");
    }
}