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