Start untangling header inclusion in a way that does not affect
[asterisk/asterisk.git] / main / image.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2006, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*! \file
20  *
21  * \brief Image Management
22  *
23  * \author Mark Spencer <markster@digium.com> 
24  */
25
26 #include "asterisk.h"
27
28 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
29
30 #include <sys/time.h>
31 #include <sys/stat.h>
32 #include <signal.h>
33 #include <errno.h>
34
35 #include "asterisk/sched.h"
36 #include "asterisk/options.h"
37 #include "asterisk/channel.h"
38 #include "asterisk/logger.h"
39 #include "asterisk/file.h"
40 #include "asterisk/image.h"
41 #include "asterisk/translate.h"
42 #include "asterisk/cli.h"
43 #include "asterisk/lock.h"
44
45 /* XXX Why don't we just use the formats struct for this? */
46 static AST_RWLIST_HEAD_STATIC(imagers, ast_imager);
47
48 int ast_image_register(struct ast_imager *img)
49 {
50         AST_RWLIST_WRLOCK(&imagers);
51         AST_RWLIST_INSERT_HEAD(&imagers, img, list);
52         AST_RWLIST_UNLOCK(&imagers);
53         ast_verb(2, "Registered format '%s' (%s)\n", img->name, img->desc);
54         return 0;
55 }
56
57 void ast_image_unregister(struct ast_imager *img)
58 {
59         AST_RWLIST_WRLOCK(&imagers);
60         img = AST_RWLIST_REMOVE(&imagers, img, list);
61         AST_RWLIST_UNLOCK(&imagers);
62
63         if (img)
64                 ast_verb(2, "Unregistered format '%s' (%s)\n", img->name, img->desc);
65 }
66
67 int ast_supports_images(struct ast_channel *chan)
68 {
69         if (!chan || !chan->tech)
70                 return 0;
71         if (!chan->tech->send_image)
72                 return 0;
73         return 1;
74 }
75
76 static int file_exists(char *filename)
77 {
78         int res;
79         struct stat st;
80         res = stat(filename, &st);
81         if (!res)
82                 return st.st_size;
83         return 0;
84 }
85
86 static void make_filename(char *buf, int len, char *filename, const char *preflang, char *ext)
87 {
88         if (filename[0] == '/') {
89                 if (!ast_strlen_zero(preflang))
90                         snprintf(buf, len, "%s-%s.%s", filename, preflang, ext);
91                 else
92                         snprintf(buf, len, "%s.%s", filename, ext);
93         } else {
94                 if (!ast_strlen_zero(preflang))
95                         snprintf(buf, len, "%s/%s/%s-%s.%s", ast_config_AST_DATA_DIR, "images", filename, preflang, ext);
96                 else
97                         snprintf(buf, len, "%s/%s/%s.%s", ast_config_AST_DATA_DIR, "images", filename, ext);
98         }
99 }
100
101 struct ast_frame *ast_read_image(char *filename, const char *preflang, int format)
102 {
103         struct ast_imager *i;
104         char buf[256];
105         char tmp[80];
106         char *e;
107         struct ast_imager *found = NULL;
108         int fd;
109         int len=0;
110         struct ast_frame *f = NULL;
111         
112         AST_RWLIST_RDLOCK(&imagers);
113         AST_RWLIST_TRAVERSE(&imagers, i, list) {
114                 if (i->format & format) {
115                         char *stringp=NULL;
116                         ast_copy_string(tmp, i->exts, sizeof(tmp));
117                         stringp=tmp;
118                         e = strsep(&stringp, "|");
119                         while (e) {
120                                 make_filename(buf, sizeof(buf), filename, preflang, e);
121                                 if ((len = file_exists(buf))) {
122                                         found = i;
123                                         break;
124                                 }
125                                 make_filename(buf, sizeof(buf), filename, NULL, e);
126                                 if ((len = file_exists(buf))) {
127                                         found = i;
128                                         break;
129                                 }
130                                 e = strsep(&stringp, "|");
131                         }
132                 }
133                 if (found)
134                         break;  
135         }
136
137         if (found) {
138                 fd = open(buf, O_RDONLY);
139                 if (fd > -1) {
140                         if (!found->identify || found->identify(fd)) {
141                                 /* Reset file pointer */
142                                 lseek(fd, 0, SEEK_SET);
143                                 f = found->read_image(fd,len); 
144                         } else
145                                 ast_log(LOG_WARNING, "%s does not appear to be a %s file\n", buf, found->name);
146                         close(fd);
147                 } else
148                         ast_log(LOG_WARNING, "Unable to open '%s': %s\n", buf, strerror(errno));
149         } else
150                 ast_log(LOG_WARNING, "Image file '%s' not found\n", filename);
151         
152         AST_RWLIST_UNLOCK(&imagers);
153         
154         return f;
155 }
156
157 int ast_send_image(struct ast_channel *chan, char *filename)
158 {
159         struct ast_frame *f;
160         int res = -1;
161         if (chan->tech->send_image) {
162                 f = ast_read_image(filename, chan->language, -1);
163                 if (f) {
164                         res = chan->tech->send_image(chan, f);
165                         ast_frfree(f);
166                 }
167         }
168         return res;
169 }
170
171 static char *handle_core_show_image_formats(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
172 {
173 #define FORMAT "%10s %10s %50s %10s\n"
174 #define FORMAT2 "%10s %10s %50s %10s\n"
175         struct ast_imager *i;
176         int count_fmt = 0;
177
178         switch (cmd) {
179         case CLI_INIT:
180                 e->command = "core show image formats";
181                 e->usage =
182                         "Usage: core show image formats\n"
183                         "       Displays currently registered image formats (if any).\n";
184                 return NULL;
185         case CLI_GENERATE:
186                 return NULL;
187         }
188         if (a->argc != 4)
189                 return CLI_SHOWUSAGE;
190         ast_cli(a->fd, FORMAT, "Name", "Extensions", "Description", "Format");
191         ast_cli(a->fd, FORMAT, "----", "----------", "-----------", "------");
192         AST_RWLIST_RDLOCK(&imagers);
193         AST_RWLIST_TRAVERSE(&imagers, i, list) {
194                 ast_cli(a->fd, FORMAT2, i->name, i->exts, i->desc, ast_getformatname(i->format));
195                 count_fmt++;
196         }
197         AST_RWLIST_UNLOCK(&imagers);
198         ast_cli(a->fd, "\n%d image format%s registered.\n", count_fmt, count_fmt == 1 ? "" : "s");
199         return CLI_SUCCESS;
200 }
201
202 struct ast_cli_entry cli_image[] = {
203         AST_CLI_DEFINE(handle_core_show_image_formats, "Displays image formats")
204 };
205
206 int ast_image_init(void)
207 {
208         ast_cli_register_multiple(cli_image, ARRAY_LEN(cli_image));
209         return 0;
210 }