summaryrefslogtreecommitdiff
path: root/src/plugins/irc/irc-typing.c
blob: 396460b13baa9a2bf1a4cfcf9cbb137be0279f0c (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
/*
 * irc-typing.c - manage typing status on channels/private
 *
 * Copyright (C) 2021 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/>.
 */

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

#include "../weechat-plugin.h"
#include "irc.h"
#include "irc-buffer.h"
#include "irc-channel.h"
#include "irc-config.h"
#include "irc-server.h"


/*
 * Callback for signals "typing_self_*".
 */

int
irc_typing_signal_typing_self_cb (const void *pointer, void *data,
                                  const char *signal, const char *type_data,
                                  void *signal_data)
{
    struct t_irc_server *ptr_server;
    struct t_irc_channel *ptr_channel;
    int new_state;

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

    /* sending self typing status is allowed? */
    if (!weechat_config_boolean (irc_config_look_typing_status_self))
        return WEECHAT_RC_OK;

    /* search server/channel with buffer */
    irc_buffer_get_server_and_channel (signal_data, &ptr_server, &ptr_channel);
    if (!ptr_server || !ptr_channel)
        return WEECHAT_RC_OK;

    /* typing not allowed on server? */
    if (!ptr_server->typing_allowed)
        return WEECHAT_RC_OK;

    /* typing works only if capability "message-tags" is enabled */
    if (!weechat_hashtable_has_key (ptr_server->cap_list, "message-tags"))
        return WEECHAT_RC_OK;

    if (strcmp (signal, "typing_self_typing") == 0)
        new_state = IRC_CHANNEL_TYPING_STATE_ACTIVE;
    else if (strcmp (signal, "typing_self_paused") == 0)
        new_state = IRC_CHANNEL_TYPING_STATE_PAUSED;
    else if (strcmp (signal, "typing_self_cleared") == 0)
        new_state = IRC_CHANNEL_TYPING_STATE_DONE;
    else if (strcmp (signal, "typing_self_sent") == 0)
        new_state = IRC_CHANNEL_TYPING_STATE_OFF;
    else
        new_state = -1;

    if ((new_state >= 0) && (new_state != ptr_channel->typing_state))
    {
        ptr_channel->typing_state = new_state;
        ptr_channel->typing_status_sent = 0;
    }

    return WEECHAT_RC_OK;
}

/*
 * Sends self typing status to channels/privates of a server.
 */

void
irc_typing_send_to_targets (struct t_irc_server *server)
{
    struct t_irc_channel *ptr_channel;
    time_t current_time;

    if (!weechat_config_boolean (irc_config_look_typing_status_self)
        || !server->typing_allowed)
    {
        return;
    }

    current_time = time (NULL);

    for (ptr_channel = server->channels; ptr_channel;
         ptr_channel = ptr_channel->next_channel)
    {
        if (!ptr_channel->part
            && (ptr_channel->typing_state != IRC_CHANNEL_TYPING_STATE_OFF)
            && (ptr_channel->typing_status_sent + 3 < current_time))
        {
            irc_server_sendf (
                server,
                IRC_SERVER_SEND_OUTQ_PRIO_LOW, NULL,
                "@+typing=%s TAGMSG %s",
                irc_channel_typing_state_string[ptr_channel->typing_state],
                ptr_channel->name);
            if (ptr_channel->typing_state == IRC_CHANNEL_TYPING_STATE_ACTIVE)
            {
                ptr_channel->typing_status_sent = current_time;
            }
            else
            {
                ptr_channel->typing_state = IRC_CHANNEL_TYPING_STATE_OFF;
                ptr_channel->typing_status_sent = 0;
            }
        }
    }
}

/*
 * Sets state of a nick on a channel.
 */

void
irc_typing_channel_set_nick (struct t_irc_channel *channel, const char *nick,
                             int state)
{
    char signal_data[1024];

    snprintf (signal_data, sizeof (signal_data),
              "0x%lx;%s;%s",
              (unsigned long)channel->buffer,
              (state == IRC_CHANNEL_TYPING_STATE_ACTIVE) ? "typing" :
              ((state == IRC_CHANNEL_TYPING_STATE_PAUSED) ? "paused" : "off"),
              nick);
    weechat_hook_signal_send ("typing_set_nick",
                              WEECHAT_HOOK_SIGNAL_STRING,
                              signal_data);
}

/*
 * Resets all nicks state on a channel.
 */

void
irc_typing_channel_reset (struct t_irc_channel *channel)
{
    weechat_hook_signal_send ("typing_reset_buffer",
                              WEECHAT_HOOK_SIGNAL_POINTER,
                              channel->buffer);
}