1 #include "../helper/locales.h"
2 
3 #include <gammu.h>
4 #include <string.h>
5 #include <stdlib.h>
6 #include <signal.h>
7 
8 #include "backup.h"
9 #include "memory.h"
10 #include "message.h"
11 #include "common.h"
12 #include "../helper/formats.h"
13 #include "../helper/cmdline.h"
14 
15 #include "../helper/message-display.h"
16 #include "../helper/printing.h"
17 #include "../libgammu/misc/string.h"
18 
BackupSMS(int argc UNUSED,char * argv[])19 void BackupSMS(int argc UNUSED, char *argv[])
20 {
21 	GSM_Error error;
22 	GSM_SMS_Backup		*Backup;
23 	GSM_MultiSMSMessage 	*sms;
24 	GSM_SMSFolders		folders;
25 	gboolean			BackupFromFolder[GSM_MAX_SMS_FOLDERS];
26 	gboolean			start = TRUE;
27 	gboolean			DeleteAfter = FALSE, askdelete = TRUE;
28 	int			j, smsnum = 0;
29 
30 	sms = malloc(sizeof(GSM_MultiSMSMessage));
31 	if (sms == NULL) {
32 		return;
33 	}
34 	Backup = malloc(sizeof(GSM_SMS_Backup));
35 	if (Backup == NULL) {
36 		free(sms);
37 		return;
38 	}
39 
40 	if (argc == 4) {
41 		if (strcasecmp(argv[3],"-yes") == 0) {
42 			always_answer_yes = TRUE;
43 		}
44 		if (strcasecmp(argv[3],"-all") == 0) {
45 			always_answer_yes = TRUE;
46 			askdelete = FALSE;
47 			DeleteAfter = FALSE;
48 		}
49 	}
50 
51 	GSM_Init(TRUE);
52 
53 	Backup->SMS[0] = NULL;
54 	sms->SMS[0].Location = 0;
55 	sms->Number = 0;
56 
57 	error=GSM_GetSMSFolders(gsm, &folders);
58 	Print_Error(error);
59 
60 	if (askdelete) {
61 		DeleteAfter = answer_yes("%s", _("Delete each sms after backup?"));
62 	}
63 
64 	for (j=0;j<folders.Number;j++) {
65 		BackupFromFolder[j] = FALSE;
66 		if (answer_yes(_("Backup sms from folder \"%s\"%s?"),
67 				DecodeUnicodeConsole(folders.Folder[j].Name),
68 				folders.Folder[j].Memory == MEM_SM ? _(" (SIM)") : ""
69 				))
70 			BackupFromFolder[j] = TRUE;
71 	}
72 
73 	while (error == ERR_NONE) {
74 		sms->SMS[0].Folder=0x00;
75 		error=GSM_GetNextSMS(gsm, sms, start);
76 		switch (error) {
77 		case ERR_EMPTY:
78 			break;
79 		default:
80 			Print_Error(error);
81 			for (j=0;j<sms->Number;j++) {
82 				if (BackupFromFolder[sms->SMS[j].Folder-1]) {
83 					switch (sms->SMS[j].PDU) {
84 					case SMS_Status_Report:
85 						break;
86 					case SMS_Submit:
87 					case SMS_Deliver:
88 						if (sms->SMS[j].Length == 0) break;
89 						if (smsnum < GSM_BACKUP_MAX_SMS) {
90 							Backup->SMS[smsnum] = malloc(sizeof(GSM_SMSMessage));
91 						        if (Backup->SMS[smsnum] == NULL) Print_Error(ERR_MOREMEMORY);
92 							Backup->SMS[smsnum + 1] = NULL;
93 						} else {
94 							printf(_("   Increase %s\n") , "GSM_BACKUP_MAX_SMS");
95 							GSM_Terminate();
96 							Terminate(1);
97 						}
98 						*(Backup->SMS[smsnum]) = sms->SMS[j];
99 						smsnum++;
100 						break;
101 					}
102 				}
103 			}
104 		}
105 		start=FALSE;
106 	}
107 
108 	error = GSM_AddSMSBackupFile(argv[2], Backup);
109 	Print_Error(error);
110 
111 	if (DeleteAfter) {
112 		for (j=0;j<smsnum;j++) {
113 			Backup->SMS[j]->Folder = 0;
114 			error=GSM_DeleteSMS(gsm, Backup->SMS[j]);
115 			Print_Error(error);
116 			fprintf(stderr, "\r");
117 			fprintf(stderr, "%s ", _("Deleting:"));
118 			fprintf(stderr, _("%i percent"),
119 				(j + 1) * 100 / smsnum);
120 		}
121 	}
122 
123 	GSM_FreeSMSBackup(Backup);
124 
125 	free(Backup);
126 	free(sms);
127 
128 	GSM_Terminate();
129 }
130 
AddSMS(int argc,char * argv[])131 void AddSMS(int argc, char *argv[])
132 {
133 	GSM_Error error;
134 	GSM_MultiSMSMessage 	*SMS;
135 	GSM_SMS_Backup		*Backup;
136 	int			smsnum = 0;
137 	int			folder;
138 
139 	if (argc == 5 && strcasecmp(argv[4],"-yes") == 0) always_answer_yes = TRUE;
140 
141 	SMS = malloc(sizeof(GSM_MultiSMSMessage));
142 	if (SMS == NULL) {
143 		return;
144 	}
145 	Backup = malloc(sizeof(GSM_SMS_Backup));
146 	if (Backup == NULL) {
147 		free(SMS);
148 		return;
149 	}
150 
151 
152 	folder = GetInt(argv[2]);
153 
154 	error = GSM_ReadSMSBackupFile(argv[3], Backup);
155 	Print_Error(error);
156 
157 	GSM_Init(TRUE);
158 
159 	while (Backup->SMS[smsnum] != NULL) {
160 		Backup->SMS[smsnum]->Folder = folder;
161 		Backup->SMS[smsnum]->SMSC.Location = 1;
162 		SMS->Number = 1;
163 		SMS->SMS[0] = *(Backup->SMS[smsnum]);
164 		DisplayMultiSMSInfo(SMS, FALSE, FALSE, NULL, gsm);
165 		if (answer_yes("%s", _("Restore message?"))) {
166 			error=GSM_AddSMS(gsm, Backup->SMS[smsnum]);
167 			Print_Error(error);
168 		}
169 		smsnum++;
170 	}
171 
172 	/* We don't need this anymore */
173 	GSM_FreeSMSBackup(Backup);
174 
175 	free(Backup);
176 	free(SMS);
177 
178 	GSM_Terminate();
179 }
180 
RestoreSMS(int argc,char * argv[])181 void RestoreSMS(int argc, char *argv[])
182 {
183 	GSM_Error error;
184 	GSM_MultiSMSMessage 	*SMS;
185 	GSM_SMS_Backup		*Backup;
186 	GSM_SMSFolders		folders;
187 	int			smsnum = 0;
188 	gboolean			restore8bit;
189 
190 	SMS = malloc(sizeof(GSM_MultiSMSMessage));
191 	if (SMS == NULL) {
192 		return;
193 	}
194 	Backup = malloc(sizeof(GSM_SMS_Backup));
195 	if (Backup == NULL) {
196 		free(SMS);
197 		return;
198 	}
199 
200 	if (argc == 4 && strcasecmp(argv[3],"-yes") == 0) always_answer_yes = TRUE;
201 
202 	error = GSM_ReadSMSBackupFile(argv[2], Backup);
203 	Print_Error(error);
204 
205 	restore8bit = answer_yes("%s", _("Do you want to restore binary SMS?"));
206 
207 	GSM_Init(TRUE);
208 
209 	error = GSM_GetSMSFolders(gsm, &folders);
210 	Print_Error(error);
211 
212 	while (Backup->SMS[smsnum] != NULL) {
213 		if (restore8bit || Backup->SMS[smsnum]->Coding != SMS_Coding_8bit) {
214 			SMS->Number = 1;
215 			memcpy(&(SMS->SMS[0]), Backup->SMS[smsnum], sizeof(GSM_SMSMessage));
216 			DisplayMultiSMSInfo(SMS, FALSE, FALSE, NULL, gsm);
217 			if (answer_yes(_("Restore %03i sms to folder \"%s\"%s?"),
218 					smsnum + 1,
219 					DecodeUnicodeConsole(folders.Folder[Backup->SMS[smsnum]->Folder - 1].Name),
220 					folders.Folder[Backup->SMS[smsnum]->Folder - 1].Memory == MEM_SM ? _(" (SIM)") : "")) {
221 				smprintf(gsm, _("saving %i SMS\n"),smsnum);
222 				error = GSM_AddSMS(gsm, Backup->SMS[smsnum]);
223 				Print_Error(error);
224 			}
225 		}
226 		smsnum++;
227 	}
228 
229 	/* We don't need this anymore */
230 	GSM_FreeSMSBackup(Backup);
231 
232 	free(Backup);
233 	free(SMS);
234 
235 	GSM_Terminate();
236 }
237 
238 /* How should editor hadle tabs in this file? Add editor commands here.
239  * vim: noexpandtab sw=8 ts=8 sts=8:
240  */
241 
242