about summary refs log tree commit diff stats
path: root/src/chat_state.c
blob: 9909ec4a4df5e7453366839a4aa75be10191a3da (plain) (blame)
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
/*
 * chat_state.c
 *
 * Copyright (C) 2012 - 2016 James Booth <boothj5@gmail.com>
 *
 * This file is part of Profanity.
 *
 * Profanity 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.
 *
 * Profanity 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 Profanity.  If not, see <http://www.gnu.org/licenses/>.
 *
 * In addition, as a special exception, the copyright holders give permission to
 * link the code of portions of this program with the OpenSSL library under
 * certain conditions as described in each individual source file, and
 * distribute linked combinations including the two.
 *
 * You must obey the GNU General Public License in all respects for all of the
 * code used other than OpenSSL. If you modify file(s) with this exception, you
 * may extend this exception to your version of the file(s), but you are not
 * obligated to do so. If you do not wish to do so, delete this exception
 * statement from your version. If you delete this exception statement from all
 * source files in the program, then also delete it here.
 *
 */

#include <stdlib.h>

#include <glib.h>

#include "chat_state.h"
#include "chat_session.h"
#include "xmpp/xmpp.h"
#include "config/preferences.h"

#define PAUSED_TIMEOUT 10.0
#define INACTIVE_TIMEOUT 30.0

static void _send_if_supported(const char *const barejid, void (*send_func)(const char *const));

ChatState*
chat_state_new(void)
{
    ChatState *new_state = malloc(sizeof(struct prof_chat_state_t));
    new_state->type = CHAT_STATE_GONE;
    new_state->timer = g_timer_new();

    return new_state;
}

void
chat_state_free(ChatState *state)
{
    if (state && state->timer!= NULL) {
        g_timer_destroy(state->timer);
    }
    free(state);
}

void
chat_state_handle_idle(const char *const barejid, ChatState *state)
{
    gdouble elapsed = g_timer_elapsed(state->timer, NULL);

    // TYPING -> PAUSED
    if (state->type == CHAT_STATE_COMPOSING && elapsed > PAUSED_TIMEOUT) {
        state->type = CHAT_STATE_PAUSED;
        g_timer_start(state->timer);
        if (prefs_get_boolean(PREF_STATES) && prefs_get_boolean(PREF_OUTTYPE)) {
            _send_if_supported(barejid, message_send_paused);
        }
        return;
    }

    // PAUSED|ACTIVE -> INACTIVE
    if ((state->type == CHAT_STATE_PAUSED || state->type == CHAT_STATE_ACTIVE) && elapsed > INACTIVE_TIMEOUT) {
        state->type = CHAT_STATE_INACTIVE;
        g_timer_start(state->timer);
        if (prefs_get_boolean(PREF_STATES)) {
            _send_if_supported(barejid, message_send_inactive);
        }
        return;

    }

    // INACTIVE -> GONE
    if (state->type == CHAT_STATE_INACTIVE) {
        if (prefs_get_gone() != 0 && (elapsed > (prefs_get_gone() * 60.0))) {
            ChatSession *session = chat_session_get(barejid);
            if (session) {
                // never move to GONE when resource override
                if (!session->resource_override) {
                    if (prefs_get_boolean(PREF_STATES)) {
                        _send_if_supported(barejid, message_send_gone);
                    }
                    chat_session_remove(barejid);
                    state->type = CHAT_STATE_GONE;
                    g_timer_start(state->timer);
                }
            } else {
                if (prefs_get_boolean(PREF_STATES)) {
                    message_send_gone(barejid);
                }
                state->type = CHAT_STATE_GONE;
                g_timer_start(state->timer);
            }
            return;
        }
    }
}

void
chat_state_handle_typing(const char *const barejid, ChatState *state)
{
    // ACTIVE|INACTIVE|PAUSED|GONE -> COMPOSING
    if (state->type != CHAT_STATE_COMPOSING) {
        state->type = CHAT_STATE_COMPOSING;
        g_timer_start(state->timer);
        if (prefs_get_boolean(PREF_STATES) && prefs_get_boolean(PREF_OUTTYPE)) {
            _send_if_supported(barejid, message_send_composing);
        }
    }
}

void
chat_state_active(ChatState *state)
{
    state->type = CHAT_STATE_ACTIVE;
    g_timer_start(state->timer);
}

void
chat_state_gone(const char *const barejid, ChatState *state)
{
    if (state->type != CHAT_STATE_GONE) {
        if (prefs_get_boolean(PREF_STATES)) {
            _send_if_supported(barejid, message_send_gone);
        }
        state->type = CHAT_STATE_GONE;
        g_timer_start(state->timer);
    }
}

