ARI: Add ability to raise arbitrary User Events
[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 /*** MODULEINFO
27         <support_level>core</support_level>
28  ***/
29
30 #include "asterisk.h"
31
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
33
34 #include <sys/time.h>
35 #include <sys/stat.h>
36 #include <signal.h>
37
38 #include "asterisk/paths.h"     /* use ast_config_AST_DATA_DIR */
39 #include "asterisk/sched.h"
40 #include "asterisk/channel.h"
41 #include "asterisk/file.h"
42 #include "asterisk/image.h"
43 #include "asterisk/translate.h"
44 #include "asterisk/cli.h"
45 #include "asterisk/lock.h"
46
47 /* XXX Why don't we just use the formats struct for this? */
48 static AST_RWLIST_HEAD_STATIC(imagers, ast_imager);
49
50 int ast_image_register(struct ast_imager *img)
51 {
52         AST_RWLIST_WRLOCK(&imagers);
53         AST_RWLIST_INSERT_HEAD(&imagers, img, list);
54         AST_RWLIST_UNLOCK(&imagers);
55         ast_verb(2, "Registered format '%s' (%s)\n", img->name, img->desc);
56         return 0;
57 }
58
59 void ast_image_unregister(struct ast_imager *img)
60 {
61         AST_RWLIST_WRLOCK(&imagers);
62         img = AST_RWLIST_REMOVE(&imagers, img, list);
63         AST_RWLIST_UNLOCK(&imagers);
64
65         if (img)
66                 ast_verb(2, "Unregistered format '%s' (%s)\n", img->name, img->desc);
67 }
68
69 int ast_supports_images(struct ast_channel *chan)
70 {
71         if (!chan || !ast_channel_tech(chan))
72                 return 0;
73         if (!ast_channel_tech(chan)->send_image)
74                 return 0;
75         return 1;
76 }
77
78 static int file_exists(char *filename)
79 {
80         int res;
81         struct stat st;
82         res = stat(filename, &st);
83         if (!res)
84                 return st.st_size;
85         return 0;
86 }
87
88 static void make_filename(char *buf, int len, const char *filename, const char *preflang, char *ext)
89 {
90         if (filename[0] == '/') {
91                 if (!ast_strlen_zero(preflang))
92                         snprintf(buf, len, "%s-%s.%s", filename, preflang, ext);
93                 else
94                         snprintf(buf, len, "%s.%s", filename, ext);
95         } else {
96                 if (!ast_strlen_zero(preflang))
97                         snprintf(buf, len, "%s/%s/%s-%s.%s", ast_config_AST_DATA_DIR, "images", filename, preflang, ext);
98                 else
99                         snprintf(buf, len, "%s/%s/%s.%s", ast_config_AST_DATA_DIR, "images", filename, ext);
100         }
101 }
102
103 struct ast_frame *ast_read_image(const char *filename, const char *preflang, struct ast_format *format)
104 {
105         struct ast_imager *i;
106         char buf[256];
107         char tmp[80];
108         char *e;
109         struct ast_imager *found = NULL;
110         int fd;
111         int len=0;
112         struct ast_frame *f = NULL;
113
114         AST_RWLIST_RDLOCK(&imagers);
115         AST_RWLIST_TRAVERSE(&imagers, i, list) {
116                 /* if NULL image format, just pick the first one, otherwise match it. */
117                 if (!format || (ast_format_cmp(&i->format, format) == AST_FORMAT_CMP_EQUAL)) {
118                         char *stringp=NULL;
119                         ast_copy_string(tmp, i->exts, sizeof(tmp));
120                         stringp = tmp;
121                         e = strsep(&stringp, "|");
122                         while (e) {
123                                 make_filename(buf, sizeof(buf), filename, preflang, e);
124                                 if ((len = file_exists(buf))) {
125                                         found = i;
126                                         break;
127                                 }
128                                 make_filename(buf, sizeof(buf), filename, NULL, e);
129                                 if ((len = file_exists(buf))) {
130                                         found = i;
131                                         break;
132                                 }
133                                 e = strsep(&stringp, "|");
134                         }
135                 }
136                 if (found)
137                         break;
138         }
139
140         if (found) {
141                 fd = open(buf, O_RDONLY);
142                 if (fd > -1) {
143                         if (!found->identify || found->identify(fd)) {
144                                 /* Reset file pointer */
145                                 lseek(fd, 0, SEEK_SET);
146                                 f = found->read_image(fd, len);
147                         } else
148                                 ast_log(LOG_WARNING, "%s does not appear to be a %s file\n", buf, found->name);
149                         close(fd);
150                 } else
151                         ast_log(LOG_WARNING, "Unable to open '%s': %s\n", buf, strerror(errno));
152         } else
153                 ast_log(LOG_WARNING, "Image file '%s' not found\n", filename);
154
155         AST_RWLIST_UNLOCK(&imagers);
156
157         return f;
158 }
159
160 int ast_send_image(struct ast_channel *chan, const char *filename)
161 {
162         struct ast_frame *f;
163         int res = -1;
164         if (ast_channel_tech(chan)->send_image) {
165                 f = ast_read_image(filename, ast_channel_language(chan), NULL);
166                 if (f) {
167                         res = ast_channel_tech(chan)->send_image(chan, f);
168                         ast_frfree(f);
169                 }
170         }
171         return res;
172 }
173
174 static char *handle_core_show_image_formats(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
175 {
176 #define FORMAT "%10s %10s %50s %10s\n"
177 #define FORMAT2 "%10s %10s %50s %10s\n"
178         struct ast_imager *i;
179         int count_fmt = 0;
180
181         switch (cmd) {
182         case CLI_INIT:
183                 e->command = "core show image formats";
184                 e->usage =
185                         "Usage: core show image formats\n"
186                         "       Displays currently registered image formats (if any).\n";
187                 return NULL;
188         case CLI_GENERATE:
189                 return NULL;
190         }
191         if (a->argc != 4)
192                 return CLI_SHOWUSAGE;
193         ast_cli(a->fd, FORMAT, "Name", "Extensions", "Description", "Format");
194         ast_cli(a->fd, FORMAT, "----", "----------", "-----------", "------");
195         AST_RWLIST_RDLOCK(&imagers);
196         AST_RWLIST_TRAVERSE(&imagers, i, list) {
197                 ast_cli(a->fd, FORMAT2, i->name, i->exts, i->desc, ast_getformatname(&i->format));
198                 count_fmt++;
199         }
200         AST_RWLIST_UNLOCK(&imagers);
201         ast_cli(a->fd, "\n%d image format%s registered.\n", count_fmt, count_fmt == 1 ? "" : "s");
202         return CLI_SUCCESS;
203 }
204
205 static struct ast_cli_entry cli_image[] = {
206         AST_CLI_DEFINE(handle_core_show_image_formats, "Displays image formats")
207 };
208
209 static void image_shutdown(void)
210 {
211         ast_cli_unregister_multiple(cli_image, ARRAY_LEN(cli_image));
212 }
213
214 int ast_image_init(void)
215 {
216         ast_cli_register_multiple(cli_image, ARRAY_LEN(cli_image));
217         ast_register_atexit(image_shutdown);
218         return 0;
219 }