use string fields for some stuff in ast_channel
[asterisk/asterisk.git] / 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 <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <sys/time.h>
30 #include <sys/stat.h>
31 #include <signal.h>
32 #include <errno.h>
33 #include <unistd.h>
34
35 #include "asterisk.h"
36
37 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
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 static AST_LIST_HEAD_STATIC(imagers, ast_imager);
50
51 int ast_image_register(struct ast_imager *img)
52 {
53         if (option_verbose > 1)
54                 ast_verbose(VERBOSE_PREFIX_2 "Registered format '%s' (%s)\n", img->name, img->desc);
55         AST_LIST_LOCK(&imagers);
56         AST_LIST_INSERT_HEAD(&imagers, img, list);
57         AST_LIST_UNLOCK(&imagers);
58         return 0;
59 }
60
61 void ast_image_unregister(struct ast_imager *img)
62 {
63         struct ast_imager *i;
64         
65         AST_LIST_LOCK(&imagers);
66         AST_LIST_TRAVERSE_SAFE_BEGIN(&imagers, i, list) {       
67                 if (i == img) {
68                         AST_LIST_REMOVE_CURRENT(&imagers, list);
69                         break;
70                 }
71         }
72         AST_LIST_TRAVERSE_SAFE_END
73         AST_LIST_UNLOCK(&imagers);
74         if (i && (option_verbose > 1))
75                 ast_verbose(VERBOSE_PREFIX_2 "Unregistered format '%s' (%s)\n", img->name, img->desc);
76 }
77
78 int ast_supports_images(struct ast_channel *chan)
79 {
80         if (!chan || !chan->tech)
81                 return 0;
82         if (!chan->tech->send_image)
83                 return 0;
84         return 1;
85 }
86
87 static int file_exists(char *filename)
88 {
89         int res;
90         struct stat st;
91         res = stat(filename, &st);
92         if (!res)
93                 return st.st_size;
94         return 0;
95 }
96
97 static void make_filename(char *buf, int len, char *filename, const char *preflang, char *ext)
98 {
99         if (filename[0] == '/') {
100                 if (preflang && strlen(preflang))
101                         snprintf(buf, len, "%s-%s.%s", filename, preflang, ext);
102                 else
103                         snprintf(buf, len, "%s.%s", filename, ext);
104         } else {
105                 if (preflang && strlen(preflang))
106                         snprintf(buf, len, "%s/%s/%s-%s.%s", ast_config_AST_VAR_DIR, "images", filename, preflang, ext);
107                 else
108                         snprintf(buf, len, "%s/%s/%s.%s", ast_config_AST_VAR_DIR, "images", filename, ext);
109         }
110 }
111
112 struct ast_frame *ast_read_image(char *filename, const char *preflang, int format)
113 {
114         struct ast_imager *i;
115         char buf[256];
116         char tmp[80];
117         char *e;
118         struct ast_imager *found = NULL;
119         int fd;
120         int len=0;
121         struct ast_frame *f = NULL;
122         
123         AST_LIST_LOCK(&imagers);
124         AST_LIST_TRAVERSE(&imagers, i, list) {
125                 if (i->format & format) {
126                         char *stringp=NULL;
127                         strncpy(tmp, i->exts, sizeof(tmp)-1);
128                         stringp=tmp;
129                         e = strsep(&stringp, "|");
130                         while(e) {
131                                 make_filename(buf, sizeof(buf), filename, preflang, e);
132                                 if ((len = file_exists(buf))) {
133                                         found = i;
134                                         break;
135                                 }
136                                 make_filename(buf, sizeof(buf), filename, NULL, e);
137                                 if ((len = file_exists(buf))) {
138                                         found = i;
139                                         break;
140                                 }
141                                 e = strsep(&stringp, "|");
142                         }
143                 }
144                 if (found)
145                         break;  
146         }
147
148         if (found) {
149                 fd = open(buf, O_RDONLY);
150                 if (fd > -1) {
151                         if (!found->identify || found->identify(fd)) {
152                                 /* Reset file pointer */
153                                 lseek(fd, 0, SEEK_SET);
154                                 f = found->read_image(fd,len); 
155                         } else
156                                 ast_log(LOG_WARNING, "%s does not appear to be a %s file\n", buf, i->name);
157                         close(fd);
158                 } else
159                         ast_log(LOG_WARNING, "Unable to open '%s': %s\n", buf, strerror(errno));
160         } else
161                 ast_log(LOG_WARNING, "Image file '%s' not found\n", filename);
162         
163         AST_LIST_UNLOCK(&imagers);
164         
165         return f;
166 }
167
168
169 int ast_send_image(struct ast_channel *chan, char *filename)
170 {
171         struct ast_frame *f;
172         int res = -1;
173         if (chan->tech->send_image) {
174                 f = ast_read_image(filename, chan->language, -1);
175                 if (f) {
176                         res = chan->tech->send_image(chan, f);
177                         ast_frfree(f);
178                 }
179         }
180         return res;
181 }
182
183 static int show_image_formats(int fd, int argc, char *argv[])
184 {
185 #define FORMAT "%10s %10s %50s %10s\n"
186 #define FORMAT2 "%10s %10s %50s %10s\n"
187         struct ast_imager *i;
188         if (argc != 3)
189                 return RESULT_SHOWUSAGE;
190         ast_cli(fd, FORMAT, "Name", "Extensions", "Description", "Format");
191         AST_LIST_TRAVERSE(&imagers, i, list)
192                 ast_cli(fd, FORMAT2, i->name, i->exts, i->desc, ast_getformatname(i->format));
193         return RESULT_SUCCESS;
194 }
195
196 struct ast_cli_entry show_images =
197 {
198         { "show", "image", "formats" },
199         show_image_formats,
200         "Displays image formats",
201 "Usage: show image formats\n"
202 "       displays currently registered image formats (if any)\n"
203 };
204
205
206 int ast_image_init(void)
207 {
208         ast_cli_register(&show_images);
209         return 0;
210 }
211