static void
_send_if_supported(const char *const barejid, void (*send_func)(const char *const))
{
    gboolean send = TRUE;
    GString *jid = g_string_new(barejid);
    ChatSession *session = chat_session_get(barejid);
    if (session) {
        if (session->send_states) {
            g_string_append(jid, "/");
            g_string_append(jid, session->resource);
        } else {
            send = FALSE;
        }
    }

    if (send) {
        send_func(jid->str);
    }

    g_string_free(jid, TRUE);
}
an class="p">/deref <- write 1:channel-address/raw x:tagged-value)) ; check that read-move consumes them and then goes to sleep (when (ran-to-completion 'read-move) (prn "F - chessboard waits after each subsequent letter of move until the last")) (wipe completed-routines*) ; send final key ;? (prn "== final key") (restart read-move-routine) ;? (set dump-trace*) (run-code send-final-key (default-space:space-address <- new space:literal 30:literal/capacity) (c:character <- copy ((#\newline literal))) (x:tagged-value <- save-type c:character) (1:channel-address/raw/deref <- write 1:channel-address/raw x:tagged-value)) ; check that read-move consumes it and -- this time -- returns (when (~ran-to-completion 'read-move) (prn "F - 'read-move' completes after final letter of move")) ) (reset2) (new-trace "read-move-quit") (run-code main (default-space:space-address <- new space:literal 30:literal/capacity) (stdin:channel-address <- init-channel 1:literal) (dummy:terminal-address <- init-fake-terminal 20:literal 10:literal) (r:integer/routine <- fork-helper read-move:fn nil:literal/globals 2000:literal/limit stdin:channel-address dummy:terminal-address) (c:character <- copy ((#\q literal))) (x:tagged-value <- save-type c:character) (stdin:channel-address/deref <- write stdin:channel-address x:tagged-value) (sleep until-routine-done:literal r:integer/routine) ) (when (~ran-to-completion 'read-move) (prn "F - chessboard quits on move starting with 'q'")) (reset2) (new-trace "read-illegal-file") (run-code main (default-space:space-address <- new space:literal 30:literal/capacity) (stdin:channel-address <- init-channel 1:literal) (dummy:terminal-address <- init-fake-terminal 20:literal 10:literal) (r:integer/routine <- fork-helper read-file:fn nil:literal/globals 2000:literal/limit stdin:channel-address dummy:terminal-address) (c:character <- copy ((#\i literal))) (x:tagged-value <- save-type c:character) (stdin:channel-address/deref <- write stdin:channel-address x:tagged-value) (sleep until-routine-done:literal r:integer/routine) ) ;? (each routine completed-routines* ;? (prn " " routine)) (when (or (ran-to-completion 'read-file) (let routine routine-running!read-file (~posmatch "file too high" rep.routine!error))) (prn "F - 'read-file' checks that file lies between 'a' and 'h'")) (reset2) (new-trace "read-illegal-rank") (run-code main (default-space:space-address <- new space:literal 30:literal/capacity) (stdin:channel-address <- init-channel 1:literal) (dummy:terminal-address <- init-fake-terminal 20:literal 10:literal) (r:integer/routine <- fork-helper read-rank:fn nil:literal/globals 2000:literal/limit stdin:channel-address dummy:terminal-address) (c:character <- copy ((#\9 literal))) (x:tagged-value <- save-type c:character) (stdin:channel-address/deref <- write stdin:channel-address x:tagged-value) (sleep until-routine-done:literal r:integer/routine) ) (when (or (ran-to-completion 'read-rank) (let routine routine-running!read-rank (~posmatch "rank too high" rep.routine!error))) (prn "F - 'read-rank' checks that rank lies between '1' and '8'")) (reset2) (new-trace "print-board") (run-code main (default-space:space-address <- new space:literal 30:literal/capacity) ;? ($print (("init-array\n" literal))) ;? 1 (initial-position:integer-array-address <- init-array ((#\R literal)) ((#\P literal)) ((#\_ literal)) ((#\_ literal)) ((#\_ literal)) ((#\_ literal)) ((#\p literal)) ((#\r literal)) ((#\N literal)) ((#\P literal)) ((#\_ literal)) ((#\_ literal)) ((#\_ literal)) ((#\_ literal)) ((#\p literal)) ((#\n literal)) ((#\B literal)) ((#\P literal)) ((#\_ literal)) ((#\_ literal)) ((#\_ literal)) ((#\_ literal)) ((#\p literal)) ((#\b literal)) ((#\Q literal)) ((#\P literal)) ((#\_ literal)) ((#\_ literal)) ((#\_ literal)) ((#\_ literal)) ((#\p literal)) ((#\q literal)) ((#\K literal)) ((#\P literal)) ((#\_ literal)) ((#\_ literal)) ((#\_ literal)) ((#\_ literal)) ((#\p literal)) ((#\k literal)) ((#\B literal)) ((#\P literal)) ((#\_ literal)) ((#\_ literal)) ((#\_ literal)) ((#\_ literal)) ((#\p literal)) ((#\b literal)) ((#\N literal)) ((#\P literal)) ((#\_ literal)) ((#\_ literal)) ((#\_ literal)) ((#\_ literal)) ((#\p literal)) ((#\n literal)) ((#\R literal)) ((#\P literal)) ((#\_ literal)) ((#\_ literal)) ((#\_ literal)) ((#\_ literal)) ((#\p literal)) ((#\r literal))) ;? ($print (("init-board\n" literal))) ;? 1 (b:board-address <- init-board initial-position:integer-array-address) (screen:terminal-address <- init-fake-terminal 20:literal 10:literal) (print-board screen:terminal-address b:board-address) (1:string-address/raw <- get screen:terminal-address/deref data:offset) ) (each routine completed-routines* (awhen rep.routine!error (prn "error - " it))) ;? (prn memory*.1) (when (~screen-contains memory*.1 20 (+ "8 | r n b q k b n r " "7 | p p p p p p p p " "6 | _ _ _ _ _ _ _ _ " "5 | _ _ _ _ _ _ _ _ " "4 | _ _ _ _ _ _ _ _ " "3 | _ _ _ _ _ _ _ _ " "2 | P P P P P P P P " "1 | R N B Q K B N R " " +---------------- " " a b c d e f g h ")) (prn "F - print-board works; chessboard begins at @memory*.1")) ; todo: how to fold this more elegantly with the previous test? (reset2) (new-trace "make-move") (run-code main (default-space:space-address <- new space:literal 30:literal/capacity) ; fake screen (screen:terminal-address <- init-fake-terminal 20:literal 10:literal) ; initial position (initial-position:integer-array-address <- init-array ((#\R literal)) ((#\P literal)) ((#\_ literal)) ((#\_ literal)) ((#\_ literal)) ((#\_ literal)) ((#\p literal)) ((#\r literal)) ((#\N literal)) ((#\P literal)) ((#\_ literal)) ((#\_ literal)) ((#\_ literal)) ((#\_ literal)) ((#\p literal)) ((#\n literal)) ((#\B literal)) ((#\P literal)) ((#\_ literal)) ((#\_ literal)) ((#\_ literal)) ((#\_ literal)) ((#\p literal)) ((#\b literal)) ((#\Q literal)) ((#\P literal)) ((#\_ literal)) ((#\_ literal)) ((#\_ literal)) ((#\_ literal)) ((#\p literal)) ((#\q literal)) ((#\K literal)) ((#\P literal)) ((#\_ literal)) ((#\_ literal)) ((#\_ literal)) ((#\_ literal)) ((#\p literal)) ((#\k literal)) ((#\B literal)) ((#\P literal)) ((#\_ literal)) ((#\_ literal)) ((#\_ literal)) ((#\_ literal)) ((#\p literal)) ((#\b literal)) ((#\N literal)) ((#\P literal)) ((#\_ literal)) ((#\_ literal)) ((#\_ literal)) ((#\_ literal)) ((#\p literal)) ((#\n literal)) ((#\R literal)) ((#\P literal)) ((#\_ literal)) ((#\_ literal)) ((#\_ literal)) ((#\_ literal)) ((#\p literal)) ((#\r literal))) (b:board-address <- init-board initial-position:integer-array-address) ; move: a2-a4 (m:move-address <- new move:literal) (f:integer-integer-pair-address <- get-address m:move-address/deref from:offset) (dest:integer-address <- get-address f:integer-integer-pair-address/deref 0:offset) (dest:integer-address/deref <- copy 0:literal) ; from-file: a (dest:integer-address <- get-address f:integer-integer-pair-address/deref 1:offset) (dest:integer-address/deref <- copy 1:literal) ; from-rank: 2 (t0:integer-integer-pair-address <- get-address m:move-address/deref to:offset) (dest:integer-address <- get-address t0:integer-integer-pair-address/deref 0:offset) (dest:integer-address/deref <- copy 0:literal) ; to-file: a (dest:integer-address <- get-address t0:integer-integer-pair-address/deref 1:offset) (dest:integer-address/deref <- copy 3:literal) ; to-rank: 4 (b:board-address <- make-move b:board-address m:move-address) (print-board screen:terminal-address b:board-address) (1:string-address/raw <- get screen:terminal-address/deref data:offset) ) (each routine completed-routines* (awhen rep.routine!error (prn "error - " it))) ;? (prn memory*.1) (when (~screen-contains memory*.1 20 (+ "8 | r n b q k b n r " "7 | p p p p p p p p " "6 | _ _ _ _ _ _ _ _ " "5 | _ _ _ _ _ _ _ _ " "4 | P _ _ _ _ _ _ _ " "3 | _ _ _ _ _ _ _ _ " "2 | _ P P P P P P P " "1 | R N B Q K B N R " " +---------------- " " a b c d e f g h ")) (prn "F - make-move works; chessboard begins at @memory*.1")) (reset2)