AmendHub

Download

jcs

/

subtext

/

session.h

 

(View History)

jcs   chat: Implement private messaging Latest amendment: 290 on 2022-11-19

1 /*
2 * Copyright (c) 2021 joshua stein <jcs@jcs.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17 #ifndef __SESSION_H__
18 #define __SESSION_H__
19
20 #include <stdlib.h>
21
22 #include "db.h"
23 #include "uthread.h"
24 #include "user.h"
25
26 enum session_input_state {
27 SESSION_INPUT_NONE,
28 SESSION_INPUT_LINE,
29 SESSION_INPUT_CHAR
30 };
31
32 #define DEFAULT_TERMINAL_COLUMNS 80
33 #define DEFAULT_TERMINAL_LINES 24
34 #define MIN_TERMINAL_COLUMNS 20
35 #define MIN_TERMINAL_LINES 4
36
37 #define CONTROL_C 3
38 #define CONTROL_D 4
39 #define BACKSPACE 8
40 #define CONTROL_L 12
41 #define DELETE 127
42
43 #define KEY_UP (0x0100 | 'A')
44 #define KEY_DOWN (0x0100 | 'B')
45 #define KEY_RIGHT (0x0100 | 'C')
46 #define KEY_LEFT (0x0100 | 'D')
47 #define KEY_OTHER (0x0100 | 0xff)
48
49 typedef struct session session;
50
51 struct node_funcs {
52 void (*setup)(struct session *session);
53 short (*input)(struct session *session);
54 short (*output)(struct session *session);
55 void (*close)(struct session *session);
56 };
57
58 struct session_log {
59 unsigned long id;
60 char username[DB_USERNAME_LENGTH + 1];
61 char node[10];
62 char via[10];
63 unsigned long logged_on_at;
64 unsigned long logged_off_at;
65 unsigned long ip_address;
66 unsigned short tspeed;
67 };
68
69 struct session {
70 char node[16];
71 char via[16];
72 unsigned char obuf[512];
73 unsigned char ibuf[512];
74 short obuflen;
75 short ibuflen;
76 short ibufoff;
77 unsigned long established_at;
78 char autologin_username[DB_USERNAME_LENGTH + 1];
79 char terminal_type[24];
80 enum session_input_state input_state;
81 unsigned short last_input;
82 unsigned long last_input_at;
83 unsigned short terminal_columns;
84 unsigned short terminal_lines;
85 unsigned short tspeed;
86 bool logged_in;
87 bool ending;
88 bool abort_input;
89 bool ban_node_source;
90 bool vt100;
91 bool vt52;
92 bool color;
93 bool cp437;
94 bool transferring_file;
95 bool is_telnet;
96 char chatting_with_node[10];
97 char chat_username[DB_USERNAME_LENGTH + 1];
98 bool chatting;
99 struct session_log log;
100 struct user *user;
101 void *cookie;
102 struct node_funcs *node_funcs;
103 struct uthread *uthread;
104 };
105
106 #define MAX_SESSIONS NUM_UTHREADS
107 extern struct session *sessions[MAX_SESSIONS];
108 extern short nsessions;
109
110 struct session_tally {
111 char date[9];
112 unsigned long calls;
113 };
114 extern struct session_tally session_today_tally;
115
116 struct session_menu_option {
117 char ret;
118 char key[15];
119 char title[50];
120 };
121
122 struct session * session_create(char *node, char *via,
123 struct node_funcs *node_funcs);
124 void session_close(struct session *session);
125 void session_idle(struct session *session);
126 short session_get_char(struct session *session, unsigned char *b);
127 bool session_wait_for_chars(struct session *session,
128 unsigned short timeout_ms, unsigned short num_chars);
129 unsigned short session_input_char(struct session *session);
130 void session_clear_input(struct session *session);
131 void session_flush(struct session *session);
132 size_t session_logf(struct session *session, const char *format, ...);
133 size_t session_logf_buffered(struct session *session, const char *format, ...);
134 size_t session_output(struct session *session, const char *str, size_t len);
135 size_t session_printf(struct session *session, const char *format, ...);
136 size_t session_output_view_or_printf(struct session *session, short id,
137 const char *format, ...);
138 size_t session_output_template(struct session *session, const char *str);
139 size_t session_expand_template(struct session *session, const char *str,
140 char **ret);
141 char * session_field_input(struct session *session, unsigned short size,
142 unsigned short width, char *initial_input, bool multiline, char mask);
143 void session_pause_return(struct session *s, char enforce, char *msg);
144 void session_print_motd(struct session *s, bool force);
145 void session_recents(struct session *s);
146 void session_who(struct session *s);
147 char session_menu(struct session *s, char *title, char *prompt,
148 const struct session_menu_option *opts, size_t nopts, bool show_opts);
149 void session_purge_logs(short days);
150
151 #endif /* __SESSION_H__ */