def clean_phone(self): phone = check_phone_validity(self.cleaned_data["phone"], self.cleaned_data["username"]) try: user = User.objects.get(username__iexact=self.cleaned_data["username"]) except User.DoesNotExist: log.error("Trying to reset password for unknown user [%s]" % self.cleaned_data["username"]) raise forms.ValidationError("Unknown User and Phone combination") if user.get_profile().phone != phone: log.error( "Not matching user/phone User: %s Real Phone: %d Attempted %d" % (self.cleaned_data["username"], user.get_profile().phone, phone) ) raise forms.ValidationError("Unknown User and Phone combination") return phone
def sent_info(sess): def build_complex_html(user, msgs): result = "%s%s" % (user_html(user, sess.user), info_html(" sent an SMS to ")) for i in msgs: result += "%s, " % user_html(user, i.to) result = result[:-2] if user != sess.user: result += build_html_reply(sess.user.id) return result msgs = SMSMessage.objects.filter(session=sess).order_by('to') if not msgs: log.error('%s: Session %d has no messages' % (user, sess.id)) return None return { 'date': sess.date, 'html': build_complex_html(user, msgs), 'message': msgs[0].message, }
def send_message(user, message, to_list, phone_reply=False): log.info('%s: Starting send_message(%s)' % (user, to_list)) ses = SMSSession(user=user, date=datetime.now(), reply_type=0) ses.save() fake_name = random.choice(NAMES) name_used = 0 for to in to_list: msg = SMSMessage(date=datetime.now(), by=user, to=to, session=ses, message=message, status=0, reply=phone_reply) try: msg.save() except: log.error('Exception saving') traceback.print_exc() try: log.info('Starting..') log.info('User: %s' % user.username) log.info('To: %s' % to.username) except: log.error('Exception pre-print') traceback.print_exc() try: log.info('Start pars parse') log.info('User2: %s' % user) log.info('fake_name: %s' % fake_name) log.info('ses.id: %d' % ses.id) except: log.error('Exception parse') traceback.print_exc() try: log.info(u'%d:%d: --- SMS --- [%s] -> [%s] : %s' % (ses.id, msg.id, user, to, message)) except: log.error('Exception printing info !') traceback.print_exc() if user.username != 'boris' and (to.username == 'admin' or to.username == 'murkin'): log.error('Error sending..') log.error(u'%s: Trying to send sms to %s [%s]' % (user, to, message)) return dynamic_addr = '%s <*****@*****.**>' % (user, fake_name, ses.id) log.info('%s: Dynamic address [%s]' % (user, dynamic_addr)) send_mail_message(message, dynamic_addr, "*****@*****.**" % to.get_profile().phone) name_used += 1 if name_used > 4: fake_name = random.choice(NAMES) name_used = 0
def parse_incoming_mail(string): ''' Get a new email and parse it ''' log.info('Parsing new email') # st = SMSTracker(date=datetime.datetime.now(), data=string, parsed=False) # st.save() msg = email.message_from_string(string) # Only SMS messages have a custom reply-to (with the number to reply to) if not 'Reply-To' in msg: log.info('Got an administrative email. Skipping') return else: log.info('--- New incoming SMS email ---') data = msg.get_payload() if not len(data): log.error('No payload !') exit() try: soup = BeautifulSoup(data) except: log.error('Error loading Soup') return # Get the sending phone number from the reply-to field try: reply_to = msg['Reply-To'] str = re.search('\++\d+ ([\d\-]+)', reply_to).group(1).replace('-','') except: log.error('Error getting number [%s]' % reply_to) return if not str: log.error('Error empty number [%s]' % reply_to) return try: num = int(str) except: log.error('Error converting to num [%s]' % str) return # Get the text try: str = soup.findAll('font')[2].string except: log.error('Error getting body') return try: body = re.sub('=\d.', '', str).strip() except: log.error('Error getting body [%s]' % str) return # st.parsed = True; # st.save() # Find the session str = re.search('[0-9]+', (msg['To']).split('@')[0]).group(0) try: session_id = int(str, 10) except: log.error('Error getting session id [%s]' % str) return if not session_id: log.error("Error getting session id from %s " % msg['To']) return ses = SMSSession.objects.filter(pk=session_id) if not len(ses): log.error("Error getting session with id %d" % session_id) return ses = ses[0] # Get source user log.info('Get user for %s' % msg['From']) by_num = int(re.sub('[^0-9]', '', msg['From'])[-9:]) if not by_num: log.error("Error parsing orig number %s" % msg['From']) return by_prof = Profile.objects.filter(phone=by_num) if not len(by_prof): log.error("Error getting using with num %d" % by_num) return by = by_prof[0].user log.info("Got message from %s to %s [%s]" % (by, ses.user, body)) body_w1255 = quopri.decodestring(body) decoded_body = body_w1255.decode('windows-1255').strip() send_message(by, decoded_body, [ses.user], phone_reply=True)
def incoming_mail(string): try: parse_incoming_mail(string) except : log.error("Exception parsing email") traceback.print_exc()