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