about summary refs log tree commit diff stats
path: root/src/chat_state.c
blob: 8ddb6f1ea02a33944f41a3516b9e288910cd09e9 (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 - 2015 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);
}
le='author James Booth <boothj5@gmail.com> 2015-09-21 21:40:04 +0100 committer James Booth <boothj5@gmail.com> 2015-09-21 21:40:04 +0100 Check for libmesode, fall back to libstrophe' href='/danisanti/profani-tty/commit/install-all.sh?id=2b88e2f1bffca2800b521214df6009da673a0835'>2b88e2f1 ^
8f0bc28b ^
76fe37ef ^
b2cb4662 ^
2b88e2f1 ^
3c5d5e2e ^
b2cb4662 ^

f736a248 ^
2b88e2f1 ^
f736a248 ^


24a60449 ^
2b88e2f1 ^
8f0bc28b ^
76fe37ef ^


76fe37ef ^
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216


               
 

                  









                                                                                               
                








                                                          
                                                                                                                                                                                                   


 
                




                                                       
                                                                                                                                                                                                        

 

                  


                                                      
                                                                                                                                                                                                                         

 








                                                      
                                                                                                                                                                             

 





                                                       
                                                                     
                                 
                                                                                                                                                                                                                                                                                                  
        
                                                                                                                                                                                                                                                                                   
 
      

 
                    

        
                                                    
        
                                                      
                
                  
                           










                                                    


                                            




                     











                                                                                      
                        

        
                                                    
        
                                                      
                

                                                               
                             










                                                    


                                            




                





                                           

                                         







                                         






                               
             

            


                                       
                                         
                   

                                         

                                     
      




                             


               

                                                             

                
                          
                                   
                             

                          
                                   
                             
              
                            
                                         
                               

                             
                          
                                   


                             
                          
                                  
                                 


              
       
#!/bin/bash

set -o errtrace

STATUS=development

error_handler()
{
        ERR_CODE=$?
        echo "Error $ERR_CODE with command '$BASH_COMMAND' on line ${BASH_LINENO[0]}. Exiting."
        exit $ERR_CODE

}

trap error_handler ERR

debian_prepare()
{
    echo
    echo Profanity installer ... updating apt repositories
    echo
    sudo apt-get update

    echo
    echo Profanity installer... installing dependencies
    echo
    sudo apt-get -y install git automake autoconf libssl-dev libexpat1-dev libncursesw5-dev libglib2.0-dev libnotify-dev libcurl3-dev libxss-dev libotr5-dev libreadline-dev libtool libgpgme11-dev

}

fedora_prepare()
{
    echo
    echo Profanity installer... installing dependencies
    echo

    sudo dnf -y install gcc git autoconf automake openssl-devel expat-devel ncurses-devel glib2-devel libnotify-devel libcurl-devel libXScrnSaver-devel libotr3-devel readline-devel libtool gpgme-devel
}

opensuse_prepare()
{
    echo
    echo Profanity installer...installing dependencies
    echo
    sudo zypper -n in gcc git automake make autoconf libopenssl-devel expat libexpat-devel ncurses-devel glib2-devel libnotify-devel libcurl-devel libXScrnSaver-devel libotr-devel readline-devel libtool libgpgme-devel
}

centos_prepare()
{
    echo
    echo Profanity installer...installing dependencies
    echo

    sudo yum -y install epel-release
    sudo yum -y install git
    sudo yum -y install gcc autoconf automake cmake
    sudo yum -y install openssl-devel expat-devel ncurses-devel glib2-devel libnotify-devel libcurl-devel libXScrnSaver-devel libotr-devel readline-devel libtool gpgme-devel
}

