forked from gregkh/smugbatch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
smug_core.c
1399 lines (1225 loc) · 32.7 KB
/
smug_core.c
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2008 Greg Kroah-Hartman <[email protected]>
*
* 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 version 2 of the License.
*
* 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, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <getopt.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <ctype.h>
#include <alloca.h>
#include <curl/curl.h>
#include "list.h"
#include "md5.h"
#include "smug.h"
#include "smugbatch_version.h"
static char *api_key = "ABW1oenNznek2rD4AIiFn7OhkEkmzEIb";
static char *user_agent = "smugbatch/"SMUGBATCH_VERSION" ([email protected])";
static char *session_id_tag = "Session id";
static char *smugmug_album_list_url =
"https://api.smugmug.com/hack/rest/1.2.0/?"
"method=smugmug.albums.get"
"&SessionID=%s&APIKey=%s";
static char *smugmug_album_create_url =
"https://api.smugmug.com/hack/rest/1.2.0/?"
"method=smugmug.albums.create"
"&SessionID=%s&Title=%s&CategoryID=%s&SubCategoryID=%s"
"&AlbumTemplateID=%s";
static char *smugmug_category_list_url =
"https://api.smugmug.com/hack/rest/1.2.0/?"
"method=smugmug.categories.get"
"&SessionID=%s";
static char *smugmug_subcategory_list_url =
"https://api.smugmug.com/hack/rest/1.2.0/?"
"method=smugmug.subcategories.get"
"&CategoryID=%s&SessionID=%s";
static char *smugmug_quicksettings_list_url =
"https://api.smugmug.com/hack/rest/1.2.0/?"
"method=smugmug.albumtemplates.get"
"&SessionID=%s";
static char *smugmug_login_url =
"https://api.smugmug.com/hack/rest/1.2.0/?"
"method=smugmug.login.withPassword"
"&EmailAddress=%s&Password=%s&APIKey=%s";
static char *smugmug_logout_url =
"https://api.smugmug.com/hack/rest/1.2.0/?"
"method=smugmug.logout"
"&SessionID=%s&APIKey=%s";
static char *smugmug_image_list_url =
"https://api.smugmug.com/hack/rest/1.2.0/?"
"method=smugmug.images.get"
"&SessionID=%s&Heavy=1&AlbumID=%s&AlbumKey=%s";
static char *smugmug_upload_url =
"http://upload.smugmug.com/%s";
CURL *curl_init(void)
{
CURL *curl;
curl = curl_easy_init();
if (!curl) {
fprintf(stderr, "Can not init CURL!\n");
return NULL;
}
curl_easy_setopt(curl, CURLOPT_USERAGENT, user_agent);
/* some ssl sanity checks on the connection we are making */
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
return curl;
}
static char *get_su_cookie(CURL *curl)
{
CURLcode res;
struct curl_slist *cookies;
struct curl_slist *nc;
char *cookie = NULL;
res = curl_easy_getinfo(curl, CURLINFO_COOKIELIST, &cookies);
if (res != CURLE_OK) {
fprintf(stderr, "Curl curl_easy_getinfo failed: %s\n",
curl_easy_strerror(res));
exit(1);
}
nc = cookies;
while (nc) {
char domain[50], p1[50], p2[50], p3[50], name[50], value[50];
long unsigned int t;
int res;
/* #HttpOnly_.smugmug.com\tTRUE\t/\tFALSE\t0\tSMSESS\t99363be4c3ceb3f153f875216539524a */
res =
sscanf(nc->data, "%s\t%s\t%s\t%s\t%lu\t%s\t%s", domain, p1,
p2, p3, &t, name, value);
if (res == 7 && strcmp(name, "_su") == 0) {
cookie = valloc(strlen(value) + 1 + 4);
sprintf(cookie, "_su=%s", value);
}
nc = nc->next;
}
curl_slist_free_all(cookies);
return cookie;
}
static void curl_set_cookie(CURL *curl, const char *cookie)
{
if (cookie == NULL)
return;
curl_easy_setopt(curl, CURLOPT_COOKIE, cookie);
}
char *my_basename(char *name)
{
char *temp;
int length = strlen(name);
temp = &name[length];
while (length && *temp != '/') {
--temp;
--length;
}
if (*temp == '/')
++temp;
return strdup(temp);
}
/**
* get_string_from_stdin - reads a string from standard input
*
* returns a pointer to a buffer that must be later freed by the caller
* with a call to free().
*/
char *get_string_from_stdin(void)
{
char *temp;
char *string;
string = zalloc(100);
if (!string)
return NULL;
if (!fgets(string, 99, stdin))
return NULL;
temp = strchr(string, '\n');
*temp = '\0';
return string;
}
void album_list_free(struct list_head *albums)
{
struct album *album;
struct album *temp;
list_for_each_entry_safe(album, temp, albums, entry) {
dbg("cleaning up album %s\n", album->title);
free(album->id);
free(album->key);
free(album->title);
files_list_free(&album->files);
free(album);
}
}
void files_list_free(struct list_head *files)
{
struct filename *filename;
struct filename *temp;
list_for_each_entry_safe(filename, temp, files, entry) {
dbg("cleaning up filename %s\n", filename->filename);
free(filename->filename);
if (filename->basename)
free(filename->basename);
if (filename->id)
free(filename->id);
if (filename->key)
free(filename->key);
if (filename->caption)
free(filename->caption);
if (filename->original_url)
free(filename->original_url);
free(filename);
}
}
/**
* find_value - attempts to find a value within a string
*
* Works a lot like strstr() except we are searching through a
* "string = value" type buffer. The value that the needle is set
* to is then copied into a new buffer and returned. This buffer must be
* later freed with a call to free().
*
* This lets us do a "poor mans" xml parser without really having to parse
* xml. We just rely on the fact that the tag values come in a specific
* order. Yeah, it's cheating, but for now, we seem to get away with it.
*
* Returns NULL if the needle is not found in the haystack, or if we are
* unable to allocate the needed memory buffer.
*/
char *find_value(const char *haystack, const char *needle, char **new_pos)
{
char *location;
char *temp;
char *value;
location = strstr(haystack, needle);
if (!location)
return NULL;
value = zalloc(1000);
if (!value)
return NULL;
location += strlen(needle);
temp = value;
++location; /* '=' */
++location; /* '"' */
while (*location != '"') {
*temp = *location;
++temp;
++location;
}
*temp = '\0';
if (new_pos)
*new_pos = location;
return value;
}
/**
* smug_curl_buffer_alloc- allocates a struct smug_curl_buffer
*
* returns a pointer to a struct smug_curl_buffer that must be later freed
* with a call to smug_curl_buffer_free()
*/
struct smug_curl_buffer *smug_curl_buffer_alloc(void)
{
struct smug_curl_buffer *buffer;
buffer = zalloc(sizeof(*buffer));
if (!buffer)
return NULL;
/* start out with a data buffer of 1 byte to
* make the buffer fill logic simpler */
buffer->data = zalloc(1);
if (!buffer->data) {
free(buffer);
return NULL;
}
buffer->length = 0;
return buffer;
}
void smug_curl_buffer_free(struct smug_curl_buffer *buffer)
{
if (!buffer)
return;
free(buffer->data);
free(buffer);
}
/**
* session_alloc - creates a struct session
*
* returns a pointer to a struct session that must be later freed with a
* call to smug_curl_buffer_free()
*/
struct session *session_alloc(void)
{
struct session *session;
session = zalloc(sizeof(*session));
session->su_cookie = NULL;
if (!session)
return NULL;
INIT_LIST_HEAD(&session->albums);
INIT_LIST_HEAD(&session->files_upload);
INIT_LIST_HEAD(&session->files_download);
return session;
}
void session_free(struct session *session)
{
if (!session)
return;
free(session->password);
free(session->email);
free(session->session_id);
album_list_free(&session->albums);
files_list_free(&session->files_upload);
files_list_free(&session->files_download);
free(session);
}
size_t curl_callback(void *buffer, size_t size, size_t nmemb, void *userp)
{
struct smug_curl_buffer *curl_buf = userp;
size_t buffer_size = size * nmemb;
char *temp;
if ((!buffer) || (!buffer_size) || (!curl_buf))
return -EINVAL;
/* add to the data we already have */
temp = zalloc(curl_buf->length + buffer_size + 1);
if (!temp)
return -ENOMEM;
memcpy(temp, curl_buf->data, curl_buf->length);
free(curl_buf->data);
curl_buf->data = temp;
memcpy(&curl_buf->data[curl_buf->length], (char *)buffer, buffer_size);
curl_buf->length += buffer_size;
dbg("%s\n", curl_buf->data);
return buffer_size;
}
int get_session_id(struct smug_curl_buffer *buffer, struct session *session)
{
session->session_id = find_value(buffer->data, session_id_tag, NULL);
if (!session->session_id)
return -EINVAL;
return 0;
}
int get_albums(struct smug_curl_buffer *buffer, struct session *session)
{
char *temp = buffer->data;
struct album *album;
char *id;
char *key;
char *title;
char *category;
char *subcategory;
int found_one = 0;
int album_number = 0;
while (1) {
id = find_value(temp, "Album id", &temp);
if (!id)
break;
key = find_value(temp, "Key", &temp);
if (!key)
break;
title = find_value(temp, "Title", &temp);
if (!title)
break;
// We don't want the id, but we need to seek find_value
category = find_value(temp, "Category id", &temp);
if (!category)
break;
category = find_value(temp, "Name", &temp); // Cat name
if (!category)
break;
// We don't want the id, but we need to seek find_value
subcategory = find_value(temp, "SubCategory id", &temp);
if (subcategory)
subcategory = find_value(temp, "Name", &temp);
else
subcategory = strdup("");
dbg("%s: %s: %s: %s: %s\n", id, key, title,
category, subcategory);
album = zalloc(sizeof(*album));
INIT_LIST_HEAD(&album->files);
album->id = id;
album->key = key;
album->title = title;
album->category = category;
album->subcategory = subcategory;
album->number = ++album_number;
list_add_tail(&album->entry, &session->albums);
found_one++;
}
// NEEDFIX: error path does not clear memory
if (!found_one)
return -EINVAL;
return 0;
}
static int get_images(struct smug_curl_buffer *buffer, struct album *album)
{
char *temp = buffer->data;
struct filename *filename;
char *id;
char *key;
char *name;
char *caption;
char *size;
char *md5;
char *original_url;
int found_one = 0;
while (1) {
id = find_value(temp, "Image id", &temp);
if (!id)
break;
key = find_value(temp, "Key", &temp);
if (!key)
break;
name = find_value(temp, "FileName", &temp);
if (!name)
break;
caption = find_value(temp, "Caption", &temp);
if (!caption)
break;
size = find_value(temp, "Size", &temp);
if (!size)
break;
md5 = find_value(temp, "MD5Sum", &temp);
if (!md5)
break;
original_url = find_value(temp, "OriginalURL", &temp);
if (!original_url)
break;
dbg("%s: %s: %s: %s: %s: %s: %s:\n",
id, key, name, caption, original_url, size, md5);
filename = zalloc(sizeof(*filename));
if (!filename)
break;
filename->id = id;
filename->key = key;
filename->filename = name;
filename->caption = caption;
filename->size = size;
filename->md5_string = md5;
filename->original_url = original_url;
list_add_tail(&filename->entry, &album->files);
found_one++;
}
// NEEDFIX: error path does not clear memory
if (!found_one)
return -EINVAL;
return 0;
}
static unsigned char md5_data[100];
/* from coreutils */
static inline void *ptr_align(void const *ptr, size_t alignment)
{
char const *p0 = ptr;
char const *p1 = p0 + alignment - 1;
return (void *) (p1 - (size_t) p1 % alignment);
}
static void sprintf_md5(char *string, unsigned char *md5)
{
sprintf(string,
"%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x"
"%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x",
md5[0], md5[1], md5[2], md5[3], md5[4], md5[5], md5[6],
md5[7], md5[8], md5[9], md5[10], md5[11], md5[12], md5[13],
md5[14], md5[15]);
}
int generate_md5s(struct list_head *files)
{
struct filename *filename;
FILE *fp;
int err;
unsigned char *md5 = ptr_align(md5_data, 4);
char md5_string[64];
if (!files)
return 0;
/* let's generate the md5 of the files we are going to upload */
list_for_each_entry(filename, files, entry) {
dbg("calculating md5 of %s\n", filename->filename);
fp = fopen(filename->filename, "rb");
if (!fp) {
printf("Can not open %s, exiting\n",
filename->filename);
return -EINVAL;
}
err = md5_stream(fp, &md5[0]);
if (err) {
printf("error generating md5 for %s, exiting\n",
filename->filename);
fclose(fp);
return -err;
}
memcpy(filename->md5, md5, MD5_LEN_MAX);
sprintf_md5(md5_string, &md5[0]);
dbg("md5 of %s is %s\n", filename->filename, md5_string);
fclose(fp);
}
return 0;
}
int curl_progress_func(void *arg_progress,
double dltotal, double dlnow,
double ultotal, double ulnow)
{
int bytes_now;
int total;
int percent;
int data_avg = 0;
struct progress *progress = (struct progress *)arg_progress;
if (!progress)
return -EINVAL;
time_t now = time(NULL);
if (progress->start == 0)
progress->start = now;
time_t timediff = now - progress->start;
if (progress->upload) {
bytes_now = (int)ulnow;
total = (int)ultotal;
percent = (int)(ulnow*100.0/ultotal);
} else {
bytes_now = (int)dlnow;
total = (int)dltotal;
percent = (int)(dlnow*100.0/dltotal);
}
if (timediff > 0)
data_avg = (bytes_now / timediff / 1024);
fprintf(stdout, " \r%d of %d: %s: %d of %d (%d%%) %d KiB/s",
progress->position, progress->total,
progress->filename, bytes_now, total, percent, data_avg);
return 0;
}
int upload_file(struct session *session, struct filename *filename,
struct album *album, int position, int total)
{
struct smug_curl_buffer *buffer;
CURL *curl;
FILE *fd;
int file_handle;
struct stat file_info;
CURLcode res;
char buf[100];
char url[1000];
char md5_string[64];
struct curl_slist *headers = NULL;
struct progress *progress;
buffer = smug_curl_buffer_alloc();
if (!buffer)
return -ENOMEM;
progress = zalloc(sizeof(*progress));
if (!progress)
return -ENOMEM;
progress->filename = filename->basename;
progress->upload = 1;
progress->position = position;
progress->total = total;
curl = curl_init();
if (!curl)
return -EINVAL;
file_handle = open(filename->filename, O_RDONLY);
fstat(file_handle, &file_info);
close(file_handle);
fd = fopen(filename->filename, "rb");
if (!fd)
return -EINVAL;
dbg("%s is %d bytes big\n", filename->filename, (int)file_info.st_size);
sprintf(url, smugmug_upload_url, filename->basename);
curl_set_cookie(curl, session->su_cookie);
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_callback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, buffer);
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1);
curl_easy_setopt(curl, CURLOPT_PUT, 1);
curl_easy_setopt(curl, CURLOPT_READDATA, fd);
curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,
(curl_off_t)file_info.st_size);
sprintf_md5(&md5_string[0], &filename->md5[0]);
sprintf(buf, "Content-MD5: %s", md5_string);
dbg("%s\n", buf);
headers = curl_slist_append(headers, buf);
headers = curl_slist_append(headers, "X-Smug-Version: 1.2.0");
headers = curl_slist_append(headers, "X-Smug-ResponseType: REST");
sprintf(buf, "X-Smug-SessionID: %s", session->session_id);
dbg("%s\n", buf);
headers = curl_slist_append(headers, buf);
sprintf(buf, "X-Smug-AlbumID: %s", album->id);
dbg("%s\n", buf);
headers = curl_slist_append(headers, buf);
sprintf(buf, "X-Smug-FileName: %s", filename->basename);
dbg("%s\n", buf);
headers = curl_slist_append(headers, buf);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
if (!session->quiet) {
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0);
curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION,
curl_progress_func);
curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, progress);
}
dbg("starting upload...\n");
res = curl_easy_perform(curl);
if (res)
fprintf(stderr, "upload error %d, exiting\n", res);
curl_slist_free_all(headers);
if (!session->quiet) {
fprintf(stdout, "\n");
fflush(stdout);
}
curl_easy_cleanup(curl);
smug_curl_buffer_free(buffer);
free(progress);
return (int)res;
}
int upload_files(struct session *session, struct album *album)
{
struct filename *filename;
int num_to_upload = 0;
int i = 0;
int retval;
list_for_each_entry(filename, &session->files_upload, entry)
++num_to_upload;
dbg("num_to_upload = %d\n", num_to_upload);
list_for_each_entry(filename, &session->files_upload, entry) {
++i;
retval = upload_file(session, filename, album,
i, num_to_upload);
if (retval)
return retval;
}
return 0;
}
struct album *select_album(const char *album_title, const char *category_title,
const char *subcategory_title, const char *qs_name,
struct session *session)
{
struct album *album;
int found_album;
char *string;
int album_no = 0;
if (!album_title) {
fprintf(stdout, "Available albums:\n");
list_for_each_entry(album, &session->albums, entry)
fprintf(stdout, "\t%3d: %s\n",
album->number, album->title);
fprintf(stdout, "\n");
fprintf(stdout, "Please enter the number of the album: ");
string = get_string_from_stdin();
if (string) {
album_no = atoi(string);
free(string);
}
found_album = 0;
list_for_each_entry(album, &session->albums, entry) {
if (album->number == album_no) {
found_album = 1;
album_title = album->title;
break;
}
}
if (!found_album) {
fprintf(stdout, "Album number %d is not found\n",
album_no);
return NULL;
}
} else {
found_album = 0;
list_for_each_entry(album, &session->albums, entry) {
if (strcmp(album->title, album_title) == 0) {
found_album = 1;
break;
}
}
if (!found_album && category_title) {
album = smug_create_album(album_title, category_title,
subcategory_title, qs_name,
session);
if (album)
found_album = 1;
}
if (!found_album) {
fprintf(stdout, "Album %s is not found\n", album_title);
return NULL;
}
}
return album;
}
int smug_login(struct session *session)
{
char url[500];
struct smug_curl_buffer *curl_buf;
CURL *curl = NULL;
CURLcode res;
int retval;
if (!session)
return -EINVAL;
curl_buf = smug_curl_buffer_alloc();
if (!curl_buf)
return -ENOMEM;
curl = curl_init();
if (!curl)
return -EINVAL;
sprintf(url, smugmug_login_url, session->email,
session->password, api_key);
dbg("url = %s\n", url);
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "");
/* log into smugmug */
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_callback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, curl_buf);
res = curl_easy_perform(curl);
if (res) {
printf("error(%d) trying to login\n", res);
return -EINVAL;
}
session->su_cookie = get_su_cookie(curl);
char *rsp_stat = find_value(curl_buf->data, "rsp stat", NULL);
if (!rsp_stat)
return -EINVAL;
if (strcmp(rsp_stat, "fail") == 0) {
char *msg = find_value(curl_buf->data, "msg", NULL);
printf("error to login: %s\n", msg);
free(msg);
}
free(rsp_stat);
retval = get_session_id(curl_buf, session);
if (retval) {
fprintf(stderr, "session_id was not found\n");
return -EINVAL;
}
curl_easy_cleanup(curl);
smug_curl_buffer_free(curl_buf);
return 0;
}
int smug_logout(struct session *session)
{
char url[500];
struct smug_curl_buffer *curl_buf;
CURL *curl = NULL;
CURLcode res;
if (!session)
return -EINVAL;
curl_buf = smug_curl_buffer_alloc();
if (!curl_buf)
return -ENOMEM;
curl = curl_init();
if (!curl)
return -EINVAL;
sprintf(url, smugmug_logout_url, session->session_id, api_key);
dbg("url = %s\n", url);
curl_set_cookie(curl, session->su_cookie);
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_callback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, curl_buf);
res = curl_easy_perform(curl);
if (res) {
fprintf(stderr, "error(%d) trying to logout\n", res);
return -EINVAL;
}
curl_easy_cleanup(curl);
smug_curl_buffer_free(curl_buf);
return 0;
}
int smug_get_albums(struct session *session)
{
char url[1000];
struct smug_curl_buffer *curl_buf;
CURL *curl = NULL;
CURLcode res;
int retval;
if (!session)
return -EINVAL;
curl_buf = smug_curl_buffer_alloc();
if (!curl_buf)
return -ENOMEM;
curl = curl_init();
if (!curl)
return -EINVAL;
sprintf(url, smugmug_album_list_url, session->session_id, api_key);
dbg("url = %s\n", url);
curl_set_cookie(curl, session->su_cookie);
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_callback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, curl_buf);
res = curl_easy_perform(curl);
if (res) {
fprintf(stderr, "error(%d) trying to read list of albums\n",
res);
return -EINVAL;
}
retval = get_albums(curl_buf, session);
if (retval) {
fprintf(stderr, "error parsing albums\n");
return -EINVAL;
}
curl_easy_cleanup(curl);
smug_curl_buffer_free(curl_buf);
return 0;
}
/**
* returns a pointer to a buffer that must be later freed by the caller
* with a call to free().
*/
char *smug_get_quicksettings_id(const char *qs_name, struct session *session)
{
char url[1000];
struct smug_curl_buffer *curl_buf;
CURL *curl = NULL;
CURLcode res;
if (!session || !qs_name)
return NULL;
curl_buf = smug_curl_buffer_alloc();
if (!curl_buf)
return NULL;
curl = curl_init();
if (!curl) {
smug_curl_buffer_free(curl_buf);
return NULL;
}
sprintf(url, smugmug_quicksettings_list_url, session->session_id);
dbg("url = %s\n", url);
curl_set_cookie(curl, session->su_cookie);
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_callback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, curl_buf);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
if (res) {
fprintf(stderr, "error(%d) trying to get quicksettings\n", res);
smug_curl_buffer_free(curl_buf);
return NULL;
}
char *rsp_stat = find_value(curl_buf->data, "rsp stat", NULL);
if (!rsp_stat) {
smug_curl_buffer_free(curl_buf);
return NULL;
}
if (strcmp(rsp_stat, "fail") == 0) {
char *msg = find_value(curl_buf->data, "msg", NULL);
printf("error to get quicksettings: %s\n", msg);
free(msg);
smug_curl_buffer_free(curl_buf);
return NULL;
}
free(rsp_stat);
char *temp = curl_buf->data;
char *id = NULL;
char *name = NULL;
while (1) {
id = find_value(temp, "AlbumTemplate id", &temp);
name = find_value(temp, "AlbumTemplateName", &temp);
if (!id || !name)
break;
dbg("%s: %s\n", id, name);
if (strcmp(name, qs_name) == 0) {
free(name);
smug_curl_buffer_free(curl_buf);
return id;
}
}
if (id)
free(id);
if (name)
free(name);
smug_curl_buffer_free(curl_buf);
return NULL;
}
/**
* returns a pointer to a buffer that must be later freed by the caller
* with a call to free().
*/
char *smug_get_subcategory_id(const char *subcategory_title,
const char* category_id, struct session *session)
{
char url[1000];
struct smug_curl_buffer *curl_buf;
CURL *curl = NULL;
CURLcode res;
if (!session || !subcategory_title)
return NULL;
curl_buf = smug_curl_buffer_alloc();
if (!curl_buf)
return NULL;
curl = curl_init();
if (!curl) {
smug_curl_buffer_free(curl_buf);
return NULL;
}
sprintf(url, smugmug_subcategory_list_url, category_id,
session->session_id);
dbg("url = %s\n", url);
curl_set_cookie(curl, session->su_cookie);
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_callback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, curl_buf);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
if (res) {
fprintf(stderr, "error(%d) trying to get category\n", res);
smug_curl_buffer_free(curl_buf);
return NULL;
}
char *rsp_stat = find_value(curl_buf->data, "rsp stat", NULL);
if (!rsp_stat) {
smug_curl_buffer_free(curl_buf);
return NULL;
}
if (strcmp(rsp_stat, "fail") == 0) {
char *msg = find_value(curl_buf->data, "msg", NULL);
printf("error to get subcategory: %s\n", msg);
free(msg);
smug_curl_buffer_free(curl_buf);
return NULL;
}
free(rsp_stat);
char *temp = curl_buf->data;
char *id = NULL;