1 /*
2 ** Copyright (c) 2007 D. Richard Hipp
3 **
4 ** This program is free software; you can redistribute it and/or
5 ** modify it under the terms of the Simplified BSD License (also
6 ** known as the "2-Clause License" or "FreeBSD License".)
7 
8 ** This program is distributed in the hope that it will be useful,
9 ** but without any warranty; without even the implied warranty of
10 ** merchantability or fitness for a particular purpose.
11 **
12 ** Author contact information:
13 **   drh@hwaci.com
14 **   http://www.hwaci.com/drh/
15 **
16 *******************************************************************************
17 **
18 ** This file contains code to implement the transfer configuration
19 ** setup screens.
20 */
21 #include "config.h"
22 #include "xfersetup.h"
23 #include <assert.h>
24 
25 /*
26 ** WEBPAGE: xfersetup
27 ** Main sub-menu for configuring the transfer system.
28 */
xfersetup_page(void)29 void xfersetup_page(void){
30   login_check_credentials();
31   if( !g.perm.Setup ){
32     login_needed(0);
33     return;
34   }
35 
36   style_header("Transfer Setup");
37 
38   @ <table class="xfersetup">
39   setup_menu_entry("Common", "xfersetup_com",
40     "Common TH1 code run before all transfer request processing.");
41   setup_menu_entry("Push", "xfersetup_push",
42     "Specific TH1 code to run after \"push\" transfer requests.");
43   setup_menu_entry("Commit", "xfersetup_commit",
44     "Specific TH1 code to run after processing a commit.");
45   setup_menu_entry("Ticket", "xfersetup_ticket",
46     "Specific TH1 code to run after processing a ticket change.");
47   @ </table>
48 
49   url_parse(0, 0);
50   if( g.url.protocol ){
51     unsigned syncFlags;
52     const char *zButton;
53     char *zWarning;
54 
55     if( db_get_boolean("dont-push", 0) ){
56       syncFlags = SYNC_PULL;
57       zButton = "Pull";
58       zWarning = 0;
59     }else{
60       syncFlags = SYNC_PUSH | SYNC_PULL;
61       zButton = "Synchronize";
62       zWarning = mprintf("WARNING: Pushing to \"%s\" is enabled.",
63                          g.url.canonical);
64     }
65     @ <p>Press the <strong>%h(zButton)</strong> button below to
66     @ synchronize with the <em>%h(g.url.canonical)</em> repository now.<br />
67     @ This may be useful when testing the various transfer scripts.</p>
68     @ <p>You can use the <code>http -async</code> command in your scripts, but
69     @ make sure the <code>th1-uri-regexp</code> setting is set first.</p>
70     if( zWarning ){
71       @
72       @ <big><b>%h(zWarning)</b></big>
73       free(zWarning);
74     }
75     @
76     @ <form method="post" action="%R/%s(g.zPath)"><div>
77     login_insert_csrf_secret();
78     @ <input type="submit" name="sync" value="%h(zButton)" />
79     @ </div></form>
80     @
81     if( P("sync") ){
82       user_select();
83       url_enable_proxy(0);
84       @ <pre class="xfersetup">
85       client_sync(syncFlags, 0, 0, 0);
86       @ </pre>
87     }
88   }
89 
90   style_finish_page();
91 }
92 
93 /*
94 ** Common implementation for the transfer setup editor pages.
95 */
xfersetup_generic(const char * zTitle,const char * zDbField,const char * zDfltValue,const char * zDesc,char * (* xText)(const char *),void (* xRebuild)(void),int height)96 static void xfersetup_generic(
97   const char *zTitle,           /* Page title */
98   const char *zDbField,         /* Configuration field being edited */
99   const char *zDfltValue,       /* Default text value */
100   const char *zDesc,            /* Description of this field */
101   char *(*xText)(const char*),  /* Validity test or NULL */
102   void (*xRebuild)(void),       /* Run after successful update */
103   int height                    /* Height of the edit box */
104 ){
105   const char *z;
106   int isSubmit;
107 
108   login_check_credentials();
109   if( !g.perm.Setup ){
110     login_needed(0);
111     return;
112   }
113   if( P("setup") ){
114     cgi_redirect("xfersetup");
115   }
116   isSubmit = P("submit")!=0;
117   z = P("x");
118   if( z==0 ){
119     z = db_get(zDbField, zDfltValue);
120   }
121   style_set_current_feature("xfersetup");
122   style_header("Edit %s", zTitle);
123   if( P("clear")!=0 ){
124     login_verify_csrf_secret();
125     db_unset(zDbField/*works-like:"x"*/, 0);
126     if( xRebuild ) xRebuild();
127     z = zDfltValue;
128   }else if( isSubmit ){
129     char *zErr = 0;
130     login_verify_csrf_secret();
131     if( xText && (zErr = xText(z))!=0 ){
132       @ <p class="xfersetupError">ERROR: %h(zErr)</p>
133     }else{
134       db_set(zDbField/*works-like:"x"*/, z, 0);
135       if( xRebuild ) xRebuild();
136       cgi_redirect("xfersetup");
137     }
138   }
139   @ <form action="%R/%s(g.zPath)" method="post"><div>
140   login_insert_csrf_secret();
141   @ <p>%s(zDesc)</p>
142   @ <textarea name="x" rows="%d(height)" cols="80">%h(z)</textarea>
143   @ <p>
144   @ <input type="submit" name="submit" value="Apply Changes" />
145   @ <input type="submit" name="clear" value="Revert To Default" />
146   @ <input type="submit" name="setup" value="Cancel" />
147   @ </p>
148   @ </div></form>
149   if ( zDfltValue ){
150     @ <hr />
151     @ <h2>Default %s(zTitle)</h2>
152     @ <blockquote><pre>
153     @ %h(zDfltValue)
154     @ </pre></blockquote>
155   }
156   style_finish_page();
157 }
158 
159 static const char *zDefaultXferCommon = 0;
160 
161 /*
162 ** WEBPAGE: xfersetup_com
163 ** View or edit the TH1 script that runs prior to receiving a
164 ** transfer.
165 */
xfersetup_com_page(void)166 void xfersetup_com_page(void){
167   static const char zDesc[] =
168   @ Enter TH1 script that initializes variables prior to running
169   @ any of the transfer request scripts.
170   ;
171   xfersetup_generic(
172     "Transfer Common Script",
173     "xfer-common-script",
174     zDefaultXferCommon,
175     zDesc,
176     0,
177     0,
178     30
179   );
180 }
181 
182 static const char *zDefaultXferPush = 0;
183 
184 /*
185 ** WEBPAGE: xfersetup_push
186 ** View or edit the TH1 script that runs after receiving a "push".
187 */
xfersetup_push_page(void)188 void xfersetup_push_page(void){
189   static const char zDesc[] =
190   @ Enter TH1 script that runs after processing <strong>push</strong>
191   @ transfer requests.
192   ;
193   xfersetup_generic(
194     "Transfer Push Script",
195     "xfer-push-script",
196     zDefaultXferPush,
197     zDesc,
198     0,
199     0,
200     30
201   );
202 }
203 
204 static const char *zDefaultXferCommit = 0;
205 
206 /*
207 ** WEBPAGE: xfersetup_commit
208 ** View or edit the TH1 script that runs when a transfer commit
209 ** is processed.
210 */
xfersetup_commit_page(void)211 void xfersetup_commit_page(void){
212   static const char zDesc[] =
213   @ Enter TH1 script that runs when a commit is processed.
214   ;
215   xfersetup_generic(
216     "Transfer Commit Script",
217     "xfer-commit-script",
218     zDefaultXferCommit,
219     zDesc,
220     0,
221     0,
222     30
223   );
224 }
225 
226 static const char *zDefaultXferTicket = 0;
227 
228 /*
229 ** WEBPAGE: xfersetup_ticket
230 ** View or edit the TH1 script that runs when a ticket change artifact
231 ** is processed during a transfer.
232 */
xfersetup_ticket_page(void)233 void xfersetup_ticket_page(void){
234   static const char zDesc[] =
235   @ Enter TH1 script that runs when a ticket change is processed.
236   ;
237   xfersetup_generic(
238     "Transfer Ticket Script",
239     "xfer-ticket-script",
240     zDefaultXferTicket,
241     zDesc,
242     0,
243     0,
244     30
245   );
246 }
247