[dab82f29] | 1 | /* Copyright (c) 2002,2003,2004,2009 James M. Kretchmar |
---|
[7d4fbcd] | 2 | * |
---|
[dab82f29] | 3 | * This file is part of Owl. |
---|
| 4 | * |
---|
| 5 | * Owl is free software: you can redistribute it and/or modify |
---|
| 6 | * it under the terms of the GNU General Public License as published by |
---|
| 7 | * the Free Software Foundation, either version 3 of the License, or |
---|
| 8 | * (at your option) any later version. |
---|
| 9 | * |
---|
| 10 | * Owl is distributed in the hope that it will be useful, |
---|
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 13 | * GNU General Public License for more details. |
---|
| 14 | * |
---|
| 15 | * You should have received a copy of the GNU General Public License |
---|
| 16 | * along with Owl. If not, see <http://www.gnu.org/licenses/>. |
---|
| 17 | * |
---|
| 18 | * --------------------------------------------------------------- |
---|
[5ca5f8e] | 19 | * |
---|
[dab82f29] | 20 | * As of Owl version 2.1.12 there are patches contributed by |
---|
[fa00c5c] | 21 | * developers of the branched BarnOwl project, Copyright (c) |
---|
| 22 | * 2006-2009 The BarnOwl Developers. All rights reserved. |
---|
[7d4fbcd] | 23 | */ |
---|
| 24 | |
---|
| 25 | #include <stdio.h> |
---|
| 26 | #include <unistd.h> |
---|
| 27 | #include <stdlib.h> |
---|
| 28 | #include <string.h> |
---|
| 29 | #include <signal.h> |
---|
[5145235] | 30 | #include <time.h> |
---|
[7d4fbcd] | 31 | #include <sys/param.h> |
---|
[700c712] | 32 | #include <sys/types.h> |
---|
[8232149] | 33 | #include <termios.h> |
---|
[38cf544c] | 34 | #include <sys/stat.h> |
---|
[7d4fbcd] | 35 | #include "owl.h" |
---|
| 36 | |
---|
[2a2bb60] | 37 | #if OWL_STDERR_REDIR |
---|
[e8b95f8] | 38 | #ifdef HAVE_SYS_IOCTL_H |
---|
[2a2bb60] | 39 | #include <sys/ioctl.h> |
---|
[e8b95f8] | 40 | #endif |
---|
| 41 | #ifdef HAVE_SYS_FILIO_H |
---|
| 42 | #include <sys/filio.h> |
---|
| 43 | #endif |
---|
[2a2bb60] | 44 | int stderr_replace(void); |
---|
| 45 | #endif |
---|
| 46 | |
---|
[dab82f29] | 47 | #define STDIN 0 |
---|
| 48 | |
---|
[1aee7d9] | 49 | static const char fileIdent[] = "$Id$"; |
---|
| 50 | |
---|
[b2b0773] | 51 | owl_global g; |
---|
| 52 | |
---|
[de22c3d] | 53 | int main(int argc, char **argv, char **env) |
---|
| 54 | { |
---|
[7d4fbcd] | 55 | WINDOW *recwin, *sepwin, *typwin, *msgwin; |
---|
| 56 | owl_editwin *tw; |
---|
| 57 | owl_popwin *pw; |
---|
[dab82f29] | 58 | int ret, initialsubs, debug, argcsave, followlast; |
---|
| 59 | int newmsgs, nexttimediff; |
---|
[7d4fbcd] | 60 | struct sigaction sigact; |
---|
[19c0c55] | 61 | char *configfile, *tty, *perlout, *perlerr, **argvsave; |
---|
[7d4fbcd] | 62 | owl_filter *f; |
---|
[bd3f232] | 63 | owl_style *s; |
---|
[a15a84f] | 64 | time_t nexttime, now; |
---|
| 65 | struct tm *today; |
---|
[38cf544c] | 66 | char *dir; |
---|
[8232149] | 67 | struct termios tio; |
---|
[7d4fbcd] | 68 | |
---|
| 69 | argcsave=argc; |
---|
| 70 | argvsave=argv; |
---|
| 71 | configfile=NULL; |
---|
| 72 | tty=NULL; |
---|
| 73 | debug=0; |
---|
[bd3f232] | 74 | initialsubs=1; |
---|
[7d4fbcd] | 75 | if (argc>0) { |
---|
| 76 | argv++; |
---|
| 77 | argc--; |
---|
| 78 | } |
---|
| 79 | while (argc>0) { |
---|
| 80 | if (!strcmp(argv[0], "-n")) { |
---|
| 81 | initialsubs=0; |
---|
| 82 | argv++; |
---|
| 83 | argc--; |
---|
| 84 | } else if (!strcmp(argv[0], "-c")) { |
---|
| 85 | if (argc<2) { |
---|
| 86 | fprintf(stderr, "Too few arguments to -c\n"); |
---|
| 87 | usage(); |
---|
| 88 | exit(1); |
---|
| 89 | } |
---|
| 90 | configfile=argv[1]; |
---|
| 91 | argv+=2; |
---|
| 92 | argc-=2; |
---|
| 93 | } else if (!strcmp(argv[0], "-t")) { |
---|
| 94 | if (argc<2) { |
---|
| 95 | fprintf(stderr, "Too few arguments to -t\n"); |
---|
| 96 | usage(); |
---|
| 97 | exit(1); |
---|
| 98 | } |
---|
| 99 | tty=argv[1]; |
---|
| 100 | argv+=2; |
---|
| 101 | argc-=2; |
---|
| 102 | } else if (!strcmp(argv[0], "-d")) { |
---|
| 103 | debug=1; |
---|
| 104 | argv++; |
---|
| 105 | argc--; |
---|
[a68f05d] | 106 | } else if (!strcmp(argv[0], "-D")) { |
---|
| 107 | debug=1; |
---|
| 108 | unlink(OWL_DEBUG_FILE); |
---|
| 109 | argv++; |
---|
| 110 | argc--; |
---|
[7d4fbcd] | 111 | } else if (!strcmp(argv[0], "-v")) { |
---|
| 112 | printf("This is owl version %s\n", OWL_VERSION_STRING); |
---|
| 113 | exit(0); |
---|
| 114 | } else { |
---|
| 115 | fprintf(stderr, "Uknown argument\n"); |
---|
| 116 | usage(); |
---|
| 117 | exit(1); |
---|
| 118 | } |
---|
| 119 | } |
---|
| 120 | |
---|
[176d3443] | 121 | owl_function_debugmsg("startup: Finished parsing arguments"); |
---|
| 122 | |
---|
[be0a79f] | 123 | #ifdef HAVE_LIBZEPHYR |
---|
[7d4fbcd] | 124 | /* zephyr init */ |
---|
[be0a79f] | 125 | ret=owl_zephyr_initialize(); |
---|
| 126 | if (ret) { |
---|
[7d4fbcd] | 127 | exit(1); |
---|
| 128 | } |
---|
[be0a79f] | 129 | #endif |
---|
| 130 | |
---|
[7d4fbcd] | 131 | /* signal handler */ |
---|
[c9e72d1] | 132 | /*sigact.sa_handler=sig_handler;*/ |
---|
| 133 | sigact.sa_sigaction=sig_handler; |
---|
[7d4fbcd] | 134 | sigemptyset(&sigact.sa_mask); |
---|
[c9e72d1] | 135 | sigact.sa_flags=SA_SIGINFO; |
---|
[7d4fbcd] | 136 | sigaction(SIGWINCH, &sigact, NULL); |
---|
| 137 | sigaction(SIGALRM, &sigact, NULL); |
---|
[c9e72d1] | 138 | sigaction(SIGPIPE, &sigact, NULL); |
---|
[fe1f605] | 139 | sigaction(SIGTERM, &sigact, NULL); |
---|
| 140 | sigaction(SIGHUP, &sigact, NULL); |
---|
[7d4fbcd] | 141 | |
---|
[8232149] | 142 | /* save initial terminal settings */ |
---|
| 143 | tcgetattr(0, owl_global_get_startup_tio(&g)); |
---|
| 144 | |
---|
| 145 | /* turn ISTRIP off */ |
---|
| 146 | tcgetattr(0, &tio); |
---|
| 147 | tio.c_iflag &= ~ISTRIP; |
---|
| 148 | tcsetattr(0, TCSAFLUSH, &tio); |
---|
| 149 | |
---|
[7d4fbcd] | 150 | /* screen init */ |
---|
[b20de4f] | 151 | if (!getenv("TERMINFO")) { |
---|
[61f7250] | 152 | putenv(owl_sprintf("TERMINFO=%s", TERMINFO)); |
---|
| 153 | owl_function_debugmsg("startup: setting TERMINFO to %s", TERMINFO); |
---|
[b20de4f] | 154 | } else { |
---|
[61f7250] | 155 | owl_function_debugmsg("startup: leaving TERMINFO as %s from envrionment", getenv("TERMINFO")); |
---|
[b20de4f] | 156 | } |
---|
[61f7250] | 157 | |
---|
[7d4fbcd] | 158 | initscr(); |
---|
| 159 | start_color(); |
---|
[ac70242] | 160 | #ifdef HAVE_USE_DEFAULT_COLORS |
---|
[7d4fbcd] | 161 | use_default_colors(); |
---|
[ac70242] | 162 | #endif |
---|
[7d4fbcd] | 163 | raw(); |
---|
| 164 | noecho(); |
---|
| 165 | |
---|
| 166 | /* define simple color pairs */ |
---|
| 167 | if (has_colors() && COLOR_PAIRS>=8) { |
---|
| 168 | init_pair(OWL_COLOR_BLACK, COLOR_BLACK, -1); |
---|
| 169 | init_pair(OWL_COLOR_RED, COLOR_RED, -1); |
---|
| 170 | init_pair(OWL_COLOR_GREEN, COLOR_GREEN, -1); |
---|
| 171 | init_pair(OWL_COLOR_YELLOW, COLOR_YELLOW, -1); |
---|
| 172 | init_pair(OWL_COLOR_BLUE, COLOR_BLUE, -1); |
---|
| 173 | init_pair(OWL_COLOR_MAGENTA, COLOR_MAGENTA, -1); |
---|
| 174 | init_pair(OWL_COLOR_CYAN, COLOR_CYAN, -1); |
---|
| 175 | init_pair(OWL_COLOR_WHITE, COLOR_WHITE, -1); |
---|
| 176 | } |
---|
[38cf544c] | 177 | |
---|
[bd3f232] | 178 | /* owl global init */ |
---|
[7d4fbcd] | 179 | owl_global_init(&g); |
---|
| 180 | if (debug) owl_global_set_debug_on(&g); |
---|
[b20de4f] | 181 | owl_function_debugmsg("startup: first available debugging message"); |
---|
[7d4fbcd] | 182 | owl_global_set_startupargs(&g, argcsave, argvsave); |
---|
[dab82f29] | 183 | owl_global_set_haveaim(&g); |
---|
| 184 | |
---|
| 185 | /* prepare stdin dispatch */ |
---|
| 186 | { |
---|
| 187 | owl_dispatch *d = owl_malloc(sizeof(owl_dispatch)); |
---|
| 188 | owl_function_debugmsg("startup: creating input selector"); |
---|
| 189 | d->fd = STDIN; |
---|
| 190 | d->cfunc = &owl_process_input; |
---|
| 191 | d->destroy = NULL; |
---|
| 192 | owl_select_add_dispatch(d); |
---|
| 193 | } |
---|
| 194 | |
---|
[09489b89] | 195 | #ifdef HAVE_LIBZEPHYR |
---|
| 196 | owl_global_set_havezephyr(&g); |
---|
[dab82f29] | 197 | if (!ret) { |
---|
| 198 | owl_dispatch *d = owl_malloc(sizeof(owl_dispatch)); |
---|
| 199 | owl_function_debugmsg("startup: creating zephyr selector"); |
---|
| 200 | d->fd = ZGetFD(); |
---|
| 201 | d->cfunc = &owl_zephyr_process_events; |
---|
| 202 | d->destroy = NULL; |
---|
| 203 | owl_select_add_dispatch(d); |
---|
| 204 | owl_global_set_havezephyr(&g); |
---|
| 205 | } |
---|
[09489b89] | 206 | #endif |
---|
[2a2bb60] | 207 | |
---|
| 208 | #if OWL_STDERR_REDIR |
---|
[a0a5179] | 209 | /* Do this only after we've started curses up... */ |
---|
[dab82f29] | 210 | { |
---|
| 211 | owl_dispatch *d = owl_malloc(sizeof(owl_dispatch)); |
---|
| 212 | owl_function_debugmsg("startup: doing stderr redirection"); |
---|
| 213 | d->fd = stderr_replace(); |
---|
| 214 | d->cfunc = stderr_redirect_handler; |
---|
| 215 | d->destroy = NULL; |
---|
| 216 | owl_function_debugmsg("testing: 1"); |
---|
| 217 | owl_select_add_dispatch(d); |
---|
| 218 | owl_function_debugmsg("testing: 2"); |
---|
| 219 | } |
---|
| 220 | |
---|
[2a2bb60] | 221 | #endif |
---|
| 222 | |
---|
[38cf544c] | 223 | /* create the owl directory, in case it does not exist */ |
---|
[a0a5179] | 224 | owl_function_debugmsg("startup: creating owl directory, if not present"); |
---|
[38cf544c] | 225 | dir=owl_sprintf("%s/%s", owl_global_get_homedir(&g), OWL_CONFIG_DIR); |
---|
| 226 | mkdir(dir, S_IRWXU); |
---|
| 227 | owl_free(dir); |
---|
[5145235] | 228 | |
---|
[ced25d1] | 229 | /* set the tty, either from the command line, or by figuring it out */ |
---|
[a0a5179] | 230 | owl_function_debugmsg("startup: setting tty name"); |
---|
[61e79a9] | 231 | if (tty) { |
---|
| 232 | owl_global_set_tty(&g, tty); |
---|
| 233 | } else { |
---|
| 234 | owl_global_set_tty(&g, owl_util_get_default_tty()); |
---|
| 235 | } |
---|
[7d4fbcd] | 236 | |
---|
[bd3f232] | 237 | /* setup the built-in styles */ |
---|
[a0a5179] | 238 | owl_function_debugmsg("startup: creating built-in styles"); |
---|
[bd3f232] | 239 | s=owl_malloc(sizeof(owl_style)); |
---|
[176d3443] | 240 | owl_style_create_internal(s, "default", &owl_stylefunc_default, "Default message formatting"); |
---|
[bd3f232] | 241 | owl_global_add_style(&g, s); |
---|
| 242 | |
---|
| 243 | s=owl_malloc(sizeof(owl_style)); |
---|
[176d3443] | 244 | owl_style_create_internal(s, "basic", &owl_stylefunc_basic, "Basic message formatting."); |
---|
[bd3f232] | 245 | owl_global_add_style(&g, s); |
---|
[ec6ff52] | 246 | #if 0 |
---|
| 247 | s=owl_malloc(sizeof(owl_style)); |
---|
[176d3443] | 248 | owl_style_create_internal(s, "vt", &owl_stylefunc_vt, "VT message formatting."); |
---|
[ec6ff52] | 249 | owl_global_add_style(&g, s); |
---|
| 250 | #endif |
---|
[8b32593] | 251 | s=owl_malloc(sizeof(owl_style)); |
---|
[176d3443] | 252 | owl_style_create_internal(s, "oneline", &owl_stylefunc_oneline, "Formats for one-line-per-message"); |
---|
[8b32593] | 253 | owl_global_add_style(&g, s); |
---|
| 254 | |
---|
[7d4fbcd] | 255 | /* setup the default filters */ |
---|
[3abf28b] | 256 | /* the personal filter will need to change again when AIM chat's are |
---|
| 257 | * included. Also, there should be an %aimme% */ |
---|
[a0a5179] | 258 | owl_function_debugmsg("startup: creating default filters"); |
---|
[bd3f232] | 259 | f=owl_malloc(sizeof(owl_filter)); |
---|
[3abf28b] | 260 | owl_filter_init_fromstring(f, "personal", "( type ^zephyr$ " |
---|
| 261 | "and class ^message$ and instance ^personal$ " |
---|
| 262 | "and ( recipient ^%me%$ or sender ^%me%$ ) ) " |
---|
[3723f31] | 263 | "or ( type ^aim$ and login ^none$ )"); |
---|
[7d4fbcd] | 264 | owl_list_append_element(owl_global_get_filterlist(&g), f); |
---|
| 265 | |
---|
[bd3f232] | 266 | f=owl_malloc(sizeof(owl_filter)); |
---|
[0c502e9] | 267 | owl_filter_init_fromstring(f, "trash", "class ^mail$ or opcode ^ping$ or type ^admin$ or ( not login ^none$ )"); |
---|
[7d4fbcd] | 268 | owl_list_append_element(owl_global_get_filterlist(&g), f); |
---|
| 269 | |
---|
[bd3f232] | 270 | f=owl_malloc(sizeof(owl_filter)); |
---|
[0236842] | 271 | owl_filter_init_fromstring(f, "ping", "opcode ^ping$"); |
---|
| 272 | owl_list_append_element(owl_global_get_filterlist(&g), f); |
---|
| 273 | |
---|
[bd3f232] | 274 | f=owl_malloc(sizeof(owl_filter)); |
---|
[0236842] | 275 | owl_filter_init_fromstring(f, "auto", "opcode ^auto$"); |
---|
| 276 | owl_list_append_element(owl_global_get_filterlist(&g), f); |
---|
| 277 | |
---|
[bd3f232] | 278 | f=owl_malloc(sizeof(owl_filter)); |
---|
[0c502e9] | 279 | owl_filter_init_fromstring(f, "login", "not login ^none$"); |
---|
[0236842] | 280 | owl_list_append_element(owl_global_get_filterlist(&g), f); |
---|
| 281 | |
---|
[bd3f232] | 282 | f=owl_malloc(sizeof(owl_filter)); |
---|
[7d4fbcd] | 283 | owl_filter_init_fromstring(f, "reply-lockout", "class ^noc or class ^mail$"); |
---|
| 284 | owl_list_append_element(owl_global_get_filterlist(&g), f); |
---|
| 285 | |
---|
[bd3f232] | 286 | f=owl_malloc(sizeof(owl_filter)); |
---|
[5a6e6b9] | 287 | owl_filter_init_fromstring(f, "out", "direction ^out$"); |
---|
| 288 | owl_list_append_element(owl_global_get_filterlist(&g), f); |
---|
| 289 | |
---|
[bd3f232] | 290 | f=owl_malloc(sizeof(owl_filter)); |
---|
[31e48a3] | 291 | owl_filter_init_fromstring(f, "aim", "type ^aim$"); |
---|
| 292 | owl_list_append_element(owl_global_get_filterlist(&g), f); |
---|
| 293 | |
---|
[bd3f232] | 294 | f=owl_malloc(sizeof(owl_filter)); |
---|
[31e48a3] | 295 | owl_filter_init_fromstring(f, "zephyr", "type ^zephyr$"); |
---|
| 296 | owl_list_append_element(owl_global_get_filterlist(&g), f); |
---|
| 297 | |
---|
[bd3f232] | 298 | f=owl_malloc(sizeof(owl_filter)); |
---|
[89426ab] | 299 | owl_filter_init_fromstring(f, "none", "false"); |
---|
| 300 | owl_list_append_element(owl_global_get_filterlist(&g), f); |
---|
| 301 | |
---|
[bd3f232] | 302 | f=owl_malloc(sizeof(owl_filter)); |
---|
[89426ab] | 303 | owl_filter_init_fromstring(f, "all", "true"); |
---|
[7d4fbcd] | 304 | owl_list_append_element(owl_global_get_filterlist(&g), f); |
---|
| 305 | |
---|
| 306 | /* set the current view */ |
---|
[a0a5179] | 307 | owl_function_debugmsg("startup: setting the current view"); |
---|
[c3ab155] | 308 | owl_view_create(owl_global_get_current_view(&g), "main", f, owl_global_get_style_by_name(&g, "default")); |
---|
[7d4fbcd] | 309 | |
---|
[96f8e5b] | 310 | /* AIM init */ |
---|
[a0a5179] | 311 | owl_function_debugmsg("startup: doing AIM initialization"); |
---|
[96f8e5b] | 312 | owl_aim_init(); |
---|
| 313 | |
---|
[38cf544c] | 314 | /* process the startup file */ |
---|
[a0a5179] | 315 | owl_function_debugmsg("startup: processing startup file"); |
---|
[2404c3a] | 316 | owl_function_source(NULL); |
---|
[38cf544c] | 317 | |
---|
[7d4fbcd] | 318 | /* read the config file */ |
---|
[a0a5179] | 319 | owl_function_debugmsg("startup: processing config file"); |
---|
[38cf544c] | 320 | owl_context_set_readconfig(owl_global_get_context(&g)); |
---|
[f1e629d] | 321 | perlerr=owl_perlconfig_readconfig(configfile); |
---|
| 322 | if (perlerr) { |
---|
[7d4fbcd] | 323 | endwin(); |
---|
[4e0f545] | 324 | owl_function_error("Error parsing configfile: %s\n", perlerr); |
---|
| 325 | fprintf(stderr, "\nError parsing configfile: %s\n", perlerr); |
---|
[c3acb0b] | 326 | fflush(stderr); |
---|
[4e0f545] | 327 | printf("\nError parsing configfile: %s\n", perlerr); |
---|
[c3acb0b] | 328 | fflush(stdout); |
---|
[7d4fbcd] | 329 | exit(1); |
---|
| 330 | } |
---|
[ced25d1] | 331 | |
---|
[e075479] | 332 | /* if the config defines a formatting function, add 'perl' as a style */ |
---|
[bd3f232] | 333 | if (owl_global_is_config_format(&g)) { |
---|
| 334 | owl_function_debugmsg("Found perl formatting"); |
---|
| 335 | s=owl_malloc(sizeof(owl_style)); |
---|
[f1e629d] | 336 | owl_style_create_perl(s, "perl", "owl::_format_msg_legacy_wrap", |
---|
| 337 | "User-defined perl style that calls owl::format_msg" |
---|
| 338 | "with legacy global variable support"); |
---|
[bd3f232] | 339 | owl_global_add_style(&g, s); |
---|
[e075479] | 340 | owl_global_set_default_style(&g, "perl"); |
---|
[bd3f232] | 341 | } |
---|
| 342 | |
---|
[ced25d1] | 343 | /* execute the startup function in the configfile */ |
---|
[a0a5179] | 344 | owl_function_debugmsg("startup: executing perl startup, if applicable"); |
---|
[f1e629d] | 345 | perlout = owl_perlconfig_execute("owl::startup();"); |
---|
[7d4fbcd] | 346 | if (perlout) owl_free(perlout); |
---|
| 347 | |
---|
| 348 | /* hold on to the window names for convenience */ |
---|
| 349 | msgwin=owl_global_get_curs_msgwin(&g); |
---|
| 350 | recwin=owl_global_get_curs_recwin(&g); |
---|
| 351 | sepwin=owl_global_get_curs_sepwin(&g); |
---|
| 352 | typwin=owl_global_get_curs_typwin(&g); |
---|
| 353 | tw=owl_global_get_typwin(&g); |
---|
| 354 | |
---|
[252a5c2] | 355 | /* welcome message */ |
---|
[a0a5179] | 356 | owl_function_debugmsg("startup: creating splash message"); |
---|
[19c0c55] | 357 | owl_function_adminmsg("", |
---|
| 358 | "-----------------------------------------------------------------------\n" |
---|
| 359 | "Welcome to Owl version " OWL_VERSION_STRING ". Press 'h' for on-line help. \n" |
---|
| 360 | " \n" |
---|
| 361 | "If you would like to receive release announcements about Owl you can \n" |
---|
| 362 | "join the owl-users mailing list at http://www.ktools.org/ \n" |
---|
| 363 | " ^ ^ \n" |
---|
| 364 | " OvO \n" |
---|
| 365 | "Please report any bugs or suggestions to bug-owl@ktools.org ( ) \n" |
---|
| 366 | "-----------------------------------------------------------------m-m---\n" |
---|
| 367 | ); |
---|
[252a5c2] | 368 | sepbar(NULL); |
---|
| 369 | |
---|
[7d4fbcd] | 370 | wrefresh(sepwin); |
---|
| 371 | |
---|
| 372 | /* load zephyr subs */ |
---|
[bd3f232] | 373 | if (initialsubs) { |
---|
[4357be8] | 374 | int ret2; |
---|
[a0a5179] | 375 | owl_function_debugmsg("startup: loading initial zephyr subs"); |
---|
[4357be8] | 376 | |
---|
| 377 | /* load default subscriptions */ |
---|
| 378 | ret=owl_zephyr_loaddefaultsubs(); |
---|
[252a5c2] | 379 | |
---|
[4357be8] | 380 | /* load subscriptions from subs file */ |
---|
[95474d7] | 381 | ret2=owl_zephyr_loadsubs(NULL, 0); |
---|
[4357be8] | 382 | |
---|
| 383 | if (ret || ret2) { |
---|
| 384 | owl_function_adminmsg("", "Error loading zephyr subscriptions"); |
---|
| 385 | } else if (ret2!=-1) { |
---|
[bd3f232] | 386 | owl_global_add_userclue(&g, OWL_USERCLUE_CLASSES); |
---|
| 387 | } |
---|
[7d4fbcd] | 388 | |
---|
[bd3f232] | 389 | /* load login subscriptions */ |
---|
| 390 | if (owl_global_is_loginsubs(&g)) { |
---|
[a0a5179] | 391 | owl_function_debugmsg("startup: loading login subs"); |
---|
[bd3f232] | 392 | owl_function_loadloginsubs(NULL); |
---|
| 393 | } |
---|
[7d4fbcd] | 394 | } |
---|
| 395 | |
---|
[5a95b69] | 396 | /* First buddy check to sync the list without notifications */ |
---|
| 397 | owl_function_debugmsg("startup: doing initial zephyr buddy check"); |
---|
[4357be8] | 398 | /* owl_function_zephyr_buddy_check(0); */ |
---|
[5a95b69] | 399 | |
---|
[f933403] | 400 | /* set the startup and default style, based on userclue and presence of a |
---|
| 401 | * formatting function */ |
---|
[a0a5179] | 402 | owl_function_debugmsg("startup: setting startup and default style"); |
---|
[27c3a93] | 403 | if (0 != strcmp(owl_global_get_default_style(&g), "__unspecified__")) { |
---|
| 404 | /* the style was set by the user: leave it alone */ |
---|
| 405 | } else if (owl_global_is_config_format(&g)) { |
---|
[f933403] | 406 | owl_global_set_default_style(&g, "perl"); |
---|
| 407 | } else if (owl_global_is_userclue(&g, OWL_USERCLUE_CLASSES)) { |
---|
| 408 | owl_global_set_default_style(&g, "default"); |
---|
| 409 | } else { |
---|
| 410 | owl_global_set_default_style(&g, "basic"); |
---|
| 411 | } |
---|
| 412 | |
---|
[7d4fbcd] | 413 | /* zlog in if we need to */ |
---|
| 414 | if (owl_global_is_startuplogin(&g)) { |
---|
[a0a5179] | 415 | owl_function_debugmsg("startup: doing zlog in"); |
---|
[31e48a3] | 416 | owl_zephyr_zlog_in(); |
---|
[7d4fbcd] | 417 | } |
---|
| 418 | |
---|
[a0a5179] | 419 | owl_function_debugmsg("startup: set style for the view"); |
---|
[f1e629d] | 420 | owl_view_set_style(owl_global_get_current_view(&g), |
---|
| 421 | owl_global_get_style_by_name(&g, owl_global_get_default_style(&g))); |
---|
[a0a5179] | 422 | |
---|
| 423 | owl_function_debugmsg("startup: setting context interactive"); |
---|
[7d4fbcd] | 424 | owl_context_set_interactive(owl_global_get_context(&g)); |
---|
| 425 | |
---|
[a15a84f] | 426 | nexttimediff=10; |
---|
[4b464a4] | 427 | nexttime=time(NULL); |
---|
| 428 | |
---|
[9cd7e6a] | 429 | owl_zephyr_process_events(NULL); |
---|
[a0a5179] | 430 | owl_function_debugmsg("startup: entering main loop"); |
---|
[7d4fbcd] | 431 | /* main loop */ |
---|
| 432 | while (1) { |
---|
| 433 | |
---|
| 434 | /* if a resize has been scheduled, deal with it */ |
---|
| 435 | owl_global_resize(&g, 0, 0); |
---|
| 436 | |
---|
| 437 | /* these are here in case a resize changes the windows */ |
---|
| 438 | msgwin=owl_global_get_curs_msgwin(&g); |
---|
| 439 | recwin=owl_global_get_curs_recwin(&g); |
---|
| 440 | sepwin=owl_global_get_curs_sepwin(&g); |
---|
| 441 | typwin=owl_global_get_curs_typwin(&g); |
---|
| 442 | |
---|
| 443 | followlast=owl_global_should_followlast(&g); |
---|
[5789230] | 444 | |
---|
[a352335c] | 445 | /* Do AIM stuff */ |
---|
| 446 | if (owl_global_is_doaimevents(&g)) { |
---|
[9cd7e6a] | 447 | /* owl_aim_process_events(); */ |
---|
[de03334] | 448 | |
---|
[a352335c] | 449 | if (owl_global_is_aimloggedin(&g)) { |
---|
| 450 | if (owl_timer_is_expired(owl_global_get_aim_buddyinfo_timer(&g))) { |
---|
[f4d0975] | 451 | /* owl_buddylist_request_idletimes(owl_global_get_buddylist(&g)); */ |
---|
[a352335c] | 452 | owl_timer_reset(owl_global_get_aim_buddyinfo_timer(&g)); |
---|
| 453 | } |
---|
[de03334] | 454 | } |
---|
[5789230] | 455 | } |
---|
| 456 | |
---|
[4b464a4] | 457 | /* little hack */ |
---|
[a15a84f] | 458 | now=time(NULL); |
---|
| 459 | today=localtime(&now); |
---|
| 460 | if (today->tm_mon==9 && today->tm_mday==31 && owl_global_get_runtime(&g)<600) { |
---|
[4b464a4] | 461 | if (time(NULL)>nexttime) { |
---|
| 462 | if (nexttimediff==1) { |
---|
[a15a84f] | 463 | nexttimediff=10; |
---|
[4b464a4] | 464 | } else { |
---|
| 465 | nexttimediff=1; |
---|
| 466 | } |
---|
| 467 | nexttime+=nexttimediff; |
---|
| 468 | owl_hack_animate(); |
---|
| 469 | } |
---|
| 470 | } |
---|
| 471 | |
---|
[aac889a] | 472 | /* Grab incoming messages. */ |
---|
[d09e5a1] | 473 | newmsgs=0; |
---|
[dab82f29] | 474 | while(owl_global_messagequeue_pending(&g)) { |
---|
[e6449bc] | 475 | owl_message *m=NULL; |
---|
[ecd5dc5] | 476 | owl_filter *f; |
---|
[09489b89] | 477 | |
---|
[dab82f29] | 478 | m=owl_global_messageuque_popmsg(&g); |
---|
[7d4fbcd] | 479 | |
---|
[bd3f232] | 480 | /* if this message it on the puntlist, nuke it and continue */ |
---|
[7d4fbcd] | 481 | if (owl_global_message_is_puntable(&g, m)) { |
---|
| 482 | owl_message_free(m); |
---|
| 483 | continue; |
---|
| 484 | } |
---|
| 485 | |
---|
[280ddc6] | 486 | /* login or logout that should be ignored? */ |
---|
| 487 | if (owl_global_is_ignorelogins(&g) && owl_message_is_loginout(m)) { |
---|
| 488 | owl_message_free(m); |
---|
| 489 | continue; |
---|
| 490 | } |
---|
| 491 | |
---|
[7d4fbcd] | 492 | /* otherwise add it to the global list */ |
---|
| 493 | owl_messagelist_append_element(owl_global_get_msglist(&g), m); |
---|
[d09e5a1] | 494 | newmsgs=1; |
---|
[7d4fbcd] | 495 | |
---|
| 496 | /* let the config know the new message has been received */ |
---|
[f1e629d] | 497 | owl_perlconfig_getmsg(m, 0, NULL); |
---|
[7d4fbcd] | 498 | |
---|
| 499 | /* add it to any necessary views; right now there's only the current view */ |
---|
| 500 | owl_view_consider_message(owl_global_get_current_view(&g), m); |
---|
| 501 | |
---|
| 502 | /* do we need to autoreply? */ |
---|
[9854278] | 503 | if (owl_global_is_zaway(&g) && !owl_message_get_attribute_value(m, "isauto")) { |
---|
[1077753b] | 504 | if (owl_message_is_type_zephyr(m)) { |
---|
| 505 | owl_zephyr_zaway(m); |
---|
| 506 | } else if (owl_message_is_type_aim(m)) { |
---|
[e9f239b] | 507 | if (owl_message_is_private(m)) { |
---|
| 508 | owl_function_send_aimawymsg(owl_message_get_sender(m), owl_global_get_zaway_msg(&g)); |
---|
| 509 | } |
---|
[1077753b] | 510 | } |
---|
[7d4fbcd] | 511 | } |
---|
| 512 | |
---|
| 513 | /* ring the bell if it's a personal */ |
---|
[5d365f6] | 514 | if (!strcmp(owl_global_get_personalbell(&g), "on")) { |
---|
| 515 | if (!owl_message_is_loginout(m) && |
---|
| 516 | !owl_message_is_mail(m) && |
---|
| 517 | owl_message_is_private(m)) { |
---|
| 518 | owl_function_beep(); |
---|
| 519 | } |
---|
| 520 | } else if (!strcmp(owl_global_get_personalbell(&g), "off")) { |
---|
| 521 | /* do nothing */ |
---|
| 522 | } else { |
---|
| 523 | f=owl_global_get_filter(&g, owl_global_get_personalbell(&g)); |
---|
| 524 | if (f && owl_filter_message_match(f, m)) { |
---|
| 525 | owl_function_beep(); |
---|
| 526 | } |
---|
[7d4fbcd] | 527 | } |
---|
| 528 | |
---|
[5d365f6] | 529 | |
---|
[ecd5dc5] | 530 | /* if it matches the alert filter, do the alert action */ |
---|
| 531 | f=owl_global_get_filter(&g, owl_global_get_alert_filter(&g)); |
---|
| 532 | if (f && owl_filter_message_match(f, m)) { |
---|
| 533 | owl_function_command(owl_global_get_alert_action(&g)); |
---|
| 534 | } |
---|
| 535 | |
---|
[5a95b69] | 536 | /* if it's a zephyr login or logout, update the zbuddylist */ |
---|
| 537 | if (owl_message_is_type_zephyr(m) && owl_message_is_loginout(m)) { |
---|
| 538 | if (owl_message_is_login(m)) { |
---|
| 539 | owl_zbuddylist_adduser(owl_global_get_zephyr_buddylist(&g), owl_message_get_sender(m)); |
---|
| 540 | } else if (owl_message_is_logout(m)) { |
---|
| 541 | owl_zbuddylist_deluser(owl_global_get_zephyr_buddylist(&g), owl_message_get_sender(m)); |
---|
| 542 | } else { |
---|
| 543 | owl_function_error("Internal error: received login notice that is neither login nor logout"); |
---|
| 544 | } |
---|
| 545 | } |
---|
| 546 | |
---|
[7d4fbcd] | 547 | /* check for burning ears message */ |
---|
| 548 | /* this is an unsupported feature */ |
---|
| 549 | if (owl_global_is_burningears(&g) && owl_message_is_burningears(m)) { |
---|
[1c6c4d3] | 550 | char *buff; |
---|
| 551 | buff = owl_sprintf("@i(Burning ears message on class %s)", owl_message_get_class(m)); |
---|
[7d4fbcd] | 552 | owl_function_adminmsg(buff, ""); |
---|
[1c6c4d3] | 553 | owl_free(buff); |
---|
[7d4fbcd] | 554 | owl_function_beep(); |
---|
| 555 | } |
---|
| 556 | |
---|
[d09e5a1] | 557 | /* log the message if we need to */ |
---|
[15b34fd] | 558 | owl_log_message(m); |
---|
[7d4fbcd] | 559 | } |
---|
| 560 | |
---|
[5a95b69] | 561 | /* is it time to check zbuddies? */ |
---|
| 562 | if (owl_global_is_pseudologins(&g)) { |
---|
| 563 | if (owl_timer_is_expired(owl_global_get_zephyr_buddycheck_timer(&g))) { |
---|
| 564 | owl_function_debugmsg("Doing zephyr buddy check"); |
---|
| 565 | owl_function_zephyr_buddy_check(1); |
---|
| 566 | owl_timer_reset(owl_global_get_zephyr_buddycheck_timer(&g)); |
---|
| 567 | } |
---|
| 568 | } |
---|
| 569 | |
---|
[7d4fbcd] | 570 | /* follow the last message if we're supposed to */ |
---|
[d09e5a1] | 571 | if (newmsgs && followlast) { |
---|
[7d4fbcd] | 572 | owl_function_lastmsg_noredisplay(); |
---|
| 573 | } |
---|
[700c712] | 574 | |
---|
[ced25d1] | 575 | /* do the newmsgproc thing */ |
---|
[d09e5a1] | 576 | if (newmsgs) { |
---|
[8f44c6b] | 577 | owl_function_do_newmsgproc(); |
---|
[700c712] | 578 | } |
---|
[7d4fbcd] | 579 | |
---|
| 580 | /* redisplay if necessary */ |
---|
[d09e5a1] | 581 | /* this should be optimized to not run if the new messages won't be displayed */ |
---|
| 582 | if (newmsgs) { |
---|
[7d4fbcd] | 583 | owl_mainwin_redisplay(owl_global_get_mainwin(&g)); |
---|
| 584 | sepbar(NULL); |
---|
| 585 | if (owl_popwin_is_active(owl_global_get_popwin(&g))) { |
---|
| 586 | owl_popwin_refresh(owl_global_get_popwin(&g)); |
---|
| 587 | /* TODO: this is a broken kludge */ |
---|
| 588 | if (owl_global_get_viewwin(&g)) { |
---|
| 589 | owl_viewwin_redisplay(owl_global_get_viewwin(&g), 0); |
---|
| 590 | } |
---|
| 591 | } |
---|
| 592 | owl_global_set_needrefresh(&g); |
---|
| 593 | } |
---|
| 594 | |
---|
| 595 | /* if a popwin just came up, refresh it */ |
---|
| 596 | pw=owl_global_get_popwin(&g); |
---|
| 597 | if (owl_popwin_is_active(pw) && owl_popwin_needs_first_refresh(pw)) { |
---|
| 598 | owl_popwin_refresh(pw); |
---|
| 599 | owl_popwin_no_needs_first_refresh(pw); |
---|
| 600 | owl_global_set_needrefresh(&g); |
---|
| 601 | /* TODO: this is a broken kludge */ |
---|
| 602 | if (owl_global_get_viewwin(&g)) { |
---|
| 603 | owl_viewwin_redisplay(owl_global_get_viewwin(&g), 0); |
---|
| 604 | } |
---|
| 605 | } |
---|
| 606 | |
---|
| 607 | /* update the terminal if we need to */ |
---|
| 608 | if (owl_global_is_needrefresh(&g)) { |
---|
| 609 | /* leave the cursor in the appropriate window */ |
---|
| 610 | if (owl_global_is_typwin_active(&g)) { |
---|
| 611 | owl_function_set_cursor(typwin); |
---|
| 612 | } else { |
---|
| 613 | owl_function_set_cursor(sepwin); |
---|
| 614 | } |
---|
| 615 | doupdate(); |
---|
| 616 | owl_global_set_noneedrefresh(&g); |
---|
| 617 | } |
---|
| 618 | |
---|
[395b32e] | 619 | /* Some calls into libzephyr call Z_WaitForNotice(), which has its |
---|
| 620 | * own select loop and may leave zephyrs on the queue. Check for |
---|
| 621 | * them now, and process any we find. */ |
---|
| 622 | owl_zephyr_process_events(NULL); |
---|
| 623 | |
---|
[dab82f29] | 624 | /* select on FDs we know about. */ |
---|
| 625 | owl_select(); |
---|
[2a2bb60] | 626 | |
---|
[c9e72d1] | 627 | /* Log any error signals */ |
---|
| 628 | { |
---|
| 629 | siginfo_t si; |
---|
| 630 | int signum; |
---|
| 631 | if ((signum = owl_global_get_errsignal_and_clear(&g, &si)) > 0) { |
---|
[81634b6] | 632 | owl_function_error("Got unexpected signal: %d %s (code: %d band: %d errno: %d)", |
---|
[afbf668] | 633 | signum, signum==SIGPIPE?"SIGPIPE":"SIG????", |
---|
[81634b6] | 634 | si.si_code, si.si_band, si.si_errno); |
---|
[c9e72d1] | 635 | } |
---|
| 636 | } |
---|
| 637 | |
---|
[7d4fbcd] | 638 | } |
---|
| 639 | } |
---|
| 640 | |
---|
[dab82f29] | 641 | void owl_process_input(owl_dispatch *d) |
---|
| 642 | { |
---|
| 643 | int ret, j; |
---|
| 644 | owl_popwin *pw; |
---|
| 645 | owl_editwin *tw; |
---|
| 646 | WINDOW *typwin; |
---|
| 647 | |
---|
| 648 | typwin = owl_global_get_curs_typwin(&g); |
---|
| 649 | while (1) { |
---|
| 650 | j=wgetch(typwin); |
---|
| 651 | if (j==ERR) return; |
---|
| 652 | |
---|
| 653 | pw=owl_global_get_popwin(&g); |
---|
| 654 | tw=owl_global_get_typwin(&g); |
---|
| 655 | |
---|
| 656 | /* find and activate the current keymap. |
---|
| 657 | * TODO: this should really get fixed by activating |
---|
| 658 | * keymaps as we switch between windows... |
---|
| 659 | */ |
---|
| 660 | if (pw && owl_popwin_is_active(pw) && owl_global_get_viewwin(&g)) { |
---|
| 661 | owl_context_set_popless(owl_global_get_context(&g), |
---|
| 662 | owl_global_get_viewwin(&g)); |
---|
| 663 | owl_function_activate_keymap("popless"); |
---|
| 664 | } else if (owl_global_is_typwin_active(&g) |
---|
| 665 | && owl_editwin_get_style(tw)==OWL_EDITWIN_STYLE_ONELINE) { |
---|
| 666 | /* |
---|
| 667 | owl_context_set_editline(owl_global_get_context(&g), tw); |
---|
| 668 | owl_function_activate_keymap("editline"); |
---|
| 669 | */ |
---|
| 670 | } else if (owl_global_is_typwin_active(&g) |
---|
| 671 | && owl_editwin_get_style(tw)==OWL_EDITWIN_STYLE_MULTILINE) { |
---|
| 672 | owl_context_set_editmulti(owl_global_get_context(&g), tw); |
---|
| 673 | owl_function_activate_keymap("editmulti"); |
---|
| 674 | } else { |
---|
| 675 | owl_context_set_recv(owl_global_get_context(&g)); |
---|
| 676 | owl_function_activate_keymap("recv"); |
---|
| 677 | } |
---|
| 678 | /* now actually handle the keypress */ |
---|
| 679 | ret = owl_keyhandler_process(owl_global_get_keyhandler(&g), j); |
---|
| 680 | if (ret!=0 && ret!=1) { |
---|
| 681 | owl_function_makemsg("Unable to handle keypress"); |
---|
| 682 | } |
---|
| 683 | } |
---|
| 684 | } |
---|
| 685 | |
---|
[de22c3d] | 686 | void sig_handler(int sig, siginfo_t *si, void *data) |
---|
| 687 | { |
---|
[7d4fbcd] | 688 | if (sig==SIGWINCH) { |
---|
[bd3f232] | 689 | /* we can't inturrupt a malloc here, so it just sets a flag |
---|
| 690 | * schedulding a resize for later |
---|
| 691 | */ |
---|
[7d4fbcd] | 692 | owl_function_resize(); |
---|
[afbf668] | 693 | } else if (sig==SIGPIPE || sig==SIGCHLD) { |
---|
[c9e72d1] | 694 | /* Set a flag and some info that we got the sigpipe |
---|
| 695 | * so we can record that we got it and why... */ |
---|
| 696 | owl_global_set_errsignal(&g, sig, si); |
---|
[fe1f605] | 697 | } else if (sig==SIGTERM || sig==SIGHUP) { |
---|
| 698 | owl_function_quit(); |
---|
[7d4fbcd] | 699 | } |
---|
[c9e72d1] | 700 | |
---|
[7d4fbcd] | 701 | } |
---|
| 702 | |
---|
[de22c3d] | 703 | void usage() |
---|
| 704 | { |
---|
[e7cc1c3] | 705 | fprintf(stderr, "Owl version %s\n", OWL_VERSION_STRING); |
---|
[a68f05d] | 706 | fprintf(stderr, "Usage: owl [-n] [-d] [-D] [-v] [-h] [-c <configfile>] [-t <ttyname>]\n"); |
---|
[e7cc1c3] | 707 | fprintf(stderr, " -n don't load zephyr subscriptions\n"); |
---|
| 708 | fprintf(stderr, " -d enable debugging\n"); |
---|
[a68f05d] | 709 | fprintf(stderr, " -D enable debugging and delete previous debug file\n"); |
---|
[e7cc1c3] | 710 | fprintf(stderr, " -v print the Owl version number and exit\n"); |
---|
| 711 | fprintf(stderr, " -h print this help message\n"); |
---|
| 712 | fprintf(stderr, " -c specify an alternate config file\n"); |
---|
| 713 | fprintf(stderr, " -t set the tty name\n"); |
---|
[7d4fbcd] | 714 | } |
---|
[2a2bb60] | 715 | |
---|
| 716 | #if OWL_STDERR_REDIR |
---|
| 717 | |
---|
| 718 | /* Replaces stderr with a pipe so that we can read from it. |
---|
| 719 | * Returns the fd of the pipe from which stderr can be read. */ |
---|
[de22c3d] | 720 | int stderr_replace(void) |
---|
| 721 | { |
---|
[2a2bb60] | 722 | int pipefds[2]; |
---|
| 723 | if (0 != pipe(pipefds)) { |
---|
| 724 | perror("pipe"); |
---|
| 725 | owl_function_debugmsg("stderr_replace: pipe FAILED\n"); |
---|
| 726 | return -1; |
---|
| 727 | } |
---|
[dab82f29] | 728 | owl_function_debugmsg("stderr_replace: pipe: %d,%d", pipefds[0], pipefds[1]); |
---|
[2a2bb60] | 729 | if (-1 == dup2(pipefds[1], 2 /*stderr*/)) { |
---|
[dab82f29] | 730 | owl_function_debugmsg("stderr_replace: dup2 FAILED (%s)", strerror(errno)); |
---|
[2a2bb60] | 731 | perror("dup2"); |
---|
| 732 | return -1; |
---|
| 733 | } |
---|
| 734 | return pipefds[0]; |
---|
| 735 | } |
---|
| 736 | |
---|
[afbf668] | 737 | /* Sends stderr (read from rfd) messages to the error console */ |
---|
| 738 | void stderr_redirect_handler(int handle, int rfd, int eventmask, void *data) |
---|
[de22c3d] | 739 | { |
---|
[2a2bb60] | 740 | int navail, bread; |
---|
| 741 | char *buf; |
---|
| 742 | /*owl_function_debugmsg("stderr_redirect: called with rfd=%d\n", rfd);*/ |
---|
| 743 | if (rfd<0) return; |
---|
| 744 | if (-1 == ioctl(rfd, FIONREAD, (void*)&navail)) { |
---|
| 745 | return; |
---|
| 746 | } |
---|
| 747 | /*owl_function_debugmsg("stderr_redirect: navail = %d\n", navail);*/ |
---|
| 748 | if (navail<=0) return; |
---|
| 749 | if (navail>256) { navail = 256; } |
---|
| 750 | buf = owl_malloc(navail+1); |
---|
| 751 | bread = read(rfd, buf, navail); |
---|
| 752 | if (buf[navail-1] != '\0') { |
---|
| 753 | buf[navail] = '\0'; |
---|
| 754 | } |
---|
| 755 | owl_function_error("Err: %s", buf); |
---|
| 756 | owl_free(buf); |
---|
| 757 | } |
---|
| 758 | |
---|
| 759 | #endif /* OWL_STDERR_REDIR */ |
---|