-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathweather.c
More file actions
265 lines (231 loc) · 5.58 KB
/
weather.c
File metadata and controls
265 lines (231 loc) · 5.58 KB
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
255
256
257
258
259
260
261
262
263
264
265
/*
* weather - fetching weather stats from an GMXX-code
* Copyright © 2013 - Niels Neumann <vatriani.nn@googlemail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//#define DEBUG
//#define DBASE
#define BUFFER_SIZE 1024
#define APP_NAME "weather-cli"
#define APP_VERSION "1.5"
#define APP_HELP " [OPTION]\n\n\
-s\t\tshort output\n\
-f\t\tFORMAT\n\
-l\t\tlinking image\n\n\
-v\t\tversion\n\
-h\t\thelp"
#define SHARED_FOLDER "/usr/share/weather-cli"
#ifdef DBASE
#include "db.h"
#endif
#include <string.h>
#include <malloc.h>
#include <stdlib.h>
#include <getopt.h>
#include <sys/param.h>
#include <unistd.h>
#include "output.h"
#include "tcp.h"
#include "file.h"
#include "string.h"
#define BIT1 1 // -s small output
#define BIT2 2 // -f format support
#define BIT3 4 // -l symlink image
#define BIT4 8
#define BIT5 16
#define BIT6 32
#define BIT7 64
#define BIT8 128
#define HTTP_HEADER_OFFESET 2
static char *bez[] = {
"longitude",
"latitude",
"country",
"region",
"city",
"humidity",
"visibility",
"pressure",
"rising",
"sunrise",
"sunset",
"current code",
"current date",
"current temp",
"current temp low",
"current temp high",
"current text",
"current image",
"speed",
"direction",
"chill",
"tomorrow day",
"tomorrow date",
"tomorrow temp low",
"tomorrow temp high",
"tomorrow text",
"tomorrow code",
"tomorrow image"
};
static char *format[] = {
"%lo",
"%la",
"%lR",
"%lr",
"%lc",
"%hu",
"%vi",
"%pr",
"%ri",
"%sr",
"%ss",
"%cc",
"%cd",
"%ct",
"%ctl",
"%cth",
"%cm",
"%ci",
"%cws",
"%cwd",
"%cwc",
"%td",
"%tD",
"%ttl",
"%tth",
"%tT",
"%tc",
"%ti"
};
void parsing_infos(char **,char *);
/**
* fetching infos of buffer and copying it into array
*/
void parsing_infos(char **weather,char *input_buffer) {
char *line[80];
unsigned int i = 0;
unsigned short int counter = 0;
line[0] = strtok(input_buffer, "=");
while(line[i]) {
line[++i] = strtok(NULL, "=");
strmdelsp(line[i]);
}
for(; counter<28; ++counter) {
weather[counter] = malloc(strlen(line[HTTP_HEADER_OFFESET+counter])*sizeof(char));
memcpy(weather[counter], line[HTTP_HEADER_OFFESET+counter], strlen(line[HTTP_HEADER_OFFESET+counter])+1);
}
}
/**
* symlinking new icon to actual.gif in .cache/weather/
*/
void symlinking_image(char *image) {
char *filename_new = SHARED_FOLDER;
char *filename_old = 0;
getCachePath(&filename_old, APP_NAME);
strmcat(&filename_old, "actual.gif");
strmcat(&filename_new, "/%1.gif");
strmreplace(&filename_new, "%1", image);
unlink(filename_old);
if(symlink(filename_new, filename_old) == -1)
outerr("Error symlinking");
freeChar(&filename_new);
freeChar(&filename_old);
}
/**
* generate output
*/
void gen_output(char **weather, unsigned short int *flags, char **format_code) {
char *buffer = NULL;
unsigned short int counter;
if(*flags&BIT3)
symlinking_image(weather[11]);
if(*flags&BIT2)
strmcat(&buffer,*format_code);
else if(*flags&BIT1) { // short
strmcat(format_code,"%lc\n%cm, %ctC°\nWind: %cws, %cwd°\n\nForecats:\n%tT\n\
Temp: min %ttlC°, max %tthC°\n");
strmcat(&buffer,*format_code);
} else { // std long output
for(counter=0; counter<28; ++counter) {
char *tmp="%1 : %2\n";
strmreplace(&tmp,"%1",bez[counter]);
strmreplace(&tmp,"%2",format[counter]);
strmcat(&buffer,tmp);
free(tmp);
}
}
// inserting Weatherstats
for(counter = 0; counter<28; ++counter)
strmreplace(&buffer,format[counter],weather[counter]);
outplain(buffer);
free(buffer);
}
int main(int argc,char **argv) {
SOCKET sock; // vars for server communication
BUFFER buffer;
char *gmxxcode = NULL; // vars for optionhandling
char *server_name;
char *format_code = NULL;
unsigned short int flags = 0;
int opt;
char *weather[28]; // weather array witch stores the data
while((opt=getopt(argc,argv,"f:slvh")) != -1) {
switch(opt) {
case 'f':
strmcat(&format_code,optarg);flags|=BIT2;break;
case 's':
flags|=BIT1;break;
case 'l':
flags|=BIT3;break;
case 'v':
showVersion(APP_NAME,APP_VERSION);exit(0);
case 'h':
default:
showHelp(APP_NAME,APP_HELP);
exit(0);
}
}
// handling gmxx codes passes throug params or reading from db
if(optind<=argc && argc!=1) {
if((strncmp("GMXX",argv[optind],4)==0 || strncmp("gmxx",argv[optind],4)==0)
&& strlen(argv[optind])==8) {
strmcat(&gmxxcode,argv[optind]);
}
#ifdef DBASE
else {
gmxxcode=malloc(8*sizeof(char));
if(getGmxx(argv[optind],gmxxcode)==1) gmxxcode=NULL;
}
#endif
if(gmxxcode==NULL) {
outerr("no valid gmxx code\n");
exit(1);
}
} else {outerr("gmxxcode forget\n");exit(1);}
// building string an define servername
server_name="weather.tuxnet24.de";
buffer="GET /?id=%d HTTP/1.1\r\nHost: %h\r\nConnection: close\r\n\r\n\0";
strmreplace(&buffer,"%d",gmxxcode);
strmreplace(&buffer,"%h",server_name);
free(gmxxcode);
openSock(&sock,server_name,"80");
sendSplit(&sock,&buffer);
recvSplit(&sock,&buffer);
closeSock(&sock);
parsing_infos(weather,buffer);
gen_output(weather,&flags,&format_code);
free(buffer);
exit(0);
}