def get_memory(self, message, keywords): # Get memory usage information memory_info = os.popen('free -m') memory_info = memory_info.read().splitlines() # Get RAM specific information pieces = memory_info[1].split(' ') ram_info = [] for piece in pieces: if piece != '': ram_info.append(piece) ram_total = ram_info[1] ram_used = ram_info[2] ram_free = ram_info[3] # Get Swap specific information pieces = memory_info[3].split(' ') swap_info = [] for piece in pieces: if piece != '': swap_info.append(piece) swap_total = swap_info[1] swap_used = swap_info[2] swap_free = swap_info[3] # Format and send it! :) send = '[' + Format.color('red') + ' Memory ' + Format.normal() + ' |' send = send + ' Ram:' + Format.color('red') + ' ' + ram_used + ' / ' + ram_total + Format.normal() + ' MB |' send = send + ' Swap:' + Format.color('red') + ' ' + swap_used + ' / ' + swap_total + Format.normal() + ' MB ]' self.bot.client.send_pm(message.channel, send)
def list_plugins(self, message, keywords): self.bot.client.send_pm(message.nick, "%s%s%s" % (Format.color('darkblue'), Format.bold(), _("Plugin List"))) i = 1 for plugin_file, plugin in self.bot.plugins.plugins.iteritems(): self.bot.client.send_pm(message.nick, "%s#%d %s" % (Format.color('darkblue'), i, plugin.plugin_info['name'])) #time.sleep(0.3) i += 1 self.bot.client.send_pm(message.nick, _("Use !plugin plugin_name to view more info about a plugin").replace('!', self.bot.settings.get('Bot', 'command_prefix')))
def unload_plugin(self, message, keywords): if not self.bot.auth.check_logged_in(message.nick): self.bot.client.send_notice(message.nick, _("You do not have enough rights to use this command")) return if len(message.bot_args) == 0: return if not self.manager.unload_plugin(message.bot_args): self.bot.client.send_pm(message.channel, Format.color('red') + Format.bold() + _("Could not unload the plugin")) else: self.bot.client.send_pm(message.channel, Format.color('green') + _("Plugin succesfully unloaded"))
def send_now_playing(self, user, message): lastfm = LastFMInfo(user) try: title = lastfm.now_playing() except: import traceback traceback.print_exc() self.bot.client.send_pm(message.channel, _("Could not load data, probably the user doesn't exists")) else: if title == False: self.bot.client.send_pm(message.channel, _("No track currently playing")) else: self.bot.client.send_pm(message.channel, Format.color('darkblue') + Format.bold() + _('[%s] Last.FM Now Playing:%s %s') % (user, Format.bold(), title)) del lastfm
def reload_plugins(self, message, keywords): if not self.bot.auth.check_logged_in(message.nick): self.bot.client.send_notice(message.nick, _("You do not have enough rights to use this command")) return self.bot.client.send_pm(message.channel, Format.color('orange') + _("Reloading..")) if len(message.bot_args) != 0: success = self.manager.reload_plugin(message.bot_args) else: success = self.manager.reload_plugins() if not success: self.bot.client.send_pm(message.channel, Format.color('red') + Format.bold() + _("Could not reload the plugin(s)")) else: self.bot.client.send_pm(message.channel, Format.color('green') + _("Plugin(s) succesfully reloaded"))
def get_cpu(self, message, keywords): # Get proccessor information cpu_info = os.popen('cat /proc/cpuinfo') cpu_info = cpu_info.read().splitlines() # Now get the specific information we want to display. for info in cpu_info: info = info.split(':') info[0] = info[0].strip() if info[0] == 'model name': cpu_model = info[1] elif info[0] == 'cpu MHz': cpu_mhz = info[1] elif info[0] == 'cache size': cpu_cache = info[1] # Get temperature information show_temp = self.bot.settings.get('SysInfo', 'sensors') if (show_temp == 'true'): sensors = os.popen('sensors') sensors = sensors.read().splitlines() for sensor in sensors: type = sensor.split(':') if type[0] == 'CPU Temp': cpu_temp = type[1].split('(') pieces = cpu_temp[0].split(' ') for piece in pieces: if piece != '': cpu_temp = piece break temp = '| Temp:' + Format.color('red') + ' ' + cpu_temp + ' ' + Format.normal() + ' ]' else: temp = ']' # Now, lets format the message and send the message! send = '[' + Format.color('red') + ' CPU ' + Format.normal() + ' |' send = send + ' Model:' + Format.color('red') + cpu_model + ' ' + Format.normal() + '|' send = send + ' Speed: ' + Format.color('red') + cpu_mhz + Format.normal() send = send + ' (' + Format.color('red') + cpu_cache + Format.normal() + ' ) ' send = send + temp self.bot.client.send_pm(message.channel, send)
def read_feed(self, message, keywords): try: url = Format.remove(message.bot_args) # Check URL if not self._validate_url(url): url = self.get_url(url) if url == None: raise RssException, self.lang.get('not_found') rss = RssFeed(url) rss.parse(5) self.bot.client.send_pm(message.channel, Format.color('darkblue') + Format.bold() + rss.title) for item in rss: send = "[ %s%s%s ] - %s" % (Format.color('red'), item['link'], Format.normal(), item['title']) self.bot.client.send_pm(message.channel, send) except RssException, error: self.bot.client.send_pm(message.channel, error)
def send_weekly_top(self, user, message): lastfm = LastFMInfo(user) try: tracks = lastfm.get_weekly_tracks() except: import traceback traceback.print_exc() self.bot.client.send_pm(message.channel, _("Could not load data, probably the user doesn't exists")) else: if len(tracks) == 0: self.bot.client.send_pm(message.channel, _("No top tracks available")) else: self.bot.client.send_pm(message.channel, Format.color('darkblue') + Format.bold() + _("Weekly top tracks from %s") % user) i = 1 for track in tracks: self.bot.client.send_pm(message.channel, Format.bold() + str(i) + ". " + Format.bold() + track) i += 1
def send_top_artists(self, user, message): lastfm = LastFMInfo(user) try: artists = lastfm.get_top_artists() except: import traceback traceback.print_exc() self.bot.client.send_pm(message.channel, _("Could not load data, probably the user doesn't exists")) else: if len(artists) == 0: self.bot.client.send_pm(message.channel, _("No top artists available")) else: self.bot.client.send_pm(message.channel, Format.color('darkblue') + Format.bold() + _("Top artists from %s") % user) i = 1 for artist in artists: self.bot.client.send_pm(message.channel, Format.bold() + str(i) + ". " + Format.bold() + artist) i += 1
def format_3fm(self, artist, title, program): # None naar Onbekend :) if artist == None: artist = 'Onbekend' if title == None: title = 'Onbekend' if program == None: program = 'Onbekend' # Capitalize artist = string.capwords(artist) title = string.capwords(title) # Make the message msg = Template('[$c 3FM $n| Now playing:$c $artist $n-$c $title $n | Program:$c $program $n]') msg = msg.substitute(c = Format.color('red'), n = Format.normal(), artist = artist, title = title, program = program) return msg
def get_top_artists(self): tree = self._get_xml('http://ws.audioscrobbler.com/1.0/user/%s/topartists.xml' % self.username) artists = tree.findall('artist') to_return = [] i = 0 while i < 5: to_return.append(artists[i].find('name').text + " (" + Format.color('red') + _("Playcount:") + " " + artists[i].find('playcount').text + Format.normal() + ")") i += 1 return to_return
def add_feed(self, message, keywords): try: args = message.bot_args.split(' ', 1) url = Format.remove(args[1]) # Check URL if not self._validate_url(url): raise RssException, self.lang.get('invalid_url') name = Format.remove(args[0]) regexp = re.compile('^[a-zA-Z0-9\-_.]+$') if not regexp.match(name): raise RssException, self.lang.get('invalid_name') cursor = self.connection.cursor() cursor.execute('INSERT INTO feeds (name, url) VALUES (?, ?)', (name, url)) self.connection.commit() cursor.close() self.bot.client.send_notice(message.nick, self.lang.get('feed_added')) except RssException, error: self.bot.client.send_pm(message.channel, error)
def review_feeds(self, message, keywords): if not self.bot.auth.check_logged_in(message.nick): self.bot.client.send_notice(message.nick, self.lang.get('access_denied')) return args = message.bot_args.split() if len(args) == 0: # Display a list of unreviewed feeds cursor = self.connection.cursor() cursor.execute('SELECT * FROM feeds WHERE moderated = 0 ORDER BY id ASC') self.bot.client.send_notice(message.nick, \ Format.color('darkblue') + Format.bold() + \ self.lang.get('unreviewed_feeds')) for row in cursor: self.bot.client.send_notice(message.nick, \ "%s%s:%s %d %s%s:%s %s" % (Format.bold(), self.lang.get('id'), Format.bold(), int(row[0]), Format.bold(), self.lang.get('url'), Format.bold(), row[2] ) ) elif len(args) == 2: if not args[0].isdigit(): self.bot.client.send_notice(message.nick, self.lang.get('invalid_id')) return # Review a feed if args[1] in ['ok', 'good', 'yes']: cursor = self.connection.cursor() cursor.execute('UPDATE feeds SET moderated = 1 WHERE id = ?', (args[0],)) self.connection.commit() cursor.close() self.bot.client.send_notice(message.nick, self.lang.get('feed_reviewed')) elif args[1] in ['wrong', 'bad', 'no']: cursor = self.connection.cursor() cursor.execute('DELETE FROM feeds WHERE id = ?', (args[0],)) self.connection.commit() cursor.close() self.bot.client.send_notice(message.nick, self.lang.get('feed_reviewed')) else: self.bot.client.send_notice(message.nick, self.lang.get('review_syntax').replace('!', self.bot.settings.get('Bot', 'command_prefix'))) else: self.bot.client.send_notice(message.nick, self.lang.get('review_syntax').replace('!', self.bot.settings.get('Bot', 'command_prefix')))
def show_plugin_info(self, message, keywords): # Check if plugin exists plugin_obj = None if self.bot.plugins.plugins.has_key(message.bot_args): plugin_obj = self.bot.plugins.plugins[message.bot_args] else: for plugin_dir, plugin in self.bot.plugins.plugins.iteritems(): if self.plugin_info['name'] == message.bot_args: plugin_obj = plugin break if plugin_obj != None: self.bot.client.send_pm(message.nick, Format.color('darkblue') + Format.bold() + plugin_obj.plugin_info['name']) if plugin_obj.plugin_info.has_key('description'): self.bot.client.send_pm(message.nick, plugin_obj.plugin_info['description']) if plugin_obj.plugin_info.has_key('authors'): self.bot.client.send_pm(message.nick, Format.color('darkblue') + "%s %s" % (_("Created By: "), ', '.join(plugin_obj.plugin_info['authors']))) if hasattr(plugin_obj, 'commands') and len(plugin_obj.commands) > 0: self.bot.client.send_pm(message.nick, Format.bold() + _("Plugin commands:")) for command, tuple in plugin_obj.commands.iteritems(): callback, data = tuple str = Format.color('darkred') + self.bot.settings.get('Bot', 'command_prefix') + command if data.has_key('args'): str += " %s%s%s" % (Format.color('orange'), data['args'], Format.normal()) if data.has_key('help'): str += " - %s%s" % (Format.color('darkblue'), data['help']) self.bot.client.send_pm(message.nick, str) else: self.bot.client.send_notice(message.nick, _("The specified plugin does not exists"))
def get_usage(self, message, keywords): # Get process information pid = os.getpid() usage = os.popen('ps -eo%cpu,%mem,rss,pid | grep ' + str(pid)) pieces = usage.read().split(' ') # Strip all the data that we dont need.. data = [] for piece in pieces: if piece != ' ': data.append(piece) # Now calculate and define the data cpu = data[1] memory = int(data[5]) / 1024 # Make and send the message :) send = '[' + Format.color('red') + ' Usage ' + Format.normal() + \ '| CPU:' + Format.color('red') + ' ' + cpu + Format.normal() + ' % \ | Memory:' + Format.color('red') + ' ' + str(memory) + Format.normal() + ' MB ]' self.bot.client.send_pm(message.channel, send)
def get_os(self, message, keywords): # Get kernel version kernel = os.popen('uname -r') kernel = kernel.read().splitlines() kernel = kernel[0] # Get release version distro = os.popen('lsb_release -a 2> /dev/null').read() distro = distro.strip() distro = distro.splitlines() for line in distro: split = line.split(':') if split[0] == 'Description': distro = split[1] break # Make and send the message! send = '[ Os:' + Format.color('red') + ' Linux ' + Format.normal() + '|\ Kernel:' + Format.color('red') + ' ' + kernel + Format.normal() + ' |\ Distro:' + Format.color('red') + ' ' + distro + Format.normal() + ' ]' self.bot.client.send_pm(message.channel, send)
def get_weekly_tracks(self): tree = self._get_xml('http://ws.audioscrobbler.com/1.0/user/%s/weeklytrackchart.xml' % self.username) tracks = tree.findall('track') to_return = [] i = 0 while i < 5: to_return.append(tracks[i].find('artist').text + " - " + tracks[i].find('name').text + " (" + Format.color('red') + _("Playcount:") + " " + tracks[i].find('playcount').text + Format.normal() + ")") i += 1 return to_return
def format_538(self, artist, title, program, dj): msg = Template('[$c 538 $n| Now playing:$c $artist $n-$c $title $n | Program:$c $program $n | DJ:$c $dj $n]') msg = msg.substitute(c = Format.color('red'), n = Format.normal(), artist = artist, title = title, program = program, dj = dj) pass
def show_info(self, message, keywords): self.bot.client.send_pm(message.channel, Format.color('darkblue') + Format.bold() + _("LuckyBot, an extendable IRC bot written in python. Version %s" % (luckybot.__version__))) self.bot.client.send_pm(message.channel, Format.color('darkblue') + _("Created by Lucas van Dijk. http://www.return1.net"))