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