| 1 | /* Copyright (c) 2002,2003,2004,2009 James M. Kretchmar |
|---|
| 2 | * |
|---|
| 3 | * This file is part of Owl. |
|---|
| 4 | * |
|---|
| 5 | * Owl is free software: you can redistribute it and/or modify |
|---|
| 6 | * it under the terms of the GNU General Public License as published by |
|---|
| 7 | * the Free Software Foundation, either version 3 of the License, or |
|---|
| 8 | * (at your option) any later version. |
|---|
| 9 | * |
|---|
| 10 | * Owl is distributed in the hope that it will be useful, |
|---|
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 13 | * GNU General Public License for more details. |
|---|
| 14 | * |
|---|
| 15 | * You should have received a copy of the GNU General Public License |
|---|
| 16 | * along with Owl. If not, see <http://www.gnu.org/licenses/>. |
|---|
| 17 | * |
|---|
| 18 | * --------------------------------------------------------------- |
|---|
| 19 | * |
|---|
| 20 | * As of Owl version 2.1.12 there are patches contributed by |
|---|
| 21 | * developers of the branched BarnOwl project, Copyright (c) |
|---|
| 22 | * 2006-2009 The BarnOwl Developers. All rights reserved. |
|---|
| 23 | */ |
|---|
| 24 | |
|---|
| 25 | #include "owl.h" |
|---|
| 26 | |
|---|
| 27 | static const char fileIdent[] = "$Id$"; |
|---|
| 28 | |
|---|
| 29 | void owl_style_create_internal(owl_style *s, char *name, void (*formatfunc) (owl_fmtext *fm, owl_message *m), char *description) |
|---|
| 30 | { |
|---|
| 31 | s->type=OWL_STYLE_TYPE_INTERNAL; |
|---|
| 32 | s->name=owl_strdup(name); |
|---|
| 33 | if (description) { |
|---|
| 34 | s->description=owl_strdup(description); |
|---|
| 35 | } else { |
|---|
| 36 | s->description=owl_sprintf("Owl internal style %s", name); |
|---|
| 37 | } |
|---|
| 38 | s->perlfuncname=NULL; |
|---|
| 39 | s->formatfunc=formatfunc; |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | void owl_style_create_perl(owl_style *s, char *name, char *perlfuncname, char *description) |
|---|
| 43 | { |
|---|
| 44 | s->type=OWL_STYLE_TYPE_PERL; |
|---|
| 45 | s->name=owl_strdup(name); |
|---|
| 46 | s->perlfuncname=owl_strdup(perlfuncname); |
|---|
| 47 | if (description) { |
|---|
| 48 | s->description=owl_strdup(description); |
|---|
| 49 | } else { |
|---|
| 50 | s->description=owl_sprintf("User-defined perl style that calls %s", |
|---|
| 51 | perlfuncname); |
|---|
| 52 | } |
|---|
| 53 | s->formatfunc=NULL; |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | int owl_style_matches_name(owl_style *s, char *name) |
|---|
| 57 | { |
|---|
| 58 | if (!strcmp(s->name, name)) return(1); |
|---|
| 59 | return(0); |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | char *owl_style_get_name(owl_style *s) |
|---|
| 63 | { |
|---|
| 64 | return(s->name); |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | char *owl_style_get_description(owl_style *s) |
|---|
| 68 | { |
|---|
| 69 | return(s->description); |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | /* Use style 's' to format message 'm' into fmtext 'fm'. |
|---|
| 73 | * 'fm' should already be be initialzed |
|---|
| 74 | */ |
|---|
| 75 | void owl_style_get_formattext(owl_style *s, owl_fmtext *fm, owl_message *m) |
|---|
| 76 | { |
|---|
| 77 | if (s->type==OWL_STYLE_TYPE_INTERNAL) { |
|---|
| 78 | (* s->formatfunc)(fm, m); |
|---|
| 79 | } else if (s->type==OWL_STYLE_TYPE_PERL) { |
|---|
| 80 | char *body, *indent; |
|---|
| 81 | int curlen; |
|---|
| 82 | |
|---|
| 83 | /* run the perl function */ |
|---|
| 84 | body=owl_perlconfig_getmsg(m, 1, s->perlfuncname); |
|---|
| 85 | if (!strcmp(body, "")) { |
|---|
| 86 | owl_free(body); |
|---|
| 87 | body=owl_strdup("<unformatted message>"); |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | /* indent and ensure ends with a newline */ |
|---|
| 91 | indent=owl_malloc(strlen(body)+(owl_text_num_lines(body))*OWL_TAB+10); |
|---|
| 92 | owl_text_indent(indent, body, OWL_TAB); |
|---|
| 93 | curlen = strlen(indent); |
|---|
| 94 | if (curlen==0 || indent[curlen-1] != '\n') { |
|---|
| 95 | indent[curlen] = '\n'; |
|---|
| 96 | indent[curlen+1] = '\0'; |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | /* fmtext_append. This needs to change */ |
|---|
| 100 | owl_fmtext_append_ztext(fm, indent); |
|---|
| 101 | |
|---|
| 102 | owl_free(indent); |
|---|
| 103 | owl_free(body); |
|---|
| 104 | } |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | int owl_style_validate(owl_style *s) { |
|---|
| 108 | if (!s) { |
|---|
| 109 | return -1; |
|---|
| 110 | } else if (s->type==OWL_STYLE_TYPE_INTERNAL) { |
|---|
| 111 | return 0; |
|---|
| 112 | } else if (s->type==OWL_STYLE_TYPE_PERL |
|---|
| 113 | && s->perlfuncname |
|---|
| 114 | && owl_perlconfig_is_function(s->perlfuncname)) { |
|---|
| 115 | return 0; |
|---|
| 116 | } else { |
|---|
| 117 | return -1; |
|---|
| 118 | } |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | void owl_style_free(owl_style *s) |
|---|
| 122 | { |
|---|
| 123 | if (s->name) owl_free(s->name); |
|---|
| 124 | if (s->description) owl_free(s->description); |
|---|
| 125 | if (s->type==OWL_STYLE_TYPE_PERL && s->perlfuncname) { |
|---|
| 126 | owl_free(s->perlfuncname); |
|---|
| 127 | } |
|---|
| 128 | } |
|---|