update doxygen docs to specify authors
[asterisk/asterisk.git] / image.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2005, 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 struct ast_imager *list;
50 AST_MUTEX_DEFINE_STATIC(listlock);
51
52 int ast_image_register(struct ast_imager *img)
53 {
54         if (option_verbose > 1)
55                 ast_verbose(VERBOSE_PREFIX_2 "Registered format '%s' (%s)\n", img->name, img->desc);
56         ast_mutex_lock(&listlock);
57         img->next = list;
58         list = img;
59         ast_mutex_unlock(&listlock);
60         return 0;
61 }
62
63 void ast_image_unregister(struct ast_imager *img)
64 {
65         struct ast_imager *i, *prev = NULL;
66         ast_mutex_lock(&listlock);
67         i = list;
68         while(i) {
69                 if (i == img) {
70                         if (prev) 
71                                 prev->next = i->next;
72                         else
73                                 list = i->next;
74                         break;
75                 }
76                 prev = i;
77                 i = i->next;
78         }
79         ast_mutex_unlock(&listlock);
80         if (i && (option_verbose > 1))
81                 ast_verbose(VERBOSE_PREFIX_2 "Unregistered format '%s' (%s)\n", img->name, img->desc);
82 }
83
84 int ast_supports_images(struct ast_channel *chan)
85 {
86         if (!chan || !chan->tech)
87                 return 0;
88         if (!chan->tech->send_image)
89                 return 0;
90         return 1;
91 }
92
93 static int file_exists(char *filename)
94 {
95         int res;
96         struct stat st;
97         res = stat(filename, &st);
98         if (!res)
99                 return st.st_size;
100         return 0;
101 }
102
103 static void make_filename(char *buf, int len, char *filename, char *preflang, char *ext)
104 {
105         if (filename[0] == '/') {
106                 if (preflang && strlen(preflang))
107                         snprintf(buf, len, "%s-%s.%s", filename, preflang, ext);
108                 else
109                         snprintf(buf, len, "%s.%s", filename, ext);
110         } else {
111                 if (preflang && strlen(preflang))
112                         snprintf(buf, len, "%s/%s/%s-%s.%s", ast_config_AST_VAR_DIR, "images", filename, preflang, ext);
113                 else
114                         snprintf(buf, len, "%s/%s/%s.%s", ast_config_AST_VAR_DIR, "images", filename, ext);
115         }
116 }
117
118 struct ast_frame *ast_read_image(char *filename, char *preflang, int format)
119 {
120         struct ast_imager *i;
121         char buf[256];
122         char tmp[80];
123         char *e;
124         struct ast_imager *found = NULL;
125         int fd;
126         int len=0;
127         struct ast_frame *f = NULL;
128 #if 0 /* We need to have some sort of read-only lock */
129         ast_mutex_lock(&listlock);
130 #endif  
131         i = list;
132         while(!found && i) {
133                 if (i->format & format) {
134                         char *stringp=NULL;
135                         strncpy(tmp, i->exts, sizeof(tmp)-1);
136                         stringp=tmp;
137                         e = strsep(&stringp, "|");
138                         while(e) {
139                                 make_filename(buf, sizeof(buf), filename, preflang, e);
140                                 if ((len = file_exists(buf))) {
141                                         found = i;
142                                         break;
143                                 }
144                                 make_filename(buf, sizeof(buf), filename, NULL, e);
145                                 if ((len = file_exists(buf))) {
146                                         found = i;
147                                         break;
148                                 }
149                                 e = strsep(&stringp, "|");
150                         }
151                 }
152                 i = i->next;
153         }
154         if (found) {
155                 fd = open(buf, O_RDONLY);
156                 if (fd > -1) {
157                         if (!found->identify || found->identify(fd)) {
158                                 /* Reset file pointer */
159                                 lseek(fd, 0, SEEK_SET);
160                                 f = found->read_image(fd,len); 
161                         } else
162                                 ast_log(LOG_WARNING, "%s does not appear to be a %s file\n", buf, i->name);
163                         close(fd);
164                 } else
165                         ast_log(LOG_WARNING, "Unable to open '%s': %s\n", buf, strerror(errno));
166         } else
167                 ast_log(LOG_WARNING, "Image file '%s' not found\n", filename);
168 #if 0
169         ast_mutex_unlock(&listlock);
170 #endif  
171         return f;
172 }
173
174
175 int ast_send_image(struct ast_channel *chan, char *filename)
176 {
177         struct ast_frame *f;
178         int res = -1;
179         if (chan->tech->send_image) {
180                 f = ast_read_image(filename, chan->language, -1);
181                 if (f) {
182                         res = chan->tech->send_image(chan, f);
183                         ast_frfree(f);
184                 }
185         }
186         return res;
187 }
188
189 static int show_image_formats(int fd, int argc, char *argv[])
190 {
191 #define FORMAT "%10s %10s %50s %10s\n"
192 #define FORMAT2 "%10s %10s %50s %10s\n"
193         struct ast_imager *i;
194         if (argc != 3)
195                 return RESULT_SHOWUSAGE;
196         ast_cli(fd, FORMAT, "Name", "Extensions", "Description", "Format");
197         i = list;
198         while(i) {
199                 ast_cli(fd, FORMAT2, i->name, i->exts, i->desc, ast_getformatname(i->format));
200                 i = i->next;
201         };
202         return RESULT_SUCCESS;
203 }
204
205 struct ast_cli_entry show_images =
206 {
207         { "show", "image", "formats" },
208         show_image_formats,
209         "Displays image formats",
210 "Usage: show image formats\n"
211 "       displays currently registered image formats (if any)\n"
212 };
213
214
215 int ast_image_init(void)
216 {
217         ast_cli_register(&show_images);
218         return 0;
219 }
220