summaryrefslogtreecommitdiff
path: root/src/plugins/trigger/trigger.h
blob: 3c4804f433df3e86bf9ad17cbe54d1b6c0a63e45 (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
/*
 * Copyright (C) 2014-2023 Sébastien 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 <https://www.gnu.org/licenses/>.
 */

#ifndef WEECHAT_PLUGIN_TRIGGER_H
#define WEECHAT_PLUGIN_TRIGGER_H

#include <regex.h>

#define weechat_plugin weechat_trigger_plugin
#define TRIGGER_PLUGIN_NAME "trigger"

#define TRIGGER_HOOK_DEFAULT_CONDITIONS "${...}"
#define TRIGGER_HOOK_DEFAULT_REGEX      "/abc/def"
#define TRIGGER_HOOK_DEFAULT_COMMAND    "/cmd"

enum t_trigger_option
{
    TRIGGER_OPTION_ENABLED = 0,        /* true if trigger is enabled        */
    TRIGGER_OPTION_HOOK,               /* hook (signal, modifier, ...)      */
    TRIGGER_OPTION_ARGUMENTS,          /* arguments for hook                */
    TRIGGER_OPTION_CONDITIONS,         /* conditions for trigger            */
    TRIGGER_OPTION_REGEX,              /* replace text with 1 or more regex */
    TRIGGER_OPTION_COMMAND,            /* command run if conditions are OK  */
    TRIGGER_OPTION_RETURN_CODE,        /* return code for hook callback     */
    TRIGGER_OPTION_POST_ACTION,        /* action to take after execution    */
    /* number of trigger options */
    TRIGGER_NUM_OPTIONS,
};

enum t_trigger_hook_type
{
    TRIGGER_HOOK_SIGNAL = 0,
    TRIGGER_HOOK_HSIGNAL,
    TRIGGER_HOOK_MODIFIER,
    TRIGGER_HOOK_LINE,
    TRIGGER_HOOK_PRINT,
    TRIGGER_HOOK_COMMAND,
    TRIGGER_HOOK_COMMAND_RUN,
    TRIGGER_HOOK_TIMER,
    TRIGGER_HOOK_CONFIG,
    TRIGGER_HOOK_FOCUS,
    TRIGGER_HOOK_INFO,
    TRIGGER_HOOK_INFO_HASHTABLE,
    /* number of hook types */
    TRIGGER_NUM_HOOK_TYPES,
};

enum t_trigger_return_code
{
    TRIGGER_RC_OK = 0,
    TRIGGER_RC_OK_EAT,
    TRIGGER_RC_ERROR,
    /* number of return codes */
    TRIGGER_NUM_RETURN_CODES,
};

enum t_trigger_post_action
{
    TRIGGER_POST_ACTION_NONE = 0,
    TRIGGER_POST_ACTION_DISABLE,
    TRIGGER_POST_ACTION_DELETE,
    /* number of post actions */
    TRIGGER_NUM_POST_ACTIONS,
};

enum t_trigger_regex_command
{
    TRIGGER_REGEX_COMMAND_REPLACE = 0,
    TRIGGER_REGEX_COMMAND_TRANSLATE_CHARS,
    /* number of regex commands */
    TRIGGER_NUM_REGEX_COMMANDS,
};

struct t_trigger_regex
{
    enum t_trigger_regex_command command; /* regex command                  */
    char *variable;                    /* the hashtable key used            */
    char *str_regex;                   /* regex to search for replacement   */
    regex_t *regex;                    /* compiled regex                    */
    char *replace;                     /* replacement text                  */
    char *replace_escaped;             /* repl. text (with chars escaped)   */
};

struct t_trigger
{
    /* user choices */
    char *name;                        /* trigger name                      */
    struct t_config_option *options[TRIGGER_NUM_OPTIONS];

    /* internal vars */

    /* hooks */
    int hooks_count;                   /* number of hooks                   */
    struct t_hook **hooks;             /* array of hooks (signal, ...)      */
    unsigned long long hook_count_cb;  /* number of calls made to callback  */
    unsigned long long hook_count_cmd; /* number of commands run in callback*/
    int hook_running;                  /* 1 if one hook callback is running */
    char *hook_print_buffers;          /* buffers (for hook_print only)     */

    /* regular expressions */
    int regex_count;                   /* number of regex                   */
    struct t_trigger_regex *regex;     /* array of regex                    */

    /* commands */
    int commands_count;                /* number of commands                */
    char **commands;                   /* commands                          */

    /* links to other triggers */
    struct t_trigger *prev_trigger;    /* link to previous trigger          */
    struct t_trigger *next_trigger;    /* link to next trigger              */
};

extern struct t_weechat_plugin *weechat_trigger_plugin;
extern char *trigger_option_string[];
extern char *trigger_option_default[];
extern char *trigger_hook_type_string[];
extern char *trigger_hook_option_values;
extern char *trigger_hook_default_arguments[];
extern char *trigger_hook_default_rc[];
extern char trigger_regex_command[];
extern char *trigger_hook_regex_default_var[];
extern char *trigger_return_code_string[];
extern int trigger_return_code[];
extern char *trigger_post_action_string[];
extern struct t_trigger *triggers;
extern struct t_trigger *last_trigger;
extern int triggers_count;
extern struct t_trigger *triggers_temp;
extern struct t_trigger *last_trigger_temp;
extern int trigger_enabled;

extern int trigger_search_option (const char *option_name);
extern int trigger_search_hook_type (const char *type);
extern int trigger_search_regex_command (char command);
extern int trigger_search_return_code (const char *return_code);
extern int trigger_search_post_action (const char *post_action);
extern struct t_trigger *trigger_search (const char *name);
extern struct t_trigger *trigger_search_with_option (struct t_config_option *option);
extern void trigger_regex_free (int *regex_count,
                                struct t_trigger_regex **regex);
extern int trigger_regex_split (const char *str_regex,
                                int *regex_count,
                                struct t_trigger_regex **regex);
extern void trigger_split_command (const char *command,
                                   int *commands_count, char ***commands);
extern void trigger_unhook (struct t_trigger *trigger);
extern void trigger_hook (struct t_trigger *trigger);
extern int trigger_name_valid (const char *name);
extern struct t_trigger *trigger_alloc (const char *name);
extern void trigger_add (struct t_trigger *trigger,
                         struct t_trigger **list_triggers,
                         struct t_trigger **last_list_trigger);
extern struct t_trigger *trigger_new_with_options (const char *name,
                                                   struct t_config_option **options);
extern struct t_trigger *trigger_new (const char *name,
                                      const char *enabled,
                                      const char *hook,
                                      const char *arguments,
                                      const char *conditions,
                                      const char *replace,
                                      const char *command,
                                      const char *return_code,
                                      const char *post_action);
extern void trigger_create_default ();
extern int trigger_rename (struct t_trigger *trigger, const char *name);
extern struct t_trigger *trigger_copy (struct t_trigger *trigger,
                                       const char *name);
extern void trigger_free (struct t_trigger *trigger);
extern void trigger_free_all ();

#endif /* WEECHAT_PLUGIN_TRIGGER_H */