diff options
Diffstat (limited to 'protocols/Weather/src/weather_update.cpp')
-rw-r--r-- | protocols/Weather/src/weather_update.cpp | 192 |
1 files changed, 11 insertions, 181 deletions
diff --git a/protocols/Weather/src/weather_update.cpp b/protocols/Weather/src/weather_update.cpp index 75f73a2039..7c90f73fcf 100644 --- a/protocols/Weather/src/weather_update.cpp +++ b/protocols/Weather/src/weather_update.cpp @@ -381,199 +381,29 @@ int CWeatherProto::GetWeatherData(MCONTACT hContact) wchar_t id[256]; GetStationID(hContact, id, _countof(id)); - // test ID format - wchar_t *szInfo = wcschr(id, '/'); - if (szInfo == nullptr) - return INVALID_ID_FORMAT; - - GetID(id); - wchar_t Svc[256]; GetStationID(hContact, Svc, _countof(Svc)); - GetSvc(Svc); // check for invalid station if (id[0] == 0) return INVALID_ID; if (Svc[0] == 0) return INVALID_SVC; - // get the update strings (loaded to memory from ini files) - WIDATA *Data = GetWIData(Svc); - if (Data == nullptr) - return SVC_NOT_FOUND; // the ini for the station cannot be found - uint16_t cond = NA; char loc[256]; - for (int i = 0; i < 4; ++i) { - // generate update URL - switch (i) { - case 0: - mir_snprintf(loc, Data->UpdateURL, _T2A(id).get()); - break; - - case 1: - mir_snprintf(loc, Data->UpdateURL2, _T2A(id).get()); - break; - - case 2: - mir_snprintf(loc, Data->UpdateURL3, _T2A(id).get()); - break; - - case 3: - mir_snprintf(loc, Data->UpdateURL4, _T2A(id).get()); - break; - - default: - continue; - } - - if (loc[0] == 0) - continue; - - // download the html file from the internet - wchar_t *szData = nullptr; - int retval = InternetDownloadFile(loc, Data->Cookie, Data->UserAgent, &szData); - if (retval != 0) { - mir_free(szData); - return retval; - } - if (wcsstr(szData, L"Document Not Found") != nullptr) { - mir_free(szData); - return DOC_NOT_FOUND; - } - - szInfo = szData; - WIDATAITEMLIST *Item = Data->UpdateData; - - // begin parsing item by item - while (Item != nullptr) { - if (Item->Item.Url[0] != 0 && Item->Item.Url[0] != (i + '1')) { - Item = Item->Next; - continue; - } - wchar_t DataValue[MAX_DATA_LEN]; - switch (Item->Item.Type) { - case WID_NORMAL: - // if it is a normal item with start= and end=, then parse through the downloaded string - // to get a data value. - GetDataValue(&Item->Item, DataValue, &szInfo); - if (mir_wstrcmp(Item->Item.Name, L"Condition") && mir_wstrcmpi(Item->Item.Unit, L"Cond")) - wcsncpy(DataValue, TranslateW(DataValue), MAX_DATA_LEN - 1); - break; - - case WID_SET: - { - // for the "Set Data=" operation - DBVARIANT dbv; - wchar_t *chop, *str, str2[MAX_DATA_LEN]; - BOOL hasvar = FALSE; - size_t stl; - - // get the set data operation string - str = Item->Item.End; - DataValue[0] = 0; - // go through each part of the operation string seperated by the & operator - do { - // the end of the string, last item - chop = wcsstr(str, L" & "); - if (chop == nullptr) - chop = wcschr(str, '\0'); - - stl = min(sizeof(str2) - 1, (unsigned)(chop - str - 2)); - wcsncpy(str2, str + 1, stl); - str2[stl] = 0; - - switch (str[0]) { - case '[': // variable, add the value to the result string - hasvar = TRUE; - if (!DBGetData(hContact, _T2A(str2), &dbv)) { - mir_wstrncat(DataValue, TranslateW(dbv.pwszVal), _countof(DataValue) - mir_wstrlen(DataValue)); - DataValue[_countof(DataValue) - 1] = 0; - db_free(&dbv); - } - break; - - case'\"': // constant, add it to the result string - mir_wstrncat(DataValue, TranslateW(str2), _countof(DataValue) - mir_wstrlen(DataValue)); - DataValue[_countof(DataValue) - 1] = 0; - break; - } - - // remove the front part of the string that is done and continue parsing - str = chop + 3; - } while (chop[0] && str[0]); - - if (!hasvar) ConvertDataValue(&Item->Item, DataValue); - break; - } - case WID_BREAK: - { - // for the "Break Data=" operation - DBVARIANT dbv; - if (!DBGetData(hContact, _T2A(Item->Item.Start), &dbv)) { - wcsncpy(DataValue, dbv.pwszVal, _countof(DataValue)); - DataValue[_countof(DataValue) - 1] = 0; - db_free(&dbv); - } - else { - DataValue[0] = 0; - break; // do not continue if the source is invalid - } - - // generate the strings - wchar_t *end = wcsstr(DataValue, Item->Item.Break); - if (end == nullptr) { - DataValue[0] = 0; - break; // exit if break string is not found - } - *end = '\0'; - end += mir_wstrlen(Item->Item.Break); - while (end[0] == ' ') - end++; // remove extra space - - ConvertDataValue(&Item->Item, DataValue); - - // write the 2 strings created from the break operation - if (Item->Item.End[0]) - db_set_ws(hContact, WEATHERCONDITION, _T2A(Item->Item.End), end); - break; - } - } - - // don't store data if it is not available - if ((DataValue[0] != 0 && mir_wstrcmp(DataValue, NODATA) && - mir_wstrcmp(DataValue, TranslateW(NODATA)) && mir_wstrcmp(Item->Item.Name, L"Ignore")) || - (!mir_wstrcmp(Item->Item.Name, L"Alert") && i == 0)) { - // temporary workaround for mToolTip to show feel-like temperature - if (!mir_wstrcmp(Item->Item.Name, L"Feel")) - db_set_ws(hContact, WEATHERCONDITION, "Heat Index", DataValue); - GetStationID(hContact, Svc, _countof(Svc)); - if (!mir_wstrcmp(Svc, opt.Default)) - db_set_ws(0, DEFCURRENTWEATHER, _T2A(Item->Item.Name), DataValue); - if (!mir_wstrcmp(Item->Item.Name, L"Condition")) { - wchar_t buf[128], *cbuf; - mir_snwprintf(buf, L"#%s Weather", DataValue); - cbuf = TranslateW(buf); - if (cbuf[0] == '#') - cbuf = TranslateW(DataValue); - db_set_ws(hContact, WEATHERCONDITION, _T2A(Item->Item.Name), cbuf); - CharLowerBuff(DataValue, (uint32_t)mir_wstrlen(DataValue)); - cond = GetIcon(DataValue, Data); - } - else if (mir_wstrcmpi(Item->Item.Unit, L"Cond") == 0) { - wchar_t buf[128], *cbuf; - mir_snwprintf(buf, L"#%s Weather", DataValue); - cbuf = TranslateW(buf); - if (cbuf[0] == '#') - cbuf = TranslateW(DataValue); - db_set_ws(hContact, WEATHERCONDITION, _T2A(Item->Item.Name), cbuf); - } - else db_set_ws(hContact, WEATHERCONDITION, _T2A(Item->Item.Name), DataValue); - } - Item = Item->Next; - } + // download the html file from the internet + wchar_t *szData = nullptr; + int retval = 1; // InternetDownloadFile(loc, 0, 0, &szData); + if (retval != 0) { mir_free(szData); + return retval; } + if (wcsstr(szData, L"Document Not Found") != nullptr) { + mir_free(szData); + return DOC_NOT_FOUND; + } + + mir_free(szData); // assign condition icon setWord(hContact, "StatusIcon", cond); |