AmendHub

Download

jcs

/

subtext

/

session.h

 

(View History)

jcs   session: Add session_get_chars to take multiple chars at once Latest amendment: 494 on 2023-04-27

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[768];
73 unsigned long obuf_canary;
74 unsigned char ibuf[512];
75 unsigned long ibuf_canary;
76 short obuflen;
77 unsigned long obuflen_canary;
78 short ibuflen;
79 unsigned long ibuflen_canary;
80 short ibufoff;
81 unsigned long established_at;
82 char autologin_username[DB_USERNAME_LENGTH + 1];
83 char terminal_type[24];
84 enum session_input_state input_state;
85 unsigned short last_input;
86 unsigned long last_input_at;
87 unsigned short terminal_columns;
88 unsigned short terminal_lines;
89 unsigned short tspeed;
90 bool logged_in;
91 bool ending;
92 bool abort_input;
93 bool ban_node_source;
94 bool vt100;
95 bool vt52;
96 bool color;
97 bool direct_output;
98 bool is_telnet;
99 bool can_nomodem;
100 char chatting_with_node[16];
101 char chat_username[DB_USERNAME_LENGTH + 1];
102 bool chatting;
103 struct session_log log;
104 struct user *user;
105 void *cookie;
106 struct node_funcs *node_funcs;
107 struct uthread *uthread;
108 };
109
110 #define MAX_SESSIONS NUM_UTHREADS
111 extern struct session *sessions[MAX_SESSIONS];
112 extern short nsessions;
113
114 struct session_tally {
115 char date[9];
116 unsigned long calls;
117 };
118 extern struct session_tally session_today_tally;
119
120 struct session_menu_option {
121 char ret;
122 char key[15];
123 char title[50];
124 };
125
126 struct session * session_create(char *node, char *via,
127 struct node_funcs *node_funcs);
128 void session_close(struct session *session);
129 void session_idle(struct session *session);
130 short session_get_char(struct session *session, unsigned char *b);
131 short session_get_chars(struct session *session, unsigned char *b,
132 size_t count);
133 bool session_wait_for_chars(struct session *session,
134 unsigned short timeout_ms, unsigned short num_chars);
135 unsigned short session_input_char(struct session *session);
136 void session_clear_input(struct session *session);
137 void session_flush(struct session *session);
138 size_t session_logf(struct session *session, const char *format, ...);
139 size_t session_logf_buffered(struct session *session, const char *format, ...);
140 size_t session_output(struct session *session, const char *str, size_t len);
141 size_t session_paginate(struct session *session, const char *str,
142 size_t len, size_t first_page_printed);
143 size_t session_printf(struct session *session, const char *format, ...);
144 size_t session_output_view_or_printf(struct session *session, short id,
145 const char *format, ...);
146 size_t session_output_template(struct session *session, const char *str);
147 size_t session_expand_template(struct session *session, const char *str,
148 char **ret);
149 char * session_field_input(struct session *session, unsigned short size,
150 unsigned short width, char *initial_input, bool multiline, char mask);
151 void session_pause_return(struct session *s, char enforce, char *msg);
152 void session_print_motd(struct session *s, bool force);
153 void session_recents(struct session *s);
154 void session_who(struct session *s);
155 char session_menu(struct session *s, char *title, char *prompt,
156 char *prompt_help, const struct session_menu_option *opts, size_t nopts,
157 bool show_opts, char *number_prompt, short *ret_number);
158 void session_prune_logs(short days);
159 void session_check_buf_canaries(struct session *session);
160
161 #endif /* __SESSION_H__ */