cygwin_prepare()
{
    echo
    echo Profanity installer... installing dependencies
    echo

    if ! command -v apt-cyg &>/dev/null; then cyg_install_apt_cyg; fi
    if [ -n "$CYG_MIRROR" ]; then
        apt-cyg -m $CYG_MIRROR install git make gcc-core m4 automake autoconf pkg-config openssl-devel libexpat-devel zlib-devel libncursesw-devel libglib2.0-devel libcurl-devel libidn-devel libssh2-devel libkrb5-devel openldap-devel libgcrypt-devel libreadline-devel libgpgme-devel libtool
    else
        apt-cyg install git make gcc-core m4 automake autoconf pkg-config openssl-devel libexpat-devel zlib-devel libncursesw-devel libglib2.0-devel libcurl-devel libidn-devel libssh2-devel libkrb5-devel openldap-devel libgcrypt-devel libreadline-devel libgpgme-devel libtool

    fi
}

install_lib_mesode()
{
    echo
    echo Profanity installer... installing libmesode
    echo
    git clone https://github.com/boothj5/libmesode.git
    cd libmesode
    ./bootstrap.sh
    ./configure --prefix=$1
    make
    sudo make install

    cd ..
}

install_profanity()
{
    echo
    echo Profanity installer... installing Profanity
    echo
    if [ "${STATUS}" = "development" ]; then
        ./bootstrap.sh
    fi
    ./configure
    make
    sudo make install
}

cyg_install_apt_cyg()
{
    echo
    echo Profanity installer... installing apt-cyg
    echo
    wget https://raw.githubusercontent.com/transcode-open/apt-cyg/master/apt-cyg
    #wget --no-check-certificate https://raw.github.com/boothj5/apt-cyg/master/apt-cyg
    #wget http://apt-cyg.googlecode.com/svn/trunk/apt-cyg
    chmod +x apt-cyg
    mv apt-cyg /usr/local/bin/

}
cyg_install_lib_mesode()
{
    echo
    echo Profanity installer... installing libmesode
    echo
    git clone https://github.com/boothj5/libmesode.git
    cd libmesode
    ./bootstrap.sh
    ./bootstrap.sh # second call seems to fix problem on cygwin
    ./configure --prefix=/usr
    make
    make install

    cd ..
}

cyg_install_profanity()
{
    echo
    echo Profanity installer... installing Profanity
    echo
    if [ "${STATUS}" = "development" ]; then
        ./bootstrap.sh
    fi
    ./configure
    make
    make install
}

cleanup()
{
    echo
    echo Profanity installer... cleaning up
    echo

    echo Removing libmesode repository...
    rm -rf libmesode

    echo
    echo Profanity installer... complete!
    echo
    echo Type \'profanity\' to run.
    echo
}

while getopts m: opt
do
    case "$opt" in
        m) CYG_MIRROR=$OPTARG;;
    esac
done

OS=`uname -s`
DIST=unknown

if [ "${OS}" = "Linux" ]; then
    if [ -f /etc/fedora-release ]; then
        DIST=fedora
    elif [ -f /etc/debian_version ]; then
        DIST=debian
    elif [ -f /etc/centos-release ]; then
        DIST=centos
    elif [ -f /etc/os-release ]; then
        DIST=opensuse
    fi
else
    echo $OS | grep -i cygwin
    if [ "$?" -eq 0 ]; then
        DIST=cygwin
    fi
fi

case "$DIST" in
unknown)    echo The install script will not work on this OS.
            echo Try a manual install instead.
            exit
            ;;
fedora)     fedora_prepare
            install_lib_mesode /usr
            install_profanity
            ;;
debian)     debian_prepare
            install_lib_mesode /usr
            install_profanity
            ;;
opensuse)   opensuse_prepare
            install_lib_mesode /usr/local
            sudo /sbin/ldconfig
            install_profanity
            ;;
centos)     centos_prepare
            install_lib_mesode /usr
            sudo ldconfig
            install_profanity
            ;;
cygwin)     cygwin_prepare
            cyg_install_lib_mesode
            cyg_install_profanity
            ;;
esac

cleanup