1 /* $Id: w3mhelperpanel.c,v 1.14 2007/05/31 01:19:50 inu Exp $ */
14 /* FIXME: gettextize here */
15 #define MSG_TITLE "³°Éô¥Ó¥å¡¼¥¢¤ÎÊÔ½¸"
16 #define MSG_NEW_ENTRY "¿·µ¬ÅÐÏ¿"
17 #define MSG_TYPE "¥Ç¡¼¥¿¥¿¥¤¥×"
18 #define MSG_COMMAND "³°Éô¥³¥Þ¥ó¥É"
19 #define MSG_REGISTER "ÅÐÏ¿"
20 #define MSG_DELETE "ºï½ü"
21 #define MSG_DOIT "¼Â¹Ô"
22 #else /* LANG != JA */
23 #define MSG_TITLE "External Viewers Setup"
24 #define MSG_NEW_ENTRY "New Entry"
25 #define MSG_TYPE "Type"
26 #define MSG_COMMAND "Command"
27 #define MSG_REGISTER "Register"
28 #define MSG_DELETE "Delete"
29 #define MSG_DOIT "Do it"
30 #endif /* LANG != JA */
35 extractMailcapEntry(char *mcap_entry, char **type, char **cmd)
39 while (*mcap_entry && IS_SPACE(*mcap_entry))
42 mcap_entry[j] && mcap_entry[j] != ';' && !IS_SPACE(mcap_entry[j]);
44 *type = allocStr(mcap_entry, j);
45 if (mcap_entry[j] == ';')
47 while (mcap_entry[j] && IS_SPACE(mcap_entry[j]))
49 *cmd = allocStr(&mcap_entry[j], -1);
53 bye(const char *action, const char *mailcap)
55 printf("Content-Type: text/plain\n\n%s %s\n", action, mailcap);
60 printMailcapPanel(char *mailcap)
66 if ((f = fopen(mailcap, "rt")) == NULL) {
68 bye("Can't open", mailcap);
70 if (!(f = fopen(mailcap, "a+"))) /* if $HOME/.mailcap is not found, make it now! */
71 bye("Can't open", mailcap);
74 char *SysMailcap = getenv("SYS_MAILCAP");
75 FILE *s = fopen(SysMailcap ? SysMailcap : "/etc/mailcap", "r");
78 while (fgets(buffer, sizeof buffer, s)) /* Copy system mailcap to */
79 fputs(buffer, f); /* users' new one */
86 /* FIXME: gettextize here */
87 printf("Content-Type: text/html; charset=EUC-JP\n\n");
89 printf("Content-Type: text/html\n\n");
91 printf("<html>\n<head>\n<title>%s</title>\n</head>\n<body>\n<h1>%s</h1>\n",
92 MSG_TITLE, MSG_TITLE);
93 printf("<form method=post action=\"file:///$LIB/" W3MHELPERPANEL_CMDNAME
95 printf("<input type=hidden name=mode value=edit>\n");
96 printf("<input type=hidden name=cookie value=\"%s\">\n",
97 html_quote(local_cookie));
98 printf("<table>\n<tr><td>%s:<td>%s=<input type=text name=newtype size=40>\n\
99 <tr><td><td>%s=<input type=text name=newcmd size=40>\n\
100 <tr><td><input type=submit name=submit value=\"%s\">\n</table>\n",
101 MSG_NEW_ENTRY, MSG_TYPE, MSG_COMMAND, MSG_REGISTER);
102 printf("<p><hr width=50%%><p>\n<table border='0' cellpadding='0'>\n\
103 <tr><th align=left><b>%s</b><th><b>%s</b>\n",
104 MSG_TYPE, MSG_COMMAND);
105 while (tmp = Strfgets(f), tmp->length > 0) {
106 if (tmp->ptr[0] == '#')
109 extractMailcapEntry(tmp->ptr, &type, &viewer);
110 printf("<tr valign=top><td>%s<td>%s<td nowrap>", html_quote(type),
112 printf("<input type=checkbox name=delete value=\"%s\">%s\n",
113 html_quote(type), MSG_DELETE);
115 printf("</table>\n<input type=submit name=submit value=\"%s\">\n</form>\n\
121 editMailcap(char *mailcap, struct parsed_tagarg *args)
123 TextList *t = newTextList();
128 struct parsed_tagarg *a;
131 if ((f = fopen(mailcap, "rt")) == NULL)
132 bye("Can't open", mailcap);
134 while (tmp = Strfgets(f), tmp->length > 0) {
135 if (tmp->ptr[0] == '#')
138 extractMailcapEntry(tmp->ptr, &type, &viewer);
140 for (a = args; a != NULL; a = a->next) {
141 if (!strcmp(a->arg, "delete") && !strcmp(a->value, type)) {
147 pushText(t, Sprintf("%s;\t%s\n", type, viewer)->ptr);
149 type = tag_get_value(args, "newtype");
150 viewer = tag_get_value(args, "newcmd");
151 if (type != NULL && *type != '\0' && viewer != NULL && *viewer != '\0')
152 pushText(t, Sprintf("%s;\t%s\n", type, viewer)->ptr);
154 if ((f = fopen(mailcap, "w")) == NULL)
155 bye("Can't write to", mailcap);
157 for (ti = t->first; ti != NULL; ti = ti->next)
160 printf("Content-Type: text/plain\n");
161 printf("w3m-control: BACK\nw3m-control: BACK\n");
162 printf("w3m-control: REINIT MAILCAP\n");
166 main(int argc, char *argv[], char **envp)
169 extern char *getenv();
173 struct parsed_tagarg *cgiarg;
178 p = getenv("REQUEST_METHOD");
179 if (p == NULL || strcasecmp(p, "post"))
181 p = getenv("CONTENT_LENGTH");
182 if (p == NULL || (length = atoi(p)) <= 0)
185 qs = Strfgets(stdin);
187 if (qs->length != length)
189 cgiarg = cgistr2tagarg(qs->ptr);
191 p = getenv("LOCAL_COOKIE_FILE");
193 FILE *f = fopen(p, "r");
195 local_cookie = Strfgets(f)->ptr;
199 sent_cookie = tag_get_value(cgiarg, "cookie");
200 if (local_cookie == NULL || sent_cookie == NULL ||
201 strcmp(local_cookie, sent_cookie) != 0) {
202 /* Local cookie doesn't match */
203 bye("Local cookie doesn't match: It may be an illegal execution", "");
206 mode = tag_get_value(cgiarg, "mode");
207 mailcapfile = Strnew_charp(expandPath(USER_MAILCAP));
208 if (mode && !strcmp(mode, "edit")) {
210 /* check if I can edit my mailcap */
211 if ((referer = getenv("HTTP_REFERER")) != NULL) {
212 if (strncmp(referer, "file://", 7) != 0 &&
213 strncmp(referer, "exec://", 7) != 0) {
214 /* referer is not file: nor exec: */
215 bye("It may be an illegal execution\n referer=", referer);
219 editMailcap(mailcapfile->ptr, cgiarg);
223 printMailcapPanel(mailcapfile->ptr);
228 bye("Incomplete Request:", qs ? qs->ptr : "(null)");