Пример #1
0
int
xfer_network_connect (struct t_xfer *xfer)
{
    if (xfer->type == XFER_TYPE_CHAT_SEND)
        xfer->status = XFER_STATUS_WAITING;
    else
        xfer->status = XFER_STATUS_CONNECTING;
    
    if (xfer->sock < 0)
    {
        xfer->sock = socket (AF_INET, SOCK_STREAM, 0);
        if (xfer->sock < 0)
            return 0;
    }
    
    if (XFER_IS_SEND(xfer->type))
    {
        /* listen to socket */
        if (fcntl (xfer->sock, F_SETFL, O_NONBLOCK) == -1)
            return 0;	
        if (listen (xfer->sock, 1) == -1)
            return 0;
        if (fcntl (xfer->sock, F_SETFL, 0) == -1)
            return 0;

        xfer->hook_fd = weechat_hook_fd (xfer->sock,
                                         1, 0, 0,
                                         &xfer_network_fd_cb,
                                         xfer);
        
        /* add timeout */
        if (weechat_config_integer (xfer_config_network_timeout) > 0)
        {
            xfer->hook_timer = weechat_hook_timer (weechat_config_integer (xfer_config_network_timeout) * 1000,
                                                   0, 1,
                                                   &xfer_network_timer_cb,
                                                   xfer);
        }
    }
    
    /* for chat receiving, connect to listening host */
    if (xfer->type == XFER_TYPE_CHAT_RECV)
    {
        if (fcntl (xfer->sock, F_SETFL, O_NONBLOCK) == -1)
            return 0;
        weechat_network_connect_to (xfer->proxy, xfer->sock, xfer->address,
                                    xfer->port);
        
        xfer->hook_fd = weechat_hook_fd (xfer->sock,
                                         1, 0, 0,
                                         &xfer_chat_recv_cb,
                                         xfer);
    }
    
    /* for file receiving, connection is made in child process (blocking) */
    
    return 1;
}
Пример #2
0
void
xfer_buffer_refresh (const char *hotlist)
{
    struct t_xfer *ptr_xfer, *xfer_selected;
    char str_color[256], suffix[32], status[64], date[128], eta[128];
    char str_ip[128], str_hash[128];
    char *progress_bar, *str_pos, *str_total, *str_bytes_per_sec;
    int i, length, line, progress_bar_size, num_bars;
    unsigned long long pos, pct_complete;
    struct tm *date_tmp;

    if (xfer_buffer)
    {
        weechat_buffer_clear (xfer_buffer);
        line = 0;
        xfer_selected = xfer_search_by_number (xfer_buffer_selected_line);
        weechat_printf_y (xfer_buffer, 0,
                          "%s%s%s%s%s%s%s%s",
                          weechat_color ("green"),
                          _("Actions (letter+enter):"),
                          weechat_color ("lightgreen"),
                          /* accept */
                          (xfer_selected && XFER_IS_RECV(xfer_selected->type)
                           && (xfer_selected->status == XFER_STATUS_WAITING)) ?
                          _("  [A] Accept") : "",
                          /* cancel */
                          (xfer_selected
                           && !XFER_HAS_ENDED(xfer_selected->status)) ?
                          _("  [C] Cancel") : "",
                          /* remove */
                          (xfer_selected
                           && XFER_HAS_ENDED(xfer_selected->status)) ?
                          _("  [R] Remove") : "",
                          /* purge old */
                          _("  [P] Purge finished"),
                          /* quit */
                          _("  [Q] Close this buffer"));
        for (ptr_xfer = xfer_list; ptr_xfer; ptr_xfer = ptr_xfer->next_xfer)
        {
            suffix[0] = '\0';
            if (ptr_xfer->filename_suffix >= 0)
            {
                snprintf (suffix, sizeof (suffix),
                          " (.%d)", ptr_xfer->filename_suffix);
            }

            snprintf (str_color, sizeof (str_color),
                      "%s,%s",
                      (line == xfer_buffer_selected_line) ?
                      weechat_config_string (xfer_config_color_text_selected) :
                      weechat_config_string (xfer_config_color_text),
                      weechat_config_string (xfer_config_color_text_bg));

            str_ip[0] = '\0';
            if (ptr_xfer->remote_address_str)
            {
                snprintf (str_ip, sizeof (str_ip),
                          " (%s)",
                          ptr_xfer->remote_address_str);
            }

            str_hash[0] = '\0';
            if (ptr_xfer->hash_target
                && ptr_xfer->hash_handle
                && (ptr_xfer->hash_status != XFER_HASH_STATUS_UNKNOWN)
                && ((ptr_xfer->status == XFER_STATUS_ACTIVE)
                    || (ptr_xfer->status == XFER_STATUS_DONE)
                    || (ptr_xfer->status == XFER_STATUS_HASHING)))
            {
                snprintf (str_hash, sizeof (str_hash),
                          " (%s)",
                          _(xfer_hash_status_string[ptr_xfer->hash_status]));
            }

            /* display first line with remote nick, filename and plugin name/id */
            weechat_printf_y (xfer_buffer, (line * 2) + 2,
                              "%s%s%-24s %s%s%s%s (%s.%s)%s%s",
                              weechat_color (str_color),
                              (line == xfer_buffer_selected_line) ?
                              "*** " : "    ",
                              ptr_xfer->remote_nick,
                              (XFER_IS_FILE(ptr_xfer->type)) ? "\"" : "",
                              (XFER_IS_FILE(ptr_xfer->type)) ?
                              ptr_xfer->filename : _("xfer chat"),
                              (XFER_IS_FILE(ptr_xfer->type)) ? "\"" : "",
                              suffix,
                              ptr_xfer->plugin_name,
                              ptr_xfer->plugin_id,
                              str_ip,
                              str_hash);

            snprintf (status, sizeof (status),
                      "%s", _(xfer_status_string[ptr_xfer->status]));
            length = weechat_utf8_strlen_screen (status);
            if (length < 20)
            {
                for (i = 0; i < 20 - length; i++)
                {
                    strcat (status, " ");
                }
            }

            if (XFER_IS_CHAT(ptr_xfer->type))
            {
                /* display second line for chat with status and date */
                date[0] = '\0';
                date_tmp = localtime (&(ptr_xfer->start_time));
                if (date_tmp)
                {
                    if (strftime (date, sizeof (date),
                                  "%a, %d %b %Y %H:%M:%S", date_tmp) == 0)
                        date[0] = '\0';
                }
                weechat_printf_y (xfer_buffer, (line * 2) + 3,
                                  "%s%s%s %s%s%s%s%s",
                                  weechat_color (str_color),
                                  (line == xfer_buffer_selected_line) ?
                                  "*** " : "    ",
                                  (XFER_IS_SEND(ptr_xfer->type)) ?
                                  "<<--" : "-->>",
                                  weechat_color (weechat_config_string (xfer_config_color_status[ptr_xfer->status])),
                                  status,
                                  weechat_color ("reset"),
                                  weechat_color (str_color),
                                  date);
            }
            else
            {
                /* build progress bar */
                pos = (ptr_xfer->pos <= ptr_xfer->size) ? ptr_xfer->pos : ptr_xfer->size;
                progress_bar = NULL;
                progress_bar_size = weechat_config_integer (xfer_config_look_progress_bar_size);
                if (progress_bar_size > 0)
                {
                    progress_bar = malloc (1 + progress_bar_size + 1 + 1 + 1);
                    strcpy (progress_bar, "[");
                    if (ptr_xfer->size == 0)
                    {
                        if (ptr_xfer->status == XFER_STATUS_DONE)
                            num_bars = progress_bar_size;
                        else
                            num_bars = 0;
                    }
                    else
                        num_bars = (int)(((float)(pos)/(float)(ptr_xfer->size)) * (float)progress_bar_size);
                    for (i = 0; i < num_bars - 1; i++)
                    {
                        strcat (progress_bar, "=");
                    }
                    if (num_bars > 0)
                        strcat (progress_bar, ">");
                    for (i = 0; i < progress_bar_size - num_bars; i++)
                    {
                        strcat (progress_bar, " ");
                    }
                    strcat (progress_bar, "] ");
                }

                /* computes percentage */
                if (ptr_xfer->size == 0)
                {
                    if (ptr_xfer->status == XFER_STATUS_DONE)
                        pct_complete = 100;
                    else
                        pct_complete = 0;
                }
                else
                    pct_complete = (unsigned long long)(((float)(pos)/(float)(ptr_xfer->size)) * 100);

                /* position, total and bytes per second */
                str_pos = weechat_string_format_size (pos);
                str_total = weechat_string_format_size (ptr_xfer->size);
                str_bytes_per_sec = weechat_string_format_size (ptr_xfer->bytes_per_sec);

                /* ETA */
                eta[0] = '\0';
                if (ptr_xfer->status == XFER_STATUS_ACTIVE)
                {
                    snprintf (eta, sizeof (eta),
                              "%s: %.2llu:%.2llu:%.2llu - ",
                              _("ETA"),
                              ptr_xfer->eta / 3600,
                              (ptr_xfer->eta / 60) % 60,
                              ptr_xfer->eta % 60);
                }

                /* display second line for file with status, progress bar and estimated time */
                weechat_printf_y (xfer_buffer, (line * 2) + 3,
                                  "%s%s%s %s%s%s%s%3llu%%   %s / %s  (%s%s/s)",
                                  weechat_color (str_color),
                                  (line == xfer_buffer_selected_line) ? "*** " : "    ",
                                  (XFER_IS_SEND(ptr_xfer->type)) ? "<<--" : "-->>",
                                  weechat_color (weechat_config_string (xfer_config_color_status[ptr_xfer->status])),
                                  status,
                                  weechat_color (str_color),
                                  (progress_bar) ? progress_bar : "",
                                  pct_complete,
                                  (str_pos) ? str_pos : "?",
                                  (str_total) ? str_total : "?",
                                  eta,
                                  str_bytes_per_sec);
                if (progress_bar)
                    free (progress_bar);
                if (str_pos)
                    free (str_pos);
                if (str_total)
                    free (str_total);
                if (str_bytes_per_sec)
                    free (str_bytes_per_sec);
            }
            line++;
        }
        weechat_buffer_set (xfer_buffer, "hotlist", hotlist);
    }
}
Пример #3
0
void
xfer_command_xfer_list (int full)
{
    struct t_xfer *ptr_xfer;
    int i;
    char date[128];
    unsigned long long pct_complete;
    struct tm *date_tmp;

    if (xfer_list)
    {
        dogechat_printf (NULL, "");
        dogechat_printf (NULL, _("Xfer list:"));
        i = 1;
        for (ptr_xfer = xfer_list; ptr_xfer; ptr_xfer = ptr_xfer->next_xfer)
        {
            /* xfer info */
            if (XFER_IS_FILE(ptr_xfer->type))
            {
                if (ptr_xfer->size == 0)
                {
                    if (ptr_xfer->status == XFER_STATUS_DONE)
                        pct_complete = 100;
                    else
                        pct_complete = 0;
                }
                else
                    pct_complete = (unsigned long long)(((float)(ptr_xfer->pos)/(float)(ptr_xfer->size)) * 100);

                dogechat_printf (NULL,
                                _("%3d. %s (%s), file: \"%s\" (local: "
                                  "\"%s\"), %s %s, status: %s%s%s "
                                  "(%llu %%)"),
                                i,
                                xfer_type_string[ptr_xfer->type],
                                xfer_protocol_string[ptr_xfer->protocol],
                                ptr_xfer->filename,
                                ptr_xfer->local_filename,
                                (XFER_IS_SEND(ptr_xfer->type)) ?
                                _("sent to") : _("received from"),
                                ptr_xfer->remote_nick,
                                dogechat_color (
                                    dogechat_config_string (
                                        xfer_config_color_status[ptr_xfer->status])),
                                _(xfer_status_string[ptr_xfer->status]),
                                dogechat_color ("chat"),
                                pct_complete);
            }
            else
            {
                date[0] = '\0';
                date_tmp = localtime (&(ptr_xfer->start_time));
                if (date_tmp)
                {
                    strftime (date, sizeof (date),
                              "%a, %d %b %Y %H:%M:%S", date_tmp);
                }
                dogechat_printf (NULL,
                                /* TRANSLATORS: "%s" after "started on" is a date */
                                _("%3d. %s, chat with %s (local nick: %s), "
                                  "started on %s, status: %s%s"),
                                i,
                                xfer_type_string[ptr_xfer->type],
                                ptr_xfer->remote_nick,
                                ptr_xfer->local_nick,
                                date,
                                dogechat_color(
                                    dogechat_config_string(
                                        xfer_config_color_status[ptr_xfer->status])),
                                _(xfer_status_string[ptr_xfer->status]));
            }

            if (full)
            {
                /* second line of xfer info */
                if (XFER_IS_FILE(ptr_xfer->type))
                {
                    dogechat_printf (NULL,
                                    _("     plugin: %s (id: %s), file: %llu "
                                      "bytes (position: %llu), address: "
                                      "%s (port %d)"),
                                    ptr_xfer->plugin_name,
                                    ptr_xfer->plugin_id,
                                    ptr_xfer->size,
                                    ptr_xfer->pos,
                                    ptr_xfer->remote_address_str,
                                    ptr_xfer->port);
                    date[0] = '\0';
                    date_tmp = localtime (&(ptr_xfer->start_transfer));
                    if (date_tmp)
                    {
                        strftime (date, sizeof (date),
                                  "%a, %d %b %Y %H:%M:%S", date_tmp);
                    }
                    dogechat_printf (NULL,
                                    /* TRANSLATORS: "%s" after "started on" is a date */
                                    _("     fast_send: %s, blocksize: %d, "
                                      "started on %s"),
                                    (ptr_xfer->fast_send) ? _("yes") : _("no"),
                                    ptr_xfer->blocksize,
                                    date);
                }
            }
            i++;
        }
    }
    else
        dogechat_printf (NULL, _("No xfer"));
}
Пример #4
0
int
xfer_network_connect (struct t_xfer *xfer)
{
    int flags;

    if (xfer->type == XFER_TYPE_CHAT_SEND)
        xfer->status = XFER_STATUS_WAITING;
    else
        xfer->status = XFER_STATUS_CONNECTING;

    if (XFER_IS_SEND(xfer->type))
    {
        /* create socket */
        if (xfer->sock < 0)
        {
            xfer->sock = socket (xfer->local_address->sa_family, SOCK_STREAM,
                                 0);
            if (xfer->sock < 0)
                return 0;
        }

        /* listen to socket */
        flags = fcntl (xfer->sock, F_GETFL);
        if (flags == -1)
            flags = 0;
        if (fcntl (xfer->sock, F_SETFL, flags | O_NONBLOCK) == -1)
            return 0;
        if (listen (xfer->sock, 1) == -1)
            return 0;
        if (fcntl (xfer->sock, F_SETFL, flags) == -1)
            return 0;

        xfer->hook_fd = weechat_hook_fd (xfer->sock,
                                         1, 0, 0,
                                         &xfer_network_fd_cb,
                                         xfer, NULL);

        /* add timeout */
        if (weechat_config_integer (xfer_config_network_timeout) > 0)
        {
            xfer->hook_timer = weechat_hook_timer (weechat_config_integer (xfer_config_network_timeout) * 1000,
                                                   0, 1,
                                                   &xfer_network_timer_cb,
                                                   xfer, NULL);
        }
    }

    /* for chat receiving, connect to listening host */
    if (xfer->type == XFER_TYPE_CHAT_RECV)
    {
        xfer->hook_connect = weechat_hook_connect (xfer->proxy,
                                                   xfer->remote_address_str,
                                                   xfer->port, 1, 0, NULL, NULL,
                                                   0, "NONE", NULL,
                                                   &xfer_network_connect_chat_recv_cb,
                                                   xfer, NULL);
    }

    /* for file receiving, connection is made in child process (blocking) */

    return 1;
}