1 /*
2   Copyright (c) 2005-2014 Freetalk Core Team
3   This file is part of Freetalk.
4 
5   Freetalk is free software; you can redistribute it and/or modify
6   it under the terms of the GNU General Public License as published
7   by the Free Software Foundation; either version 3 of the License,
8   or (at your option) any later version.
9 
10   Freetalk is distributed in the hope that it will be useful, but
11   WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   General Public License for more details.
14 
15   You should have received a copy of the GNU General Public License
16   along with this program.  If not, see
17   <http://www.gnu.org/licenses/>.
18 */
19 
20 #ifndef _CONFIG_H
21 #define _CONFIG_H
22 #include "config.h"
23 #endif
24 
25 #include <string.h>
26 #include <stdlib.h>
27 #include <stdarg.h>
28 
29 #include "util.h"
30 #include "freetalk.h"
31 
32 /*
33  * Parse a string containing a JID into its component parts and put them into
34  * the jid_t.
35  *
36  * Ref. Section 3.1, RFC 3920
37  */
38 
39 void
parse_jid_string(char * s,jid_t * jid)40 parse_jid_string (char *s, jid_t *jid)
41 {
42         char *str = g_strdup (s);
43         char *at = strchr (str, '@');
44         char *domain = str;
45         char *slash;
46 
47         if (at)
48         {
49                 *at = '\0';
50                 jid->node = g_strdup (str);
51                 domain = at+1;
52         }
53 
54         slash = strchr (domain, '/');
55 
56         if (slash)
57         {
58                 *slash = '\0';
59                 jid->domain = g_strdup (domain);
60                 jid->resource = g_strdup (slash+1);
61         }
62         else
63         {
64                 jid->domain = g_strdup (domain);
65                 /* Loudmouth complains if this is NULL */
66                 jid->resource = g_strdup ("GNU Freetalk");
67         }
68 
69         g_free (str);
70 }
71 
72 char *
first_word(char * full_line)73 first_word (char *full_line)
74 {
75         while (full_line && (*full_line == ' '))
76                 full_line ++;
77         return full_line;
78 }
79 
80 char *
second_word(char * full_line)81 second_word (char *full_line)
82 {
83         if (!full_line)
84                 return NULL;
85 
86 
87         while (full_line &&
88                ( *full_line == ' ' ||
89                  *full_line == '\t' ||
90                  *full_line == '\r' ||
91                  *full_line == '\n'))
92                 full_line ++;
93 
94         if (!full_line)
95                 return NULL;
96 
97         while (full_line &&
98                *full_line != ' ' &&
99                *full_line != '\t' &&
100                *full_line != '\r' &&
101                *full_line != '\n')
102                 full_line ++;
103 
104         if (!full_line)
105                 return NULL;
106 
107         while (full_line &&
108                ( *full_line == ' ' ||
109                  *full_line == '\t' ||
110                  *full_line == '\r' ||
111                  *full_line == '\n'))
112                 full_line ++;
113 
114         return *full_line ? full_line : NULL;
115 }
116 
117 
118 void
async_printf(const char * fmt,va_list ap)119 async_printf (const char *fmt, va_list ap)
120 {
121         int tmp_rl_point = rl_point;
122         int n = rl_end;
123         unsigned int i;
124 
125         if (rl_end >= 0 ) {
126                 rl_kill_text (0, rl_end);
127                 rl_redisplay ();
128         }
129         printf ("\r");
130         for (i=0 ; i<=strlen (state.prompt) ; i++)
131                 printf (" ");
132         printf ("\r");
133         vprintf (fmt, ap);
134         printf ("\n");
135         fflush(stdout);
136         if (n) {
137                 rl_do_undo ();
138                 rl_point = tmp_rl_point;
139                 rl_reset_line_state ();
140         }
141         rl_forced_update_display ();
142 }
143 
144 void
sync_printf(const char * fmt,va_list ap)145 sync_printf (const char *fmt, va_list ap)
146 {
147         vprintf (fmt, ap);
148         printf ("\n");
149         fflush (stdout);
150 }
151 
152 void
check_first_run(void)153 check_first_run (void)
154 {
155         if( system ("sh " DATADIR "/" PACKAGE_NAME "/extensions/first-time-run.sh") >> 8 )
156                 exit (1);
157 }
158