source: titan/titan/inadyn.h @ 15288

Last change on this file since 15288 was 14583, checked in by nit, 12 years ago

fix

File size: 4.4 KB
Line 
1#ifndef INADYN_H
2#define INADYN_H
3
4int readinadyn(const char* filename, char** user, char** pw, char** host)
5{
6        debug(1000, "in");
7        FILE *fd = NULL;
8        char *fileline = NULL, *tmpstr = NULL;
9
10        fileline = malloc(MINMALLOC);
11        if(fileline == NULL)
12        {
13                err("no memory");
14                return 1;
15        }
16
17        fd = fopen(filename, "r");
18        if(fd == NULL)
19        {
20                perr("can't open %s", filename);
21                free(fileline);
22                return 1;
23        }
24
25        while(fgets(fileline, MINMALLOC, fd) != NULL)
26        {
27                if(fileline[0] == '#' || fileline[0] == '\n')
28                        continue;
29                if(fileline[strlen(fileline) - 1] == '\n')
30                        fileline[strlen(fileline) - 1] = '\0';
31                if(fileline[strlen(fileline) - 1] == '\r')
32                        fileline[strlen(fileline) - 1] = '\0';
33
34                tmpstr = ostrstrcase(fileline, "--username ");
35                if(tmpstr != NULL)
36                {
37                        tmpstr += 11;
38                        if(tmpstr != NULL)
39                        {
40                                tmpstr[strlen(tmpstr)] = '\0';
41                                free(*user); *user = NULL;
42                                *user = ostrcat(tmpstr, NULL, 0, 0);
43                        }                               
44                }
45                tmpstr = ostrstrcase(fileline, "--password ");
46                if(tmpstr != NULL)
47                {
48                        tmpstr += 11;
49                        if(tmpstr != NULL)
50                        {
51                                tmpstr[strlen(tmpstr)] = '\0';
52                                free(*pw); *pw = NULL;
53                                *pw = ostrcat(tmpstr, NULL, 0, 0);
54                        }                               
55                }
56                tmpstr = ostrstrcase(fileline, "--alias ");
57                if(tmpstr != NULL)
58                {
59                        tmpstr += 8;
60                        if(tmpstr != NULL)
61                        {
62                                tmpstr[strlen(tmpstr)] = '\0';
63                                free(*host); *host = NULL;
64                                *host = ostrcat(tmpstr, NULL, 0, 0);
65                        }                               
66                }
67        }
68
69        free(fileline);
70        fclose(fd);
71        return 0;
72}
73
74int writeinadyn(const char* filename, char* user, char* pw, char* host)
75{
76        char* savesettings = NULL;
77       
78        savesettings = ostrcat(savesettings, "--username ", 1, 0);
79        savesettings = ostrcat(savesettings, user, 1, 0);
80        savesettings = ostrcat(savesettings, "\n", 1, 0);
81
82        savesettings = ostrcat(savesettings, "--password ", 1, 0);
83        savesettings = ostrcat(savesettings, pw, 1, 0);
84        savesettings = ostrcat(savesettings, "\n", 1, 0);
85
86        savesettings = ostrcat(savesettings, "--alias ", 1, 0);
87        savesettings = ostrcat(savesettings, host, 1, 0);
88        savesettings = ostrcat(savesettings, "\n", 1, 0);
89       
90        savesettings = ostrcat(savesettings, "update_period 60000\n", 1, 0);
91
92        FILE* fd = fopen(filename, "w");
93        if(fd)
94        {
95                fprintf(fd, "%s\n", savesettings);
96                fclose(fd);
97        }
98        free(savesettings); savesettings = NULL;
99
100        return 0;
101}
102
103void screennetwork_inadyn()
104{
105        int rcret = -1, ret = 0;
106        struct skin* inadyn = getscreen("inadynsettings");
107        struct skin* listbox = getscreennode(inadyn, "listbox");
108        struct skin* startmode = getscreennode(inadyn, "startmode");
109        struct skin* user = getscreennode(inadyn, "user");
110        struct skin* pw = getscreennode(inadyn, "pw");
111        struct skin* host = getscreennode(inadyn, "host");
112        struct skin* tmp = NULL;
113
114        char* iuser = NULL, *ipw = NULL, *ihost = NULL;
115
116        readinadyn("/var/etc/inadyn.conf", &iuser, &ipw, &ihost);
117       
118        addchoicebox(startmode, "n", _("no"));
119        addchoicebox(startmode, "y", _("yes"));
120        setchoiceboxselection(startmode, getownconfig("inadyn"));
121
122        changeinput(user, iuser);
123        free(iuser); iuser = NULL;
124
125        changeinput(pw, ipw);
126        free(ipw); ipw = NULL;
127
128        changeinput(host, ihost);
129        free(ihost); ihost = NULL;
130
131        drawscreen(inadyn, 0);
132        addscreenrc(inadyn, listbox);
133
134        tmp = listbox->select;
135        while(1)
136        {
137                addscreenrc(inadyn, tmp);
138                rcret = waitrc(inadyn, 0, 0);
139                tmp = listbox->select;
140
141                if(rcret == getrcconfigint("rcexit", NULL)) break;
142
143                if(rcret == getrcconfigint("rcok", NULL) || rcret == getrcconfigint("rcgreen", NULL))
144                {
145                        writeinadyn("/var/etc/inadyn.conf", user->ret, pw->ret, host->ret);
146                        if(startmode->ret != NULL) addownconfig("inadyn", startmode->ret);
147                        if(rcret == getrcconfigint("rcok", NULL)) break;
148                        if(rcret == getrcconfigint("rcgreen", NULL))
149                        {
150                                system("killall inadyn; sleep 2; killall -9 inadyn");
151                                ret = system("inadyn --input_file /var/etc/inadyn.conf &");
152                                if(ret == 0)
153                                        textbox(_("Message"), _("DYNDNS started."), _("OK"), getrcconfigint("rcok", NULL), _("EXIT"), getrcconfigint("rcexit", NULL), NULL, 0, NULL, 0, 600, 200, 10, 0);
154                                else
155                                        textbox(_("Message"), _("DYNDNS not started,\nPlease check your config."), _("OK"), getrcconfigint("rcok", NULL), _("EXIT"), getrcconfigint("rcexit", NULL), NULL, 0, NULL, 0, 600, 200, 10, 0);
156                                drawscreen(inadyn, 0);
157                        }
158                }
159
160                if(rcret == getrcconfigint("rcyellow", NULL))
161                {
162                        system("killall inadyn; sleep 2; killall -9 inadyn");
163                        textbox(_("Message"), _("DYNDNS now stopped"), _("OK"), getrcconfigint("rcok", NULL), _("EXIT"), getrcconfigint("rcexit", NULL), NULL, 0, NULL, 0, 600, 200, 10, 0);
164                        drawscreen(inadyn, 0);
165                }
166        }
167
168        delownerrc(inadyn);
169        clearscreen(inadyn);
170}
171
172#endif
Note: See TracBrowser for help on using the repository browser.