Beispiel #1
0
void
trigger_command_rename (struct t_trigger *trigger, const char *new_name)
{
    char *name, *name2;

    name = strdup (trigger->name);
    name2 = weechat_string_remove_quotes (new_name, "'\"");

    if (name && name2)
    {
        /* check that new name is valid */
        if (!trigger_name_valid (name2))
        {
            weechat_printf_date_tags (NULL, 0, "no_trigger",
                                      _("%s%s: invalid trigger name: \"%s\""),
                                      weechat_prefix ("error"),
                                      TRIGGER_PLUGIN_NAME,
                                      name2);
            goto end;
        }
        /* check that no trigger already exists with the new name */
        if (trigger_search (name2))
        {
            weechat_printf_date_tags (NULL, 0, "no_trigger",
                                      _("%s%s: trigger \"%s\" already "
                                        "exists"),
                                      weechat_prefix ("error"), TRIGGER_PLUGIN_NAME,
                                      name2);
            goto end;
        }
        /* rename the trigger */
        if (trigger_rename (trigger, name2))
        {
            weechat_printf_date_tags (NULL, 0, "no_trigger",
                                      _("Trigger \"%s\" renamed to \"%s\""),
                                      name, trigger->name);
        }
        else
        {
            weechat_printf_date_tags (NULL, 0, "no_trigger",
                                      _("%s%s: failed to rename trigger "
                                        "\"%s\""),
                                      weechat_prefix ("error"),
                                      TRIGGER_PLUGIN_NAME,
                                      name);
        }
    }

end:
    if (name)
        free (name);
    if (name2)
        free (name2);
}
Beispiel #2
0
void
irc_ctcp_display_request (struct t_irc_server *server,
                          time_t date,
                          const char *command,
                          struct t_irc_channel *channel,
                          const char *nick,  const char *ctcp,
                          const char *arguments,
                          const char *reply)
{
    /* CTCP blocked and user doesn't want to see message? then just return */
    if (reply && !reply[0]
        && !weechat_config_boolean (irc_config_look_display_ctcp_blocked))
        return;

    weechat_printf_date_tags (irc_msgbuffer_get_target_buffer (server, nick,
                                                               NULL, "ctcp",
                                                               (channel) ? channel->buffer : NULL),
                              date,
                              irc_protocol_tags (command, "irc_ctcp", NULL),
                              _("%sCTCP requested by %s%s%s: %s%s%s%s%s%s"),
                              weechat_prefix ("network"),
                              irc_nick_color_for_message (server, NULL, nick),
                              nick,
                              IRC_COLOR_RESET,
                              IRC_COLOR_CHAT_CHANNEL,
                              ctcp,
                              IRC_COLOR_RESET,
                              (arguments) ? " " : "",
                              (arguments) ? arguments : "",
                              (reply && !reply[0]) ? _(" (blocked)") : "");
}
Beispiel #3
0
void
trigger_command_display_status ()
{
    weechat_printf_date_tags (NULL, 0, "no_trigger",
                              (trigger_enabled) ?
                              _("Triggers enabled") : _("Triggers disabled"));
}
Beispiel #4
0
void
trigger_command_list_default (int verbose)
{
    int i, regex_count, commands_count;
    struct t_trigger_regex *regex;
    char **commands;

    regex_count = 0;
    regex = NULL;
    commands_count = 0;
    commands = NULL;

    weechat_printf_date_tags (NULL, 0, "no_trigger", "");
    weechat_printf_date_tags (NULL, 0, "no_trigger",
                              _("List of default triggers:"));

    for (i = 0; trigger_config_default_list[i][0]; i++)
    {
        if (trigger_regex_split (trigger_config_default_list[i][5],
                                 &regex_count,
                                 &regex) < 0)
            continue;
        trigger_split_command (trigger_config_default_list[i][6],
                               &commands_count,
                               &commands);
        trigger_command_display_trigger_internal (
            trigger_config_default_list[i][0],
            weechat_config_string_to_boolean (trigger_config_default_list[i][1]),
            trigger_config_default_list[i][2],
            trigger_config_default_list[i][3],
            trigger_config_default_list[i][4],
            0,
            0,
            0,
            regex_count,
            regex,
            commands_count,
            commands,
            trigger_search_return_code (trigger_config_default_list[i][7]),
            verbose);
    }

    trigger_regex_free (&regex_count, &regex);
    if (commands)
        weechat_string_free_split (commands);
}
Beispiel #5
0
void
trigger_command_set_enabled (struct t_trigger *trigger,
                             int enable, const char *enable_string,
                             int display_error)
{
    if (trigger->hook_running)
    {
        trigger_command_error_running (trigger, enable_string);
        return;
    }

    if (enable == 2)
    {
        if (weechat_config_boolean (trigger->options[TRIGGER_OPTION_ENABLED]))
        {
            trigger_hook (trigger);
            weechat_printf_date_tags (NULL, 0, "no_trigger",
                                      _("Trigger \"%s\" restarted"),
                                      trigger->name);
        }
        else if (display_error)
        {
            weechat_printf_date_tags (NULL, 0, "no_trigger",
                                      _("%s%s: a disabled trigger can not be "
                                        "restarted"),
                                      weechat_prefix ("error"),
                                      TRIGGER_PLUGIN_NAME);
        }
    }
    else
    {
        if (enable < 0)
        {
            enable = weechat_config_boolean (trigger->options[TRIGGER_OPTION_ENABLED]) ?
                0 : 1;
        }
        weechat_config_option_set (trigger->options[TRIGGER_OPTION_ENABLED],
                                   (enable) ? "on" : "off", 1);
        weechat_printf_date_tags (NULL, 0, "no_trigger",
                                  (enable) ?
                                  _("Trigger \"%s\" enabled") :
                                  _("Trigger \"%s\" disabled"),
                                  trigger->name);
    }
}
Beispiel #6
0
void
trigger_command_error_running (struct t_trigger *trigger, const char *action)
{
    weechat_printf_date_tags (NULL, 0, "no_trigger",
                              _("%s%s: action \"%s\" can not be executed on "
                                "trigger \"%s\" because it is currently "
                                "running"),
                              weechat_prefix ("error"), TRIGGER_PLUGIN_NAME,
                              action, trigger->name);
}
Beispiel #7
0
void
irc_raw_message_print (struct t_irc_raw_message *raw_message)
{
    if (irc_raw_buffer && raw_message)
    {
        weechat_printf_date_tags (irc_raw_buffer,
                                  raw_message->date, NULL,
                                  "%s\t%s",
                                  raw_message->prefix,
                                  raw_message->message);
    }
}
Beispiel #8
0
void
logger_config_flush_delay_change (const void *pointer, void *data,
                                  struct t_config_option *option)
{
    /* make C compiler happy */
    (void) pointer;
    (void) data;
    (void) option;

    if (logger_config_loading)
        return;

    if (logger_timer)
    {
        if (weechat_logger_plugin->debug)
        {
            weechat_printf_date_tags (
                NULL, 0, "no_log",
                "%s: stopping timer", LOGGER_PLUGIN_NAME);
        }
        weechat_unhook (logger_timer);
        logger_timer = NULL;
    }

