00001
00002
00003
00004
00005
00006
00007
00008
00009 #include "ftpMS.h"
00010
00011
00012 #include <stdio.h>
00013
00014 #include <curl/curl.h>
00015 #include <curl/types.h>
00016 #include <curl/easy.h>
00017
00018
00019
00020
00021
00022 static size_t createAndWriteToDataObj(void *buffer, size_t size, size_t nmemb, void *stream)
00023 {
00024 dataObjFtpInp_t *dataObjFtpInp;
00025 dataObjInp_t dataObjInp;
00026 openedDataObjInp_t openedDataObjInp;
00027 bytesBuf_t bytesBuf;
00028 size_t written;
00029
00030
00031
00032 dataObjFtpInp = (dataObjFtpInp_t *)stream;
00033
00034
00035 memset(&dataObjInp, 0, sizeof(dataObjInp_t));
00036 memset(&openedDataObjInp, 0, sizeof(openedDataObjInp_t));
00037
00038
00039
00040 if (dataObjFtpInp && !dataObjFtpInp->l1descInx)
00041 {
00042 strcpy(dataObjInp.objPath, dataObjFtpInp->objPath);
00043 dataObjFtpInp->l1descInx = rsDataObjCreate(dataObjFtpInp->rsComm, &dataObjInp);
00044
00045
00046 if (dataObjFtpInp->l1descInx <= 2)
00047 {
00048 rodsLog (LOG_ERROR, "createAndWriteToDataObj: rsDataObjCreate failed for %s, status = %d", dataObjInp.objPath, dataObjFtpInp->l1descInx);
00049 return (dataObjFtpInp->l1descInx);
00050 }
00051 }
00052
00053
00054
00055 bytesBuf.len = (int)(size * nmemb);
00056 bytesBuf.buf = buffer;
00057
00058
00059
00060 if( dataObjFtpInp == NULL ) {
00061 rodsLog( LOG_ERROR, "createAndWriteToDataObj :: dataObjFtpInp is NULL" );
00062 return -1;
00063 }
00064 openedDataObjInp.l1descInx = dataObjFtpInp->l1descInx;
00065 openedDataObjInp.len = bytesBuf.len;
00066
00067
00068
00069 written = rsDataObjWrite(dataObjFtpInp->rsComm, &openedDataObjInp, &bytesBuf);
00070
00071 return (written);
00072 }
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114 int msiFtpGet(msParam_t *target, msParam_t *destObj, msParam_t *status, ruleExecInfo_t *rei)
00115 {
00116 CURL *curl;
00117 CURLcode res;
00118
00119 dataObjFtpInp_t dataObjFtpInp;
00120
00121 dataObjInp_t destObjInp, *myDestObjInp;
00122 openedDataObjInp_t openedDataObjInp;
00123 char *target_str;
00124
00125 int isCurlErr;
00126
00127
00128
00129
00130
00131
00132 RE_TEST_MACRO (" Calling msiFtpGet")
00133
00134
00135 if (rei == NULL || rei->rsComm == NULL)
00136 {
00137 rodsLog (LOG_ERROR, "msiFtpGet: input rei or rsComm is NULL.");
00138 return (SYS_INTERNAL_NULL_INPUT_ERR);
00139 }
00140
00141
00142 memset(&dataObjFtpInp, 0, sizeof(dataObjFtpInp_t));
00143 memset(&destObjInp, 0, sizeof(dataObjInp_t));
00144 memset(&openedDataObjInp, 0, sizeof(openedDataObjInp_t));
00145
00146
00147
00148
00149
00150
00151 if ((target_str = parseMspForStr(target)) == NULL)
00152 {
00153 rodsLog (LOG_ERROR, "msiFtpGet: input target is NULL.");
00154 return (USER__NULL_INPUT_ERR);
00155 }
00156
00157
00158
00159 rei->status = parseMspForDataObjInp (destObj, &destObjInp, &myDestObjInp, 0);
00160 if (rei->status < 0)
00161 {
00162 rodsLog (LOG_ERROR, "msiFtpGet: input destObj error. status = %d", rei->status);
00163 return (rei->status);
00164 }
00165
00166
00167
00168
00169
00170
00171 strcpy(dataObjFtpInp.objPath, destObjInp.objPath);
00172 dataObjFtpInp.l1descInx = 0;
00173 dataObjFtpInp.rsComm = rei->rsComm;
00174
00175
00176
00177 curl_global_init(CURL_GLOBAL_DEFAULT);
00178 curl = curl_easy_init();
00179
00180 isCurlErr = 0;
00181
00182 if(curl)
00183 {
00184
00185 curl_easy_setopt(curl, CURLOPT_URL, target_str);
00186 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, createAndWriteToDataObj);
00187 curl_easy_setopt(curl, CURLOPT_WRITEDATA, &dataObjFtpInp);
00188
00189
00190
00191
00192
00193 res = curl_easy_perform(curl);
00194
00195
00196 curl_easy_cleanup(curl);
00197
00198
00199 if(CURLE_OK != res)
00200 {
00201 rodsLog (LOG_ERROR, "msiFtpGet: libcurl error: %d", res);
00202 isCurlErr = 1;
00203 }
00204 }
00205
00206
00207
00208 if (dataObjFtpInp.l1descInx)
00209 {
00210 openedDataObjInp.l1descInx = dataObjFtpInp.l1descInx;
00211 rei->status = rsDataObjClose(rei->rsComm, &openedDataObjInp);
00212 }
00213
00214
00215
00216
00217
00218
00219 curl_global_cleanup();
00220
00221 if (isCurlErr)
00222 {
00223
00224 rei->status = -1;
00225 }
00226
00227 fillIntInMsParam (status, rei->status);
00228
00229 return (rei->status);
00230 }
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277 int msiTwitterPost(msParam_t *twittername, msParam_t *twitterpass, msParam_t *message, msParam_t *status, ruleExecInfo_t *rei)
00278 {
00279 CURL *curl;
00280 CURLcode res;
00281
00282 char *username, *passwd, *tweet;
00283
00284 char userpwd[LONG_NAME_LEN];
00285 char form_msg[160];
00286
00287 int isCurlErr;
00288
00289
00290
00291
00292
00293
00294 RE_TEST_MACRO (" Calling msiTwitterPost")
00295
00296
00297 if (rei == NULL || rei->rsComm == NULL)
00298 {
00299 rodsLog (LOG_ERROR, "msiTwitterPost: input rei or rsComm is NULL.");
00300 return (SYS_INTERNAL_NULL_INPUT_ERR);
00301 }
00302
00303
00304
00305
00306
00307 if ((username = parseMspForStr(twittername)) == NULL)
00308 {
00309 rodsLog (LOG_ERROR, "msiTwitterPost: input twittername is NULL.");
00310 return (USER__NULL_INPUT_ERR);
00311 }
00312
00313
00314 if ((passwd = parseMspForStr(twitterpass)) == NULL)
00315 {
00316 rodsLog (LOG_ERROR, "msiTwitterPost: input twitterpass is NULL.");
00317 return (USER__NULL_INPUT_ERR);
00318 }
00319
00320
00321 if ((tweet = parseMspForStr(message)) == NULL)
00322 {
00323 rodsLog (LOG_ERROR, "msiTwitterPost: input message is NULL.");
00324 return (USER__NULL_INPUT_ERR);
00325 }
00326
00327
00328
00329 strcpy(form_msg, "status=");
00330 strncat(form_msg, tweet, 140);
00331
00332
00333 snprintf(userpwd, LONG_NAME_LEN, "%s:%s", username, passwd);
00334
00335
00336
00337
00338
00339 curl_global_init(CURL_GLOBAL_DEFAULT);
00340 curl = curl_easy_init();
00341
00342 isCurlErr = 0;
00343
00344 if(curl)
00345 {
00346
00347 curl_easy_setopt(curl, CURLOPT_URL, "http://twitter.com/statuses/update.xml");
00348 curl_easy_setopt(curl, CURLOPT_USERPWD, userpwd);
00349 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, form_msg);
00350
00351
00352
00353
00354
00355 res = curl_easy_perform(curl);
00356
00357
00358 curl_easy_cleanup(curl);
00359
00360
00361 if(CURLE_OK != res)
00362 {
00363 rodsLog (LOG_ERROR, "msiTwitterPost: libcurl error: %d", res);
00364 isCurlErr = 1;
00365 }
00366 }
00367
00368
00369
00370
00371
00372 curl_global_cleanup();
00373
00374 if (isCurlErr)
00375 {
00376
00377 rei->status = -1;
00378 }
00379
00380 fillIntInMsParam (status, rei->status);
00381
00382 return (rei->status);
00383 }
00384
00385
00386
00387
00388
00389
00390
00391
00392 int msiPostThis(msParam_t *url, msParam_t *data, msParam_t *status, ruleExecInfo_t *rei)
00393 {
00394 CURL *curl;
00395 CURLcode res;
00396
00397 char *myurl, *mydata;
00398
00399
00400
00401
00402
00403 RE_TEST_MACRO (" Calling msiPostThis")
00404
00405
00406 if (rei == NULL || rei->rsComm == NULL)
00407 {
00408 rodsLog (LOG_ERROR, "msiPostThis: input rei or rsComm is NULL.");
00409 return (SYS_INTERNAL_NULL_INPUT_ERR);
00410 }
00411
00412
00413
00414
00415
00416 if ((myurl = parseMspForStr(url)) == NULL)
00417 {
00418 rodsLog (LOG_ERROR, "msiPostThis: input url is NULL.");
00419 return (USER__NULL_INPUT_ERR);
00420 }
00421
00422
00423 mydata = parseMspForStr(data);
00424
00425
00426
00427
00428
00429 curl_global_init(CURL_GLOBAL_DEFAULT);
00430 curl = curl_easy_init();
00431
00432 if(curl)
00433 {
00434
00435 curl_easy_setopt(curl, CURLOPT_URL, myurl);
00436 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, mydata);
00437
00438
00439
00440
00441
00442 res = curl_easy_perform(curl);
00443
00444
00445 curl_easy_cleanup(curl);
00446
00447
00448 if(res != CURLE_OK)
00449 {
00450 rodsLog (LOG_ERROR, "msiPostThis: cURL error: %s", curl_easy_strerror(res));
00451 rei->status = SYS_HANDLER_DONE_WITH_ERROR;
00452 }
00453 else
00454 {
00455 rei->status = 0;
00456 }
00457 }
00458
00459
00460
00461
00462
00463 curl_global_cleanup();
00464
00465 fillIntInMsParam (status, rei->status);
00466 return (rei->status);
00467 }
00468
00469