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