def add_mail(chatId, mails): mails = list(set(mails)) conn, cursor = connect() already_exist = [] added_mail = [] for mail in mails: try: if (is_valid_mail(mail)): already_exist.append(mail) else: query = "insert into mails (mail_id) values ('{0}');".format( mail) cursor.execute(query) conn.commit() added_mail.append(mail) except (MySQLdb.Error, MySQLdb.Warning) as e: app.logger.info(e) send_error_message(chatId) conn.close() return message = "No changes done\n" if (len(added_mail) > 0): message = "Inserted the following into the database\n" for mail in added_mail: message = message + " ->" + mail + '\n' if (len(already_exist)): message = message + "The following already exist in the database\n" for mail in already_exist: message = message + " ->" + mail send_message(chatId, message) return
def remove_mail(chatId, mails): mails = list(set(mails)) conn, cursor = connect() not_present = [] removed_mail = [] for mail in mails: try: if (is_valid_mail(mail)): query = "delete from mail where mail_id = '{0}';".format(mail) cursor.execute(query) conn.commit() removed_mail.append(mail) else: not_present.append(mail) except (MySQLdb.Error, MySQLdb.Warning) as e: app.logger.info(e) send_error_message(chatId) conn.close() return message = "No changes done\n" if (len(removed_mail) > 0): message = "Deleted the following from the database\n" for mail in removed_mail: message = message + " ->" + mail + '\n' if (len(not_present)): message = message + "The following do not exist in the database\n" for mail in not_present: message = message + " ->" + mail + '\n' send_message(chatId, message) return
def request_access(request_data): conn,cursor = connect() chatId = str(request_data['message']['chat']['id']) firstName = "" if ('first_name' in request_data['message']['chat']): firstName = request_data['message']['chat']['first_name'] lastName = "" if ('last_name' in request_data['message']['chat']): lastName = request_data['message']['chat']['last_name'] tusername = request_data['message']['from']['username'] message = "" try: query = "select * from pending_requests where chatId = {0}".format(chatId) result = cursor.execute(query) if (result > 0): message = "You have already requested for access. Please wait patiently the admin will soon accept your request\n" else: query = "select * from accepted_users where chatId = {0}".format(chatId) result = cursor.execute(query) if (result > 0): message = "You already have access\n" else: query = "insert into pending_requests (chatId,firstName,lastName,tusername) values ('{0}','{1}','{2}','{3}');".format(chatId,firstName,lastName,tusername) cursor.execute(query) conn.commit() send_admin_message("You have a pending request from " + tusername) message = "The admin will be notified about your request to use this bot. Once he accepts it you will be able to get emails. You can keep modifiying your preferences till then" send_message(chatId,message) conn.close() except (MySQLdb.Error, MySQLdb.Warning) as e: app.logger.info(e) send_error_message(chatId) conn.close()
def revoke_access(chatId, usernames): usernames = list(set(usernames)) conn, cursor = connect() revoked_for = [] for user in usernames: cId = "" granted = False requesting = False message = "" try: query = "select chatId from accepted_users where tusername = '******'".format( user) result = cursor.execute(query) if (result > 0): granted = True cId = cursor.fetchall()[0][0] revoked_for.append(user) query = "delete from accepted_users where tusername = '******'".format( user) cursor.execute(query) query = "select chatId from pending_requests where tusername = '******'".format( user) result = cursor.execute(query) if (result > 0): requesting = True cId = cursor.fetchall()[0][0] revoked_for.append(user) query = "delete from pending_requests where tusername = '******'".format( user) cursor.execute(query) conn.commit() conn.close() if (granted and not requesting): message = "Your access has been removed by the Admin\n" if (requesting and not granted): message = "Your request has been cancelled by the Admin\n" if (requesting and granted): message = "Your access has been removed\n" send_message(chatId, "Data Redundancy in the server database!!!!") if (granted or requesting): send_message(cId, message) except (MySQLdb.Error, MySQLdb.Warning) as e: app.logger.info(e) send_error_message(chatId) conn.close() return revoked_for = list(set(revoked_for)) message = "" if (len(revoked_for) > 0): message = "Access has been revoked for \n" for user in usernames: message = message + " -> " + user + '\n' else: message = "No valid users to revoke access" send_message(chatId, message)
def add_entry_to_user_in_db(request_data): conn,cursor = connect() chatId = str(request_data['message']['chat']['id']) try: nullstr = "" query = "insert into user(chatId,following) values ('{0}','{1}');".format(chatId,nullstr) cursor.execute(query) conn.commit() except (MySQLdb.Error, MySQLdb.Warning) as e: send_error_message(chatId) conn.close()
def exists_in_db(chatId): conn,cursor = connect() query = "select * from user where chatId = '" + chatId + "';" try: result = cursor.execute(query) conn.commit() conn.close() except (MySQLdb.Error, MySQLdb.Warning) as e: send_error_message(chatId) if (result > 0): return True return False
def isadmin(chatId): try: query = "select * from admin where chatId = '{0}'".format(chatId) conn,cursor = connect() result = cursor.execute(query) conn.close() except (MySQLdb.Error, MySQLdb.Warning) as e: send_error_message(chatId) return False #Added for safety reasons. For any reason if the query does not execute as expected. The answer should be False if (result > 0): return True return False
def list_following(chatId): valid, following = get_following(chatId) if (not valid): send_error_message(chatId) return if (len(following) == 0): message = "You are currently not following anyone\n" else: message = "You are currently following\n" for mail in following: message = message + " -> " + mail + '\n' send_message(chatId, message)
def follow(chatId, new_following): new_following = list(set(new_following)) if (len(new_following) == 0): message = "No input given\n" send_message(chatId, message) return valid, following = get_following(chatId) if (not valid): send_error_message(chatId) return already_following = [] invalid_mail = [] addedmail = [] changes_made = False for mail in new_following: if (mail not in following): if (is_valid_mail(mail)): changes_made = True following.append(mail) addedmail.append(mail) else: invalid_mail.append(mail) else: already_following.append(mail) try: conn, cursor = connect() query = "update user set following = '" + ' '.join( following) + "' where chatId = '" + chatId + "'" cursor.execute(query) if (changes_made): message = "Your preferences have been updated\n" else: message = "No changes have been made\n" if (len(addedmail) > 0): message = message + "You are now also following\n" for mail in addedmail: message = message + " -> " + mail + '\n' if (len(already_following) > 0): message = message + "You were already following\n" for mail in already_following: message = message + " -> " + mail + '\n' if (len(invalid_mail) > 0): message = message + "The following mails do not exist in the database\n" for mail in invalid_mail: message = message + " -> " + mail + '\n' conn.commit() conn.close() send_message(chatId, message) except (MySQLdb.Error, MySQLdb.Warning) as e: app.logger.info(e) send_error_message(chatId) conn.close()
def unfollow(chatId, new_following): new_following = list(set(new_following)) if (len(new_following) == 0): message = "No input given\n" send_message(chatId, message) return valid, following = get_following(chatId) if (not valid): send_error_message(chatId) return not_following = [] invalid_mail = [] removedmails = [] changes_made = False for mail in new_following: if (mail in following): changes_made = True following.remove(mail) removedmails.append(mail) else: if (is_valid_mail(mail)): not_following.append(mail) else: invalid_mail.append(mail) try: conn, cursor = connect() query = "update user set following = '" + ' '.join( following) + "' where chatId = '" + chatId + "'" cursor.execute(query) if (changes_made): message = "Selected Emails have been successfully dropped\n" else: message = "No changes made\n" if (len(removedmails) > 0): message = message + "You are no longer following\n" for mail in removedmails: message = message + " -> " + mail + '\n' if (len(not_following) > 0): message = message + "You were not following the following mails\n" for mail in not_following: message = message + " -> " + mail + '\n' if (len(invalid_mail) > 0): message = message + "The following mails do not exist in the database\n" for mail in invalid_mail: message = message + " -> " + mail + '\n' conn.commit() send_message(chatId, message) conn.close() except (MySQLdb.Error, MySQLdb.Warning) as e: app.logger.info() send_error_message(chatId) conn.close()
def unfollow_all(chatId): conn, cursor = connect() try: query = "update user set following = ' ' where chatId = '" + chatId + "'" cursor.execute(query) message = "Unfollowed all users\n" conn.commit() send_message(chatId, message) conn.close() except (MySQLdb.Error, MySQLdb.Warning) as e: app.logger.info(e) send_error_message(chatId) conn.close()
def view_accepted_users(chatId): try: conn,cursor = connect() query = "select firstName,lastName,tusername from accepted_users" cursor.execute(query) names = cursor.fetchall() conn.close() except (MySQLdb.Error, MySQLdb.Warning) as e: app.logger.info(e) send_error_message(chatId) conn.close() return message = "Accepted Users are \n" for name in names: message = message + "Name: " +name[0] + " " +name[1] + ", Handle: " + name[2] + '\n' send_message(chatId,message)
def grant_access(chatId, usernames): usernames = list(set(usernames)) if (len(usernames) == 0): message = "No input given\n" send_message(chatId, message) return try: conn, cursor = connect() query = "select * from pending_requests" cursor.execute(query) pending_requests = cursor.fetchall() query = "select tusername from accepted_users" cursor.execute(query) accepted_users = cursor.fetchall() conn.commit() except (MySQLdb.Error, MySQLdb.Warning) as e: app.logger.info(e) send_error_message(chatId) return accepted_users = [x[0] for x in accepted_users] toaccept = [] alreadydone = [] invalid_handle = [] for user in usernames: flag = False for request in pending_requests: if (request[3] == user or user == '*'): toaccept.append(request) flag = True if (not flag): if (user in accepted_users): alreadydone.append(user) else: invalid_handle.append(user) try: message = "" if (len(toaccept) > 0): for row in toaccept: query = "insert into accepted_users (chatId,firstName,lastName,tusername) values ('{0}','{1}','{2}','{3}')".format( row[0], row[1], row[2], row[3]) cursor.execute(query) conn.commit() for row in toaccept: query = "delete from pending_requests where chatId = {0}".format( row[0]) cursor.execute(query) conn.commit() for row in toaccept: message = "Congratulation's you are granted access to use this bot!!!!" send_message(row[0], message) message = "granted access to\n" for row in toaccept: message = message + " ->" + row[3] + '\n' else: message = "No changes done\n" if (len(alreadydone) > 0): message = message + "The following users already have access\n" for user in alreadydone: message = message + " ->" + user + '\n' if (len(invalid_handle) > 0): message = message + "The following users either have not requested for access or they do not exist in our database\n" for user in invalid_handle: message = message + " ->" + user + '\n' conn.commit() conn.close() send_message(chatId, message) except (MySQLdb.Error, MySQLdb.Warning) as e: app.logger.info(e) send_error_message(chatId) conn.close()