about summary refs log tree commit diff stats
path: root/client.c
Commit message (Expand)AuthorAgeFilesLines
* uppercasing all define'd values (uppercase-prefixed should only be enum field...arg@10ksloc.org2006-08-011-3/+3
* applied Sanders patchesarg@10ksloc.org2006-08-011-3/+3
* committed a patch which fixes the hints of Jukkaarg@10ksloc.org2006-08-011-34/+8
* sanitization of several clunky stuff, removed heretag (rarely of use), simpli...arg@10ksloc.org2006-07-211-2/+15
* applied sanders no_sizehints for tiled mode patch (thx!)arg@10ksloc.org2006-07-201-10/+10
* serious mistake in pop() (forgot to set c->prev to NULL on pop)arg@10ksloc.org2006-07-201-0/+1
* using double-linked list in order to get correct prev focus handlingarg@10ksloc.org2006-07-201-15/+31
* made status bar drawing more robust, implemented togglemax and togglemode, wo...arg@10ksloc.org2006-07-201-15/+38
* cleaned up codearg@10ksloc.org2006-07-201-6/+10
* removed c->f{x,y,w,h} and c->t{x,y,w,h} in favor for the new rule handling re...arg@10ksloc.org2006-07-201-76/+52
* implemented regexp matching for rulesarg@10ksloc.org2006-07-191-2/+3
* applied Jukka's patch with s/ModKeyMask/MODKEY/garg@10ksloc.org2006-07-191-3/+3
* fixed the bug mentioned by Sanderarg@10ksloc.org2006-07-191-3/+7
* refactored Sanders code somewhatarg@10ksloc.org2006-07-191-2/+2
* implemented fallback for too many clients in stacked modearg@10ksloc.org2006-07-191-2/+3
* and another fix...arg@10ksloc.org2006-07-191-1/+1
* yet another typo fixarg@10ksloc.org2006-07-191-2/+2
* fixed a typoarg@10ksloc.org2006-07-191-1/+1
* floating clients get random (x,y) offsets nowarg@10ksloc.org2006-07-191-0/+7
* applied Sanders resize patch, fixed lower bugarg@10ksloc.org2006-07-191-2/+8
* implemened distinguishing float/managed geometries of clients (works quite well)Anselm R. Garbe2006-07-181-48/+68
* pop on heretagAnselm R. Garbe2006-07-181-7/+14
* added heretag command which allows to tag a client of a foreign tag with curr...Anselm R. Garbe2006-07-181-4/+4
* another XSyncAnselm R. Garbe2006-07-161-0/+1
* several additions in mouse handling ;)Anselm R. Garbe2006-07-161-6/+7
* fixed XSync handling and finished man pageAnselm R. Garbe2006-07-151-4/+3
* sanitized other stuffAnselm R. Garbe2006-07-151-2/+2
* proceeded with cleaning up, sorting functions, etcAnselm R. Garbe2006-07-151-205/+206
* rearranged several stuffAnselm R. Garbe2006-07-151-58/+87
* sanitized namesAnselm R. Garbe2006-07-141-14/+14
* rearrangedAnselm R. Garbe2006-07-141-227/+3
* bar shows if currently is tiled (Mod1-space) or floating (Mod1-Shift-space) modeAnselm R. Garbe2006-07-141-0/+2
* fixed crash on zoom if only 1 client existsAnselm R. Garbe2006-07-141-3/+9
* removed a bunch of lines through swap removalAnselm R. Garbe2006-07-141-2/+2
* searching for a better way to discard enter notifiesAnselm R. Garbe2006-07-141-13/+11
* focus on view change as wellAnselm R. Garbe2006-07-141-0/+3
* made stdin reader more robustAnselm R. Garbe2006-07-141-4/+9
* implemented bar for dwm (I miss status text), I plan that status text is read...Anselm R. Garbe2006-07-141-12/+18
* continued with man pageAnselm R. Garbe2006-07-141-1/+1
* except improvements to the mouse handling this is already nearly feature comp...Anselm R. Garbe2006-07-131-0/+5
* added mini stuffAnselm R. Garbe2006-07-131-20/+31
* fixed several things, nearly feature completeAnselm R. Garbe2006-07-131-25/+30
* new stuffAnselm R. Garbe2006-07-131-44/+78
* several other additions/fixes, dwm is quite usable alreadyAnselm R. Garbe2006-07-131-95/+140
* implemented tagging a clientAnselm R. Garbe2006-07-131-57/+113
* added xlock command (I need it regularly)Anselm R. Garbe2006-07-131-20/+10
* added pointer warps on kb-driven/manage-driven focusAnselm R. Garbe2006-07-131-0/+3
* changed back to urxvt, dwm has no problems with it, because it doesn't use fr...Anselm R. Garbe2006-07-131-1/+1
* changed default colorsAnselm R. Garbe2006-07-131-7/+8
* added logo+descriptionAnselm R. Garbe2006-07-131-22/+33
f='/andinus/cetus/blame/cmd/cetus/main.go?id=2f15edf548ec131d2eb97bd5338a1adef768acea'>^
b6a5878 ^

