comparison main.c @ 622:bb6b07b44800

Network code is being generalized into a th-libs module.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 27 May 2014 07:31:20 +0300
parents 29b8ff5b625b
children 118276b60667
comparison
equal deleted inserted replaced
621:29b8ff5b625b 622:bb6b07b44800
1 /* 1 /*
2 * NNChat - Custom chat client for NewbieNudes.com chatrooms 2 * NNChat - Custom chat client for NewbieNudes.com chatrooms
3 * Written by Matti 'ccr' Hämäläinen 3 * Written by Matti 'ccr' Hämäläinen
4 * (C) Copyright 2008-2014 Tecnic Software productions (TNSP) 4 * (C) Copyright 2008-2014 Tecnic Software productions (TNSP)
5 */ 5 */
6 #include "util.h"
7 #include "network.h"
8 #include "ui.h"
9 #include "th_args.h" 6 #include "th_args.h"
10 #include "th_config.h" 7 #include "th_config.h"
8 #include "th_network.h"
9 #include "util.h"
10 #include "ui.h"
11 #include <unistd.h> 11 #include <unistd.h>
12 #include <fcntl.h> 12 #include <fcntl.h>
13 #ifdef __WIN32 13 #ifdef __WIN32
14 #include <shlwapi.h> 14 #include <shlwapi.h>
15 #include <shfolder.h> 15 #include <shfolder.h>
348 348
349 return TRUE; 349 return TRUE;
350 } 350 }
351 351
352 352
353 BOOL nn_conn_send_msg(th_conn_t *conn, const char *user, const char *str)
354 {
355 char *msg;
356
357 if (str == NULL)
358 return FALSE;
359
360 msg = th_strdup_printf("<USER>%s</USER><MESSAGE>%s</MESSAGE>", user, str);
361
362 if (msg != NULL)
363 {
364 BOOL ret = nn_conn_send_buf(conn, msg, strlen(msg) + 1);
365 th_free(msg);
366 return ret;
367 }
368 else
369 return FALSE;
370 }
371
372
373 BOOL nn_conn_send_msg_v(th_conn_t *conn, const char *user, const char *fmt, ...)
374 {
375 BOOL res;
376 char *tmp;
377 va_list ap;
378
379 va_start(ap, fmt);
380 tmp = th_strdup_vprintf(fmt, ap);
381 va_end(ap);
382
383 res = nn_conn_send_msg(conn, user, tmp);
384 th_free(tmp);
385 return res;
386 }
387
388
353 int printFile(FILE *outFile, const char *fmt) 389 int printFile(FILE *outFile, const char *fmt)
354 { 390 {
355 const char *s = fmt; 391 const char *s = fmt;
356 392
357 while (*s) 393 while (*s)
528 va_end(ap); 564 va_end(ap);
529 } 565 }
530 } 566 }
531 567
532 568
533 void nn_network_errfunc(struct _nn_conn_t *conn, const char *msg) 569 void nn_network_errfunc(struct _th_conn_t *conn, const char *msg)
534 { 570 {
535 (void) conn; 571 (void) conn;
536 errorMsg("%s", msg); 572 errorMsg("%s", msg);
537 } 573 }
538 574
539 575
540 void nn_network_msgfunc(struct _nn_conn_t *conn, const char *msg) 576 void nn_network_msgfunc(struct _th_conn_t *conn, const char *msg)
541 { 577 {
542 (void) conn; 578 (void) conn;
543 printMsgConst(NULL, LOG_STAMP | LOG_WINDOW | LOG_FILE, msg); 579 printMsgConst(NULL, LOG_STAMP | LOG_WINDOW | LOG_FILE, msg);
544 } 580 }
545 581
571 607
572 return FALSE; 608 return FALSE;
573 } 609 }
574 610
575 611
576 int nnproto_parse_user(nn_conn_t *conn) 612 int nnproto_parse_user(th_conn_t *conn)
577 { 613 {
578 BOOL isMine, isIgnored = FALSE; 614 BOOL isMine, isIgnored = FALSE;
579 char *name, *msg, *t; 615 char *name, *msg, *t;
580 616
581 // Find start of the message 617 // Find start of the message
582 name = conn->ptr; 618 name = conn->ptr;
583 t = nn_conn_buf_strstr(conn, "</USER>"); 619 t = th_conn_buf_strstr(conn, "</USER>");
584 if (!t) return 1; 620 if (!t) return 1;
585 *t = 0; 621 *t = 0;
586 622
587 // Find end of the message 623 // Find end of the message
588 t = nn_conn_buf_strstr(conn, "<MESSAGE>"); 624 t = th_conn_buf_strstr(conn, "<MESSAGE>");
589 if (!t) return 2; 625 if (!t) return 2;
590 msg = conn->ptr; 626 msg = conn->ptr;
591 627
592 t = nn_conn_buf_strstr(conn, "</MESSAGE>"); 628 t = th_conn_buf_strstr(conn, "</MESSAGE>");
593 if (!t) return 3; 629 if (!t) return 3;
594 *t = 0; 630 *t = 0;
595 631
596 // Decode message string 632 // Decode message string
597 msg = nn_decode_str1(msg); 633 msg = nn_decode_str1(msg);
713 th_free(name); 749 th_free(name);
714 return 0; 750 return 0;
715 } 751 }
716 752
717 753
718 int nnproto_parse_login(nn_conn_t *conn) 754 int nnproto_parse_login(th_conn_t *conn)
719 { 755 {
720 char tmpStr[256]; 756 char tmpStr[256];
721 str_get_timestamp(tmpStr, sizeof(tmpStr), "%c"); 757 str_get_timestamp(tmpStr, sizeof(tmpStr), "%c");
722 758
723 if (!nn_conn_buf_strcmp(conn, "FAILURE>")) 759 if (!th_conn_buf_strcmp(conn, "FAILURE>"))
724 { 760 {
725 nn_conn_buf_strstr(conn, "</LOGIN_FAILURE>"); 761 th_conn_buf_strstr(conn, "</LOGIN_FAILURE>");
726 nn_conn_buf_strstr(conn, "</USER>"); 762 th_conn_buf_strstr(conn, "</USER>");
727 printMsg(NULL, "½1½Login failure½0½ - ½3½%s½0½\n", tmpStr); 763 printMsg(NULL, "½1½Login failure½0½ - ½3½%s½0½\n", tmpStr);
728 return -2; 764 return -2;
729 } 765 }
730 else if (!nn_conn_buf_strcmp(conn, "SUCCESS>")) 766 else if (!th_conn_buf_strcmp(conn, "SUCCESS>"))
731 { 767 {
732 nn_conn_buf_strstr(conn, "</LOGIN_SUCCESS>"); 768 th_conn_buf_strstr(conn, "</LOGIN_SUCCESS>");
733 nn_conn_buf_strstr(conn, "</USER>"); 769 th_conn_buf_strstr(conn, "</USER>");
734 printMsg(NULL, "½2½Login success½0½ - ½3½%s½0½\n", tmpStr); 770 printMsg(NULL, "½2½Login success½0½ - ½3½%s½0½\n", tmpStr);
735 nn_conn_send_msg(conn, optUserNameEnc, "%2FRequestUserList"); 771 nn_conn_send_msg(conn, optUserNameEnc, "%2FRequestUserList");
736 return 0; 772 return 0;
737 } 773 }
738 else 774 else
739 return 1; 775 return 1;
740 } 776 }
741 777
742 778
743 int nnproto_parse_add_user(nn_conn_t *conn) 779 int nnproto_parse_add_user(th_conn_t *conn)
744 { 780 {
745 char *p, *s, *str = conn->ptr; 781 char *p, *s, *str = conn->ptr;
746 nn_window_t *win; 782 nn_window_t *win;
747 783
748 s = nn_conn_buf_strstr(conn, "</ADD_USER>"); 784 s = th_conn_buf_strstr(conn, "</ADD_USER>");
749 if (!s) return 1; 785 if (!s) return 1;
750 *s = 0; 786 *s = 0;
751 787
752 p = nn_dbldecode_str(str); 788 p = nn_dbldecode_str(str);
753 if (!p) 789 if (!p)
766 th_free(p); 802 th_free(p);
767 return 0; 803 return 0;
768 } 804 }
769 805
770 806
771 int nnproto_parse_delete_user(nn_conn_t *conn) 807 int nnproto_parse_delete_user(th_conn_t *conn)
772 { 808 {
773 char *p, *s, *str = conn->ptr; 809 char *p, *s, *str = conn->ptr;
774 nn_window_t *win; 810 nn_window_t *win;
775 811
776 s = nn_conn_buf_strstr(conn, "</DELETE_USER>"); 812 s = th_conn_buf_strstr(conn, "</DELETE_USER>");
777 if (!s) return 1; 813 if (!s) return 1;
778 *s = 0; 814 *s = 0;
779 815
780 p = nn_dbldecode_str(str); 816 p = nn_dbldecode_str(str);
781 if (!p) 817 if (!p)
794 th_free(p); 830 th_free(p);
795 return 0; 831 return 0;
796 } 832 }
797 833
798 834
799 int nnproto_parse_num_clients(nn_conn_t *conn) 835 int nnproto_parse_num_clients(th_conn_t *conn)
800 { 836 {
801 nn_conn_buf_strstr(conn, "</NUMCLIENTS>"); 837 th_conn_buf_strstr(conn, "</NUMCLIENTS>");
802 return 0; 838 return 0;
803 } 839 }
804 840
805 841
806 int nnproto_parse_boot(nn_conn_t *conn) 842 int nnproto_parse_boot(th_conn_t *conn)
807 { 843 {
808 (void) conn; 844 (void) conn;
809 errorMsg("Booted by server.\n"); 845 errorMsg("Booted by server.\n");
810 return -1; 846 return -1;
811 } 847 }
813 849
814 typedef struct 850 typedef struct
815 { 851 {
816 char *name; 852 char *name;
817 size_t len; 853 size_t len;
818 int (*handler)(nn_conn_t *); 854 int (*handler)(th_conn_t *);
819 } nn_protocolcmd_t; 855 } nn_protocolcmd_t;
820 856
821 857
822 static nn_protocolcmd_t protoCmds[] = 858 static nn_protocolcmd_t protoCmds[] =
823 { 859 {
830 }; 866 };
831 867
832 static const int nprotoCmds = sizeof(protoCmds) / sizeof(protoCmds[0]); 868 static const int nprotoCmds = sizeof(protoCmds) / sizeof(protoCmds[0]);
833 869
834 870
835 int nn_parse_protocol(nn_conn_t *conn) 871 int nn_parse_protocol(th_conn_t *conn)
836 { 872 {
837 static BOOL protoCmdsInit = FALSE; 873 static BOOL protoCmdsInit = FALSE;
838 int i; 874 int i;
839 875
840 if (!protoCmdsInit) 876 if (!protoCmdsInit)
845 protoCmdsInit = TRUE; 881 protoCmdsInit = TRUE;
846 } 882 }
847 883
848 for (i = 0; i < nprotoCmds; i++) 884 for (i = 0; i < nprotoCmds; i++)
849 { 885 {
850 if (!nn_conn_buf_strncmp(conn, protoCmds[i].name, protoCmds[i].len)) 886 if (!th_conn_buf_strncmp(conn, protoCmds[i].name, protoCmds[i].len))
851 return protoCmds[i].handler(conn); 887 return protoCmds[i].handler(conn);
852 } 888 }
853 889
854 if (optDebug) 890 if (optDebug)
855 { 891 {
903 939
904 return 0; 940 return 0;
905 } 941 }
906 942
907 943
908 int nncmd_send_raw(nn_conn_t *conn, char *str) 944 int nncmd_send_raw(th_conn_t *conn, char *str)
909 { 945 {
910 #if 1 946 #if 1
911 char *tmp = nn_encode_str1(str); 947 char *tmp = nn_encode_str1(str);
912 if (tmp == NULL) return -2; 948 if (tmp == NULL) return -2;
913 nn_conn_send_msg(conn, optUserNameEnc, tmp); 949 nn_conn_send_msg(conn, optUserNameEnc, tmp);
918 954
919 return 0; 955 return 0;
920 } 956 }
921 957
922 958
923 int nncmd_open_profile(nn_conn_t *conn, char *name) 959 int nncmd_open_profile(th_conn_t *conn, char *name)
924 { 960 {
925 char *enc_name = nn_encode_str1(name); 961 char *enc_name = nn_encode_str1(name);
926 char *uri = th_strdup_printf(SET_PROFILE_PREFIX, name); 962 char *uri = th_strdup_printf(SET_PROFILE_PREFIX, name);
927 (void) conn; 963 (void) conn;
928 964
934 th_free(enc_name); 970 th_free(enc_name);
935 return 0; 971 return 0;
936 } 972 }
937 973
938 974
939 int nncmd_change_list(nn_conn_t *conn, const char *listname, qlist_t **list, const char *name) 975 int nncmd_change_list(th_conn_t *conn, const char *listname, qlist_t **list, const char *name)
940 { 976 {
941 (void) conn; 977 (void) conn;
942 978
943 if (name[0]) 979 if (name[0])
944 { 980 {
977 1013
978 return 0; 1014 return 0;
979 } 1015 }
980 1016
981 1017
982 int nncmd_ignore(nn_conn_t *conn, char *name) 1018 int nncmd_ignore(th_conn_t *conn, char *name)
983 { 1019 {
984 return nncmd_change_list(conn, "ignore", &setIgnoreList, name); 1020 return nncmd_change_list(conn, "ignore", &setIgnoreList, name);
985 } 1021 }
986 1022
987 1023
988 int nncmd_friend(nn_conn_t *conn, char *name) 1024 int nncmd_friend(th_conn_t *conn, char *name)
989 { 1025 {
990 return nncmd_change_list(conn, "friend", &setFriendList, name); 1026 return nncmd_change_list(conn, "friend", &setFriendList, name);
991 } 1027 }
992 1028
993 1029
994 int nncmd_set_color(nn_conn_t *conn, char *arg) 1030 int nncmd_set_color(th_conn_t *conn, char *arg)
995 { 1031 {
996 int val; 1032 int val;
997 (void) conn; 1033 (void) conn;
998 1034
999 if ((val = th_get_hex_triplet(arg)) < 0) 1035 if ((val = th_get_hex_triplet(arg)) < 0)
1007 nn_conn_send_msg_v(conn, optUserNameEnc, "%%2FSetFontColor%%20%%2Dcolor%%20%06X", optUserColor); 1043 nn_conn_send_msg_v(conn, optUserNameEnc, "%%2FSetFontColor%%20%%2Dcolor%%20%06X", optUserColor);
1008 return 0; 1044 return 0;
1009 } 1045 }
1010 1046
1011 1047
1012 int nncmd_open_query(nn_conn_t *conn, char *name) 1048 int nncmd_open_query(th_conn_t *conn, char *name)
1013 { 1049 {
1014 (void) conn; 1050 (void) conn;
1015 1051
1016 if (name[0]) 1052 if (name[0])
1017 { 1053 {
1040 return 1; 1076 return 1;
1041 } 1077 }
1042 } 1078 }
1043 1079
1044 1080
1045 int nncmd_close_query(nn_conn_t *conn, char *name) 1081 int nncmd_close_query(th_conn_t *conn, char *name)
1046 { 1082 {
1047 (void) conn; 1083 (void) conn;
1048 1084
1049 if (name[0]) 1085 if (name[0])
1050 { 1086 {
1076 1112
1077 return 0; 1113 return 0;
1078 } 1114 }
1079 1115
1080 1116
1081 int nncmd_window_info(nn_conn_t *conn, char *arg) 1117 int nncmd_window_info(th_conn_t *conn, char *arg)
1082 { 1118 {
1083 (void) conn; 1119 (void) conn;
1084 1120
1085 if (arg[0]) 1121 if (arg[0])
1086 { 1122 {
1101 } 1137 }
1102 return 0; 1138 return 0;
1103 } 1139 }
1104 1140
1105 1141
1106 int nncmd_list_all_users(nn_conn_t *conn, char *buf) 1142 int nncmd_list_all_users(th_conn_t *conn, char *buf)
1107 { 1143 {
1108 (void) buf; 1144 (void) buf;
1109 1145
1110 // Alias /listallusers 1146 // Alias /listallusers
1111 return nn_conn_send_msg(conn, optUserNameEnc, "%2Flistallusers"); 1147 return nn_conn_send_msg(conn, optUserNameEnc, "%2Flistallusers");
1129 nncmd_namedata_t *d = data; 1165 nncmd_namedata_t *d = data;
1130 char name[NAME_ENTRY_SIZE]; 1166 char name[NAME_ENTRY_SIZE];
1131 size_t len; 1167 size_t len;
1132 int color; 1168 int color;
1133 1169
1134 if (checkNameList(setFriendList, user->name)) 1170 if (nn_check_name_list(setFriendList, user->name))
1135 color = 11; 1171 color = 11;
1136 else 1172 else
1137 if (checkNameList(setIgnoreList, user->name)) 1173 if (nn_check_name_list(setIgnoreList, user->name))
1138 color = 1; 1174 color = 1;
1139 else 1175 else
1140 color = 3; 1176 color = 3;
1141 1177
1142 snprintf(name, sizeof(name), "[½%d½%-20s½0½] ", color, user->name); 1178 snprintf(name, sizeof(name), "[½%d½%-20s½0½] ", color, user->name);
1156 1192
1157 return 0; 1193 return 0;
1158 } 1194 }
1159 1195
1160 1196
1161 int nncmd_names(nn_conn_t *conn, char *arg) 1197 int nncmd_names(th_conn_t *conn, char *arg)
1162 { 1198 {
1163 nncmd_namedata_t data; 1199 nncmd_namedata_t data;
1164 (void) conn; 1200 (void) conn;
1165 (void) arg; 1201 (void) arg;
1166 1202
1178 printMsgQ(currWin, "%d users total.\n", data.total); 1214 printMsgQ(currWin, "%d users total.\n", data.total);
1179 return 0; 1215 return 0;
1180 } 1216 }
1181 1217
1182 1218
1183 int nncmd_save_config(nn_conn_t *conn, char *buf) 1219 int nncmd_save_config(th_conn_t *conn, char *buf)
1184 { 1220 {
1185 (void) conn; 1221 (void) conn;
1186 (void) buf; 1222 (void) buf;
1187 th_ioctx_t ctx; 1223 th_ioctx_t ctx;
1188 #ifndef __WIN32 1224 #ifndef __WIN32
1215 th_ioctx_close(&ctx); 1251 th_ioctx_close(&ctx);
1216 return 0; 1252 return 0;
1217 } 1253 }
1218 1254
1219 1255
1220 int nncmd_quit(nn_conn_t *conn, char *buf) 1256 int nncmd_quit(th_conn_t *conn, char *buf)
1221 { 1257 {
1222 (void) conn; 1258 (void) conn;
1223 (void) buf; 1259 (void) buf;
1224 1260
1225 appQuitFlag = TRUE; 1261 appQuitFlag = TRUE;
1238 typedef struct 1274 typedef struct
1239 { 1275 {
1240 char *name; 1276 char *name;
1241 int flags; 1277 int flags;
1242 size_t len; 1278 size_t len;
1243 int (*handler)(nn_conn_t *, char *buf); 1279 int (*handler)(th_conn_t *, char *buf);
1244 } nn_usercmd_t; 1280 } nn_usercmd_t;
1245 1281
1246 1282
1247 static nn_usercmd_t userCmdsTable[] = 1283 static nn_usercmd_t userCmdsTable[] =
1248 { 1284 {
1287 userCmdsTable[i].len = strlen(userCmdsTable[i].name); 1323 userCmdsTable[i].len = strlen(userCmdsTable[i].name);
1288 } 1324 }
1289 } 1325 }
1290 1326
1291 1327
1292 int nn_handle_command(nn_conn_t *conn, char *buf) 1328 int nn_handle_command(th_conn_t *conn, char *buf)
1293 { 1329 {
1294 qlist_t *curr; 1330 qlist_t *curr;
1295 1331
1296 for (curr = userCmds; curr != NULL; curr = curr->next) 1332 for (curr = userCmds; curr != NULL; curr = curr->next)
1297 { 1333 {
1396 1432
1397 return NULL; 1433 return NULL;
1398 } 1434 }
1399 1435
1400 1436
1401 int nn_handle_input(nn_conn_t *conn, char *buf, size_t bufLen) 1437 int nn_handle_input(th_conn_t *conn, char *buf, size_t bufLen)
1402 { 1438 {
1403 BOOL result; 1439 BOOL result;
1404 char *tmp; 1440 char *tmp;
1405 1441
1406 // Trim right side 1442 // Trim right side
1934 int main(int argc, char *argv[]) 1970 int main(int argc, char *argv[])
1935 { 1971 {
1936 char *tmpStr; 1972 char *tmpStr;
1937 int index, updateCount = 0; 1973 int index, updateCount = 0;
1938 BOOL argsOK, colorSet = FALSE; 1974 BOOL argsOK, colorSet = FALSE;
1939 nn_conn_t *conn = NULL; 1975 th_conn_t *conn = NULL;
1940 nn_editbuf_t *editBuf = nn_editbuf_new(NN_TMPBUF_SIZE); 1976 nn_editbuf_t *editBuf = nn_editbuf_new(NN_TMPBUF_SIZE);
1941 nn_editstate_t editState; 1977 nn_editstate_t editState;
1942 th_cfgitem_t *tmpcfg; 1978 th_cfgitem_t *tmpcfg;
1943 char *setHomeDir = NULL, *setProxyURI = NULL; 1979 char *setHomeDir = NULL, *setProxyURI = NULL;
1944 1980
2168 errorMsg("Username and/or password not specified.\n"); 2204 errorMsg("Username and/or password not specified.\n");
2169 goto err_exit; 2205 goto err_exit;
2170 } 2206 }
2171 2207
2172 // Create a connection 2208 // Create a connection
2173 conn = nn_conn_new(nn_network_errfunc, nn_network_msgfunc); 2209 conn = th_conn_new(nn_network_errfunc, nn_network_msgfunc);
2174 if (conn == NULL) 2210 if (conn == NULL)
2175 { 2211 {
2176 errorMsg("Could not create connection structure.\n"); 2212 errorMsg("Could not create connection structure.\n");
2177 goto err_exit; 2213 goto err_exit;
2178 } 2214 }
2183 if (optProxyType != NN_PROXY_NONE && optProxyServer != NULL) 2219 if (optProxyType != NN_PROXY_NONE && optProxyServer != NULL)
2184 { 2220 {
2185 if (optProxyUserID == NULL) 2221 if (optProxyUserID == NULL)
2186 optProxyUserID = "James Bond"; 2222 optProxyUserID = "James Bond";
2187 2223
2188 if (nn_conn_set_proxy(conn, optProxyType, optProxyPort, optProxyServer, optProxyAuthType) != 0 || 2224 if (th_conn_set_proxy(conn, optProxyType, optProxyPort, optProxyServer, optProxyAuthType) != 0 ||
2189 nn_conn_set_proxy_auth_user(conn, optProxyUserID, optProxyPassword) != 0) 2225 th_conn_set_proxy_auth_user(conn, optProxyUserID, optProxyPassword) != 0)
2190 { 2226 {
2191 errorMsg("Error setting proxy information.\n"); 2227 errorMsg("Error setting proxy information.\n");
2192 goto err_exit; 2228 goto err_exit;
2193 } 2229 }
2194 } 2230 }
2201 2237
2202 #ifdef FINAL_BUILD 2238 #ifdef FINAL_BUILD
2203 /* To emulate the official client, we first make a request for 2239 /* To emulate the official client, we first make a request for
2204 * policy file, even though we don't use it for anything... 2240 * policy file, even though we don't use it for anything...
2205 */ 2241 */
2206 if (nn_conn_open(conn, 843, NULL) != 0) 2242 if (th_conn_open(conn, 843, NULL) != 0)
2207 { 2243 {
2208 errorMsg("Policy file request connection setup failed!\n"); 2244 errorMsg("Policy file request connection setup failed!\n");
2209 goto err_exit; 2245 goto err_exit;
2210 } 2246 }
2211 2247
2212 tmpStr = "<policy-file-request/>"; 2248 tmpStr = "<policy-file-request/>";
2213 if (nn_conn_send_buf(conn, tmpStr, strlen(tmpStr) + 1) == FALSE) 2249 if (th_conn_send_buf(conn, tmpStr, strlen(tmpStr) + 1) == FALSE)
2214 { 2250 {
2215 errorMsg("Failed to send policy file request.\n"); 2251 errorMsg("Failed to send policy file request.\n");
2216 goto err_exit; 2252 goto err_exit;
2217 } 2253 }
2218 else 2254 else
2219 { 2255 {
2220 int cres = nn_conn_pull(conn); 2256 int cres = th_conn_pull(conn);
2221 if (cres == 0) 2257 if (cres == 0)
2222 { 2258 {
2223 printMsg(currWin, "Probe got: %s\n", conn->buf); 2259 printMsg(currWin, "Probe got: %s\n", conn->buf);
2224 } 2260 }
2225 else 2261 else
2226 { 2262 {
2227 printMsg(currWin, "Could not get policy probe.\n"); 2263 printMsg(currWin, "Could not get policy probe.\n");
2228 } 2264 }
2229 } 2265 }
2230 nn_conn_free(conn); 2266 th_conn_free(conn);
2231 #endif 2267 #endif
2232 2268
2233 // Okay, now do the proper connection ... 2269 // Okay, now do the proper connection ...
2234 if (nn_conn_open(conn, optPort, NULL) != 0) 2270 if (th_conn_open(conn, optPort, NULL) != 0)
2235 { 2271 {
2236 errorMsg("Main connection setup failed!\n"); 2272 errorMsg("Main connection setup failed!\n");
2237 goto err_exit; 2273 goto err_exit;
2238 } 2274 }
2239 2275
2249 // Initialize random numbers 2285 // Initialize random numbers
2250 editState.prevKeepAlive = time(NULL); 2286 editState.prevKeepAlive = time(NULL);
2251 srandom((int) editState.prevKeepAlive); 2287 srandom((int) editState.prevKeepAlive);
2252 2288
2253 // Enter mainloop 2289 // Enter mainloop
2254 nn_conn_reset(conn); 2290 th_conn_reset(conn);
2255 while (!editState.isError && !appQuitFlag) 2291 while (!editState.isError && !appQuitFlag)
2256 { 2292 {
2257 int retries = 3, cres; 2293 int retries = 3, cres;
2258 editState.update = FALSE; 2294 editState.update = FALSE;
2259 2295
2260 packet_retry: 2296 packet_retry:
2261 cres = nn_conn_pull(conn); 2297 cres = th_conn_pull(conn);
2262 if (cres == 0) 2298 if (cres == 0)
2263 { 2299 {
2264 while (conn->ptr < conn->in_ptr && 2300 while (conn->ptr < conn->in_ptr &&
2265 *(conn->in_ptr - 1) == 0 && 2301 *(conn->in_ptr - 1) == 0 &&
2266 retries > 0 && !editState.isError) 2302 retries > 0 && !editState.isError)
2267 { 2303 {
2268 // nn_conn_dump_buffer(stderr, conn); 2304 // th_conn_dump_buffer(stderr, conn);
2269 int result = nn_parse_protocol(conn); 2305 int result = nn_parse_protocol(conn);
2270 if (result == 0) 2306 if (result == 0)
2271 { 2307 {
2272 nn_conn_buf_skip(conn, 1); 2308 th_conn_buf_skip(conn, 1);
2273 } 2309 }
2274 else 2310 else
2275 if (result > 0) 2311 if (result > 0)
2276 { 2312 {
2277 // Retry if possible 2313 // Retry if possible
2278 if (--retries > 0) 2314 if (--retries > 0)
2279 goto packet_retry; 2315 goto packet_retry;
2280 2316
2281 // Couldn't handle the message for some reason 2317 // Couldn't handle the message for some reason
2282 printMsg(currWin, "Could not handle: %s\n", conn->ptr); 2318 printMsg(currWin, "Could not handle: %s\n", conn->ptr);
2283 nn_conn_buf_skip(conn, strlen(conn->ptr) + 1); 2319 th_conn_buf_skip(conn, strlen(conn->ptr) + 1);
2284 } 2320 }
2285 else 2321 else
2286 editState.isError = TRUE; 2322 editState.isError = TRUE;
2287 } 2323 }
2288 } 2324 }
2289 else 2325 else
2290 if (cres < 0 || !nn_conn_check(conn)) 2326 if (cres < 0 || !th_conn_check(conn))
2291 editState.isError = TRUE; 2327 editState.isError = TRUE;
2292 2328
2293 // Handle user input 2329 // Handle user input
2294 BOOL flushed = FALSE; 2330 BOOL flushed = FALSE;
2295 if (appCursesInit) 2331 if (appCursesInit)
2351 if (errorMessages) 2387 if (errorMessages)
2352 THERR("%s", errorMessages); 2388 THERR("%s", errorMessages);
2353 #endif 2389 #endif
2354 2390
2355 th_free(optUserNameEnc); 2391 th_free(optUserNameEnc);
2356 nn_conn_free(conn); 2392 th_conn_free(conn);
2357 nn_network_close(); 2393 nn_network_close();
2358 2394
2359 THMSG(1, "Connection terminated.\n"); 2395 THMSG(1, "Connection terminated.\n");
2360 2396
2361 return 0; 2397 return 0;