    if (weechat_config_integer (logger_config_file_flush_delay) > 0)
    {
        if (weechat_logger_plugin->debug)
        {
            weechat_printf_date_tags (
                NULL, 0, "no_log",
                "%s: starting timer (interval: %d seconds)",
                LOGGER_PLUGIN_NAME,
                weechat_config_integer (logger_config_file_flush_delay));
        }
        logger_timer = weechat_hook_timer (weechat_config_integer (logger_config_file_flush_delay) * 1000,
                                           0, 0,
                                           &logger_timer_cb, NULL, NULL);
    }
}
Beispiel #9
0
void
trigger_command_list (const char *message, int verbose)
{
    struct t_trigger *ptr_trigger;

    weechat_printf_date_tags (NULL, 0, "no_trigger", "");
    trigger_command_display_status ();

    if (!triggers)
    {
        weechat_printf_date_tags (NULL, 0, "no_trigger",
                                  _("No trigger defined"));
        return;
    }

    weechat_printf_date_tags (NULL, 0, "no_trigger", message);

    for (ptr_trigger = triggers; ptr_trigger;
         ptr_trigger = ptr_trigger->next_trigger)
    {
        trigger_command_display_trigger (ptr_trigger, verbose);
    }
}
Beispiel #10
0
void
irc_notify_set_is_on_server (struct t_irc_notify *notify, const char *host,
                             int is_on_server)
{
    if (!notify)
        return;

    /* same status, then do nothing */
    if (notify->is_on_server == is_on_server)
        return;

    weechat_printf_date_tags (
        notify->server->buffer,
        0,
        irc_notify_get_tags (irc_config_look_notify_tags_ison,
                             (is_on_server) ? "join" : "quit",
                             notify->nick),
        (notify->is_on_server < 0) ?
        ((is_on_server) ?
         _("%snotify: %s%s%s%s%s%s%s%s%s is connected") :
         _("%snotify: %s%s%s%s%s%s%s%s%s is offline")) :
        ((is_on_server) ?
         _("%snotify: %s%s%s%s%s%s%s%s%s has connected") :
         _("%snotify: %s%s%s%s%s%s%s%s%s has quit")),
        weechat_prefix ("network"),
        irc_nick_color_for_msg (notify->server, 1, NULL, notify->nick),
        notify->nick,
        (host && host[0]) ? IRC_COLOR_CHAT_DELIMITERS : "",
        (host && host[0]) ? " (" : "",
        (host && host[0]) ? IRC_COLOR_CHAT_HOST : "",
        (host && host[0]) ? host : "",
        (host && host[0]) ? IRC_COLOR_CHAT_DELIMITERS : "",
        (host && host[0]) ? ")" : "",
        (is_on_server) ? IRC_COLOR_MESSAGE_JOIN : IRC_COLOR_MESSAGE_QUIT);
    irc_notify_send_signal (notify, (is_on_server) ? "join" : "quit", NULL);

    notify->is_on_server = is_on_server;
}
Beispiel #11
0
void
irc_ctcp_display_reply_from_nick (struct t_irc_server *server, time_t date,
                                  const char *command, const char *nick,
                                  char *arguments)
{
    char *pos_end, *pos_space, *pos_args, *pos_usec;
    struct timeval tv;
    long sec1, usec1, sec2, usec2, difftime;

    while (arguments && arguments[0])
    {
        pos_end = strchr (arguments + 1, '\01');
        if (pos_end)
            pos_end[0] = '\0';

        pos_space = strchr (arguments + 1, ' ');
        if (pos_space)
        {
            pos_space[0] = '\0';
            pos_args = pos_space + 1;
            while (pos_args[0] == ' ')
            {
                pos_args++;
            }
            if (strcmp (arguments + 1, "PING") == 0)
            {
                pos_usec = strchr (pos_args, ' ');
                if (pos_usec)
                {
                    pos_usec[0] = '\0';

                    gettimeofday (&tv, NULL);
                    sec1 = atol (pos_args);
                    usec1 = atol (pos_usec + 1);
                    sec2 = tv.tv_sec;
                    usec2 = tv.tv_usec;

                    difftime = ((sec2 * 1000000) + usec2) -
                        ((sec1 * 1000000) + usec1);
                    weechat_printf_date_tags (irc_msgbuffer_get_target_buffer (server,
                                                                               nick,
                                                                               NULL,
                                                                               "ctcp",
                                                                               NULL),
                                              date,
                                              irc_protocol_tags (command,
                                                                 "irc_ctcp",
                                                                 NULL),
                                              _("%sCTCP reply from %s%s%s: %s%s%s "
                                                "%ld.%ld %s"),
                                              weechat_prefix ("network"),
                                              irc_nick_color_for_message (server,
                                                                          NULL,
                                                                          nick),
                                              nick,
                                              IRC_COLOR_RESET,
                                              IRC_COLOR_CHAT_CHANNEL,
                                              arguments + 1,
                                              IRC_COLOR_RESET,
                                              difftime / 1000000,
                                              (difftime % 1000000) / 1000,
                                              (NG_("second", "seconds",
                                                   (difftime / 1000000))));

                    pos_usec[0] = ' ';
                }
            }
            else
            {
                weechat_printf_date_tags (irc_msgbuffer_get_target_buffer (server,
                                                                           nick,
                                                                           NULL,
                                                                           "ctcp",
                                                                           NULL),
                                          date,
                                          irc_protocol_tags (command,
                                                             "irc_ctcp",
                                                             NULL),
                                          _("%sCTCP reply from %s%s%s: %s%s%s%s%s"),
                                          weechat_prefix ("network"),
                                          irc_nick_color_for_message (server, NULL,
                                                                      nick),
                                          nick,
                                          IRC_COLOR_RESET,
                                          IRC_COLOR_CHAT_CHANNEL,
                                          arguments + 1,
                                          IRC_COLOR_RESET,
                                          " ",
                                          pos_args);
            }
            pos_space[0] = ' ';
        }
        else
        {
            weechat_printf_date_tags (irc_msgbuffer_get_target_buffer (server, nick,
                                                                       NULL, "ctcp",
                                                                       NULL),
                                      date,
                                      irc_protocol_tags (command, NULL, NULL),
                                      _("%sCTCP reply from %s%s%s: %s%s%s%s%s"),
                                      weechat_prefix ("network"),
                                      irc_nick_color_for_message (server, NULL,
                                                                  nick),
                                      nick,
                                      IRC_COLOR_RESET,
                                      IRC_COLOR_CHAT_CHANNEL,
                                      arguments + 1,
                                      "",
                                      "",
                                      "");
        }

        if (pos_end)
            pos_end[0] = '\01';

        arguments = (pos_end) ? pos_end + 1 : NULL;
    }
}
Beispiel #12
0
void
logger_backlog (struct t_gui_buffer *buffer, const char *filename, int lines)
{
    const char *charset;
    struct t_logger_line *last_lines, *ptr_lines;
    char *pos_message, *pos_tab, *error, *message;
    time_t datetime, time_now;
    struct tm tm_line;
    int num_lines;

    charset = weechat_info_get ("charset_terminal", "");

    weechat_buffer_set (buffer, "print_hooks_enabled", "0");

    num_lines = 0;
    last_lines = logger_tail_file (filename, lines);
    ptr_lines = last_lines;
    while (ptr_lines)
    {
        datetime = 0;
        pos_message = strchr (ptr_lines->data, '\t');
        if (pos_message)
        {
            /* initialize structure, because strptime does not do it */
            memset (&tm_line, 0, sizeof (struct tm));
            /*
             * we get current time to initialize daylight saving time in
             * structure tm_line, otherwise printed time will be shifted
             * and will not use DST used on machine
             */
            time_now = time (NULL);
            localtime_r (&time_now, &tm_line);
            pos_message[0] = '\0';
            error = strptime (ptr_lines->data,
                              weechat_config_string (logger_config_file_time_format),
                              &tm_line);
            if (error && !error[0] && (tm_line.tm_year > 0))
                datetime = mktime (&tm_line);
            pos_message[0] = '\t';
        }
        pos_message = (pos_message && (datetime != 0)) ?
            pos_message + 1 : ptr_lines->data;
        message = (charset) ?
            weechat_iconv_to_internal (charset, pos_message) : strdup (pos_message);
        if (message)
        {
            pos_tab = strchr (message, '\t');
            if (pos_tab)
                pos_tab[0] = '\0';
            weechat_printf_date_tags (buffer, datetime,
                                      "no_highlight,notify_none,logger_backlog",
                                      "%s%s%s%s%s",
                                      weechat_color (weechat_config_string (logger_config_color_backlog_line)),
                                      message,
                                      (pos_tab) ? "\t" : "",
                                      (pos_tab) ? weechat_color (weechat_config_string (logger_config_color_backlog_line)) : "",
                                      (pos_tab) ? pos_tab + 1 : "");
            if (pos_tab)
                pos_tab[0] = '\t';
            free (message);
        }
        num_lines++;
        ptr_lines = ptr_lines->next_line;
    }
    if (last_lines)
        logger_tail_free (last_lines);
    if (num_lines > 0)
    {
        weechat_printf_date_tags (buffer, datetime,
                                  "no_highlight,notify_none,logger_backlog_end",
                                  _("%s===\t%s========== End of backlog (%d lines) =========="),
                                  weechat_color (weechat_config_string (logger_config_color_backlog_end)),
                                  weechat_color (weechat_config_string (logger_config_color_backlog_end)),
                                  num_lines);
        weechat_buffer_set (buffer, "unread", "");
    }
    weechat_buffer_set (buffer, "print_hooks_enabled", "1");
}
Beispiel #13
0
void
exec_end_command (struct t_exec_cmd *exec_cmd, int return_code)
{
    struct t_gui_buffer *ptr_buffer;
    struct t_hashtable *hashtable;
    char str_number[32], *output;
    int i, buffer_type;

    if (exec_cmd->hsignal)
    {
        hashtable = weechat_hashtable_new (32,
                                           WEECHAT_HASHTABLE_STRING,
                                           WEECHAT_HASHTABLE_STRING,
                                           NULL, NULL);
        if (hashtable)
        {
            weechat_hashtable_set (hashtable, "command", exec_cmd->command);
            snprintf (str_number, sizeof (str_number), "%d", exec_cmd->number);
            weechat_hashtable_set (hashtable, "number", str_number);
            weechat_hashtable_set (hashtable, "name", exec_cmd->name);
            output = exec_decode_color (exec_cmd, exec_cmd->output[EXEC_STDOUT]);
            weechat_hashtable_set (hashtable, "out", output);
            if (output)
                free (output);
            output = exec_decode_color (exec_cmd, exec_cmd->output[EXEC_STDERR]);
            weechat_hashtable_set (hashtable, "err", output);
            if (output)
                free (output);
            snprintf (str_number, sizeof (str_number), "%d", return_code);
            weechat_hashtable_set (hashtable, "rc", str_number);
            weechat_hook_hsignal_send (exec_cmd->hsignal, hashtable);
            weechat_hashtable_free (hashtable);
        }
    }
    else
    {
        ptr_buffer = weechat_buffer_search ("==", exec_cmd->buffer_full_name);

        /* display the last line of output (if not ending with '\n') */
        exec_display_line (exec_cmd, ptr_buffer, EXEC_STDOUT,
                           exec_cmd->output[EXEC_STDOUT]);
        exec_display_line (exec_cmd, ptr_buffer, EXEC_STDERR,
                           exec_cmd->output[EXEC_STDERR]);

        /*
         * display return code (only if command is not detached, if output is
         * NOT sent to buffer, and if command is not piped)
         */
        if (exec_cmd->display_rc
            && !exec_cmd->detached && !exec_cmd->output_to_buffer
            && !exec_cmd->pipe_command)
        {
            buffer_type = weechat_buffer_get_integer (ptr_buffer, "type");
            if (return_code >= 0)
            {
                if (buffer_type == 1)
                {
                    weechat_printf_y (ptr_buffer, -1,
                                      ("%s: end of command %d (\"%s\"), "
                                       "return code: %d"),
                                      EXEC_PLUGIN_NAME, exec_cmd->number,
                                      exec_cmd->command, return_code);
                }
                else
                {
                    weechat_printf_date_tags (
                        ptr_buffer, 0, "exec_rc",
                        _("%s: end of command %d (\"%s\"), "
                          "return code: %d"),
                        EXEC_PLUGIN_NAME, exec_cmd->number,
                        exec_cmd->command, return_code);
                }
            }
            else
            {
                if (buffer_type == 1)
                {
                    weechat_printf_y (ptr_buffer, -1,
                                      _("%s: unexpected end of command %d "
                                        "(\"%s\")"),
                                      EXEC_PLUGIN_NAME, exec_cmd->number,
                                      exec_cmd->command);
                }
                else
                {
                    weechat_printf_date_tags (
                        ptr_buffer, 0, "exec_rc",
                        _("%s: unexpected end of command %d "
                          "(\"%s\")"),
                        EXEC_PLUGIN_NAME, exec_cmd->number,
                        exec_cmd->command);
                }
            }
        }
    }

    /* (re)set some variables after the end of command */
    exec_cmd->hook = NULL;
    exec_cmd->pid = 0;
    exec_cmd->end_time = time (NULL);
    exec_cmd->return_code = return_code;
    for (i = 0; i < 2; i++)
    {
        if (exec_cmd->output[i])
        {
            free (exec_cmd->output[i]);
            exec_cmd->output[i] = NULL;
        }
        exec_cmd->output_size[i] = 0;
    }

    /* schedule a timer to remove the executed command */
    if (weechat_config_integer (exec_config_command_purge_delay) >= 0)
    {
        weechat_hook_timer (1 + (1000 * weechat_config_integer (exec_config_command_purge_delay)),
                            0, 1,
                            &exec_timer_delete_cb, exec_cmd, NULL);
    }
}
Beispiel #14
0
void
irc_ctcp_recv (struct t_irc_server *server, time_t date, const char *command,
               struct t_irc_channel *channel, const char *address,
               const char *nick, const char *remote_nick, char *arguments,
               char *message)
{
    char *pos_end, *pos_space, *pos_args;
    const char *reply;
    char *decoded_reply;
    struct t_irc_channel *ptr_channel;
    struct t_irc_nick *ptr_nick;
    int nick_is_me;

    while (arguments && arguments[0])
    {
        pos_end = strrchr (arguments + 1, '\01');
        if (pos_end)
            pos_end[0] = '\0';

        pos_args = NULL;
        pos_space = strchr (arguments + 1, ' ');
        if (pos_space)
        {
            pos_space[0] = '\0';
            pos_args = pos_space + 1;
            while (pos_args[0] == ' ')
            {
                pos_args++;
            }
        }

        /* CTCP ACTION */
        if (strcmp (arguments + 1, "ACTION") == 0)
        {
            nick_is_me = (irc_server_strcasecmp (server, server->nick, nick) == 0);
            if (channel)
            {
                ptr_nick = irc_nick_search (server, channel, nick);
                irc_channel_nick_speaking_add (channel,
                                               nick,
                                               (pos_args) ?
                                               weechat_string_has_highlight (pos_args,
                                                                             server->nick) : 0);
                irc_channel_nick_speaking_time_remove_old (channel);
                irc_channel_nick_speaking_time_add (server, channel, nick,
                                                    time (NULL));
                weechat_printf_date_tags (channel->buffer,
                                          date,
                                          irc_protocol_tags (command,
                                                             (nick_is_me) ?
                                                             "irc_action,notify_none,no_highlight" :
                                                             "irc_action,notify_message",
                                                             nick),
                                          "%s%s%s%s%s%s%s",
                                          weechat_prefix ("action"),
                                          irc_nick_mode_for_display (server, ptr_nick, 0),
                                          (ptr_nick) ? ptr_nick->color : ((nick) ? irc_nick_find_color (nick) : IRC_COLOR_CHAT_NICK),
                                          nick,
                                          (pos_args) ? IRC_COLOR_RESET : "",
                                          (pos_args) ? " " : "",
                                          (pos_args) ? pos_args : "");
            }
            else
            {
                ptr_channel = irc_channel_search (server, remote_nick);
                if (!ptr_channel)
                {
                    ptr_channel = irc_channel_new (server,
                                                   IRC_CHANNEL_TYPE_PRIVATE,
                                                   remote_nick, 0, 0);
                    if (!ptr_channel)
                    {
                        weechat_printf (server->buffer,
                                        _("%s%s: cannot create new "
                                          "private buffer \"%s\""),
                                        weechat_prefix ("error"),
                                        IRC_PLUGIN_NAME, remote_nick);
                    }
                }
                if (ptr_channel)
                {
                    if (!ptr_channel->topic)
                        irc_channel_set_topic (ptr_channel, address);

                    weechat_printf_date_tags (ptr_channel->buffer,
                                              date,
                                              irc_protocol_tags (command,
                                                                 (nick_is_me) ?
                                                                 "irc_action,notify_none,no_highlight" :
                                                                 "irc_action,notify_private",
                                                                 nick),
                                              "%s%s%s%s%s%s",
                                              weechat_prefix ("action"),
                                              (nick_is_me) ?
                                              IRC_COLOR_CHAT_NICK_SELF : irc_nick_color_for_pv (ptr_channel, nick),
                                              nick,
                                              (pos_args) ? IRC_COLOR_RESET : "",
                                              (pos_args) ? " " : "",
                                              (pos_args) ? pos_args : "");
                    weechat_hook_signal_send ("irc_pv",
                                              WEECHAT_HOOK_SIGNAL_STRING,
                                              message);
                }
            }
        }
        /* CTCP PING */
        else if (strcmp (arguments + 1, "PING") == 0)
        {
            reply = irc_ctcp_get_reply (server, arguments + 1);
            irc_ctcp_display_request (server, date, command, channel, nick,
                                      arguments + 1, pos_args, reply);
            if (!reply || reply[0])
            {
                irc_ctcp_reply_to_nick (server, command, channel, nick,
                                        arguments + 1, pos_args);
            }
        }
        /* CTCP DCC */
        else if (strcmp (arguments + 1, "DCC") == 0)
        {
            irc_ctcp_recv_dcc (server, nick, pos_args, message);
        }
        /* other CTCP */
        else
        {
            reply = irc_ctcp_get_reply (server, arguments + 1);
            if (reply)
            {
                irc_ctcp_display_request (server, date, command, channel, nick,
                                          arguments + 1, pos_args, reply);

                if (reply[0])
                {
                    decoded_reply = irc_ctcp_replace_variables (server, reply);
                    if (decoded_reply)
                    {
                        irc_ctcp_reply_to_nick (server, command, channel, nick,
                                                arguments + 1, decoded_reply);
                        free (decoded_reply);
                    }
                }
            }
            else
            {
                if (weechat_config_boolean (irc_config_look_display_ctcp_unknown))
                {
                    weechat_printf_date_tags (irc_msgbuffer_get_target_buffer (server,
                                                                               nick,
                                                                               NULL,
                                                                               "ctcp",
                                                                               (channel) ? channel->buffer : NULL),
                                              date,
                                              irc_protocol_tags (command,
                                                                 "irc_ctcp",
                                                                 NULL),
                                              _("%sUnknown CTCP requested by %s%s%s: "
                                                "%s%s%s%s%s"),
                                              weechat_prefix ("network"),
                                              irc_nick_color_for_message (server,
                                                                          NULL,
                                                                          nick),
                                              nick,
                                              IRC_COLOR_RESET,
                                              IRC_COLOR_CHAT_CHANNEL,
                                              arguments + 1,
                                              (pos_args) ? IRC_COLOR_RESET : "",
                                              (pos_args) ? " " : "",
                                              (pos_args) ? pos_args : "");
                }
            }
        }

        weechat_hook_signal_send ("irc_ctcp",
                                  WEECHAT_HOOK_SIGNAL_STRING,
                                  message);

        if (pos_space)
            pos_space[0] = ' ';

        if (pos_end)
            pos_end[0] = '\01';

        arguments = (pos_end) ? pos_end + 1 : NULL;
    }
}
Beispiel #15
0
void
exec_display_line (struct t_exec_cmd *exec_cmd, struct t_gui_buffer *buffer,
                   int out, const char *line)
{
    char *line_color, *line_color2, *line2, str_number[32], str_tags[1024];
    const char *ptr_line_color;
    int length;

    if (!exec_cmd || !line)
        return;

    /*
     * if output is sent to the buffer, the buffer must exist
     * (we don't send output by default to core buffer)
     */
    if (exec_cmd->output_to_buffer && !exec_cmd->pipe_command && !buffer)
        return;

    /* decode colors */
    line_color = exec_decode_color (exec_cmd, line);
    if (!line_color)
        return;

    exec_cmd->output_line_nb++;

    if (exec_cmd->pipe_command)
    {
        if (strstr (exec_cmd->pipe_command, "$line"))
        {
            /* replace $line by line content */
            line2 = weechat_string_replace (exec_cmd->pipe_command,
                                            "$line", line_color);
            if (line2)
            {
                weechat_command (buffer, line2);
                free (line2);
            }
        }
        else
        {
            /* add line at the end of command, after a space */
            length = strlen (exec_cmd->pipe_command) + 1 + strlen (line_color) + 1;
            line2 = malloc (length);
            if (line2)
            {
                snprintf (line2, length,
                          "%s %s", exec_cmd->pipe_command, line_color);
                weechat_command (buffer, line2);
                free (line2);
            }
        }
    }
    else if (exec_cmd->output_to_buffer)
    {
        if (exec_cmd->line_numbers)
        {
            length = 32 + strlen (line_color) + 1;
            line2 = malloc (length);
            if (line2)
            {
                snprintf (line2, length,
                          "%d. %s", exec_cmd->output_line_nb, line_color);
                weechat_command (buffer, line2);
                free (line2);
            }
        }
        else
        {
            if (exec_cmd->output_to_buffer_exec_cmd)
                ptr_line_color = line_color;
            else
                ptr_line_color = weechat_string_input_for_buffer (line_color);

            if (ptr_line_color)
            {
                weechat_command (buffer,
                                 (ptr_line_color[0]) ? ptr_line_color : " ");
            }
            else
            {
                length = 1 + strlen (line_color) + 1;
                line_color2 = malloc (length);
                if (line_color2)
                {
                    snprintf (line_color2, length, "%c%s",
                              line_color[0], line_color);
                    weechat_command (buffer,
                                     (line_color2[0]) ? line_color2 : " ");
                    free (line_color2);
                }
            }
        }
    }
    else
    {
        snprintf (str_number, sizeof (str_number), "%d", exec_cmd->number);
        snprintf (str_tags, sizeof (str_tags),
                  "exec_%s,exec_cmd_%s",
                  (out == EXEC_STDOUT) ? "stdout" : "stderr",
                  (exec_cmd->name) ? exec_cmd->name : str_number);
        if (weechat_buffer_get_integer (buffer, "type") == 1)
        {
            snprintf (str_number, sizeof (str_number),
                      "%d. ", exec_cmd->output_line_nb);
            weechat_printf_y (buffer, -1,
                              "%s%s",
                              (exec_cmd->line_numbers) ? str_number : " ",
                              line_color);
        }
        else
        {
            snprintf (str_number, sizeof (str_number),
                      "%d\t", exec_cmd->output_line_nb);
            weechat_printf_date_tags (
                buffer, 0, str_tags,
                "%s%s",
                (exec_cmd->line_numbers) ? str_number : " \t",
                line_color);
        }
    }

    free (line_color);
}
Beispiel #16
0
void
irc_input_user_message_display (struct t_gui_buffer *buffer, int action,
                                const char *text)
{
    struct t_irc_nick *ptr_nick;
    char *pos, *text2, *text_decoded, str_tags[256], *str_color;
    const char *ptr_text;

    /* if message is an action, force "action" to 1 and extract message */
    if (strncmp (text, "\01ACTION ", 8) == 0)
    {
        action = 1;
        pos = strrchr (text + 8, '\01');
        if (pos)
            text2 = weechat_strndup (text + 8, pos - text - 8);
        else
            text2 = strdup (text + 8);
    }
    else
        text2 = strdup (text);

    text_decoded = irc_color_decode (
        (text2) ? text2 : text,
        weechat_config_boolean (irc_config_network_colors_send));

    IRC_BUFFER_GET_SERVER_CHANNEL(buffer);

    if (ptr_channel)
    {
        ptr_nick = NULL;
        if (ptr_channel->type == IRC_CHANNEL_TYPE_CHANNEL)
        {
            ptr_nick = irc_nick_search (ptr_server, ptr_channel,
                                        ptr_server->nick);
        }

        if (action)
        {
            snprintf (str_tags, sizeof (str_tags),
                      "irc_action,self_msg,notify_none,no_highlight");
        }
        else
        {
            str_color = irc_color_for_tags (
                weechat_config_color (
                    weechat_config_get ("weechat.color.chat_nick_self")));
            snprintf (str_tags, sizeof (str_tags),
                      "notify_none,self_msg,no_highlight,prefix_nick_%s",
                      (str_color) ? str_color : "default");
            if (str_color)
                free (str_color);
        }
        ptr_text = (text_decoded) ? text_decoded : ((text2) ? text2 : text);
        if (action)
        {
            weechat_printf_date_tags (
                buffer,
                0,
                irc_protocol_tags (
                    "privmsg", str_tags,
                    (ptr_nick) ? ptr_nick->name : ptr_server->nick,
                    NULL),
                "%s%s%s%s%s %s",
                weechat_prefix ("action"),
                irc_nick_mode_for_display (ptr_server, ptr_nick, 0),
                IRC_COLOR_CHAT_NICK_SELF,
                ptr_server->nick,
                IRC_COLOR_RESET,
                ptr_text);
        }
        else
        {
            weechat_printf_date_tags (
                buffer,
                0,
                irc_protocol_tags (
                    "privmsg", str_tags,
                    (ptr_nick) ? ptr_nick->name : ptr_server->nick,
                    NULL),
                "%s%s",
                irc_nick_as_prefix (
                    ptr_server,
                    (ptr_nick) ? ptr_nick : NULL,
                    (ptr_nick) ? NULL : ptr_server->nick,
                    IRC_COLOR_CHAT_NICK_SELF),
                ptr_text);
        }
    }

    if (text2)
        free (text2);
    if (text_decoded)
        free (text_decoded);
}
Beispiel #17
0
void
trigger_command_display_trigger_internal (const char *name,
                                          int enabled,
                                          const char *hook,
                                          const char *arguments,
                                          const char *conditions,
                                          int hooks_count,
                                          int hook_count_cb,
                                          int hook_count_cmd,
                                          int regex_count,
                                          struct t_trigger_regex *regex,
                                          int commands_count,
                                          char **commands,
                                          int return_code,
                                          int verbose)
{
    char str_conditions[64], str_regex[64], str_command[64], str_rc[64];
    char spaces[256];
    int i, length;

    if (verbose >= 1)
    {
        weechat_printf_date_tags (
            NULL, 0, "no_trigger",
            "  %s%s%s: %s%s%s%s%s%s%s",
            (enabled) ?
            weechat_color (weechat_config_string (trigger_config_color_trigger)) :
            weechat_color (weechat_config_string (trigger_config_color_trigger_disabled)),
            name,
            weechat_color ("reset"),
            hook,
            weechat_color ("chat_delimiters"),
            (arguments && arguments[0]) ? "(" : "",
            weechat_color ("reset"),
            arguments,
            weechat_color ("chat_delimiters"),
            (arguments && arguments[0]) ? ")" : "");
        length = weechat_strlen_screen (name) + 3;
        if (length >= (int)sizeof (spaces))
            length = sizeof (spaces) - 1;
        memset (spaces, ' ', length);
        spaces[length] = '\0';
        if (verbose >= 2)
        {
            weechat_printf_date_tags (NULL, 0, "no_trigger",
                                      "%s hooks: %d", spaces, hooks_count);
            weechat_printf_date_tags (NULL, 0, "no_trigger",
                                      "%s callback: %d",
                                      spaces, hook_count_cb);
            weechat_printf_date_tags (NULL, 0, "no_trigger",
                                      "%s commands: %d",
                                      spaces, hook_count_cmd);
        }
        if (conditions && conditions[0])
        {
            weechat_printf_date_tags (
                NULL, 0, "no_trigger",
                "%s %s=? %s\"%s%s%s\"",
                spaces,
                weechat_color (weechat_config_string (trigger_config_color_flag_conditions)),
                weechat_color ("chat_delimiters"),
                weechat_color ("reset"),
                conditions,
                weechat_color ("chat_delimiters"));
        }
        for (i = 0; i < regex_count; i++)
        {
            weechat_printf_date_tags (
                NULL, 0, "no_trigger",
                "%s %s~%d %s\"%s%s%s\" --> "
                "\"%s%s%s\"%s%s%s%s",
                spaces,
                weechat_color (weechat_config_string (trigger_config_color_flag_regex)),
                i + 1,
                weechat_color ("chat_delimiters"),
                weechat_color (weechat_config_string (trigger_config_color_regex)),
                regex[i].str_regex,
                weechat_color ("chat_delimiters"),
                weechat_color (weechat_config_string (trigger_config_color_replace)),
                regex[i].replace,
                weechat_color ("chat_delimiters"),
                weechat_color ("reset"),
                (regex[i].variable) ? " (" : "",
                (regex[i].variable) ? regex[i].variable : "",
                (regex[i].variable) ? ")" : "");
        }
        if (commands)
        {
            for (i = 0; commands[i]; i++)
            {
                weechat_printf_date_tags (
                    NULL, 0, "no_trigger",
                    "%s %s/%d %s\"%s%s%s\"",
                    spaces,
                    weechat_color (weechat_config_string (trigger_config_color_flag_command)),
                    i + 1,
                    weechat_color ("chat_delimiters"),
                    weechat_color ("reset"),
                    commands[i],
                    weechat_color ("chat_delimiters"));
            }
        }
        if ((return_code >= 0) && (return_code != TRIGGER_RC_OK))
        {
            weechat_printf_date_tags (
                NULL, 0, "no_trigger",
                "%s %s=> %s%s",
                spaces,
                weechat_color (weechat_config_string (trigger_config_color_flag_return_code)),
                weechat_color ("reset"),
                trigger_return_code_string[return_code]);
        }
    }
    else
    {
        str_conditions[0] ='\0';
        str_regex[0] = '\0';
        str_command[0] = '\0';
        str_rc[0] = '\0';
        if (conditions && conditions[0])
        {
            snprintf (str_conditions, sizeof (str_conditions),
                      " %s=?%s",
                      weechat_color (weechat_config_string (trigger_config_color_flag_conditions)),
                      weechat_color ("reset"));
        }
        if (regex_count > 0)
        {
            snprintf (str_regex, sizeof (str_regex),
                      " %s~%d%s",
                      weechat_color (weechat_config_string (trigger_config_color_flag_regex)),
                      regex_count,
                      weechat_color ("reset"));
        }
        if (commands_count > 0)
        {
            snprintf (str_command, sizeof (str_command),
                      " %s/%d%s",
                      weechat_color (weechat_config_string (trigger_config_color_flag_command)),
                      commands_count,
                      weechat_color ("reset"));
        }
        if ((return_code >= 0) && (return_code != TRIGGER_RC_OK))
        {
            snprintf (str_rc, sizeof (str_rc),
                      " %s=>%s",
                      weechat_color (weechat_config_string (trigger_config_color_flag_return_code)),
                      weechat_color ("reset"));
        }
        weechat_printf_date_tags (
            NULL, 0, "no_trigger",
            "  %s%s%s: %s%s%s%s%s%s%s%s%s%s%s%s",
            (enabled) ?
            weechat_color (weechat_config_string (trigger_config_color_trigger)) :
            weechat_color (weechat_config_string (trigger_config_color_trigger_disabled)),
            name,
            weechat_color ("reset"),
            hook,
            weechat_color ("chat_delimiters"),
            (arguments && arguments[0]) ? "(" : "",
            weechat_color ("reset"),
            arguments,
            weechat_color ("chat_delimiters"),
            (arguments && arguments[0]) ? ")" : "",
            weechat_color ("reset"),
            str_conditions,
            str_regex,
            str_command,
            str_rc);
    }
}
Beispiel #18
0
int
trigger_command_trigger (const void *pointer, void *data,
                         struct t_gui_buffer *buffer, int argc,
                         char **argv, char **argv_eol)
{
    struct t_trigger *ptr_trigger, *ptr_trigger2;
    struct t_trigger_regex *regex;
    char *value, **sargv, **items, input[1024], str_pos[16];
    int rc, i, j, type, count, index_option, enable, sargc, num_items, add_rc;
    int regex_count, regex_rc;

    /* make C compiler happy */
    (void) pointer;
    (void) data;

    rc = WEECHAT_RC_OK;
    sargv = NULL;

    /* list all triggers */
    if ((argc == 1)
        || ((argc == 2) && (weechat_strcasecmp (argv[1], "list") == 0)))
    {
        trigger_command_list (_("List of triggers:"), 0);
        goto end;
    }

    /* full list of all triggers */
    if ((argc == 2) && (weechat_strcasecmp (argv[1], "listfull") == 0))
    {
        trigger_command_list (_("List of triggers:"), 1);
        goto end;
    }

    /* list of default triggers */
    if ((argc == 2) && (weechat_strcasecmp (argv[1], "listdefault") == 0))
    {
        trigger_command_list_default (1);
        goto end;
    }

    /* add a trigger */
    if ((weechat_strcasecmp (argv[1], "add") == 0)
        || (weechat_strcasecmp (argv[1], "addoff") == 0)
        || (weechat_strcasecmp (argv[1], "addreplace") == 0))
    {
        sargv = weechat_string_split_shell (argv_eol[2], &sargc);
        if (!sargv || (sargc < 2))
            goto error;
        if (!trigger_name_valid (sargv[0]))
        {
            weechat_printf_date_tags (NULL, 0, "no_trigger",
                                      _("%s%s: invalid trigger name: \"%s\""),
                                      weechat_prefix ("error"),
                                      TRIGGER_PLUGIN_NAME,
                                      sargv[0]);
            goto end;
        }
        type = trigger_search_hook_type (sargv[1]);
        if (type < 0)
        {
            weechat_printf_date_tags (NULL, 0, "no_trigger",
                                      _("%s%s: invalid hook type: \"%s\""),
                                      weechat_prefix ("error"),
                                      TRIGGER_PLUGIN_NAME,
                                      sargv[1]);
            goto end;
        }
        if ((sargc > 4) && sargv[4][0])
        {
            regex_count = 0;
            regex = NULL;
            regex_rc = trigger_regex_split (sargv[4], &regex_count, &regex);
            trigger_regex_free (&regex_count, &regex);
            switch (regex_rc)
            {
                case 0: /* OK */
                    break;
                case -1: /* format error */
                    weechat_printf (NULL,
                                    _("%s%s: invalid format for regular "
                                      "expression"),
                                    weechat_prefix ("error"),
                                    TRIGGER_PLUGIN_NAME);
                    goto end;
                    break;
                case -2: /* regex compilation error */
                    weechat_printf (NULL,
                                    _("%s%s: invalid regular expression "
                                      "(compilation failed)"),
                                    weechat_prefix ("error"),
                                    TRIGGER_PLUGIN_NAME);
                    goto end;
                    break;
                case -3: /* memory error */
                    weechat_printf (NULL,
                                    _("%s%s: not enough memory"),
                                    weechat_prefix ("error"),
                                    TRIGGER_PLUGIN_NAME);
                    goto end;
                    break;
            }
        }
        if ((sargc > 6) && sargv[6][0]
            && (trigger_search_return_code (sargv[6]) < 0))
        {
            weechat_printf_date_tags (NULL, 0, "no_trigger",
                                      _("%s%s: invalid return code: \"%s\""),
                                      weechat_prefix ("error"),
                                      TRIGGER_PLUGIN_NAME,
                                      sargv[6]);
            goto end;
        }
        ptr_trigger = trigger_search (sargv[0]);
        if (ptr_trigger)
        {
            if (weechat_strcasecmp (argv[1], "addreplace") == 0)
            {
                if (ptr_trigger)
                {
                    if (ptr_trigger->hook_running)
                    {
                        trigger_command_error_running (ptr_trigger, argv[1]);
                        goto end;
                    }
                    trigger_free (ptr_trigger);
                }
            }
            else
            {
                weechat_printf_date_tags (
                    NULL, 0, "no_trigger",
                    _("%s%s: trigger \"%s\" already exists "
                      "(choose another name or use option "
                      "\"addreplace\" to overwrite it)"),
                    weechat_prefix ("error"),
                    TRIGGER_PLUGIN_NAME, sargv[0]);
                goto end;
            }
        }
        ptr_trigger = trigger_alloc (sargv[0]);
        if (!ptr_trigger)
        {
            weechat_printf_date_tags (
                NULL, 0, "no_trigger",
                _("%s%s: failed to create trigger \"%s\""),
                weechat_prefix ("error"), TRIGGER_PLUGIN_NAME,
                sargv[0]);
            goto end;
        }
        ptr_trigger = trigger_new (
            sargv[0],                      /* name */
            (weechat_strcasecmp (argv[1], "addoff") == 0) ? "off" : "on",
            sargv[1],                      /* hook */
            (sargc > 2) ? sargv[2] : "",   /* arguments */
            (sargc > 3) ? sargv[3] : "",   /* conditions */
            (sargc > 4) ? sargv[4] : "",   /* regex */
            (sargc > 5) ? sargv[5] : "",   /* command */
            (sargc > 6) ? sargv[6] : "");  /* return code */
        if (ptr_trigger)
        {
            weechat_printf_date_tags (
                NULL, 0, "no_trigger",
                _("Trigger \"%s\" created"), sargv[0]);
        }
        else
        {
            weechat_printf_date_tags (
                NULL, 0, "no_trigger",
                _("%s%s: failed to create trigger \"%s\""),
                weechat_prefix ("error"), TRIGGER_PLUGIN_NAME,
                sargv[0]);
        }
        goto end;
    }

    /* add trigger command in input (to help trigger creation) */
    if (weechat_strcasecmp (argv[1], "addinput") == 0)
    {
        type = TRIGGER_HOOK_SIGNAL;
        if (argc >= 3)
        {
            type = trigger_search_hook_type (argv[2]);
            if (type < 0)
            {
                weechat_printf_date_tags (NULL, 0, "no_trigger",
                                          _("%s%s: invalid hook type: \"%s\""),
                                          weechat_prefix ("error"),
                                          TRIGGER_PLUGIN_NAME, argv[2]);
                goto end;
            }
        }
        items = weechat_string_split (trigger_hook_default_rc[type], ",", 0, 0,
                                      &num_items);
        snprintf (input, sizeof (input),
                  "/trigger add name %s \"%s\" \"%s\" \"%s\" \"%s\"%s%s%s",
                  trigger_hook_type_string[type],
                  trigger_hook_default_arguments[type],
                  TRIGGER_HOOK_DEFAULT_CONDITIONS,
                  TRIGGER_HOOK_DEFAULT_REGEX,
                  TRIGGER_HOOK_DEFAULT_COMMAND,
                  (items && (num_items > 0)) ? " \"" : "",
                  (items && (num_items > 0)) ? items[0] : "",
                  (items && (num_items > 0)) ? "\"" : "");
        weechat_buffer_set (buffer, "input", input);
        weechat_buffer_set (buffer, "input_pos", "13");
        goto end;
    }

    /*
     * get command to create a trigger, and according to option:
     * - input: put the command in input
     * - output: send the command to the buffer
     * - recreate: same as input, but the trigger is first deleted
     */
    if ((weechat_strcasecmp (argv[1], "input") == 0)
        || (weechat_strcasecmp (argv[1], "output") == 0)
        || (weechat_strcasecmp (argv[1], "recreate") == 0))
    {
        if (argc < 3)
            goto error;
        ptr_trigger = trigger_search (argv[2]);
        if (!ptr_trigger)
        {
            weechat_printf_date_tags (NULL, 0, "no_trigger",
                                      _("%s%s: trigger \"%s\" not found"),
                                      weechat_prefix ("error"),
                                      TRIGGER_PLUGIN_NAME,
                                      argv[2]);
            goto end;
        }
        add_rc = trigger_hook_default_rc[weechat_config_integer (ptr_trigger->options[TRIGGER_OPTION_HOOK])][0];
        snprintf (input, sizeof (input),
                  "//trigger %s %s %s \"%s\" \"%s\" \"%s\" \"%s\"%s%s%s",
                  (weechat_strcasecmp (argv[1], "recreate") == 0) ? "addreplace" : "add",
                  ptr_trigger->name,
                  weechat_config_string (ptr_trigger->options[TRIGGER_OPTION_HOOK]),
                  weechat_config_string (ptr_trigger->options[TRIGGER_OPTION_ARGUMENTS]),
                  weechat_config_string (ptr_trigger->options[TRIGGER_OPTION_CONDITIONS]),
                  weechat_config_string (ptr_trigger->options[TRIGGER_OPTION_REGEX]),
                  weechat_config_string (ptr_trigger->options[TRIGGER_OPTION_COMMAND]),
                  (add_rc) ? " \"" : "",
                  (add_rc) ? weechat_config_string (ptr_trigger->options[TRIGGER_OPTION_RETURN_CODE]) : "",
                  (add_rc) ? "\"" : "");
        if (weechat_strcasecmp (argv[1], "output") == 0)
        {
            weechat_command (buffer, input);
        }
        else
        {
            weechat_buffer_set (buffer, "input", input + 1);
            snprintf (str_pos, sizeof (str_pos),
                      "%d", weechat_utf8_strlen (input + 1));
            weechat_buffer_set (buffer, "input_pos", str_pos);
        }
        goto end;
    }

    /* set option in a trigger */
    if (weechat_strcasecmp (argv[1], "set") == 0)
    {
        if (argc < 5)
            goto error;
        ptr_trigger = trigger_search (argv[2]);
        if (!ptr_trigger)
        {
            weechat_printf_date_tags (NULL, 0, "no_trigger",
                                      _("%s%s: trigger \"%s\" not found"),
                                      weechat_prefix ("error"),
                                      TRIGGER_PLUGIN_NAME,
                                      argv[2]);
            goto end;
        }
        if (ptr_trigger->hook_running)
        {
            trigger_command_error_running (ptr_trigger, argv[1]);
            goto end;
        }
        if (weechat_strcasecmp (argv[3], "name") == 0)
        {
            trigger_command_rename (ptr_trigger, argv[4]);
            goto end;
        }
        value = weechat_string_remove_quotes (argv_eol[4], "'\"");
        if (value)
        {
            index_option = trigger_search_option (argv[3]);
            if (index_option >= 0)
            {
                weechat_config_option_set (ptr_trigger->options[index_option],
                                           value, 1);
                weechat_printf_date_tags (NULL, 0, "no_trigger",
                                          _("Trigger \"%s\" updated"),
                                          ptr_trigger->name);
            }
            else
            {
                weechat_printf_date_tags (NULL, 0, "no_trigger",
                                          _("%s%s: trigger option \"%s\" not "
                                            "found"),
                                          weechat_prefix ("error"),
                                          TRIGGER_PLUGIN_NAME, argv[3]);
            }
            free (value);
        }
        goto end;
    }

    /* rename a trigger */
    if (weechat_strcasecmp (argv[1], "rename") == 0)
    {
        if (argc < 4)
            goto error;
        ptr_trigger = trigger_search (argv[2]);
        if (!ptr_trigger)
        {
            weechat_printf_date_tags (NULL, 0, "no_trigger",
                                      _("%s%s: trigger \"%s\" not found"),
                                      weechat_prefix ("error"),
                                      TRIGGER_PLUGIN_NAME,
                                      argv[2]);
            goto end;
        }
        if (ptr_trigger->hook_running)
        {
            trigger_command_error_running (ptr_trigger, argv[1]);
            goto end;
        }
        trigger_command_rename (ptr_trigger, argv[3]);
        goto end;
    }

    /* copy a trigger */
    if (weechat_strcasecmp (argv[1], "copy") == 0)
    {
        if (argc < 4)
            goto error;
        ptr_trigger = trigger_search (argv[2]);
        if (!ptr_trigger)
        {
            weechat_printf_date_tags (NULL, 0, "no_trigger",
                                      _("%s%s: trigger \"%s\" not found"),
                                      weechat_prefix ("error"),
                                      TRIGGER_PLUGIN_NAME,
                                      argv[2]);
            goto end;
        }
        /* check that new name is valid */
        if (!trigger_name_valid (argv[3]))
        {
            weechat_printf_date_tags (NULL, 0, "no_trigger",
                                      _("%s%s: invalid trigger name: \"%s\""),
                                      weechat_prefix ("error"),
                                      TRIGGER_PLUGIN_NAME,
                                      argv[3]);
            goto end;
        }
        /* check that no trigger already exists with the new name */
        if (trigger_search (argv[3]))
        {
            weechat_printf_date_tags (NULL, 0, "no_trigger",
                                      _("%s%s: trigger \"%s\" already "
                                        "exists"),
                                      weechat_prefix ("error"),
                                      TRIGGER_PLUGIN_NAME,
                                      argv[3]);
            goto end;
        }
        /* copy the trigger */
        ptr_trigger2 = trigger_copy (ptr_trigger, argv[3]);
        if (ptr_trigger2)
        {
            weechat_printf_date_tags (NULL, 0, "no_trigger",
                                      _("Trigger \"%s\" copied to \"%s\""),
                                      ptr_trigger->name, ptr_trigger2->name);
        }
        else
        {
            weechat_printf_date_tags (NULL, 0, "no_trigger",
                                      _("%s%s: failed to copy trigger "
                                        "\"%s\""),
                                      weechat_prefix ("error"),
                                      TRIGGER_PLUGIN_NAME,
                                      ptr_trigger->name);
        }
        goto end;
    }

    /* enable/disable/toggle/restart trigger(s) */
    if ((weechat_strcasecmp (argv[1], "enable") == 0)
        || (weechat_strcasecmp (argv[1], "disable") == 0)
        || (weechat_strcasecmp (argv[1], "toggle") == 0)
        || (weechat_strcasecmp (argv[1], "restart") == 0))
    {
        if (argc < 3)
        {
            if (weechat_strcasecmp (argv[1], "restart") == 0)
                goto error;
            if (weechat_strcasecmp (argv[1], "enable") == 0)
                weechat_config_option_set (trigger_config_look_enabled, "1", 1);
            else if (weechat_strcasecmp (argv[1], "disable") == 0)
                weechat_config_option_set (trigger_config_look_enabled, "0", 1);
            else if (weechat_strcasecmp (argv[1], "toggle") == 0)
            {
                weechat_config_option_set (trigger_config_look_enabled,
                                           (trigger_enabled) ? "0" : "1",
                                           1);
            }
            trigger_command_display_status ();
            goto end;
        }
        enable = -1;
        if (weechat_strcasecmp (argv[1], "enable") == 0)
            enable = 1;
        else if (weechat_strcasecmp (argv[1], "disable") == 0)
            enable = 0;
        else if (weechat_strcasecmp (argv[1], "restart") == 0)
            enable = 2;
        if (weechat_strcasecmp (argv[2], "-all") == 0)
        {
            for (ptr_trigger = triggers; ptr_trigger;
                 ptr_trigger = ptr_trigger->next_trigger)
            {
                trigger_command_set_enabled (ptr_trigger, enable, argv[1], 0);
            }
        }
        else
        {
            for (i = 2; i < argc; i++)
            {
                ptr_trigger = trigger_search (argv[i]);
                if (ptr_trigger)
                    trigger_command_set_enabled (ptr_trigger, enable, argv[1],
                                                 1);
                else
                {
                    weechat_printf_date_tags (NULL, 0, "no_trigger",
                                              _("%sTrigger \"%s\" not found"),
                                              weechat_prefix ("error"),
                                              argv[i]);
                }
            }
        }
        goto end;
    }

    /* delete trigger(s) */
    if (weechat_strcasecmp (argv[1], "del") == 0)
    {
        if (argc < 3)
            goto error;
        if (weechat_strcasecmp (argv[2], "-all") == 0)
        {
            count = triggers_count;
            ptr_trigger = triggers;
            while (ptr_trigger)
            {
                ptr_trigger2 = ptr_trigger->next_trigger;
                if (ptr_trigger->hook_running)
                {
                    trigger_command_error_running (ptr_trigger, argv[1]);
                }
                else
                {
                    trigger_free (ptr_trigger);
                }
                ptr_trigger = ptr_trigger2;
            }
            count = count - triggers_count;
            if (count > 0)
                weechat_printf_date_tags (NULL, 0, "no_trigger",
                                          _("%d triggers removed"), count);
        }
        else
        {
            for (i = 2; i < argc; i++)
            {
                ptr_trigger = trigger_search (argv[i]);
                if (ptr_trigger)
                {
                    if (ptr_trigger->hook_running)
                    {
                        trigger_command_error_running (ptr_trigger, argv[1]);
                    }
                    else
                    {
                        trigger_free (ptr_trigger);
                        weechat_printf_date_tags (NULL, 0, "no_trigger",
                                                  _("Trigger \"%s\" removed"),
                                                  argv[i]);
                    }
                }
                else
                {
                    weechat_printf_date_tags (NULL, 0, "no_trigger",
                                              _("%sTrigger \"%s\" not found"),
                                              weechat_prefix ("error"),
                                              argv[i]);
                }
            }
        }
        goto end;
    }

    /* show detailed info on a trigger */
    if (weechat_strcasecmp (argv[1], "show") == 0)
    {
        if (argc < 3)
            goto error;
        ptr_trigger = trigger_search (argv[2]);
        if (!ptr_trigger)
        {
            weechat_printf_date_tags (NULL, 0, "no_trigger",
                                      _("%s%s: trigger \"%s\" not found"),
                                      weechat_prefix ("error"),
                                      TRIGGER_PLUGIN_NAME,
                                      argv[2]);
            goto end;
        }
        weechat_printf_date_tags (NULL, 0, "no_trigger", "");
        weechat_printf_date_tags (NULL, 0, "no_trigger", _("Trigger:"));
        trigger_command_display_trigger (ptr_trigger, 2);
        goto end;
    }

    /* restore default trigger(s) */
    if (weechat_strcasecmp (argv[1], "restore") == 0)
    {
        if (argc < 3)
            goto error;
        for (i = 2; i < argc; i++)
        {
            for (j = 0; trigger_config_default_list[j][0]; j++)
            {
                if (weechat_strcasecmp (trigger_config_default_list[j][0],
                                        argv[i]) == 0)
                {
                    break;
                }
            }
            if (trigger_config_default_list[j][0])
            {
                ptr_trigger = trigger_search (argv[i]);
                if (ptr_trigger && ptr_trigger->hook_running)
                {
                    trigger_command_error_running (ptr_trigger, argv[1]);
                }
                else
                {
                    if (ptr_trigger)
                        trigger_free (ptr_trigger);
                    trigger_new (
                        trigger_config_default_list[j][0],   /* name */
                        trigger_config_default_list[j][1],   /* enabled */
                        trigger_config_default_list[j][2],   /* hook */
                        trigger_config_default_list[j][3],   /* arguments */
                        trigger_config_default_list[j][4],   /* conditions */
                        trigger_config_default_list[j][5],   /* regex */
                        trigger_config_default_list[j][6],   /* command */
                        trigger_config_default_list[j][7]);  /* return code */
                    weechat_printf_date_tags (NULL, 0, "no_trigger",
                                              _("Trigger \"%s\" restored"),
                                              argv[i]);
                }
            }
            else
            {
                weechat_printf_date_tags (
                    NULL, 0, "no_trigger",
                    _("%sDefault trigger \"%s\" not found"),
                    weechat_prefix ("error"), argv[i]);
            }
        }
        goto end;
    }

    /* delete all triggers and restore default ones */
    if (weechat_strcasecmp (argv[1], "default") == 0)
    {
        if ((argc >= 3) && (weechat_strcasecmp (argv[2], "-yes") == 0))
        {
            ptr_trigger = triggers;
            while (ptr_trigger)
            {
                ptr_trigger2 = ptr_trigger->next_trigger;
                if (ptr_trigger->hook_running)
                {
                    trigger_command_error_running (ptr_trigger, argv[1]);
                }
                else
                {
                    trigger_free (ptr_trigger);
                }
                ptr_trigger = ptr_trigger2;
            }
            if (triggers_count == 0)
            {
                trigger_create_default ();
                trigger_command_list (_("Default triggers restored:"), 0);
            }
        }
        else
        {
            weechat_printf (NULL,
                            _("%s%s: \"-yes\" argument is required for "
                              "restoring default triggers (security reason)"),
                            weechat_prefix ("error"), TRIGGER_PLUGIN_NAME);
        }
        goto end;
    }

    /* open the trigger monitor buffer */
    if (weechat_strcasecmp (argv[1], "monitor") == 0)
    {
        trigger_buffer_open ((argc > 2) ? argv_eol[2] : NULL, 1);
        goto end;
    }

error:
    rc = WEECHAT_RC_ERROR;

end:
    if (sargv)
        weechat_string_free_split (sargv);

    if (rc == WEECHAT_RC_ERROR)
        WEECHAT_COMMAND_ERROR;

    return rc;
}
Beispiel #19
0
void
irc_notify_set_away_message (struct t_irc_notify *notify,
                             const char *away_message)
{
    if (!notify)
        return;

    /* same away message, then do nothing */
    if ((!notify->away_message && !away_message)
        || (notify->away_message && away_message
            && (strcmp (notify->away_message, away_message) == 0)))
        return;

    if (!notify->away_message && away_message)
    {
        weechat_printf_date_tags (
            notify->server->buffer,
            0,
            irc_notify_get_tags (
                irc_config_look_notify_tags_whois, "away", notify->nick),
            _("%snotify: %s%s%s is now away: \"%s\""),
            weechat_prefix ("network"),
            irc_nick_color_for_msg (notify->server, 1, NULL, notify->nick),
            notify->nick,
            IRC_COLOR_RESET,
            away_message);
        irc_notify_send_signal (notify, "away", away_message);
    }
    else if (notify->away_message && !away_message)
    {
        weechat_printf_date_tags (
            notify->server->buffer,
            0,
            irc_notify_get_tags (
                irc_config_look_notify_tags_whois, "back", notify->nick),
            _("%snotify: %s%s%s is back"),
            weechat_prefix ("network"),
            irc_nick_color_for_msg (notify->server, 1, NULL, notify->nick),
            notify->nick,
            IRC_COLOR_RESET);
        irc_notify_send_signal (notify, "back", NULL);
    }
    else if (notify->away_message && away_message)
    {
        weechat_printf_date_tags (
            notify->server->buffer,
            0,
            irc_notify_get_tags (
                irc_config_look_notify_tags_whois, "still_away", notify->nick),
            _("%snotify: %s%s%s is still away: \"%s\""),
            weechat_prefix ("network"),
            irc_nick_color_for_msg (notify->server, 1, NULL, notify->nick),
            notify->nick,
            IRC_COLOR_RESET,
            away_message);
        irc_notify_send_signal (notify, "still_away", away_message);
    }

    if (notify->away_message)
        free (notify->away_message);
    notify->away_message = (away_message) ? strdup (away_message) : NULL;
}