052140f ^
b6a5878 ^



052140f ^
b6a5878 ^

052140f ^
b6a5878 ^




052140f ^
b6a5878 ^













052140f ^
b6a5878 ^




052140f ^
052140f ^
b6a5878 ^

052140f ^







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254











                                                   

 















                            










                                                                      

                           
                                    
                                                 















                                                                   






                                                                                                 



                                                            




                                                                                               






















                                                                      


                                     

                                          


                                     




                                                               
 
 
                   
                                                                   







                                                                
 














                                                              
 

                                          
 



                                
 


                                            
 





                                                          
 













                                                              
 




                                                             

         






                                                             
 












                                                              
 

                                          
 



                                
 

                                            
 




                                                                      
 













                                                              
 




                                                             
         
 

                                     







                                
package main

import (
	"flag"
	"fmt"
	"math/rand"
	"os"
	"time"

	"framagit.org/andinus/cetus/pkg/apod"
	"framagit.org/andinus/cetus/pkg/background"
	"framagit.org/andinus/cetus/pkg/bpod"
)

var (
	apodAPI      *string
	apodKey      *string
	apodDate     *string
	apodRand     *bool
	apodPathOnly *bool
	apodQuiet    *bool
	apodDump     *bool

	bpodAPI      *string
	bpodRand     *bool
	bpodPathOnly *bool
	bpodQuiet    *bool
	bpodDump     *bool
)

func main() {
	// Early Check: If command was not passed then print usage and
	// exit. Later command & service both are checked, this check
	// if for version command. If not checked then running cetus
	// without any args will fail because os.Args[1] will panic
	// the program & produce runtime error.
	if len(os.Args) == 1 {
		printUsage()
		os.Exit(1)
	}

	version := "v0.5.0"

	if os.Args[1] == "version" {
		fmt.Printf("Cetus %s\n", version)
		os.Exit(0)
	}

	// If command & service was not passed then print usage and
	// exit.
	if len(os.Args) < 3 {
		printUsage()
		os.Exit(1)
	}

	rand.Seed(time.Now().Unix())

	apodCmd := flag.NewFlagSet("apod", flag.ExitOnError)
	defDate := time.Now().UTC().Format("2006-01-02")

	// Flags to parse for apod service.
	apodAPI = apodCmd.String("api", "https://api.nasa.gov/planetary/apod", "APOD API link")
	apodKey = apodCmd.String("api-key", "DEMO_KEY", "NASA API Key for expanded usage")
	apodDate = apodCmd.String("date", defDate, "Date of NASA APOD to retrieve")
	apodRand = apodCmd.Bool("random", false, "Choose a date random starting from 1995-06-16")
	apodPathOnly = apodCmd.Bool("path-only", false, "Print only the path")
	apodQuiet = apodCmd.Bool("quiet", false, "Stay quiet")
	apodDump = apodCmd.Bool("dump", false, "Dump received response")

	bpodCmd := flag.NewFlagSet("bpod", flag.ExitOnError)

	// Flags to parse for bpod service.
	bpodAPI = bpodCmd.String("api", "https://www.bing.com/HPImageArchive.aspx", "BPOD API")
	bpodRand = bpodCmd.Bool("random", false, "Choose a random image from last week's BPOD")
	bpodPathOnly = bpodCmd.Bool("path-only", false, "Print only the path")
	bpodQuiet = bpodCmd.Bool("quiet", false, "Stay quiet")
	bpodDump = bpodCmd.Bool("dump", false, "Dump received response")

	// Switching on commands will cause more repetition than
	// switching on service. If we switch on commands then switch
	// on service will have to be replicated on every command
	// switch. Reverse is also true, this way we will repeat
	// command switch in every service but we do so in a better
	// way.
	//
	// However we check if the correct command was passed. version
	// command is not included because it has been dealt with
	// earlier in the program & the program should've exited after
	// that, if it reaches here then it's an error.
	switch os.Args[1] {
	case "set", "fetch":
	default:
		fmt.Printf("Invalid command: %q\n", os.Args[1])
		printUsage()
		os.Exit(1)
	}

	switch os.Args[2] {
	case "apod", "nasa":
		apodCmd.Parse(os.Args[3:])
		if apodCmd.Parsed() {
			execAPOD()
		}
	case "bpod", "bing":
		bpodCmd.Parse(os.Args[3:])
		if bpodCmd.Parsed() {
			execBPOD()
		}
	default:
		fmt.Printf("Invalid service: %q\n", os.Args[2])
		printUsage()
		os.Exit(1)
	}
}

