PageRenderTime 40ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/main.c

https://github.com/SpaceFalcon/chandumper
C | 328 lines | 287 code | 35 blank | 6 comment | 40 complexity | deaea23d978b7f5f4b636020ec9ee104 MD5 | raw file
  1. /* Standard includes */
  2. #define _WIN32_WINNT 0x500
  3. #define _WIN32_IE 0x300
  4. #include <windows.h>
  5. #include <commctrl.h>
  6. #include <stdio.h>
  7. /* liblist : http://github.com/adamlamers/liblist */
  8. #include <liblist/list.h>
  9. /* project includes */
  10. #include "version.h"
  11. #include "resource.h"
  12. #include "dialogs.h"
  13. #include "fileutil.h"
  14. #include "thread.h"
  15. #include "randstring.h"
  16. #include "init.h"
  17. #include "boards.h"
  18. HINSTANCE hInst;
  19. HWND ThreadNo;
  20. HWND DumpDirectory;
  21. HWND ImageCountLabel;
  22. HWND UploadCountLabel;
  23. HWND CounterLabel;
  24. HWND NameText;
  25. HWND EmailText;
  26. HWND SubjectText;
  27. HWND CommentText;
  28. HWND ShowConsoleButton;
  29. char *RandomPassword = NULL;
  30. static int FileCount = 0;
  31. static int UploadedCount = 0;
  32. static int consoleShown = 0;
  33. int dumpActive = FALSE;
  34. BOOL ListViewAddColumn(HWND listview, int colIndex, char *headerText, int colWidth)
  35. {
  36. LVCOLUMN lvc;
  37. memset(&lvc, 0, sizeof(lvc));
  38. lvc.mask = LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
  39. lvc.iSubItem = colIndex;
  40. lvc.pszText = headerText;
  41. lvc.cx = colWidth;
  42. if(ListView_InsertColumn(listview, colIndex, &lvc) == -1) return FALSE;
  43. return TRUE;
  44. }
  45. BOOL FileListAddRow(HWND listview, char *message, char *fileLocation)
  46. {
  47. LVITEM item;
  48. memset(&item, 0, sizeof(LVITEM));
  49. item.mask = LVIF_TEXT;
  50. item.iItem = 0;
  51. item.iSubItem = 0;
  52. item.pszText = "";
  53. ListView_InsertItem(listview, &item);
  54. item.iSubItem = 1;
  55. item.pszText = message;
  56. ListView_SetItem(listview, &item);
  57. item.iSubItem = 2;
  58. item.pszText = fileLocation;
  59. ListView_SetItem(listview, &item);
  60. return FALSE;
  61. }
  62. void ClearFileList(HWND listview)
  63. {
  64. SendMessage(listview, LVM_DELETEALLITEMS, 0, 0L);
  65. }
  66. /* This thread handles collecting all the information, posting and printing all info to stdout */
  67. DWORD WINAPI DumpThread(void *param)
  68. {
  69. char itemText[255];
  70. char fileLocation[MAX_PATH];
  71. char postURL[255];
  72. int counter = 0;
  73. char buf[125];
  74. float timeleft = 0;
  75. char threadno[128];
  76. char name[255];
  77. char email[255];
  78. char subject[255];
  79. char message[1024];
  80. const float TIMEBETWEEN = 60.0f;
  81. int i = 0;
  82. int numItems = ListView_GetItemCount(FileList);
  83. while(1 && dumpActive)
  84. {
  85. if((int)(timeleft * 100.0f) == 0) //if 60 seconds passed...
  86. {
  87. //Get the data from the edit controls...
  88. SendMessage(PostLocation, WM_GETTEXT, 255, (LPARAM)postURL);
  89. SendMessage(ThreadNo, WM_GETTEXT, 128, (LPARAM)threadno);
  90. SendMessage(NameText, WM_GETTEXT, 128, (LPARAM)name);
  91. SendMessage(EmailText, WM_GETTEXT, 128, (LPARAM)email);
  92. SendMessage(SubjectText, WM_GETTEXT, 128, (LPARAM)subject);
  93. SendMessage(CommentText, WM_GETTEXT, 128, (LPARAM)message);
  94. //Set status display to uploading
  95. SendMessage(CounterLabel, WM_SETTEXT, 0, (LPARAM)"Uploading...");
  96. counter = 0;
  97. LVITEM item;
  98. memset(&item, 0, sizeof(item));
  99. item.mask = LVIF_TEXT; //get the text
  100. item.pszText = itemText;
  101. item.cchTextMax = 255;
  102. item.iSubItem = 2; //from the "file" column
  103. item.iItem = i;
  104. ListView_GetItem(FileList, &item);
  105. printf("%s\n", fileLocation);
  106. printf("Posting data to: %s thread number %s\n", postURL, threadno);
  107. if(chan_threadreply(postURL , threadno, item.pszText, name, email, subject, message, RandomPassword) == 0)
  108. {
  109. UploadedCount++;
  110. char upcbuf[12];
  111. sprintf(upcbuf, "%d", UploadedCount);
  112. SendMessage(UploadCountLabel, WM_SETTEXT, 0, (LPARAM)upcbuf);
  113. }
  114. ListView_SetCheckState(FileList, i, 1);
  115. i++;
  116. if(i == numItems) break;
  117. }
  118. counter++;
  119. timeleft = TIMEBETWEEN - ((float)counter / 100.0f);
  120. sprintf(buf, "Next Upload In: %2.2f\n Seconds", timeleft);
  121. SendMessage(CounterLabel, WM_SETTEXT, 0, (LPARAM)buf);
  122. Sleep(10); //sleep for 1/100th of a second
  123. }
  124. SendMessage(CounterLabel, WM_SETTEXT, 0, (LPARAM)"Done");
  125. return TRUE;
  126. }
  127. void ToggleConsole()
  128. {
  129. if(ConsoleWindow)
  130. {
  131. if(consoleShown == 0)
  132. {
  133. ModifyMenu(sysMenu, showconsolepos, MF_BYPOSITION | MF_STRING, IDM_SHOW_CONSOLE, "Hide Console");
  134. ShowWindow(ConsoleWindow, SW_SHOW);
  135. consoleShown = 1;
  136. }
  137. else
  138. {
  139. ModifyMenu(sysMenu, showconsolepos, MF_BYPOSITION | MF_STRING, IDM_SHOW_CONSOLE, "Show Console");
  140. ShowWindow(ConsoleWindow, SW_HIDE);
  141. consoleShown = 0;
  142. }
  143. }
  144. }
  145. void DumpDir_OnChange(HWND hwndDlg)
  146. {
  147. char dumpdirPath[MAX_PATH];
  148. GetWindowText(DumpDirectory, dumpdirPath, MAX_PATH);
  149. if(isDirectory(dumpdirPath))
  150. {
  151. ListView_DeleteAllItems(FileList);
  152. FileCount = 0;
  153. list *filesList = GetDirectoryFiles(dumpdirPath);
  154. list *temp = list_reverse(filesList);
  155. char buffer[MAX_PATH];
  156. while(temp)
  157. {
  158. if(get_file_extension(temp->data, buffer) == 0)
  159. {
  160. if(strncasecmp(buffer, "jpg", 3) == 0 || strncasecmp(buffer, "png", 3) == 0 || strncasecmp(buffer, "gif", 3) == 0)
  161. {
  162. char fullfilepath[1024];
  163. snprintf(fullfilepath, 1024, "%s\\%s", dumpdirPath, (char*)temp->data);
  164. FileListAddRow(FileList, "", fullfilepath);
  165. FileCount++;
  166. char buf[10];
  167. sprintf(buf, "%d", FileCount);
  168. SendMessage(ImageCountLabel, WM_SETTEXT, 0, (LPARAM)buf);
  169. SendMessage(UploadCountLabel, WM_SETTEXT, 0, (LPARAM)"0");
  170. }
  171. }
  172. free(temp->data);
  173. temp = temp->next;
  174. }
  175. delete_list(filesList);
  176. }
  177. }
  178. void AddFile_OnClick(HWND hwndDlg)
  179. {
  180. list *files = MultipleFileSelectDialog(hwndDlg, "Image Files (*.jpg, *.png, *.gif)\0*.jpg;*.png;*.gif\0\0");
  181. char buffer[MAX_PATH];
  182. list *temp = files;
  183. while(temp)
  184. {
  185. if(get_file_extension(temp->data, buffer) == 0)
  186. {
  187. if(strncasecmp(buffer, "jpg", 3) == 0 || strncasecmp(buffer, "png", 3) == 0 || strncasecmp(buffer, "gif", 3) == 0)
  188. {
  189. FileListAddRow(FileList, "", temp->data);
  190. }
  191. }
  192. free(temp->data);
  193. temp = temp->next;
  194. }
  195. delete_list(files);
  196. }
  197. BOOL CALLBACK DialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
  198. {
  199. switch(uMsg)
  200. {
  201. case WM_INITDIALOG:
  202. PostLocation = GetDlgItem(hwndDlg, IDC_POSTLOCATION);
  203. ThreadNo = GetDlgItem(hwndDlg, IDC_THREADNO);
  204. InitFileList(hwndDlg);
  205. InitBoardSelect(hwndDlg);
  206. InitConsole(hwndDlg);
  207. DumpDirectory = GetDlgItem(hwndDlg, IDC_DUMPDIR);
  208. ImageCountLabel = GetDlgItem(hwndDlg, IDC_IMGCOUNT);
  209. UploadCountLabel = GetDlgItem(hwndDlg, IDC_UPLOADCOUNT);
  210. CounterLabel = GetDlgItem(hwndDlg, IDC_COUNTER);
  211. NameText = GetDlgItem(hwndDlg, IDC_NAMETEXT);
  212. EmailText = GetDlgItem(hwndDlg, IDC_EMAILTEXT);
  213. SubjectText = GetDlgItem(hwndDlg, IDC_SUBJECTTEXT);
  214. CommentText = GetDlgItem(hwndDlg, IDC_COMMENTTEXT);
  215. SendMessage(hwndDlg, WM_SETTEXT, 0, (LPARAM)VERSION_FULLSTRING);
  216. RandomPassword = getRandomString(12);
  217. return TRUE;
  218. case WM_CLOSE:
  219. FreeConsole();
  220. EndDialog(hwndDlg, 0);
  221. return TRUE;
  222. case WM_COMMAND:
  223. switch(LOWORD(wParam))
  224. {
  225. case IDC_BOARDSELECT:
  226. if(HIWORD(wParam) == CBN_SELCHANGE)
  227. {
  228. SendMessage(PostLocation, WM_SETTEXT, (WPARAM)0, (LPARAM)postlocations[SendMessage(BoardSelect, CB_GETCURSEL, 0, 0)]);
  229. }
  230. return TRUE;
  231. case IDC_DUMPDIRBROWSE:
  232. if(HIWORD(wParam) == BN_CLICKED)
  233. {
  234. char *selectedfolder = FolderBrowserDialog(hwndDlg);
  235. SendMessage(DumpDirectory, WM_SETTEXT, (WPARAM)0, (LPARAM)selectedfolder);
  236. free(selectedfolder);
  237. }
  238. return TRUE;
  239. case IDC_ADDFILE:
  240. if(HIWORD(wParam) == BN_CLICKED)
  241. {
  242. AddFile_OnClick(hwndDlg);
  243. }
  244. return TRUE;
  245. case IDC_BTNDUMP:
  246. if(HIWORD(wParam) == BN_CLICKED)
  247. {
  248. if(!dumpActive)
  249. {
  250. dumpActive = TRUE;
  251. CreateThread(NULL, 0, &DumpThread, NULL, 0, NULL);
  252. SendMessage(CounterLabel, WM_SETTEXT, 0, (LPARAM)"Starting...");
  253. SendMessage(GetDlgItem(hwndDlg, IDC_BTNDUMP), WM_SETTEXT, 0, "Cancel");
  254. }
  255. else
  256. {
  257. dumpActive = FALSE;
  258. SendMessage(GetDlgItem(hwndDlg, IDC_BTNDUMP), WM_SETTEXT, 0, "Dump");
  259. SendMessage(CounterLabel, WM_SETTEXT, 0, (LPARAM)"Cancelled");
  260. }
  261. }
  262. return TRUE;
  263. case IDC_DUMPDIR:
  264. if(HIWORD(wParam) == EN_CHANGE)
  265. {
  266. DumpDir_OnChange(hwndDlg);
  267. }
  268. return TRUE;
  269. }
  270. break;
  271. case WM_SYSCOMMAND:
  272. switch(LOWORD(wParam))
  273. {
  274. case IDM_SHOW_CONSOLE:
  275. ToggleConsole();
  276. return TRUE;
  277. case IDM_GO_TO_WEBSITE:
  278. ShellExecute(NULL, "open", "http://github.com/spacefalcon/chandumper", NULL, NULL, SW_SHOWNORMAL);
  279. return TRUE;
  280. }
  281. break;
  282. }
  283. return FALSE;
  284. }
  285. int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
  286. {
  287. hInst = hInstance;
  288. INITCOMMONCONTROLSEX InitCtrls;
  289. InitCtrls.dwICC = ICC_LISTVIEW_CLASSES;
  290. InitCtrls.dwSize = sizeof(INITCOMMONCONTROLSEX);
  291. InitCommonControlsEx(&InitCtrls);
  292. return DialogBox(hInstance, MAKEINTRESOURCE(IDD_DIALOG1), NULL, DialogProc);
  293. }