[81001c0] | 1 | /* Copyright (c) 2006-2008 The BarnOwl Developers. All rights reserved. |
---|
| 2 | * Copyright (c) 2004 James Kretchmar. All rights reserved. |
---|
[7d4fbcd] | 3 | * |
---|
[81001c0] | 4 | * This program is free software. You can redistribute it and/or |
---|
| 5 | * modify under the terms of the Sleepycat License. See the COPYING |
---|
| 6 | * file included with the distribution for more information. |
---|
[7d4fbcd] | 7 | */ |
---|
| 8 | |
---|
| 9 | #include <stdio.h> |
---|
| 10 | #include <unistd.h> |
---|
| 11 | #include <stdlib.h> |
---|
| 12 | #include <string.h> |
---|
| 13 | #include <signal.h> |
---|
[5145235] | 14 | #include <time.h> |
---|
[7d4fbcd] | 15 | #include <sys/param.h> |
---|
[700c712] | 16 | #include <sys/types.h> |
---|
[9c7a701] | 17 | #include <sys/time.h> |
---|
[8232149] | 18 | #include <termios.h> |
---|
[38cf544c] | 19 | #include <sys/stat.h> |
---|
[34509d5] | 20 | #include <locale.h> |
---|
[7d4fbcd] | 21 | #include "owl.h" |
---|
| 22 | |
---|
[2a2bb60] | 23 | #if OWL_STDERR_REDIR |
---|
[e8b95f8] | 24 | #ifdef HAVE_SYS_IOCTL_H |
---|
[2a2bb60] | 25 | #include <sys/ioctl.h> |
---|
[e8b95f8] | 26 | #endif |
---|
| 27 | #ifdef HAVE_SYS_FILIO_H |
---|
| 28 | #include <sys/filio.h> |
---|
| 29 | #endif |
---|
[2a2bb60] | 30 | int stderr_replace(void); |
---|
| 31 | #endif |
---|
| 32 | |
---|
[9c7a701] | 33 | #define STDIN 0 |
---|
| 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; |
---|
[47519e1b] | 44 | int ret, initialsubs, debug, argcsave, followlast; |
---|
[13a3c1db] | 45 | int newmsgs, nexttimediff; |
---|
[7d4fbcd] | 46 | struct sigaction sigact; |
---|
[2fa15f0] | 47 | char *configfile, *tty, *perlout, *perlerr, **argvsave, buff[LINE], startupmsg[LINE]; |
---|
[b363d83] | 48 | char *confdir; |
---|
[7d4fbcd] | 49 | owl_filter *f; |
---|
[bd3f232] | 50 | owl_style *s; |
---|
[a15a84f] | 51 | time_t nexttime, now; |
---|
| 52 | struct tm *today; |
---|
[38cf544c] | 53 | char *dir; |
---|
[8232149] | 54 | struct termios tio; |
---|
[13a3c1db] | 55 | owl_message *m; |
---|
[cf0cc64] | 56 | |
---|
[c60ade2] | 57 | if (!GLIB_CHECK_VERSION (2, 12, 0)) |
---|
| 58 | g_error ("GLib version 2.12.0 or above is needed."); |
---|
[7d4fbcd] | 59 | |
---|
| 60 | argcsave=argc; |
---|
| 61 | argvsave=argv; |
---|
| 62 | configfile=NULL; |
---|
[b363d83] | 63 | confdir = NULL; |
---|
[7d4fbcd] | 64 | tty=NULL; |
---|
| 65 | debug=0; |
---|
[bd3f232] | 66 | initialsubs=1; |
---|
[34509d5] | 67 | |
---|
| 68 | setlocale(LC_ALL, ""); |
---|
| 69 | |
---|
[7d4fbcd] | 70 | if (argc>0) { |
---|
| 71 | argv++; |
---|
| 72 | argc--; |
---|
| 73 | } |
---|
| 74 | while (argc>0) { |
---|
| 75 | if (!strcmp(argv[0], "-n")) { |
---|
| 76 | initialsubs=0; |
---|
| 77 | argv++; |
---|
| 78 | argc--; |
---|
| 79 | } else if (!strcmp(argv[0], "-c")) { |
---|
| 80 | if (argc<2) { |
---|
[b363d83] | 81 | fprintf(stderr, "Too few arguments to -c\n"); |
---|
| 82 | usage(); |
---|
| 83 | exit(1); |
---|
[7d4fbcd] | 84 | } |
---|
| 85 | configfile=argv[1]; |
---|
| 86 | argv+=2; |
---|
| 87 | argc-=2; |
---|
| 88 | } else if (!strcmp(argv[0], "-t")) { |
---|
| 89 | if (argc<2) { |
---|
[b363d83] | 90 | fprintf(stderr, "Too few arguments to -t\n"); |
---|
| 91 | usage(); |
---|
| 92 | exit(1); |
---|
[7d4fbcd] | 93 | } |
---|
| 94 | tty=argv[1]; |
---|
| 95 | argv+=2; |
---|
| 96 | argc-=2; |
---|
[b363d83] | 97 | } else if (!strcmp(argv[0], "-s")){ |
---|
| 98 | if (argc<2) { |
---|
| 99 | fprintf(stderr, "Too few arguments to -s\n"); |
---|
| 100 | usage(); |
---|
| 101 | exit(1); |
---|
| 102 | } |
---|
| 103 | confdir = argv[1]; |
---|
| 104 | argv+=2; |
---|
| 105 | argc-=2; |
---|
[7d4fbcd] | 106 | } else if (!strcmp(argv[0], "-d")) { |
---|
| 107 | debug=1; |
---|
| 108 | argv++; |
---|
| 109 | argc--; |
---|
[a68f05d] | 110 | } else if (!strcmp(argv[0], "-D")) { |
---|
| 111 | debug=1; |
---|
| 112 | unlink(OWL_DEBUG_FILE); |
---|
| 113 | argv++; |
---|
| 114 | argc--; |
---|
[7d4fbcd] | 115 | } else if (!strcmp(argv[0], "-v")) { |
---|
[78667f3] | 116 | printf("This is barnowl version %s\n", OWL_VERSION_STRING); |
---|
[7d4fbcd] | 117 | exit(0); |
---|
| 118 | } else { |
---|
[7ba9ead9] | 119 | fprintf(stderr, "Unknown argument\n"); |
---|
[7d4fbcd] | 120 | usage(); |
---|
| 121 | exit(1); |
---|
| 122 | } |
---|
| 123 | } |
---|
| 124 | |
---|
[176d3443] | 125 | owl_function_debugmsg("startup: Finished parsing arguments"); |
---|
| 126 | |
---|
[7d4fbcd] | 127 | /* signal handler */ |
---|
[c9e72d1] | 128 | /*sigact.sa_handler=sig_handler;*/ |
---|
| 129 | sigact.sa_sigaction=sig_handler; |
---|
[7d4fbcd] | 130 | sigemptyset(&sigact.sa_mask); |
---|
[c9e72d1] | 131 | sigact.sa_flags=SA_SIGINFO; |
---|
[7d4fbcd] | 132 | sigaction(SIGWINCH, &sigact, NULL); |
---|
| 133 | sigaction(SIGALRM, &sigact, NULL); |
---|
[c9e72d1] | 134 | sigaction(SIGPIPE, &sigact, NULL); |
---|
[fe1f605] | 135 | sigaction(SIGTERM, &sigact, NULL); |
---|
| 136 | sigaction(SIGHUP, &sigact, NULL); |
---|
[7d4fbcd] | 137 | |
---|
[8232149] | 138 | /* save initial terminal settings */ |
---|
| 139 | tcgetattr(0, owl_global_get_startup_tio(&g)); |
---|
| 140 | |
---|
| 141 | /* turn ISTRIP off */ |
---|
| 142 | tcgetattr(0, &tio); |
---|
| 143 | tio.c_iflag &= ~ISTRIP; |
---|
| 144 | tcsetattr(0, TCSAFLUSH, &tio); |
---|
| 145 | |
---|
[7d4fbcd] | 146 | /* screen init */ |
---|
[b20de4f] | 147 | if (!getenv("TERMINFO")) { |
---|
[18e28a4] | 148 | owl_function_debugmsg("startup: Not setting TERMINFO"); |
---|
[b20de4f] | 149 | } else { |
---|
| 150 | owl_function_debugmsg("startup: leaving TERMINFO as %s from envrionment", getenv("TERMINFO")); |
---|
| 151 | } |
---|
[7d4fbcd] | 152 | initscr(); |
---|
| 153 | start_color(); |
---|
[ac70242] | 154 | #ifdef HAVE_USE_DEFAULT_COLORS |
---|
[7d4fbcd] | 155 | use_default_colors(); |
---|
[ac70242] | 156 | #endif |
---|
[7d4fbcd] | 157 | raw(); |
---|
| 158 | noecho(); |
---|
| 159 | |
---|
| 160 | /* define simple color pairs */ |
---|
| 161 | if (has_colors() && COLOR_PAIRS>=8) { |
---|
[8fa9562] | 162 | int bg = COLOR_BLACK; |
---|
| 163 | #ifdef HAVE_USE_DEFAULT_COLORS |
---|
| 164 | bg = -1; |
---|
| 165 | #endif |
---|
| 166 | init_pair(OWL_COLOR_BLACK, COLOR_BLACK, bg); |
---|
| 167 | init_pair(OWL_COLOR_RED, COLOR_RED, bg); |
---|
| 168 | init_pair(OWL_COLOR_GREEN, COLOR_GREEN, bg); |
---|
| 169 | init_pair(OWL_COLOR_YELLOW, COLOR_YELLOW, bg); |
---|
| 170 | init_pair(OWL_COLOR_BLUE, COLOR_BLUE, bg); |
---|
| 171 | init_pair(OWL_COLOR_MAGENTA, COLOR_MAGENTA, bg); |
---|
| 172 | init_pair(OWL_COLOR_CYAN, COLOR_CYAN, bg); |
---|
| 173 | init_pair(OWL_COLOR_WHITE, COLOR_WHITE, bg); |
---|
[7d4fbcd] | 174 | } |
---|
[38cf544c] | 175 | |
---|
[bd3f232] | 176 | /* owl global init */ |
---|
[7d4fbcd] | 177 | owl_global_init(&g); |
---|
[b363d83] | 178 | if (debug) owl_global_set_debug_on(&g); |
---|
| 179 | if (confdir) owl_global_set_confdir(&g, confdir); |
---|
[b20de4f] | 180 | owl_function_debugmsg("startup: first available debugging message"); |
---|
[7d4fbcd] | 181 | owl_global_set_startupargs(&g, argcsave, argvsave); |
---|
[09489b89] | 182 | owl_global_set_haveaim(&g); |
---|
[2a2bb60] | 183 | |
---|
[9c7a701] | 184 | /* prepare stdin dispatch */ |
---|
| 185 | { |
---|
| 186 | owl_dispatch *d = owl_malloc(sizeof(owl_dispatch)); |
---|
| 187 | d->fd = STDIN; |
---|
| 188 | d->cfunc = &owl_process_input; |
---|
[f36cd97] | 189 | d->destroy = NULL; |
---|
[9c7a701] | 190 | owl_select_add_dispatch(d); |
---|
| 191 | } |
---|
| 192 | |
---|
[596c22df] | 193 | #ifdef HAVE_LIBZEPHYR |
---|
| 194 | /* zephyr init */ |
---|
| 195 | ret=owl_zephyr_initialize(); |
---|
[9c7a701] | 196 | if (!ret) { |
---|
| 197 | owl_dispatch *d = owl_malloc(sizeof(owl_dispatch)); |
---|
| 198 | d->fd = ZGetFD(); |
---|
| 199 | d->cfunc = &owl_zephyr_process_events; |
---|
[f36cd97] | 200 | d->destroy = NULL; |
---|
[9c7a701] | 201 | owl_select_add_dispatch(d); |
---|
| 202 | owl_global_set_havezephyr(&g); |
---|
| 203 | } |
---|
| 204 | |
---|
[596c22df] | 205 | #endif |
---|
| 206 | |
---|
[2a2bb60] | 207 | #if OWL_STDERR_REDIR |
---|
[a0a5179] | 208 | /* Do this only after we've started curses up... */ |
---|
[cf0cc64] | 209 | { |
---|
| 210 | owl_dispatch *d = owl_malloc(sizeof(owl_dispatch)); |
---|
| 211 | owl_function_debugmsg("startup: doing stderr redirection"); |
---|
| 212 | d->fd = stderr_replace(); |
---|
| 213 | d->cfunc = stderr_redirect_handler; |
---|
| 214 | d->destroy = NULL; |
---|
| 215 | owl_select_add_dispatch(d); |
---|
| 216 | } |
---|
| 217 | #endif |
---|
[2a2bb60] | 218 | |
---|
[38cf544c] | 219 | /* create the owl directory, in case it does not exist */ |
---|
[a0a5179] | 220 | owl_function_debugmsg("startup: creating owl directory, if not present"); |
---|
[b363d83] | 221 | dir=owl_global_get_confdir(&g); |
---|
[38cf544c] | 222 | mkdir(dir, S_IRWXU); |
---|
[5145235] | 223 | |
---|
[ced25d1] | 224 | /* set the tty, either from the command line, or by figuring it out */ |
---|
[a0a5179] | 225 | owl_function_debugmsg("startup: setting tty name"); |
---|
[61e79a9] | 226 | if (tty) { |
---|
| 227 | owl_global_set_tty(&g, tty); |
---|
| 228 | } else { |
---|
| 229 | owl_global_set_tty(&g, owl_util_get_default_tty()); |
---|
| 230 | } |
---|
[7d4fbcd] | 231 | |
---|
[b6c067a] | 232 | /* Initialize perl */ |
---|
| 233 | owl_function_debugmsg("startup: processing config file"); |
---|
| 234 | owl_context_set_readconfig(owl_global_get_context(&g)); |
---|
| 235 | perlerr=owl_perlconfig_initperl(configfile); |
---|
| 236 | if (perlerr) { |
---|
| 237 | endwin(); |
---|
| 238 | owl_function_error("Internal perl error: %s\n", perlerr); |
---|
| 239 | fprintf(stderr, "Internal perl error: %s\n", perlerr); |
---|
| 240 | fflush(stderr); |
---|
| 241 | printf("Internal perl error: %s\n", perlerr); |
---|
| 242 | fflush(stdout); |
---|
| 243 | exit(1); |
---|
| 244 | } |
---|
| 245 | |
---|
[7d4fbcd] | 246 | /* setup the default filters */ |
---|
[3abf28b] | 247 | /* the personal filter will need to change again when AIM chat's are |
---|
| 248 | * included. Also, there should be an %aimme% */ |
---|
[a0a5179] | 249 | owl_function_debugmsg("startup: creating default filters"); |
---|
[bd3f232] | 250 | f=owl_malloc(sizeof(owl_filter)); |
---|
[095e717] | 251 | owl_filter_init_fromstring(f, "personal", "isprivate ^true$ and ( not type ^zephyr$" |
---|
| 252 | " or ( class ^message and" |
---|
| 253 | " ( instance ^personal$ or instance ^urgent$ ) ) )"); |
---|
[7d4fbcd] | 254 | owl_list_append_element(owl_global_get_filterlist(&g), f); |
---|
| 255 | |
---|
[bd3f232] | 256 | f=owl_malloc(sizeof(owl_filter)); |
---|
[fa86732] | 257 | owl_filter_init_fromstring(f, "wordwrap", "not ( type ^admin$ or type ^zephyr$ ) "); |
---|
| 258 | owl_list_append_element(owl_global_get_filterlist(&g), f); |
---|
| 259 | |
---|
| 260 | f=owl_malloc(sizeof(owl_filter)); |
---|
[0c502e9] | 261 | owl_filter_init_fromstring(f, "trash", "class ^mail$ or opcode ^ping$ or type ^admin$ or ( not login ^none$ )"); |
---|
[7d4fbcd] | 262 | owl_list_append_element(owl_global_get_filterlist(&g), f); |
---|
| 263 | |
---|
[bd3f232] | 264 | f=owl_malloc(sizeof(owl_filter)); |
---|
[0236842] | 265 | owl_filter_init_fromstring(f, "ping", "opcode ^ping$"); |
---|
| 266 | owl_list_append_element(owl_global_get_filterlist(&g), f); |
---|
| 267 | |
---|
[bd3f232] | 268 | f=owl_malloc(sizeof(owl_filter)); |
---|
[0236842] | 269 | owl_filter_init_fromstring(f, "auto", "opcode ^auto$"); |
---|
| 270 | owl_list_append_element(owl_global_get_filterlist(&g), f); |
---|
| 271 | |
---|
[bd3f232] | 272 | f=owl_malloc(sizeof(owl_filter)); |
---|
[0c502e9] | 273 | owl_filter_init_fromstring(f, "login", "not login ^none$"); |
---|
[0236842] | 274 | owl_list_append_element(owl_global_get_filterlist(&g), f); |
---|
| 275 | |
---|
[bd3f232] | 276 | f=owl_malloc(sizeof(owl_filter)); |
---|
[7d4fbcd] | 277 | owl_filter_init_fromstring(f, "reply-lockout", "class ^noc or class ^mail$"); |
---|
| 278 | owl_list_append_element(owl_global_get_filterlist(&g), f); |
---|
| 279 | |
---|
[bd3f232] | 280 | f=owl_malloc(sizeof(owl_filter)); |
---|
[5a6e6b9] | 281 | owl_filter_init_fromstring(f, "out", "direction ^out$"); |
---|
| 282 | owl_list_append_element(owl_global_get_filterlist(&g), f); |
---|
| 283 | |
---|
[bd3f232] | 284 | f=owl_malloc(sizeof(owl_filter)); |
---|
[31e48a3] | 285 | owl_filter_init_fromstring(f, "aim", "type ^aim$"); |
---|
| 286 | owl_list_append_element(owl_global_get_filterlist(&g), f); |
---|
| 287 | |
---|
[bd3f232] | 288 | f=owl_malloc(sizeof(owl_filter)); |
---|
[31e48a3] | 289 | owl_filter_init_fromstring(f, "zephyr", "type ^zephyr$"); |
---|
| 290 | owl_list_append_element(owl_global_get_filterlist(&g), f); |
---|
| 291 | |
---|
[bd3f232] | 292 | f=owl_malloc(sizeof(owl_filter)); |
---|
[89426ab] | 293 | owl_filter_init_fromstring(f, "none", "false"); |
---|
| 294 | owl_list_append_element(owl_global_get_filterlist(&g), f); |
---|
| 295 | |
---|
[bd3f232] | 296 | f=owl_malloc(sizeof(owl_filter)); |
---|
[89426ab] | 297 | owl_filter_init_fromstring(f, "all", "true"); |
---|
[7d4fbcd] | 298 | owl_list_append_element(owl_global_get_filterlist(&g), f); |
---|
| 299 | |
---|
| 300 | /* set the current view */ |
---|
[a0a5179] | 301 | owl_function_debugmsg("startup: setting the current view"); |
---|
[c3ab155] | 302 | owl_view_create(owl_global_get_current_view(&g), "main", f, owl_global_get_style_by_name(&g, "default")); |
---|
[7d4fbcd] | 303 | |
---|
[96f8e5b] | 304 | /* AIM init */ |
---|
[a0a5179] | 305 | owl_function_debugmsg("startup: doing AIM initialization"); |
---|
[96f8e5b] | 306 | owl_aim_init(); |
---|
| 307 | |
---|
[ced25d1] | 308 | /* execute the startup function in the configfile */ |
---|
[a0a5179] | 309 | owl_function_debugmsg("startup: executing perl startup, if applicable"); |
---|
[0337203] | 310 | perlout = owl_perlconfig_execute("BarnOwl::Hooks::_startup();"); |
---|
[7d4fbcd] | 311 | if (perlout) owl_free(perlout); |
---|
[212764e] | 312 | |
---|
[7d4fbcd] | 313 | /* hold on to the window names for convenience */ |
---|
| 314 | msgwin=owl_global_get_curs_msgwin(&g); |
---|
| 315 | recwin=owl_global_get_curs_recwin(&g); |
---|
| 316 | sepwin=owl_global_get_curs_sepwin(&g); |
---|
| 317 | typwin=owl_global_get_curs_typwin(&g); |
---|
| 318 | tw=owl_global_get_typwin(&g); |
---|
| 319 | |
---|
[252a5c2] | 320 | /* welcome message */ |
---|
[a0a5179] | 321 | owl_function_debugmsg("startup: creating splash message"); |
---|
[52f3507] | 322 | strcpy(startupmsg, "-----------------------------------------------------------------------\n"); |
---|
[c2c5c77] | 323 | sprintf(buff, "Welcome to barnowl version %s. Press 'h' for on-line help. \n", OWL_VERSION_STRING); |
---|
[252a5c2] | 324 | strcat(startupmsg, buff); |
---|
[799b60e] | 325 | strcat(startupmsg, "To see a quick introduction, type ':show quickstart'. \n"); |
---|
[52f3507] | 326 | strcat(startupmsg, " \n"); |
---|
[628c897] | 327 | strcat(startupmsg, "BarnOwl is free software. Type ':show license' for more \n"); |
---|
[799b60e] | 328 | strcat(startupmsg, "information. ^ ^ \n"); |
---|
[e132a2d] | 329 | strcat(startupmsg, " OvO \n"); |
---|
[628c897] | 330 | strcat(startupmsg, "Please report any bugs or suggestions to bug-barnowl@mit.edu ( ) \n"); |
---|
[52f3507] | 331 | strcat(startupmsg, "-----------------------------------------------------------------m-m---\n"); |
---|
[252a5c2] | 332 | owl_function_adminmsg("", startupmsg); |
---|
| 333 | sepbar(NULL); |
---|
| 334 | |
---|
[687c674] | 335 | /* process the startup file */ |
---|
| 336 | owl_function_debugmsg("startup: processing startup file"); |
---|
| 337 | owl_function_source(NULL); |
---|
| 338 | |
---|
[7d4fbcd] | 339 | wrefresh(sepwin); |
---|
| 340 | |
---|
| 341 | /* load zephyr subs */ |
---|
[bd3f232] | 342 | if (initialsubs) { |
---|
[4357be8] | 343 | int ret2; |
---|
[a0a5179] | 344 | owl_function_debugmsg("startup: loading initial zephyr subs"); |
---|
[4357be8] | 345 | |
---|
| 346 | /* load default subscriptions */ |
---|
| 347 | ret=owl_zephyr_loaddefaultsubs(); |
---|
[252a5c2] | 348 | |
---|
[4357be8] | 349 | /* load subscriptions from subs file */ |
---|
[95474d7] | 350 | ret2=owl_zephyr_loadsubs(NULL, 0); |
---|
[4357be8] | 351 | |
---|
| 352 | if (ret || ret2) { |
---|
| 353 | owl_function_adminmsg("", "Error loading zephyr subscriptions"); |
---|
| 354 | } else if (ret2!=-1) { |
---|
[bd3f232] | 355 | owl_global_add_userclue(&g, OWL_USERCLUE_CLASSES); |
---|
| 356 | } |
---|
[7d4fbcd] | 357 | |
---|
[bd3f232] | 358 | /* load login subscriptions */ |
---|
| 359 | if (owl_global_is_loginsubs(&g)) { |
---|
[a0a5179] | 360 | owl_function_debugmsg("startup: loading login subs"); |
---|
[bd3f232] | 361 | owl_function_loadloginsubs(NULL); |
---|
| 362 | } |
---|
[7d4fbcd] | 363 | } |
---|
| 364 | |
---|
[5a95b69] | 365 | /* First buddy check to sync the list without notifications */ |
---|
| 366 | owl_function_debugmsg("startup: doing initial zephyr buddy check"); |
---|
[4357be8] | 367 | /* owl_function_zephyr_buddy_check(0); */ |
---|
[5a95b69] | 368 | |
---|
[f933403] | 369 | /* set the startup and default style, based on userclue and presence of a |
---|
| 370 | * formatting function */ |
---|
[a0a5179] | 371 | owl_function_debugmsg("startup: setting startup and default style"); |
---|
[27c3a93] | 372 | if (0 != strcmp(owl_global_get_default_style(&g), "__unspecified__")) { |
---|
| 373 | /* the style was set by the user: leave it alone */ |
---|
[f933403] | 374 | } else { |
---|
[52f8dd6] | 375 | owl_global_set_default_style(&g, "default"); |
---|
[f933403] | 376 | } |
---|
| 377 | |
---|
[7d4fbcd] | 378 | /* zlog in if we need to */ |
---|
| 379 | if (owl_global_is_startuplogin(&g)) { |
---|
[a0a5179] | 380 | owl_function_debugmsg("startup: doing zlog in"); |
---|
[31e48a3] | 381 | owl_zephyr_zlog_in(); |
---|
[7d4fbcd] | 382 | } |
---|
| 383 | |
---|
[39dc159] | 384 | owl_function_debugmsg("startup: set style for the view: %s", owl_global_get_default_style(&g)); |
---|
[72c210f] | 385 | s = owl_global_get_style_by_name(&g, owl_global_get_default_style(&g)); |
---|
| 386 | if(s) |
---|
| 387 | owl_view_set_style(owl_global_get_current_view(&g), s); |
---|
| 388 | else |
---|
| 389 | owl_function_error("No such style: %s", owl_global_get_default_style(&g)); |
---|
[a0a5179] | 390 | |
---|
| 391 | owl_function_debugmsg("startup: setting context interactive"); |
---|
[7d4fbcd] | 392 | owl_context_set_interactive(owl_global_get_context(&g)); |
---|
| 393 | |
---|
[a15a84f] | 394 | nexttimediff=10; |
---|
[4b464a4] | 395 | nexttime=time(NULL); |
---|
| 396 | |
---|
[b7bb454] | 397 | |
---|
[c675b39] | 398 | owl_select_add_timer(180, 180, owl_zephyr_buddycheck_timer, NULL, NULL); |
---|
[b7bb454] | 399 | |
---|
| 400 | /* If we ever deprecate the mainloop hook, remove this. */ |
---|
[c675b39] | 401 | owl_select_add_timer(0, 1, owl_perlconfig_mainloop, NULL, NULL); |
---|
[b7bb454] | 402 | |
---|
| 403 | |
---|
[a0a5179] | 404 | owl_function_debugmsg("startup: entering main loop"); |
---|
[7d4fbcd] | 405 | /* main loop */ |
---|
| 406 | while (1) { |
---|
| 407 | |
---|
| 408 | /* if a resize has been scheduled, deal with it */ |
---|
| 409 | owl_global_resize(&g, 0, 0); |
---|
| 410 | |
---|
| 411 | /* these are here in case a resize changes the windows */ |
---|
| 412 | msgwin=owl_global_get_curs_msgwin(&g); |
---|
| 413 | recwin=owl_global_get_curs_recwin(&g); |
---|
| 414 | sepwin=owl_global_get_curs_sepwin(&g); |
---|
| 415 | typwin=owl_global_get_curs_typwin(&g); |
---|
| 416 | |
---|
| 417 | followlast=owl_global_should_followlast(&g); |
---|
[bc220b2] | 418 | |
---|
[4b464a4] | 419 | /* little hack */ |
---|
[a15a84f] | 420 | now=time(NULL); |
---|
| 421 | today=localtime(&now); |
---|
| 422 | if (today->tm_mon==9 && today->tm_mday==31 && owl_global_get_runtime(&g)<600) { |
---|
[4b464a4] | 423 | if (time(NULL)>nexttime) { |
---|
| 424 | if (nexttimediff==1) { |
---|
[a15a84f] | 425 | nexttimediff=10; |
---|
[4b464a4] | 426 | } else { |
---|
| 427 | nexttimediff=1; |
---|
| 428 | } |
---|
| 429 | nexttime+=nexttimediff; |
---|
| 430 | owl_hack_animate(); |
---|
| 431 | } |
---|
| 432 | } |
---|
| 433 | |
---|
[aac889a] | 434 | /* Grab incoming messages. */ |
---|
[d09e5a1] | 435 | newmsgs=0; |
---|
[13a3c1db] | 436 | while(owl_global_messagequeue_pending(&g)) { |
---|
[5a95b69] | 437 | |
---|
[13a3c1db] | 438 | m = owl_global_messagequeue_popmsg(&g); |
---|
[7d4fbcd] | 439 | |
---|
[13a3c1db] | 440 | if(owl_process_message(m)) |
---|
| 441 | newmsgs = 1; |
---|
[7d4fbcd] | 442 | } |
---|
| 443 | |
---|
| 444 | /* follow the last message if we're supposed to */ |
---|
[d09e5a1] | 445 | if (newmsgs && followlast) { |
---|
[7d4fbcd] | 446 | owl_function_lastmsg_noredisplay(); |
---|
| 447 | } |
---|
[700c712] | 448 | |
---|
[ced25d1] | 449 | /* do the newmsgproc thing */ |
---|
[d09e5a1] | 450 | if (newmsgs) { |
---|
[8f44c6b] | 451 | owl_function_do_newmsgproc(); |
---|
[700c712] | 452 | } |
---|
[7d4fbcd] | 453 | |
---|
| 454 | /* redisplay if necessary */ |
---|
[d09e5a1] | 455 | /* this should be optimized to not run if the new messages won't be displayed */ |
---|
| 456 | if (newmsgs) { |
---|
[7d4fbcd] | 457 | owl_mainwin_redisplay(owl_global_get_mainwin(&g)); |
---|
| 458 | sepbar(NULL); |
---|
| 459 | if (owl_popwin_is_active(owl_global_get_popwin(&g))) { |
---|
| 460 | owl_popwin_refresh(owl_global_get_popwin(&g)); |
---|
| 461 | /* TODO: this is a broken kludge */ |
---|
| 462 | if (owl_global_get_viewwin(&g)) { |
---|
| 463 | owl_viewwin_redisplay(owl_global_get_viewwin(&g), 0); |
---|
| 464 | } |
---|
| 465 | } |
---|
| 466 | owl_global_set_needrefresh(&g); |
---|
| 467 | } |
---|
| 468 | |
---|
| 469 | /* if a popwin just came up, refresh it */ |
---|
| 470 | pw=owl_global_get_popwin(&g); |
---|
| 471 | if (owl_popwin_is_active(pw) && owl_popwin_needs_first_refresh(pw)) { |
---|
| 472 | owl_popwin_refresh(pw); |
---|
| 473 | owl_popwin_no_needs_first_refresh(pw); |
---|
| 474 | owl_global_set_needrefresh(&g); |
---|
| 475 | /* TODO: this is a broken kludge */ |
---|
| 476 | if (owl_global_get_viewwin(&g)) { |
---|
| 477 | owl_viewwin_redisplay(owl_global_get_viewwin(&g), 0); |
---|
| 478 | } |
---|
| 479 | } |
---|
| 480 | |
---|
| 481 | /* update the terminal if we need to */ |
---|
| 482 | if (owl_global_is_needrefresh(&g)) { |
---|
| 483 | /* leave the cursor in the appropriate window */ |
---|
| 484 | if (owl_global_is_typwin_active(&g)) { |
---|
| 485 | owl_function_set_cursor(typwin); |
---|
| 486 | } else { |
---|
| 487 | owl_function_set_cursor(sepwin); |
---|
| 488 | } |
---|
| 489 | doupdate(); |
---|
| 490 | owl_global_set_noneedrefresh(&g); |
---|
| 491 | } |
---|
| 492 | |
---|
[9c7a701] | 493 | /* select on FDs we know about. */ |
---|
| 494 | owl_select(); |
---|
[2a2bb60] | 495 | |
---|
[c9e72d1] | 496 | /* Log any error signals */ |
---|
| 497 | { |
---|
| 498 | siginfo_t si; |
---|
| 499 | int signum; |
---|
| 500 | if ((signum = owl_global_get_errsignal_and_clear(&g, &si)) > 0) { |
---|
[6eaf35b] | 501 | owl_function_error("Got unexpected signal: %d %s (code: %d band: %ld errno: %d)", |
---|
[afbf668] | 502 | signum, signum==SIGPIPE?"SIGPIPE":"SIG????", |
---|
[81634b6] | 503 | si.si_code, si.si_band, si.si_errno); |
---|
[c9e72d1] | 504 | } |
---|
| 505 | } |
---|
| 506 | |
---|
[7d4fbcd] | 507 | } |
---|
| 508 | } |
---|
| 509 | |
---|
[13a3c1db] | 510 | /* |
---|
| 511 | * Process a new message passed to us on the message queue from some |
---|
| 512 | * protocol. This includes adding it to the message list, updating the |
---|
| 513 | * view and scrolling if appropriate, logging it, and so on. |
---|
| 514 | * |
---|
| 515 | * Either a pointer is kept to the message internally, or it is freed |
---|
| 516 | * if unneeded. The caller no longer ``owns'' the message's memory. |
---|
| 517 | * |
---|
| 518 | * Returns 1 if the message was added to the message list, and 0 if it |
---|
| 519 | * was ignored due to user settings or otherwise. |
---|
| 520 | */ |
---|
| 521 | int owl_process_message(owl_message *m) { |
---|
| 522 | owl_filter *f; |
---|
| 523 | /* if this message it on the puntlist, nuke it and continue */ |
---|
| 524 | if (owl_global_message_is_puntable(&g, m)) { |
---|
| 525 | owl_message_free(m); |
---|
| 526 | return 0; |
---|
| 527 | } |
---|
| 528 | |
---|
| 529 | /* login or logout that should be ignored? */ |
---|
| 530 | if (owl_global_is_ignorelogins(&g) |
---|
| 531 | && owl_message_is_loginout(m)) { |
---|
| 532 | owl_message_free(m); |
---|
| 533 | return 0; |
---|
| 534 | } |
---|
| 535 | |
---|
| 536 | if (!owl_global_is_displayoutgoing(&g) |
---|
| 537 | && owl_message_is_direction_out(m)) { |
---|
| 538 | owl_message_free(m); |
---|
| 539 | return 0; |
---|
| 540 | } |
---|
| 541 | |
---|
| 542 | /* add it to the global list */ |
---|
| 543 | owl_messagelist_append_element(owl_global_get_msglist(&g), m); |
---|
| 544 | /* add it to any necessary views; right now there's only the current view */ |
---|
| 545 | owl_view_consider_message(owl_global_get_current_view(&g), m); |
---|
| 546 | |
---|
| 547 | if(owl_message_is_direction_in(m)) { |
---|
| 548 | /* let perl know about it*/ |
---|
[b67ab6b] | 549 | owl_perlconfig_getmsg(m, NULL); |
---|
[13a3c1db] | 550 | |
---|
| 551 | /* do we need to autoreply? */ |
---|
| 552 | if (owl_global_is_zaway(&g) && !owl_message_get_attribute_value(m, "isauto")) { |
---|
| 553 | if (owl_message_is_type_zephyr(m)) { |
---|
| 554 | owl_zephyr_zaway(m); |
---|
| 555 | } else if (owl_message_is_type_aim(m)) { |
---|
| 556 | if (owl_message_is_private(m)) { |
---|
| 557 | owl_function_send_aimawymsg(owl_message_get_sender(m), owl_global_get_zaway_msg(&g)); |
---|
| 558 | } |
---|
| 559 | } |
---|
| 560 | } |
---|
| 561 | |
---|
| 562 | /* ring the bell if it's a personal */ |
---|
| 563 | if (!strcmp(owl_global_get_personalbell(&g), "on")) { |
---|
| 564 | if (!owl_message_is_loginout(m) && |
---|
| 565 | !owl_message_is_mail(m) && |
---|
| 566 | owl_message_is_personal(m)) { |
---|
| 567 | owl_function_beep(); |
---|
| 568 | } |
---|
| 569 | } else if (!strcmp(owl_global_get_personalbell(&g), "off")) { |
---|
| 570 | /* do nothing */ |
---|
| 571 | } else { |
---|
| 572 | f=owl_global_get_filter(&g, owl_global_get_personalbell(&g)); |
---|
| 573 | if (f && owl_filter_message_match(f, m)) { |
---|
| 574 | owl_function_beep(); |
---|
| 575 | } |
---|
| 576 | } |
---|
| 577 | |
---|
| 578 | /* if it matches the alert filter, do the alert action */ |
---|
| 579 | f=owl_global_get_filter(&g, owl_global_get_alert_filter(&g)); |
---|
| 580 | if (f && owl_filter_message_match(f, m)) { |
---|
| 581 | owl_function_command(owl_global_get_alert_action(&g)); |
---|
| 582 | } |
---|
| 583 | |
---|
| 584 | /* if it's a zephyr login or logout, update the zbuddylist */ |
---|
| 585 | if (owl_message_is_type_zephyr(m) && owl_message_is_loginout(m)) { |
---|
| 586 | if (owl_message_is_login(m)) { |
---|
| 587 | owl_zbuddylist_adduser(owl_global_get_zephyr_buddylist(&g), owl_message_get_sender(m)); |
---|
| 588 | } else if (owl_message_is_logout(m)) { |
---|
| 589 | owl_zbuddylist_deluser(owl_global_get_zephyr_buddylist(&g), owl_message_get_sender(m)); |
---|
| 590 | } else { |
---|
| 591 | owl_function_error("Internal error: received login notice that is neither login nor logout"); |
---|
| 592 | } |
---|
| 593 | } |
---|
| 594 | } |
---|
| 595 | |
---|
[0f9eca7] | 596 | /* let perl know about it */ |
---|
| 597 | owl_perlconfig_newmsg(m, NULL); |
---|
[13a3c1db] | 598 | /* log the message if we need to */ |
---|
| 599 | owl_log_message(m); |
---|
| 600 | |
---|
| 601 | return 1; |
---|
| 602 | } |
---|
| 603 | |
---|
[f36cd97] | 604 | void owl_process_input(owl_dispatch *d) |
---|
[9c7a701] | 605 | { |
---|
[5f3168a] | 606 | int ret; |
---|
| 607 | owl_input j; |
---|
[9c7a701] | 608 | owl_popwin *pw; |
---|
| 609 | owl_editwin *tw; |
---|
[5f3168a] | 610 | WINDOW *typwin; |
---|
[9c7a701] | 611 | |
---|
[5f3168a] | 612 | typwin = owl_global_get_curs_typwin(&g); |
---|
[4cc0ee0b] | 613 | while (1) { |
---|
[07bfbc2] | 614 | j.ch = wgetch(typwin); |
---|
| 615 | if (j.ch == ERR) return; |
---|
| 616 | |
---|
[4cc0ee0b] | 617 | owl_global_set_lastinputtime(&g, time(NULL)); |
---|
| 618 | pw=owl_global_get_popwin(&g); |
---|
| 619 | tw=owl_global_get_typwin(&g); |
---|
[9c7a701] | 620 | |
---|
[07bfbc2] | 621 | j.uch = '\0'; |
---|
| 622 | if (j.ch >= KEY_MIN && j.ch <= KEY_MAX) { |
---|
| 623 | /* This is a curses control character. */ |
---|
[5f3168a] | 624 | } |
---|
[07bfbc2] | 625 | else if (j.ch > 0x7f && j.ch < 0xfe) { |
---|
| 626 | /* Pull in a full utf-8 character. */ |
---|
| 627 | int bytes, i; |
---|
| 628 | char utf8buf[7]; |
---|
| 629 | memset(utf8buf, '\0', 7); |
---|
| 630 | |
---|
| 631 | utf8buf[0] = j.ch; |
---|
| 632 | |
---|
| 633 | if ((j.ch & 0xc0) && (~j.ch & 0x20)) bytes = 2; |
---|
| 634 | else if ((j.ch & 0xe0) && (~j.ch & 0x10)) bytes = 3; |
---|
| 635 | else if ((j.ch & 0xf0) && (~j.ch & 0x08)) bytes = 4; |
---|
| 636 | else if ((j.ch & 0xf8) && (~j.ch & 0x04)) bytes = 5; |
---|
| 637 | else if ((j.ch & 0xfc) && (~j.ch & 0x02)) bytes = 6; |
---|
| 638 | else bytes = 1; |
---|
| 639 | |
---|
| 640 | for (i = 1; i < bytes; i++) { |
---|
| 641 | int tmp = wgetch(typwin); |
---|
| 642 | /* If what we got was not a byte, or not a continuation byte */ |
---|
| 643 | if (tmp > 0xff || !(tmp & 0x80 && ~tmp & 0x40)) { |
---|
| 644 | /* ill-formed UTF-8 code unit subsequence, put back the |
---|
| 645 | char we just got. */ |
---|
| 646 | ungetch(tmp); |
---|
| 647 | j.ch = ERR; |
---|
| 648 | break; |
---|
| 649 | } |
---|
| 650 | utf8buf[i] = tmp; |
---|
[5f3168a] | 651 | } |
---|
[07bfbc2] | 652 | |
---|
| 653 | if (j.ch != ERR) { |
---|
| 654 | if (g_utf8_validate(utf8buf, -1, NULL)) { |
---|
| 655 | j.uch = g_utf8_get_char(utf8buf); |
---|
| 656 | } |
---|
| 657 | else { |
---|
| 658 | j.ch = ERR; |
---|
| 659 | } |
---|
[5f3168a] | 660 | } |
---|
| 661 | } |
---|
[07bfbc2] | 662 | else if (j.ch <= 0x7f) { |
---|
| 663 | j.uch = j.ch; |
---|
| 664 | } |
---|
[4cc0ee0b] | 665 | |
---|
[07bfbc2] | 666 | owl_global_set_lastinputtime(&g, time(NULL)); |
---|
[4cc0ee0b] | 667 | /* find and activate the current keymap. |
---|
| 668 | * TODO: this should really get fixed by activating |
---|
| 669 | * keymaps as we switch between windows... |
---|
| 670 | */ |
---|
| 671 | if (pw && owl_popwin_is_active(pw) && owl_global_get_viewwin(&g)) { |
---|
| 672 | owl_context_set_popless(owl_global_get_context(&g), |
---|
| 673 | owl_global_get_viewwin(&g)); |
---|
| 674 | owl_function_activate_keymap("popless"); |
---|
| 675 | } else if (owl_global_is_typwin_active(&g) |
---|
[07bfbc2] | 676 | && owl_editwin_get_style(tw)==OWL_EDITWIN_STYLE_ONELINE) { |
---|
[4cc0ee0b] | 677 | /* |
---|
| 678 | owl_context_set_editline(owl_global_get_context(&g), tw); |
---|
| 679 | owl_function_activate_keymap("editline"); |
---|
| 680 | */ |
---|
| 681 | } else if (owl_global_is_typwin_active(&g) |
---|
[07bfbc2] | 682 | && owl_editwin_get_style(tw)==OWL_EDITWIN_STYLE_MULTILINE) { |
---|
[4cc0ee0b] | 683 | owl_context_set_editmulti(owl_global_get_context(&g), tw); |
---|
| 684 | owl_function_activate_keymap("editmulti"); |
---|
| 685 | } else { |
---|
| 686 | owl_context_set_recv(owl_global_get_context(&g)); |
---|
| 687 | owl_function_activate_keymap("recv"); |
---|
| 688 | } |
---|
| 689 | /* now actually handle the keypress */ |
---|
| 690 | ret = owl_keyhandler_process(owl_global_get_keyhandler(&g), j); |
---|
[07bfbc2] | 691 | if (ret!=0 && ret!=1) { |
---|
[4cc0ee0b] | 692 | owl_function_makemsg("Unable to handle keypress"); |
---|
| 693 | } |
---|
[9c7a701] | 694 | } |
---|
| 695 | } |
---|
| 696 | |
---|
[de22c3d] | 697 | void sig_handler(int sig, siginfo_t *si, void *data) |
---|
| 698 | { |
---|
[7d4fbcd] | 699 | if (sig==SIGWINCH) { |
---|
[bd3f232] | 700 | /* we can't inturrupt a malloc here, so it just sets a flag |
---|
| 701 | * schedulding a resize for later |
---|
| 702 | */ |
---|
[7d4fbcd] | 703 | owl_function_resize(); |
---|
[afbf668] | 704 | } else if (sig==SIGPIPE || sig==SIGCHLD) { |
---|
[c9e72d1] | 705 | /* Set a flag and some info that we got the sigpipe |
---|
| 706 | * so we can record that we got it and why... */ |
---|
| 707 | owl_global_set_errsignal(&g, sig, si); |
---|
[fe1f605] | 708 | } else if (sig==SIGTERM || sig==SIGHUP) { |
---|
| 709 | owl_function_quit(); |
---|
[7d4fbcd] | 710 | } |
---|
| 711 | } |
---|
| 712 | |
---|
[de22c3d] | 713 | void usage() |
---|
| 714 | { |
---|
[78667f3] | 715 | fprintf(stderr, "Barnowl version %s\n", OWL_VERSION_STRING); |
---|
| 716 | fprintf(stderr, "Usage: barnowl [-n] [-d] [-D] [-v] [-h] [-c <configfile>] [-s <confdir>] [-t <ttyname>]\n"); |
---|
[e7cc1c3] | 717 | fprintf(stderr, " -n don't load zephyr subscriptions\n"); |
---|
| 718 | fprintf(stderr, " -d enable debugging\n"); |
---|
[a68f05d] | 719 | fprintf(stderr, " -D enable debugging and delete previous debug file\n"); |
---|
[78667f3] | 720 | fprintf(stderr, " -v print the Barnowl version number and exit\n"); |
---|
[e7cc1c3] | 721 | fprintf(stderr, " -h print this help message\n"); |
---|
| 722 | fprintf(stderr, " -c specify an alternate config file\n"); |
---|
[b363d83] | 723 | fprintf(stderr, " -s specify an alternate config dir (default ~/.owl)\n"); |
---|
[e7cc1c3] | 724 | fprintf(stderr, " -t set the tty name\n"); |
---|
[7d4fbcd] | 725 | } |
---|
[2a2bb60] | 726 | |
---|
| 727 | #if OWL_STDERR_REDIR |
---|
| 728 | |
---|
| 729 | /* Replaces stderr with a pipe so that we can read from it. |
---|
| 730 | * Returns the fd of the pipe from which stderr can be read. */ |
---|
[de22c3d] | 731 | int stderr_replace(void) |
---|
| 732 | { |
---|
[2a2bb60] | 733 | int pipefds[2]; |
---|
| 734 | if (0 != pipe(pipefds)) { |
---|
| 735 | perror("pipe"); |
---|
| 736 | owl_function_debugmsg("stderr_replace: pipe FAILED\n"); |
---|
| 737 | return -1; |
---|
| 738 | } |
---|
| 739 | owl_function_debugmsg("stderr_replace: pipe: %d,%d\n", pipefds[0], pipefds[1]); |
---|
| 740 | if (-1 == dup2(pipefds[1], 2 /*stderr*/)) { |
---|
| 741 | owl_function_debugmsg("stderr_replace: dup2 FAILED (%s)\n", strerror(errno)); |
---|
| 742 | perror("dup2"); |
---|
| 743 | return -1; |
---|
| 744 | } |
---|
| 745 | return pipefds[0]; |
---|
| 746 | } |
---|
| 747 | |
---|
[afbf668] | 748 | /* Sends stderr (read from rfd) messages to the error console */ |
---|
[cf0cc64] | 749 | void stderr_redirect_handler(owl_dispatch *d) |
---|
[de22c3d] | 750 | { |
---|
[2a2bb60] | 751 | int navail, bread; |
---|
[924f7cd] | 752 | char buf[4096]; |
---|
[cf0cc64] | 753 | int rfd = d->fd; |
---|
[2a2bb60] | 754 | if (rfd<0) return; |
---|
| 755 | if (-1 == ioctl(rfd, FIONREAD, (void*)&navail)) { |
---|
| 756 | return; |
---|
| 757 | } |
---|
| 758 | /*owl_function_debugmsg("stderr_redirect: navail = %d\n", navail);*/ |
---|
[924f7cd] | 759 | if (navail <= 0) return; |
---|
| 760 | if (navail > sizeof(buf)-1) { |
---|
| 761 | navail = sizeof(buf)-1; |
---|
| 762 | } |
---|
[2a2bb60] | 763 | bread = read(rfd, buf, navail); |
---|
| 764 | if (buf[navail-1] != '\0') { |
---|
| 765 | buf[navail] = '\0'; |
---|
| 766 | } |
---|
[924f7cd] | 767 | owl_function_error("[stderr]\n%s", buf); |
---|
[2a2bb60] | 768 | } |
---|
| 769 | |
---|
| 770 | #endif /* OWL_STDERR_REDIR */ |
---|
[b7bb454] | 771 | |
---|
| 772 | void owl_zephyr_buddycheck_timer(owl_timer *t, void *data) |
---|
| 773 | { |
---|
[80a465c] | 774 | if (owl_global_is_pseudologins(&g)) { |
---|
[b7bb454] | 775 | owl_function_debugmsg("Doing zephyr buddy check"); |
---|
| 776 | owl_function_zephyr_buddy_check(1); |
---|
[80a465c] | 777 | } |
---|
[b7bb454] | 778 | } |
---|