File: hooks.c

package info (click to toggle)
kbtin 1.0.22-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,172 kB
  • sloc: ansic: 16,836; perl: 214; makefile: 133; sh: 92
file content (148 lines) | stat: -rw-r--r-- 3,769 bytes parent folder | download
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
#include "tintin.h"
#include "protos/action.h"
#include "protos/globals.h"
#include "protos/print.h"
#include "protos/parse.h"
#include "protos/utils.h"


static bool magic_close_hook=true;

const char *hook_names[]=
{
    "open",
    "close",
    "zap",
    "end",
    "send",
    "activate",
    "deactivate",
    "title",
    "tick",
    "pretick",
};

/*********************/
/* the #hook command */
/*********************/
void hooks_command(const char *arg, struct session *ses)
{
    char left[BUFFER_SIZE], right[BUFFER_SIZE];

    arg=get_arg(arg, left, 0, ses);
    arg=space_out(arg);
    if (!*left || !*arg)
    {
        bool flag=true;
        for (int t=0;t<NHOOKS;t++)
            if (ses->hooks[t] && (!*left || is_abrev(left, hook_names[t])))
            {
                if (flag && !*left)
                    tintin_printf(ses, "#Defined hooks:"), flag=false;
                tintin_printf(ses, "%-10s: {%s}", hook_names[t], ses->hooks[t]);
                if (*left)
                    return;
            }
        if (flag)
            tintin_printf(ses, *left?"#No such hook.":"#No hooks defined.");
        return;
    }
    arg=get_arg_in_braces(arg, right, 1);
    for (int t=0;t<NHOOKS;t++)
        if (is_abrev(left, hook_names[t]))
        {
            SFREE(ses->hooks[t]);
            ses->hooks[t]=mystrdup(right);
            if (ses->mesvar[MSG_HOOK])
                tintin_printf(ses, "#Ok, will do {%s} on %s.", right, hook_names[t]);
            magic_close_hook=false;
            hooknum++;
            return;
        }
    tintin_eprintf(ses, "#Invalid hook: {%s}", left);
}

/***********************/
/* the #unhook command */
/***********************/
void unhook_command(const char *arg, struct session *ses)
{
    char left[BUFFER_SIZE];

    arg=get_arg(arg, left, 1, ses);
    if (!*left)
    {
        bool flag=true;
        for (int t=0;t<NHOOKS;t++)
            if (ses->hooks[t])
            {
                if (flag)
                    tintin_printf(ses, "#Defined hooks:"), flag=false;
                tintin_printf(ses, "%-10s: {%s}", hook_names[t], ses->hooks[t]);
            }
        if (flag)
            tintin_printf(ses, "#No hooks defined.");
        return;
    }
    for (int t=0;t<NHOOKS;t++)
        if (is_abrev(left, hook_names[t]))
        {
            if (ses->mesvar[MSG_HOOK])
                tintin_printf(ses, ses->hooks[t]?"#Removing hook on {%s}":
                    "#There was no hook on {%s} anyway", hook_names[t]);
            SFREE(ses->hooks[t]);
            ses->hooks[t]=0;
            return;
        }
    tintin_eprintf(ses, "#Invalid hook: {%s}", left);
}

struct session* do_hook(struct session *ses, int t, const char *data, bool blockzap)
{
    char hookcmd[BUFFER_SIZE];

    pvars_t vars, *lastvars;
    int oldclos=oldclos;

    if (!ses->hooks[t])
        return ses;

    if (blockzap)
    {
        oldclos=ses->closing;
        ses->closing=-1;
    }
    lastvars=pvars;
    pvars=&vars;
    for (int i=0;i<10;i++)
        vars[i][0]=0;
    if (data)
    {
        strcpy(vars[0], data);
        strcpy(vars[1], data);
    }
    if (ses->mesvar[MSG_HOOK]&&!magic_close_hook)
    {
        char buffer[BUFFER_SIZE];

        prepare_actionalias(ses->hooks[t], buffer, ses);
        tintin_printf(ses, "[HOOK: %s]", buffer);
    }
    in_alias=true;
    strcpy(hookcmd, ses->hooks[t]);
    ses=parse_input(hookcmd, true, ses);
    if (blockzap)
        ses->closing=oldclos;
    pvars=lastvars;
    return ses;
}

void set_magic_hook(struct session *ses)
{
    char temp[BUFFER_SIZE];

    sprintf(temp, "%cif {1==%clistlength {$SESSIONS}} %cend",
        tintin_char, tintin_char, tintin_char);
    SFREE(ses->hooks[1]);
    ses->hooks[1]=mystrdup(temp);
}