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