func printUsage() {
	fmt.Println("Usage: cetus <command> <service> [<flags>]\n")
	fmt.Println("Commands: ")
	fmt.Println(" set   Set the latest image as background")
	fmt.Println(" fetch Fetch the latest image information")
	fmt.Println(" version Print version")
	fmt.Println("\nServices: ")
	fmt.Println(" apod   NASA Astronomy Picture of the Day")
	fmt.Println(" bpod   Bing Photo of the Day")
}

func execAPOD() {
	// reqInfo holds all the parameters that needs to be
	// sent with the request. GetJson() will pack apiKey &
	// date in params map before sending it to another
	// function. Adding params here will not change the
	// behaviour of the function, changes have to be made
	// in GetJson() too.
	reqInfo := make(map[string]string)
	reqInfo["api"] = string(*apodAPI)
	reqInfo["apiKey"] = string(*apodKey)
	reqInfo["date"] = string(*apodDate)

	if *apodRand {
		reqInfo["apiKey"] = apod.RandDate()
	}

	body, err := apod.GetJson(reqInfo)
	chkErr(err)

	if *apodDump {
		fmt.Printf(body)
		os.Exit(0)
	}

	res := apod.Res{}
	err = apod.UnmarshalJson(&res, body)
	chkErr(err)

	// res.Msg will be returned when there is error on
	// user input or the api server.
	if len(res.Msg) != 0 {
		fmt.Printf("Message: %s", res.Msg)
		os.Exit(1)
	}

	// If path-only is passed then it will only print the
	// path, even if quiet is passed. If the user wants
	// the program to be quiet then path-only shouldn't be
	// passed. If path-only is not passed & quiet is also
	// not passed then print the response.
	//
	// Path is only printed when the media type is an
	// image because res.HDURL is empty on non image media
	// type.
	if *apodPathOnly {
		fmt.Println(res.HDURL)
	} else if !*apodQuiet {
		apod.Print(res)
	}

	// Proceed only if the command was set because if it
	// was fetch then it's already finished & should exit
	// now.
	if os.Args[1] == "fetch" {
		os.Exit(0)
	}

	// Try to set background only if the media type is an
	// image.
	if res.MediaType == "image" {
		err = background.Set(res.HDURL)
		chkErr(err)
	}
}

func execBPOD() {
	// reqInfo holds all the parameters that needs to be
	// sent with the request. GetJson() will pack apiKey &
	// date in params map before sending it to another
	// function. Adding params here will not change the
	// behaviour of the function, changes have to be made
	// in GetJson() too.
	reqInfo := make(map[string]string)
	reqInfo["api"] = string(*bpodAPI)

	if *bpodRand {
		reqInfo["random"] = "true"
	}

	body, err := bpod.GetJson(reqInfo)
	chkErr(err)

	if *bpodDump {
		fmt.Printf(body)
		os.Exit(0)
	}

	res, err := bpod.UnmarshalJson(body)
	chkErr(err)

	// Correct format
	res.Url = fmt.Sprintf("%s%s", "https://www.bing.com", res.Url)
	dt, err := time.Parse("20060102", res.StartDate)
	chkErr(err)
	res.StartDate = dt.Format("2006-01-02")

	// If path-only is passed then it will only print the
	// path, even if quiet is passed. If the user wants
	// the program to be quiet then path-only shouldn't be
	// passed. If path-only is not passed & quiet is also
	// not passed then print the response.
	//
	// Path is only printed when the media type is an
	// image because res.HDURL is empty on non image media
	// type.
	if *bpodPathOnly {
		fmt.Println(res.Url)
	} else if !*bpodQuiet {
		bpod.Print(res)
	}

	// Proceed only if the command was set because if it
	// was fetch then it's already finished & should exit
	// now.
	if os.Args[1] == "fetch" {
		os.Exit(0)
	}

	err = background.Set(res.Url)
	chkErr(err)
}

func chkErr(err error) {
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
}