#include "main.h" #include "readline.h" #include #include #include #include #include #include #include #include "wind.h" /* * Entry point of mesms and event loop */ #ifdef DEBUG #include #endif const int color[] = { COLOR_BLACK, COLOR_RED, COLOR_GREEN, COLOR_YELLOW, COLOR_BLUE, COLOR_MAGENTA, COLOR_CYAN, COLOR_WHITE}; #define MAX_PATH 1024 extern char* msg_win_str; extern conversation* currentConv; static bool visual_mode = false; WINDOW *cmd_win; WINDOW *sep_win; char status[10]; extern int firstSMSIndex; extern int canAugment; extern int showName; #define DEBUG void fail_exit(const char *msg) { if (visual_mode) endwin(); fprintf(stderr, "%s\n", msg); exit(EXIT_FAILURE); } int main(int argc, char* argv[]){ #ifdef DEBUG freopen("/tmp/mesms.debug", "w", stderr); #endif /* Gestion des paramètres */ while (1) { int c; int optIndex = 0; static struct option optlv[] = { {"help", no_argument, 0, 'h' }, {"version", no_argument, 0, 'v' }, {"contact", required_argument, 0, 'c' }, {NULL, 0, 0, 0 } }; c = getopt_long(argc, argv, "hvc:n:", optlv, &optIndex); if (c == -1) break; switch (c) { case 'v': if( !strcmp(optlv[optIndex].name, "version") ) { puts( VERSION_MESMS ); } break; case 'h': puts( HELP_STRING_MESMS ); return EXIT_SUCCESS; break; case 'c': if( optarg ) { int r = 0; if( r = fillContactList(DEFAULT_CONTACT_FILENAME) ) { fprintf(stderr, "Erreur à la lecture de la liste des contacts"); return r; } if( r = loadConv() ) { fprintf(stderr, "Erreur à la lecture de la liste des contacts"); return r; } contact* contact = NULL; int nameOrNumber = 0; if( nameOrNumber = (*optarg == '+' ) ) r = findContact(optarg, &contact); else r = findContactFromName(optarg, &contact); if( r ) { fprintf(stderr, "Contact introuvable"); printf("%s",optarg); return r; } if( nameOrNumber ) printf("%s", contact->display_name); else printf("%s", contact->number); return 0; } else { fprintf(stderr, "Numéro de téléphone manquant"); } break; case '?': default: return EXIT_FAILURE; } } fprintf(stderr, "test"); /* Initialisation */ int size = 0; int c; int comp = 0; setlocale(LC_ALL, ""); initscr(); if (has_colors()) { CHECK(start_color); } CHECK(noecho); CHECK(nonl); CHECK(intrflush, stdscr, FALSE); CHECK(keypad, stdscr, TRUE); visual_mode = true; curs_set(0); cmd_win = newwin(1, COLS, LINES - 1, 0); sep_win = newwin(1, COLS, LINES - 2, 0); init_pair( 12, COLOR_WHITE, COLOR_BLUE); init_pair( 11, COLOR_RED, COLOR_RED); init_pair( 10, COLOR_GREEN, COLOR_GREEN); CHECK(wbkgd, sep_win, COLOR_PAIR(12)); CHECK(wrefresh, sep_win); init_readline(); int r = 0; fprintf(stderr, "test1"); if( r = fillContactList(DEFAULT_CONTACT_FILENAME) ) { return r; } if( r = loadConv() ) { return r; } fprintf(stderr, "test2"); sortContacts(); fprintf(stderr, "test3"); for(int i = 0 ; i < 9 ; i++) { init_pair( i+1, i, COLOR_BLACK); } /* Premier affichage */ resize(); int tabed = 0; /* Event loop */ while( c = getch() ) { if (c == KEY_RESIZE) { resize(); refresh(); continue; } if ( c == 'q' ) { break; } switch( c ) { case KEY_DOWN: case KEY_RIGHT: case 'j': if( !tabed ) { selectNextContact(); showContactListW(); } else { if( canAugment ) firstSMSIndex++; clearWind(); showMessageWind(); } break; case KEY_UP: case KEY_LEFT: case 'k': if( !tabed ) { selectPreviousContact(); showContactListW(); } else { if( firstSMSIndex > 0 ) firstSMSIndex--; canAugment=1; clearWind(); showMessageWind(); } break; case '\t': if( currentConv ) tabed = !tabed; break; case '/': // Search by name if( !tabed ) { contact* found = NULL; move(LINES-1,0); readline_n(); int r = findContactFromName(msg_win_str, &found); if( r == 0 ) { setContactSelection(found); } else { printf("[%d]%s\n", r, msg_win_str); } curs_set(0); showContactListW(); } break; case 'n': showName = !showName; showContactListW(); break; case '\r': { contact* tmp = getContactSelected(); currentConv = tmp->conversation; toTheEnd(); resize(); } break; case 'i': case 's': { char* number = NULL; if( ! currentConv ) { contact* tmp = getContactSelected(); if( !tmp ) break; number = tmp->number; } else number = currentConv->number; const char* defaultEditor = "vim", * const envEditor = getenv("EDITOR"), * const visEditor = getenv("VISUAL"); if( envEditor && envEditor[0] ) { defaultEditor = envEditor; } if( visEditor && visEditor[0] ) { defaultEditor = visEditor; } FILE* tmp = NULL; char fileName[] = "mesms_XXXXXX"; int fd = mkstemp(fileName); close(fd); char* cmd = malloc( 2 + strlen(defaultEditor) + strlen(fileName) ); sprintf( cmd, "%s %s", defaultEditor, fileName ); edit: system(cmd); { FILE* r = fopen(fileName,"r"); FILE* t = tmpfile(); int c = 0; int nb = 0; while( (c = fgetc(r)) != EOF ) { nb++; fputc(c,t); } char* string = malloc( nb+1 ); fclose(r); rewind(t); nb = 0; while( (c = fgetc(t)) != EOF ) { string[nb++] = c; } string[nb] = 0; contact* d = NULL; findContact(number, &d); int ret = 0; if( (ret = confirmSend(string, d->display_name)) == 1 ) { char* cmd = malloc(2048); sprintf( cmd, "cat %s | gammu-smsd-inject TEXT %s -len 400 -unicode", fileName, number); system(cmd); }else if( ret == 2 ) { goto edit; } } free(cmd); unlink( fileName ); resize(); } } } refresh(); end: endwin(); return 0; }